本次在之前的基础上增加了一个比较重要的功能就是将输入的数据与数据库进行连接录入,这里我连接数据库的文件写入到单独一个java类中,在test文件中进行调用连接数据库的文件中我加入了判断语句 ,在control栏中显示数据库是否连接成功。
至于将数据插入数据库的部分使用了statment类中的函数来实现传输操作,现在实现了对题目进行加括号的操作还无法实现对有括号题目的运算,以及在线答题的动能尚未实现具体代码如下
//数据库连接
package tianaoweb.com; import java.sql.*; public class Shuju{ public static Connection getConn(){ String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; Connection dbConn = null; String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=hh"; String userName="sa"; String userPwd="g15034014195"; try { Class.forName(driverName); System.out.println("加载驱动成功!"); }catch(Exception e){ e.printStackTrace(); System.out.println("加载驱动失败!"); } try{ dbConn=DriverManager.getConnection(dbURL,userName,userPwd); System.out.println("连接数据库成功!"); }catch(Exception e) { e.printStackTrace(); System.out.print("SQL Server连接失败!"); } return dbConn; } }
//数据插入数据库
package tianaoweb.com; import java.sql.*; public class Chuanshu { public static void input(String timu[],int answer[]) { Connection conn = null; Statement st = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加载驱动类 conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=hh", "sa","g15034014195");//搜索 conn.setAutoCommit(false); st = conn.createStatement(); // 模拟一个 str[i] = nd.getNodeValue().trim() String sqlStr = null; for (int i = 0; i < timu.length; i++) { sqlStr = "INSERT INTO log ([user],name[])VALUES('" + timu[i] + "')('" + answer[i] + "')";//向数据库中插入数据 } conn.commit(); } catch (Exception e) { e.printStackTrace(); } finally {//释放数据库的资源 try { if (st != null) st.close(); if(conn != null && !conn.isClosed()){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
//有括号的四则运算出题代码
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import ="tianaoweb.com.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% JspWriter mout=pageContext.getOut(); String cho = request.getParameter("choose");//运算方式 String mm = request.getParameter("numm");//参与运算的个数 String require=request.getParameter("cho");//是否具有乘除 String numm=request.getParameter("num");//出题目的个数 String fw=request.getParameter("fanwei"); String kuohao=request.getParameter("kuo"); String daan1=request.getParameter("daan"); int choose1=Integer.parseInt(cho);//运算方式 int req=Integer.parseInt(require);//是否乘除 int m=Integer.parseInt(mm);//参与运算个数 int num1=Integer.parseInt(numm);//出题个数 int fw1=Integer.parseInt(fw); int kuohao1=Integer.parseInt(kuohao); int daan2=Integer.parseInt(daan1); String pp[]=new String[num1+1]; int answer[]=new int[num1+1]; int i=0; Shuju AA=new Shuju(); AA.getConn(); Chuanshu BB=new Chuanshu(); if(choose1==1) { for(i=0;i<num1;i++) { pp[i]=zhengshu(m,i,pp[i],req,fw1,kuohao1); A jisuan=new A(); if(kuohao1==1) { answer[i]=(int)A.fun2(pp[i]); } mout.println(i+1+"、"+pp[i]+" "+"=?"); out.print("<input type='text' name='ans1' /><br/>"); mout.print("<br>"); if(kuohao1==2) { answer[i]=(int)A.fun1(pp[i]); mout.println(i+1+"、"+pp[i]+" "+"=?"); BB.input(pp,answer); out.print("<input type='text' name='ans1' /><br/>"); mout.print("<br>"); } } if(daan2==1) { for(i=0;i<num1;i++) { mout.print("参考答案"+"<br>"); mout.print(i+1+"、"+answer[i]+"<br>"); } } } if(choose1==2) { for(i=0;i<num1;i++) { pp[i]=fenshu(m,i,pp[i],req,fw1,kuohao1); mout.println(pp[i]+" "+"=?"); mout.print("<br>"); } } %> <%! public String zhengshu(int numm,int i,String ti,int p,int fw,int kuo) { ti=""; int data=0; for(i=0;i<numm;i++) { int a=(int)(Math.random()*fw+1); int c=(int)(Math.random()*4+1); int d=(int)(Math.random()*2+1); int kuohao=(int)(Math.random()*3); if(kuo==1) { String kuohao1=""; if(data==1)//判断是前(后)括号data为1 是已有前括号 data为 0 已有后括号 { while(kuohao==1) { kuohao=(int)(Math.random()*3); } } else if(data==0) { while (kuohao==2) { kuohao=(int)(Math.random()*3); } } if(kuohao==1) { kuohao1="("; data=1; } else if(kuohao==2) { kuohao1=")"; data=0; } else { kuohao1=""; } if(data==1) { ti=ti+kuohao1+a; } if(i==numm-1&&data==1) { ti=ti+")"; } if(data==0) { ti=ti+a+kuohao1; } if(i==numm-2&&data==0) { ti=ti+a; } if(p==1)//添加运算号 { if(i<numm-1) { if(c==1) {ti=ti+'+';} if(c==2) {ti=ti+'-';} if(c==3) {ti=ti+'*';} if(c==4) {ti=ti+'/';} } if(i>numm-1) {} } if(p==2) { if(i<numm-1) { if(d==1) {ti=ti+'+';} if(d==2) {ti=ti+'-';} } if(i>numm-1) {} } } else { ti=ti+a; if(p==1)//添加运算号 { if(i<numm-1) { if(c==1) {ti=ti+'+';} if(c==2) {ti=ti+'-';} if(c==3) {ti=ti+'*';} if(c==4) {ti=ti+'/';} } if(i>numm-1) {} } if(p==2) { if(i<numm-1) { if(d==1) {ti=ti+'+';} if(d==2) {ti=ti+'-';} } if(i>numm-1) {} } } } return ti; } %> <%! public String fenshu(int m,int i,String ti,int w,int fw,int kuo) { ti=""; String gg=""; int data=0; for(i=0;i<m;i++) { int p=(int)(Math.random()*fw+1); int q=(int)(Math.random()*fw+1); int x=(int)(Math.random()*4+1); int o=(int)(Math.random()*2+1); int kuohao=(int)(Math.random()*6); if(kuo==1) {String kuohao1=""; while(p==q) { p=(int)(Math.random()*fw+1); q=(int)(Math.random()*fw+1); } if(p>q) { gg=p+"/"+q; } if(q>p) { gg=q+"/"+p; } if(data==1)//判断是前(后)括号data为1 是已有前括号 data为 0 已有后括号 { while(kuohao==1) { kuohao=(int)(Math.random()*3); } } else if(data==0) { while (kuohao==2) { kuohao=(int)(Math.random()*6); } } if(kuohao==1) { kuohao1="("; data=1; } else if(kuohao==2) { kuohao1=")"; data=0; } else { kuohao1=""; } if(data==1) { ti=ti+kuohao1+gg; } if(data==0) { ti=ti+gg+kuohao1; } if(i==m-1&&data==1) { ti=ti+")"; } else if(i==m-2&&data==0) { ti=ti+""; } if(w==1) { if(i<m-1) { if(x==1) { ti=ti+"+"; } if(x==2) { ti=ti+"-"; } if(x==3) { ti=ti+"*"; } if(x==4) { ti=ti+"/"; } else{} } } if(w==2) { if(i<m-1) { if(o==1) { ti=ti+"+"; } if(o==2) { ti=ti+"-"; } } else {} } } } return ti; } %> </form> </body> </html>
//界面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="test.jsp" method="post"> <center> <h1> 出题系统 </h1> 出题形式:整数运算<input type="radio" name=choose value=1>分数运算<input type="radio" name=choose value=2 ><br> 具有乘除运算<input type="radio" name=cho value=1>不具有乘除运算<input type="radio" name=cho value=2><br> 有括号<input type="radio" name=kuo value=1>无括号<input type="radio"name=kuo value=2><br> 有答案<input type="radio" name=daan value=1> 无答案 <input type="radio"name=daan value=2><br> 参与运算个数<input type="text" name=numm><br> 数据范围:<input type ="text" name=fanwei><br> 出题数量:<input type="text" name=num ><br> <input type="submit" value="提交"> <center> </body> </form> </html>
//计算
package tianaoweb.com; import java.util.HashMap; import java.util.Map; import java.util.Stack; public class A { public static void main(String[] args) { String p="(2+1)*(2)"; System.out.print(fun2(p)); } public static Map pro=new HashMap(); public static void init() { pro.put('+', 1); pro.put('-', 1); pro.put('*', 2); pro.put('/', 2); } public static int getIndex(String str) { int index1=(str.indexOf('+')==-1?str.length():str.indexOf('+')); int index2=(str.indexOf('-')==-1?str.length():str.indexOf('-')); int index3=(str.indexOf('*')==-1?str.length():str.indexOf('*')); int index4=(str.indexOf('/')==-1?str.length():str.indexOf('/')); int index=index1<index2?index1:index2; index=index<index3?index:index3; index=index<index4?index:index4; return index; } public static double cal(char op,double num1,double num2) { switch(op) { case '+': return num1+num2; case '-': return num1-num2; case '*': return num1*num2; default: return num1/num2; } } public static double fun1(String str) { init(); Stack st1=new Stack(); Stack st2=new Stack(); int fop=0; while(str.length()>0) { int index=getIndex(str); st1.push(Double.parseDouble(str.substring(0,index))); if(index!=str.length()) { char op=str.charAt(index); str=str.substring(index+1); while(true) { if((int)pro.get(op)>fop) { st2.push(op); fop=(int)pro.get(op); break; } else { double num2= (double) st1.pop(); double num1=(double) st1.pop(); double result=cal((char)st2.pop(),num1,num2); st1.push(result); if(st2.size()==0) { st2.push(op); fop=(int)pro.get(op); break; } char cop=(char) st2.pop(); fop=(int)pro.get(cop); st2.push(cop); } } } else { break; } } while(st2.size()!=0) { double num2=(double) st1.pop(); double num1=(double) st1.pop(); char op=(char) st2.pop(); st1.push(cal(op,num1,num2)); } double result=(double) st1.pop(); return result; } public static double fun2(String str) { while(str.indexOf('(')!=-1) { int left=0; int right=str.length(); char op; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='(') { left=i; } if(str.charAt(i)==')') { right=i; break; } } str=str.substring(0,left)+fun1(str.substring(left+1,right))+str.substring(right+1); } return fun1(str); } }
结果如下