结对队友:胡康臻,杨寒寒
1、设计思想:先编写javabean,在javabean中实现四则运算,并把生成的表达式和答案写入数据库;接下来写四则运算用户选择界面,针对用户的选择,编写处理界面,表达式输出界面,核对答案界面
2、源代码:
四则运算javabean:
package 四则运算4; import java.sql.*; public class T { //只有整数运算 public void zhengshu(int num,int num_max) throws Exception { System.out.println("整数四则运算:"); String biaodashi; String result = null; //清空数据表 Connection conn=null; try{ //连接驱动 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加载Mysql驱动"); //数据库操作 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mldn","root","hanhan"); Statement stmt = conn.createStatement(); //清空数据表 String sql; sql="truncate table zhengshu"; stmt.execute(sql); } catch(SQLException e1) { System.out.println("Mysql操作错误"); e1.printStackTrace(); } catch(Exception e1) { e1.printStackTrace(); } for(int i = 0;i<num;i++){ //获得两个数 int x =1+ (int)(Math.random()*num_max); int y = 1+(int)(Math.random()*num_max); //随机获得符号 int fuhao = 1+(int)(Math.random()*4); //加法运算 if(fuhao == 1) { biaodashi = x+ " + " +y; result = String.valueOf(x+y); } //减法运算并检验结果无负数 else if(fuhao == 2) { if(x<y) { biaodashi = y+ " - " +x; result = String.valueOf(y-x); } else { biaodashi = x+ "-" +y; result = String.valueOf(x-y); } } //乘法运算 else if(fuhao ==3) { biaodashi = x+ " * " +y; result = String.valueOf(x*y); } //除法运算并判断结果有无余数 else { while(y==0) { y = 1+(int)(Math.random()*10); } biaodashi = x+ " / " +y; result = fenshu_print(x,y); System.out.println(result); }//else结束 conn = null; try{ //连接驱动 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加载Mysql驱动"); //数据库操作 conn = null; conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mldn","root","hanhan"); Statement stmt = conn.createStatement(); String sql; sql = "insert into zhengshu(biaodashi,answer) values('"+biaodashi+"','"+result+"')"; stmt.executeUpdate(sql); } catch(SQLException e) { System.out.println("Mysql操作错误"); e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } finally{ conn.close(); } } } //分数化简 public static int huajian(int a,int b) { int m = a; if(m<b) m = b; int i = 0; for(i=m;i>1;i--) { if(a%i==0&&b%i==0) break; } return i; } //整数球结果 public static int integer(int num1,int num2,int fuhao) { if(fuhao == 0) fuhao = num1 + num2; else if(fuhao == 1) if(num1 >num2) { fuhao = num1 - num2; } else fuhao = num2 - num1; else if(fuhao == 2) fuhao = num1 * num2; return fuhao; } //分数输出 public static String fenshu_print(int x,int y) { String result = null; if(x>y&&x%y!=0) { result = x/y + "’" + (x/huajian(x,y))%(y/huajian(x,y)) + "/" + y/huajian(x,y); } else if(x<y) { result = x/huajian(x,y) + "/" + y/huajian(x,y); } else if(x%y==0) result = String.valueOf(x/y); return result; } //分数运算 public void fenshu(int num,int num_max) throws Exception { System.out.println("分数四则运算:"); String biaodashi; String result = null; //清空数据表 Connection conn=null; try{ //连接驱动 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加载Mysql驱动"); //数据库操作 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mldn","root","hanhan"); Statement stmt = conn.createStatement(); //清空数据表 String sql; sql="truncate table fenshu"; stmt.execute(sql); } catch(SQLException e1) { System.out.println("Mysql操作错误"); e1.printStackTrace(); } catch(Exception e1) { e1.printStackTrace(); } for(int i = 0;i<num;i++){ //获得两个数 int a =1+ (int)(Math.random()*num_max); int b = 1+(int)(Math.random()*num_max); int c = 1+(int)(Math.random()*num_max); int d = 1+(int)(Math.random()*num_max); int fuhao = 1+(int)(Math.random()*4); String fu = null; if(fuhao ==1) fu = "+"; else if(fuhao ==2) fu = "-"; else if(fuhao==3) fu = "*"; else fu = "/"; biaodashi = a+"/"+b+" "+fu+" "+c+"/"+d; result = fenshu_result(a,b,c,d,fuhao); conn = null; try{ Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加载Mysql驱动"); //数据库操作 conn = null; conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mldn","root","hanhan"); Statement stmt = conn.createStatement(); String sql; sql = "insert into fenshu(biaodashi,answer) values('"+biaodashi+"','"+result+"')"; stmt.execute(sql); } catch(SQLException e) { System.out.println("Mysql操作错误"); e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } finally{ conn.close(); } } } //分数求结果 public static String fenshu_result(int a,int b,int c,int d,int fuhao) { String result = null; if(fuhao == 1) { result = fenshu_print(a*d+b*c,b*d); } else if(fuhao == 2) { if(a*d-b*c>0) result = fenshu_print(a*d-b*c,b*d); else result = "-"+fenshu_print(a*d-b*c,b*d); } else if(fuhao == 3) result = fenshu_print(a*c,b*d); else result = fenshu_print(a*d,b*c); return result; } public void kuohao(int number,int num_max) throws Exception { //清空数据表 Connection conn=null; try{ //连接驱动 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加载Mysql驱动"); //数据库操作 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mldn","root","hanhan"); Statement stmt = conn.createStatement(); //清空数据表 String sql; sql="truncate table kuohao"; stmt.execute(sql); } catch(SQLException e1) { System.out.println("Mysql操作错误"); e1.printStackTrace(); } catch(Exception e1) { e1.printStackTrace(); } for(int ti = 0;ti<number;ti++){ int x=(int) (1+(Math.random()*num_max)); int y=0;//符号随机 int j;//寻找( int p;//确定)的位置 int e=0;//确定表达式中是否有右括号 int a = 10; String f = null ; String biaodashi = x+""; String result = null; int num = 8; for(int i = 0;i<5-1;i++){ y = (int)(1+(Math.random()*num)); if(y==1) f = "+"; else if(y==2) f = "-"; else if(y==3) f = "*"; else if(y==4) f = "/"; else if(y==5) f = "+("; else if(y==6) f = "-("; else if(y==7) f = "*("; else if(y==8) f = "/("; biaodashi = biaodashi+ f + x ; x=(int)(1+(Math.random()*num_max)); //控制左括号 if(y>4) { num = 4; a = i; } else if(4-i>0) num = 4; //控制右括号 if(i-a>=1&&e==0) { p=(int)(1+(Math.random()*2)); if(p==1) biaodashi+=""; else { biaodashi+=")"; e=1; } } } for(int q = 0;q<biaodashi.length();q++){ if(biaodashi.charAt(q)=='(') for(int w = q;w<biaodashi.length();w++) if(biaodashi.charAt(w)==')') break; else { if(w==biaodashi.length()-1) biaodashi+=")"; } } result = String.valueOf(opt(biaodashi)); conn=null; try{ //连接驱动 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加载Mysql驱动"); //数据库操作 conn = null; conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mldn","root","hanhan"); Statement stmt = conn.createStatement(); String sql; sql = "insert into kuohao(biaodashi,answer) values('"+biaodashi+"','"+result+"')"; stmt.execute(sql); } catch(SQLException e1) { System.out.println("Mysql操作错误"); e1.printStackTrace(); } catch(Exception e1) { e1.printStackTrace(); } finally{ conn.close(); } } } public static float opt(String s) throws Exception{ if(s == null || "".equals(s.trim())) { return 0f; } int a1=s.indexOf("+"); int a2=s.indexOf("-"); int a3=s.indexOf("*"); int a4=s.indexOf("/"); int a5=s.indexOf("("); if(a1==-1&&a2==-1&&a3==-1&&a4==-1){ if(s.trim()==null||"".equals(s.trim())){ throw new Exception("operate error"); } return Float.parseFloat(s.trim()); } if(a5!=-1){ int a6=s.indexOf(")"); if(a6==-1){ throw new Exception("括号不匹配"); }else{ float f=opt(s.substring(a5+1,a6).trim()); s=s.replace(s.substring(a5,a6+1), String.valueOf(f)); return opt(s); } } if(a1!=-1){ return opt(s.substring(0,a1))+opt(s.substring(a1+1,s.length())); } if(a2!=-1){ return opt(s.substring(0,a2))-opt(s.substring(a2+1,s.length())); } if(a3!=-1){ return opt(s.substring(0,a3))*opt(s.substring(a3+1,s.length())); } if(a4!=-1){ return opt(s.substring(0,a4))/opt(s.substring(a4+1,s.length())); } return Integer.parseInt(s.trim()); } private int num; private int max; private int if_fenshu; private int if_kuohao; }
用户选择界面:
<!-- js验证输入是否正确 --> <!DOCTYPE html> <html> <head> <script language = "JavaScript"> function fun(form) { max = form.max.value; num = form.num.value; if(num.length==0) { alert("请输入题的数量"); form.num.focus(); return false; } else if(max.length==0) { alert("请输入数值范围"); form.max.focus(); return false; } } </script> <meta charset="UTF-8"> <title>请选择题型</title> </head> <body> <h2 align = "center">请做选择</h2> <form name = "form1" action = "confirm.jsp" method = "post" name = "mychose" onsubmit = " return fun(form1)"> <table align = "center"> <tr> <td>题数:</td> <td><input type ="text" name = "num" value = ""></td> </tr> <tr> <td>数值范围:</td> <td><input type = "text" name = "max" value = ""></td> </tr> <tr> <td>是否有分数:</td> <td><input type = "radio" name = "checked_fenshu" value = "1" checked>有分数</td> <td><input type = "radio" name = "checked_fenshu" value = "2">没有分数</td> </tr> <tr> <td>是否有括号:</td> <td><input type = "radio" name = "checked_kuohao" value = "1" checked>有括号</td> <td><input type = "radio" name = "checked_kuohao" value = "2">没有括号</td> </tr> <tr> <td> <input type="reset" value="重置"></td> <td> <input type="submit" value="开始做题" ></td> </tr> </table> </form> </body> </html>
处理界面:
<%@ page language="java" import = "java.sql.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import = "四则运算4.T" %> <!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>确认信息 </title> </head> <body> <% request.setCharacterEncoding("utf-8"); int num = Integer.parseInt(request.getParameter("num")); int num_max = Integer.parseInt(request.getParameter("max")); int checked_fenshu = Integer.parseInt(request.getParameter("checked_fenshu")); int checked_kuohao = Integer.parseInt(request.getParameter("checked_kuohao")); out.println(num+" "+num_max+" "+checked_fenshu+" "+checked_kuohao); T t = new T(); if(checked_fenshu==2&&checked_kuohao==2) t.zhengshu(num, num_max); else if(checked_fenshu==1&&checked_kuohao==2) { t.zhengshu(num/2, num_max); t.fenshu(num-num/2, num_max); } else if(checked_fenshu==2&&checked_kuohao==1) { t.zhengshu(num/2, num_max); t.kuohao(num-num/2, num_max); } else { t.zhengshu(num/3, num_max); t.fenshu(num/3, num_max); t.kuohao(num-num/3*2, num_max); } %> <jsp:forward page = "print.jsp"></jsp:forward> </body> </html>
表达式输出界面:
<%@ page language="java" import = "java.sql.*" 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>请开始做题</title> </head> <style type="text/css"> .text1{100px; height:20px} </style><style type="text/css"> .text1{100px; height:20px} </style> <body><form action = "check.jsp"> <table align = "center" > <tr><td>表达式</td><td >答案</td> <% Connection conn=null; try{ //连接驱动 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加载Mysql驱动"); //数据库操作 conn = null; conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mldn","root","hanhan"); Statement stmt = conn.createStatement(); String sql; sql = "select biaodashi from zhengshu"; ResultSet rs=stmt.executeQuery(sql); //执行查询语句 //从结果集中读取各字段并输出 int i = 0; while(rs.next()) {i++; out.println("<tr><td>"+rs.getString(1)); %> <td><input type = "text" name = "text<%=i%>" class = "text1"></td> <% } sql = "select biaodashi from fenshu"; rs=stmt.executeQuery(sql); //执行查询语句 //从结果集中读取各字段并输出 while(rs.next()) {i++; out.println("<tr><td>"+rs.getString(1)); %> <td><input type = "text" name = "text<%=i%>" class = "text1"></td> <% } sql = "select biaodashi from kuohao"; rs=stmt.executeQuery(sql); //执行查询语句 //从结果集中读取各字段并输出 while(rs.next()) { out.println("<tr><td>"+rs.getString(1)); %> <td><input type = "text" name = "text<%=i%>" class = "text1"></td> <% } } catch(SQLException e1) { System.out.println("Mysql操作错误"); e1.printStackTrace(); } catch(Exception e1) { e1.printStackTrace(); } finally{ conn.close(); } %> <tr><td><input type = "reset" value = "重置" ></td> <td><input type = "submit" value = "提交"</td></tr> </table> </body> </html>
答案处理界面
<%@ page language="java" import = "java.sql.*" 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> <% out.println(request.getParameter("text"+1)); Connection conn=null; try{ //连接驱动 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加载Mysql驱动"); //数据库操作 conn = null; conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mldn","root","hanhan"); Statement stmt = conn.createStatement(); String sql; int zhengshu_num=0,fenshu_num = 0,kuohao_num = 0; String int_an[] = new String[100]; String fen_an[] = new String[100]; String kuo_an[] = new String[100]; //查询整数的个数 sql = "select answer from zhengshu"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) { zhengshu_num++; int_an[zhengshu_num] = rs.getString(1); } out.println(zhengshu_num); //查询分数的个数 sql = "select answer from fenshu"; rs=stmt.executeQuery(sql); while(rs.next()) { fenshu_num++; fen_an[fenshu_num] = rs.getString(1); } //查询括号的个数 sql = "select answer from kuohao"; rs=stmt.executeQuery(sql); while(rs.next()) { kuohao_num++; kuo_an[kuohao_num] = rs.getString(1); } int dui[] = new int[zhengshu_num+fenshu_num+kuohao_num]; int cuo[] = new int[zhengshu_num+fenshu_num+kuohao_num]; int dui_num = 0; int cuo_num = 0; String daan[] = new String[100]; for(int i = 1;i<=zhengshu_num;i++) { daan[i] = request.getParameter("text"+i); out.println(daan[i]); if(int_an[i].equals(daan[i])) { dui[dui_num] = i; dui_num++; } else { cuo[cuo_num] = i; cuo_num++; } } for(int i = 1;i<=fenshu_num;i++) { daan[i] = request.getParameter("text"+i+zhengshu_num); if(fen_an[i].equals(daan[i])) { dui[dui_num] = i+zhengshu_num; dui_num++; } else { cuo[cuo_num] = i+zhengshu_num; cuo_num++; } } for(int i = 1;i<=kuohao_num;i++) { daan[i] = request.getParameter("text"+i+zhengshu_num+fenshu_num); if(kuo_an[i].equals(daan[i])) { dui[dui_num] = i+zhengshu_num+fenshu_num; dui_num++; } else { cuo[cuo_num] = i+zhengshu_num+fenshu_num; cuo_num++; } } %><br><% out.println("做错的题目有:"); for(int i = 0;i<cuo_num;i++) { if(i==cuo_num-1) out.println(cuo[i]); else out.println(cuo[i]+","); } out.println("做对的题目有:"); for(int i = 0;i<dui_num;i++) { if(i==dui_num-1) { out.println(dui[i]); } else out.println(dui[i]+","); } } catch(Exception e) { e.printStackTrace(); } %> </body> </html>
程序结果截图