四则运算最终篇-网页版四则运算

1.实验要求:

 

2.实验思路:使用jsp Javabean和servlet来实现,Javabean定义实体类,定义能根据设置的参数产生出对应的方法,jsp页面用来让用户选择参数,做题和查看历史记录,servlet根据jsp传的数据进行响应和处理。

首先有一个选择界面的jsp,如果选择做题就跳转到设置参数的jsp,设置好参数后,传到servlet,servlet根据传过来的参数产生题目,存储好以后,跳转到显示题目的jsp页面,用户可以输入答案,当交卷的时候会提交到判断的servlet,servlet根据传过的答案和正确答案进行比较,并输出做对和做错的题号,然后输出每道题和每道题的判断结果;如果选择的是查看历史记录,就会查看以前所做的题目。

3.实验代码:

//选择做题还是查询历史记录

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10         <h1>请选择是做题还是查询历史记录</h1>
11         <hr>
12         <br>
13         <br>
14         <br>
15         <a href="setParam.jsp"><input type="button" value="开始做题"></a>&nbsp;&nbsp;
16         <a href="selectServlet"><input type="button" value="查询历史记录"></a>
17 </body>
18 </html>
View Code

//设置参数,比如是否要括号,做真分数还是整数等等

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 
 8 <style>
 9     span {
10             color:blue;
11          }
12 
13 </style>
14 <script type="text/javascript">
15     $(function()
16         {
17         $('#formbackground').height($(window).height());
18         $('#formbackground').width($(window).width());
19         }
20     );
21 </script>
22 <script type="text/javascript">
23     function $(id)
24     {
25         return document.getElementById(id);
26     }
27     function check() {
28         var num=$("num").value;
29         var scope=$("scope").value;
30         $("numinfo").innerHTML="";
31         $("scopeinfo").innerHTML="";
32         if(num=="")
33             {
34                 $("numinfo").innerHTML="题数不能为空,请输入题数";
35                 $("num").focus();
36                 return false;
37             }
38         if(scope=="")
39             {
40                 $("scopeinfo").innerHTML="取值范围不能为空,请输入范围";
41                 $("scope").focus();
42                 return false;
43             }
44         return true;
45     }
46 </script>
47 <title>Insert title here</title>
48 </head>
49 <body>
50 <h1>请选择做题的类型</h1>
51     <br>
52         <form action="setParamServlet" method="post" onsubmit="return check()">
53             <table>
54                 <tr>
55                     <td>类型:</td>
56                     <td>整数<input type="radio" name="type" value="2" checked="checked"></td>&nbsp;&nbsp;
57                     <td>真分数<input type="radio" name="type" value="1"></td>
58                 </tr>
59                 <tr>
60                     <td>题数:</td>
61                     <td><input type="text" name="num" id="num"><span id="numinfo"></span></td>
62                 </tr>
63                 <tr>
64                     <td>取值范围:</td>
65                     <td><input type="text" name="scope" id="scope"><span id="scopeinfo"></span></td>
66                 </tr>
67                 <tr> 
68                     <td>是否含有括号</td>
69                     <td>无<input type="radio" name="isBracket" value="2" checked="checked"></td>
70                     <td>有<input type="radio" name="isBracket" value="1"></td>
71                 </tr>
72                 <tr>
73                     <td><input type="submit" value="确认">&nbsp;&nbsp;<input type="reset" value="取消"></td>
74                 </tr>
75             </table>
76         </form>
77 </body>
78 </html>
设置参数jsp

//产生题目的servlet

 1 package servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.sql.SQLException;
 6 
 7 import javax.servlet.ServletConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 
14 import util.publicUse;
15 
16 /**
17  * Servlet implementation class setParamServlet
18  */
19 @WebServlet("/setParamServlet")
20 public class setParamServlet extends HttpServlet {
21     private static final long serialVersionUID = 1L;
22        
23     /**
24      * @see HttpServlet#HttpServlet()
25      */
26     public setParamServlet() {
27         super();
28         // TODO Auto-generated constructor stub
29     }
30 
31     /**
32      * @see Servlet#init(ServletConfig)
33      */
34     public void init(ServletConfig config) throws ServletException {
35         // TODO Auto-generated method stub
36     }
37 
38     /**
39      * @see Servlet#destroy()
40      */
41     public void destroy() {
42         // TODO Auto-generated method stub
43     }
44 
45     /**
46      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
47      */
48     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49         // TODO Auto-generated method stub
50         doPost(request, response);
51     }
52 
53     /**
54      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
55      */
56     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
57         // TODO Auto-generated method stub
58 
59         // TODO Auto-generated method stub
60         publicUse P = new publicUse();
61         response.setContentType("text/html;charset=utf-8");
62         String type;//题目类型
63         int num;//题数
64         int scope;//范围
65         String isBracket;//是否含有括号
66         type=request.getParameter("type");
67         num=Integer.parseInt(request.getParameter("num"));
68         scope=Integer.parseInt(request.getParameter("scope"));
69         isBracket=request.getParameter("isBracket");
70         PrintWriter out = response.getWriter();
71         System.out.println(type+"	"+num+"	"+scope+"	"+isBracket);
72         int choose1=Integer.parseInt(type);
73         int choose2=Integer.parseInt(isBracket);
74         try {
75 
76             String rs[]=new String[2*num];
77             rs=P.operationAndStatistical(choose1, choose2, num,scope);
78             String []bds = new String[num];//存取表达式
79             String []rs1 = new String[num];//存取结果
80             for(int i=0;i<num;i++)
81             {
82                 bds[i] = rs[i];
83                 rs1[i] = rs[i + num];
84             }
85             
86             request.getSession().setAttribute("bds", bds);
87             request.getSession().setAttribute("rs1", rs1);
88             request.getSession().setAttribute("num", num);
89             request.getRequestDispatcher("show.jsp").forward(request, response);
90 //            response.sendRedirect(request.getContextPath()+"/show.jsp");
91             
92         } catch (ClassNotFoundException | SQLException e) {
93             // TODO Auto-generated catch block
94             e.printStackTrace();
95         }
96     
97     }
98 
99 }
产生题目的servlet

//显示题目

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 <script type="text/javascript">
 9     function $(id) {
10         return document.getElementById(id);
11     }
12     function check(){
13         var result = document.getElementsByName('result');
14         for(var i = 0;i < result.length;i++)
15         {
16             if(result[i].value=="")
17             {
18                 $("" + i).innerHTML = "请输入答案";
19                 result[i].focus();
20                 return false;
21             }
22             else{
23                 $("" + i).innerHTML ="";
24                 result[i].focus();
25             }
26         }
27         function init(){
28         }
29         return true;
30     }
31 </script>
32 </head>
33 <body>
34 
35     <h1>请在此处答题:</h1>
36     <hr>
37     <%
38     
39     %>
40     <form action="judgeServlet" method="post" onsubmit="return check()">
41         <table>
42         <%
43         int num=(Integer)request.getSession().getAttribute("num");
44         String []a=(String[])request.getSession().getAttribute("bds");
45         for(int i=0;i<num;i++)
46         {
47             %><tr>
48                 <td>请作答第<%=i+1 %>道题:</td>
49                 <td><%=a[i] %></td>
50                 <td><input type="text" name="result" /><span id=<%=i %>></span></td>
51                 
52             </tr>
53             
54             <%
55         }
56             %>
57             <tr>
58                 <td><input type="submit" value="交卷"></td>
59             </tr>
60         </table>
61     </form>
62 </body>
63 </html>
做题页面jsp
  1 package servlet;
  2 
  3 import java.io.IOException;
  4 import java.io.PrintWriter;
  5 import java.sql.SQLException;
  6 
  7 import javax.servlet.ServletConfig;
  8 import javax.servlet.ServletException;
  9 import javax.servlet.annotation.WebServlet;
 10 import javax.servlet.http.HttpServlet;
 11 import javax.servlet.http.HttpServletRequest;
 12 import javax.servlet.http.HttpServletResponse;
 13 
 14 import entity.data;
 15 import jdbc.insertsj;
 16 import util.solve;
 17 
 18 /**
 19  * Servlet implementation class judgeServlet
 20  */
 21 @WebServlet("/judgeServlet")
 22 public class judgeServlet extends HttpServlet {
 23     private static final long serialVersionUID = 1L;
 24        
 25     /**
 26      * @see HttpServlet#HttpServlet()
 27      */
 28     public judgeServlet() {
 29         super();
 30         // TODO Auto-generated constructor stub
 31     }
 32 
 33     /**
 34      * @see Servlet#init(ServletConfig)
 35      */
 36     public void init(ServletConfig config) throws ServletException {
 37         // TODO Auto-generated method stub
 38     }
 39 
 40     /**
 41      * @see Servlet#destroy()
 42      */
 43     public void destroy() {
 44         // TODO Auto-generated method stub
 45     }
 46 
 47     /**
 48      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 49      */
 50     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 51         // TODO Auto-generated method stub
 52         doPost(request, response);
 53     }
 54 
 55     /**
 56      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 57      */
 58     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 59         // TODO Auto-generated method stub
 60         response.setContentType("text/html;charset=utf-8");
 61         PrintWriter out = response.getWriter();
 62         String []bds =(String[]) request.getSession().getAttribute("bds");//获取表达式
 63         String []rs =(String[]) request.getSession().getAttribute("rs1");//获取正确结果
 64         String []inputrs = request.getParameterValues("result");//获取输入的结果
 65         solve s = new solve();
 66         boolean[]sz = s.judgeIfTrue(rs, inputrs);//判断是否正确
 67         int[] idnum = new int[rs.length];//存取题号
 68         int[]count = s.zongjie(sz);
 69         String[]qiq = s.qiq(sz);
 70         String[]count1 = s.countexpression(bds, rs, inputrs, sz);
 71          out.println("正确的题数"+count[0]+"<br>");
 72          out.println("错误的题数"+count[1]+"<br>");
 73          out.println(qiq[0]+"<br>");
 74          out.println(qiq[1]+"<br>");
 75          for(int i=0;i<rs.length;i++)
 76          {
 77              idnum[i]=i+1;
 78              out.println(count1[i]+"<br>");
 79          }
 80          data []a = new data[rs.length];
 81          for(int i=0;i<rs.length;i++)
 82          {
 83              a[i] = new data();
 84              a[i].setId(idnum[i]);
 85              a[i].setTitleexception(bds[i]);
 86              a[i].setResult(rs[i]);
 87              a[i].setInputrs(inputrs[i]);
 88              a[i].setIftrue(sz[i]);
 89          }
 90         
 91          insertsj out1 =new insertsj();
 92          try {
 93             out1.cunchu(a);
 94         } catch (SQLException e) {
 95             // TODO Auto-generated catch block
 96             e.printStackTrace();
 97         }
 98         // response.sendRedirect(request.getContextPath()+"/showResult.jsp");
 99         // request.getRequestDispatcher("showResult.jsp").forward(request, response);
100          out.print("<a href='choose.jsp'>返回</a>");
101          
102     }
103 
104 }
判断是否正确,并将结果输出的servlet和和讲题目存档
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h1>历史记录页面</h1>
11     <%
12         request.setCharacterEncoding("utf-8");
13         response.setCharacterEncoding("utf-8");
14         String s=(String)request.getSession().getAttribute("s1");
15         String []s1 = s.split(s);
16         if(s1.length==0)
17         {
18             %>你还没有历史记录<%
19         }
20         else
21         {
22             for(int i=0;i<s1.length;i++)
23             {
24                 out.print(s1[i] + "<br>");
25             }
26         }
27     %>
28     <a href="choose.jsp">返回</a>
29 </body>
30 </html>
查询历史做题的servlet
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h1>历史记录页面</h1>
11     <%
12         request.setCharacterEncoding("utf-8");
13         response.setCharacterEncoding("utf-8");
14         String s=(String)request.getSession().getAttribute("s1");
15         String []s1 = s.split(s);
16         if(s1.length==0)
17         {
18             %>你还没有历史记录<%
19         }
20         else
21         {
22             for(int i=0;i<s1.length;i++)
23             {
24                 out.print(s1[i] + "<br>");
25             }
26         }
27     %>
28     <a href="choose.jsp">返回</a>
29 </body>
30 </html>
显示做题记录的jsp

我把需要用的方法和用调用的参数都已经分好类:

如下图:

 entity用来存取实体类,也就是数据库对应的那些参数,一开始写了三个,最后只有一个用上了,内容如下:

 1 package entity;
 2 
 3 public class data {
 4     private int id;//题号
 5     private String    titleexception;//表达式
 6     private String result;//正确结果
 7     private String inputrs;//输入结果
 8     private boolean iftrue;//判断对错
 9     public data()
10     {
11         
12     }
13     public int getId() {
14         return id;
15     }
16     public void setId(int id) {
17         this.id = id;
18     }
19     public String getTitleexception() {
20         return titleexception;
21     }
22     public void setTitleexception(String titleexception) {
23         this.titleexception = titleexception;
24     }
25     public String getResult() {
26         return result;
27     }
28     public void setResult(String result) {
29         this.result = result;
30     }
31     public String getInputrs() {
32         return inputrs;
33     }
34     public void setInputrs(String inputrs) {
35         this.inputrs = inputrs;
36     }
37     public boolean isIftrue() {
38         return iftrue;
39     }
40     public void setIftrue(boolean iftrue) {
41         this.iftrue = iftrue;
42     }
43     
44     
45 }
entity

 第二个是jdbc,也就是连接数据库,查询和插入的方法:

 1 package jdbc;
 2 
 3 import java.sql.DriverManager;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 import com.mysql.jdbc.Connection;
 9 import com.mysql.jdbc.Statement;
10 
11 import entity.data;
12 
13 public class insertsj {
14     public void cunchu(data[]a) throws SQLException
15     {
16         Connection conn = null;
17         PreparedStatement ps = null;
18         ResultSet rs = null;
19         try
20         {
21             conn = (Connection) JdbcUtils.getConnection();
22             String sql = "insert into wzw3 (idnum,titleexception,result,inputrs,iftrue) values(?,?,?,?,?)";
23             
24             ps = conn.prepareStatement(sql);
25             for(int k=0;k<a.length;k++)
26             {
27                 ps.setInt(1, a[k].getId());
28                 ps.setString(2, a[k].getTitleexception());
29                 ps.setString(3, a[k].getResult());
30                 ps.setString(4, a[k].getInputrs());
31                 ps.setBoolean(5, a[k].isIftrue());
32                 ps.executeUpdate();
33             }
34         }
35         finally 
36         {
37             JdbcUtils.free(rs, ps, conn);
38         }
39         
40     }
41     
42 }
插入的
 1 package jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 import java.sql.Statement;
 9 
10 public final class JdbcUtils {
11     private static String url = "jdbc:mysql://localhost:3306/wzw1";
12     private static String user = "wzw1";
13     private static String password = "121203";
14     
15     private JdbcUtils()
16     {}
17     
18     static {
19         try{
20             Class.forName("com.mysql.jdbc.Driver");
21         }catch (ClassNotFoundException e) {
22             e.printStackTrace();
23         }
24     }
25     
26     public static Connection getConnection() throws SQLException
27     {
28         return DriverManager.getConnection(url,user,password);
29     }
30     
31     public static void free(ResultSet rs, Statement ps, com.mysql.jdbc.Connection conn) throws SQLException {
32         // TODO Auto-generated method stub
33 
34         try{
35             if(rs!=null)
36             {
37                 rs.close();
38             }
39         }
40         catch(SQLException e)
41         {
42             e.printStackTrace();
43         }
44         finally {
45             try{
46                 if(ps!=null)
47                 ps.close();
48             }
49             catch(SQLException e)
50                 {
51                     e.printStackTrace();
52                 }
53                 finally {
54                     if(conn!=null)
55                     {
56                         conn.close();
57                     }
58                 }
59                 }
60             
61     }
62 
63     public static void free(ResultSet rs, PreparedStatement ps, com.mysql.jdbc.Connection conn) throws SQLException {
64         // TODO Auto-generated method stub
65 
66         try{
67             if(rs!=null)
68             {
69                 rs.close();
70             }
71         }
72         catch(SQLException e)
73         {
74             e.printStackTrace();
75         }
76         finally {
77             try{
78                 if(ps!=null)
79                 ps.close();
80             }
81             catch(SQLException e)
82                 {
83                     e.printStackTrace();
84                 }
85                 finally {
86                     if(conn!=null)
87                     {
88                         conn.close();
89                     }
90                 }
91                 }
92             
93     }
94 }
jdbc连接的工具类
 1 package jdbc;
 2 
 3 import java.sql.PreparedStatement;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 
 7 import com.mysql.jdbc.Connection;
 8 
 9 public class select {
10     public static String selectbyIdTime() throws SQLException
11     {
12         Connection conn = null;
13         PreparedStatement ps = null;
14         ResultSet rs = null;
15         try{
16             conn = (Connection) JdbcUtils.getConnection();
17             String sql="select * from wzw3";
18             ps = conn.prepareStatement(sql);
19             rs=ps.executeQuery();
20             String a1 = "";
21             while(rs.next())
22             {
23                 a1+=rs.getInt("idnum")+rs.getString("titleexception")+rs.getString("result")+rs.getString("inputrs")+rs.getBoolean("iftrue") + "#";
24             }
25             return a1;
26         }
27         finally 
28         {
29             JdbcUtils.free(rs, ps, conn);
30         }
31     }
32 }
查询

第三个是用来相应的servlet,在上面都已经展示过了

第四个是产生题目的方法,跟上一次写的差不多,但是因为太多,我把分成三个Java文件,还有一个是判断所做的体是否正确的方法:

  1 package util;
  2 
  3 public class integer {
  4     //整数运算
  5     public static String generateExpressionkh(int num,int scope)//产生带括号的整数表达式
  6     {
  7         publicUse u = new publicUse();
  8         int a1[]=new int[num];
  9         int a2[]=new int[num-1];
 10         int a3[]=new int[num];
 11         String[]a5=new String[num];
 12         String[] a4={"+","-","*","/"};
 13         for(int i=0;i<num;i++)
 14         {
 15             a1[i]=(int) (Math.random()*(scope-1)+1);
 16         }
 17         for(int i=0;i<num-1;i++)
 18         {
 19             a2[i]=(int) (Math.random()*4);
 20         }
 21         a3=u.chansheng(num);
 22         for(int i=0;i<num;i++)
 23         {
 24             a5[i]="";
 25             if(a3[i]<0)
 26             {
 27                 int c=0-a3[i];
 28                 for(int j=0;j<c;j++)
 29                 {
 30                     a5[i]+=")";
 31                 }
 32             }
 33             else
 34             {
 35                 for(int j=0;j<a3[i];j++)
 36                 {
 37                     a5[i]+="(";
 38                 }
 39             }
 40         }
 41         String t="";
 42         for(int i=0;i<num-1;i++)
 43         {
 44             if(a3[i]>0)
 45             {
 46                 t+=a5[i]+" "+a1[i]+" "+a4[a2[i]];
 47             }
 48             else
 49             {
 50                 t+=" "+a1[i]+" "+a5[i]+a4[a2[i]];
 51             }
 52         }
 53         if(a3[num-1]>0)
 54         {
 55             t+=a5[num-1]+" "+a1[num-1]+" ";
 56         }
 57         else
 58         {
 59             t+=" "+a1[num-1]+" "+a5[num-1];
 60         }
 61         return t;
 62     }
 63     public static String generationexception(int num,int scope)//产生不带括号的表达式
 64     {
 65         int a1[]=new int[num];
 66         int a2[]=new int[num-1];
 67         int a3[]=new int[num];
 68         String[] a4={"+","-","*","/"};
 69         for(int i=0;i<num;i++)
 70         {
 71             a1[i]=(int) (Math.random()*(scope-1)+1);
 72         }
 73         for(int i=0;i<num-1;i++)
 74         {
 75             a2[i]=(int) (Math.random()*4);
 76         }
 77         String t="";
 78         for(int i=0;i<num-1;i++)
 79         {
 80             if(a3[i]>0)
 81             {
 82                 t+=" "+a1[i]+" "+a4[a2[i]];
 83             }
 84             else
 85             {
 86                 t+=" "+a1[i]+" "+a4[a2[i]];
 87             }
 88         }
 89         if(a3[num-1]>0)
 90         {
 91             t+=" "+a1[num-1]+" ";
 92         }
 93         else
 94         {
 95             t+=" "+a1[num-1]+" ";
 96         }
 97         return t;
 98     }
 99     
100 }
产生整数的题目
  1 package util;
  2 
  3 public class properFraction {
  4 
  5     //真分数运算
  6     public static String properFractionExit(int num,int scope)//产生不含括号含有真分数的表达式
  7     {
  8         publicUse u =new publicUse();
  9         
 10         int []r1=new int[2*num];//接受产生的数值
 11         int []r2=new int[num-1];//接受符号
 12         String[]r3={"+","-","*","/"};
 13         
 14         String rs="";//接受含括号的和不含括号的表达式
 15         char ch='z';
 16         while(ch=='z')
 17         {
 18             int i=0;
 19             for(;i<2*num;i++)
 20             {
 21                 r1[i]=(int) (Math.random()*(scope-1)+1);
 22             }
 23             ch='y';
 24             int j=0;
 25             while(j<2*num)
 26             {
 27                 if(r1[j]>=r1[j+1])
 28                 {
 29                     ch='z';
 30                     break;
 31                 }
 32                 j++;
 33                 j++;
 34             }
 35         }
 36         for(int i=0;i<num-1;i++)
 37         {
 38             r2[i]=(int) (Math.random()*4);
 39         }
 40         int j=0;
 41         while(j<2*num-2)
 42         {
 43             
 44             int commondivisor=u.maxyue(r1[j], r1[j+1]);
 45             r1[j]/=commondivisor;
 46             r1[j+1]/=commondivisor;
 47             if(r1[j]==r1[j+1])
 48             {
 49                 rs+=" "+1+" ";
 50             }
 51             else
 52             {
 53                 rs+=" "+r1[j]+"/"+r1[j+1]+" "+r3[r2[(j+1)/2]];
 54             }
 55             j++;
 56             j++;
 57         }
 58         int commondivisor1=u.maxyue(r1[2*num-2],r1[2*num-1]);
 59         r1[2*num-2]/=commondivisor1;
 60         r1[2*num-1]/=commondivisor1;
 61         if(r1[2*num-2]==r1[2*num-1])
 62         {
 63             rs+=" "+1+" ";
 64         }
 65         else
 66         {
 67             rs+=" "+r1[2*num-2]+"/"+r1[2*num-1]+" ";
 68         }
 69         return rs;
 70     }
 71     public static String properFractionExithk(int num,int scope)//产生含括号含有真分数的表达式
 72     {
 73         publicUse u =new publicUse();
 74         
 75         int []r1=new int[2*num];//接受产生的数值
 76         int []r2=new int[num-1];//接受符号
 77         String[]r3={"+","-","*","/"};
 78         int []r4=new int[num];//接受随机产生的括号
 79         String []r5=new String[2*num];//将接受的括号的个数,转成字符串
 80         String rs="";//接受含括号的和不含括号的表达式
 81         r4=u.chansheng(num);
 82         char ch='z';
 83         
 84         //产生数值
 85         while(ch=='z')
 86         {
 87             
 88             for(int i=0;i<2*num;i++)
 89             {
 90                 r1[i]=(int) (Math.random()*(scope-1)+1);
 91             }
 92             ch='y';
 93             int j=0;
 94             while(j<2*num)
 95             {
 96                 if(r1[j]>=r1[j+1])
 97                 {
 98                     ch='z';
 99                     break;
100                 }
101                 j++;
102                 j++;
103             }
104         }
105         
106         
107         //产生符号
108         for(int i=0;i<num-1;i++)
109         {
110             r2[i]=(int) (Math.random()*4);
111         }
112         
113         
114         //产生括号的数组
115         for(int i=0;i<2*num;i++)
116         {
117             r5[i]="";
118             if(i%2==0)
119             {
120                 if(r4[i/2]>0)
121                 {
122                     for(int j=0;j<r4[i/2];j++)
123                     {
124                         r5[i]+="(";
125                     }
126                 }
127                 else if(r4[i/2]<0)
128                 {
129                     for(int j=0;j<0-r4[i/2];j++)
130                     {
131                         r5[i]+=")";
132                     }
133                 }
134             }
135         }
136         
137         //添加到一个String类型的表达式中
138         int j=0;
139         while(j<2*num-2)
140         {
141             int commondivisor=u.maxyue(r1[j], r1[j+1]);
142             r1[j]/=commondivisor;
143             r1[j+1]/=commondivisor;
144                 if(r5[j].equals(""))
145                 {
146                     rs+=" "+r1[j]+"/"+r1[j+1]+" "+r3[r2[(j+1)/2]];
147                 }
148                 else if(r5[j].substring(0, 1).equals("("))
149                 {
150                     rs+=r5[j]+" "+r1[j]+"/"+r1[j+1]+" "+r3[r2[(j+1)/2]];
151                 }
152                 else
153                 {
154                     rs+=" "+r1[j]+"/"+r1[j+1]+" "+r5[j]+r3[r2[(j+1)/2]];
155                 }
156             
157             j++;
158             j++;
159         }
160         //算最后一个数
161         int commondivisor1=u.maxyue(r1[2*num-2],r1[2*num-1]);
162         r1[2*num-2]/=commondivisor1;
163         r1[2*num-1]/=commondivisor1;
164         
165             if(r5[2*num-2].equals(""))
166             {
167                 rs+=" "+r1[2*num-2]+"/"+r1[2*num-1]+" ";
168             }
169             else if(r5[2*num-2].substring(0, 1).equals("("))
170             {
171                 rs+=r5[2*num-2]+" "+r1[2*num-2]+"/"+r1[2*num-1]+" ";
172             }
173             else
174             {
175                 rs+=" "+r1[2*num-2]+"/"+r1[2*num-1]+" "+r5[2*num-2];
176             }
177         return rs;
178     }
179     
180 }
产生真分书类型的题目
  1 package util;
  2 
  3 import java.sql.SQLException;
  4 import java.util.Date;
  5 import java.util.Random;
  6 import java.util.Scanner;
  7 import java.util.Stack;
  8 
  9 public class publicUse {
 10 //    public static void main(String[] args) throws ClassNotFoundException, SQLException
 11 //    {
 12 //        
 13 //        System.out.println("请选择题目类型:1.真分数。2整数");
 14 //        int choose1=sc.nextInt();
 15 //        System.out.println("请输入产生题的个数");
 16 //        int num=sc.nextInt();
 17 //        System.out.println("请输入取值范围");
 18 //        int scope=sc.nextInt();
 19 //        System.out.println("请选择题目类型中是否有括号:1.有。2.没有");
 20 //        int choose2=sc.nextInt();
 21 //        String rs[]=new String[2*num];
 22 //        rs=operationAndStatistical(choose1, choose2, num,scope);
 23 //        for(int i=0;i<num;i++)
 24 //        {
 25 //            System.out.println(rs[i]+"="+rs[i+num]);
 26 //        }
 27 //    }
 28     
 29     public static Scanner sc=new Scanner(System.in);
 30     
 31     //真分数和整数都需要用到
 32     public static int maxyue(int y,int x)//最大公约数
 33     {
 34         int r=y;
 35         while(r!=0)
 36         {
 37             r=x%y;
 38             x=y;
 39             y=r;
 40         }
 41         return x;
 42     }
 43     public     static char youxian(String f,String s)//计算两个符号的优先级
 44     {
 45         char a1[][]={
 46                 {'>','>','<','<','<','>','>'},
 47                 {'>','>','<','<','<','>','>'},
 48                 {'>','>','>','>','<','>','>'},
 49                 {'>','>','>','>','<','>','>'},
 50                 {'<','<','<','<','<','=',' '},
 51                 {'>','>','>','>',' ','>','>'},
 52                 {'<','<','<','<','<',' ','='}
 53         };
 54         String a="+-*/()#";
 55         int a11=a.indexOf(f);
 56         int a12=a.indexOf(s);
 57         return a1[a11][a12];
 58     }
 59     static int [] chansheng(int num)//随机产生括号
 60     {
 61         int []b=new int[num];
 62         for(int i=0;i<b.length;i++)
 63         {
 64             b[i]=0;
 65         }
 66         Random rd=new Random();
 67         for(int i=2;i<num;i++)
 68         {
 69             for(int j=0;j<num-i+1;j++)
 70             {
 71                 int t=rd.nextInt(2);
 72                 if(t==1)
 73                 {
 74                     if(b[j]>=0&&b[j+i-1]<=0)
 75                     {
 76                         int c=0;
 77                         for(int k=j;k<j+i;k++)
 78                         {
 79                             c+=b[k];
 80                         }
 81                         if(c==0)
 82                         {
 83                             b[j]++;
 84                             b[j+i-1]--;
 85                         }
 86                     }
 87                     
 88                 }
 89             }
 90         }
 91         return b;
 92     }
 93         //运算
 94     public     static String jisuanbh(String a)//表达式的运算
 95     {
 96         Stack <String>num=new Stack <String>();
 97         Stack <String>fuhao=new Stack<String>();
 98         a+="#";
 99         fuhao.push("#");
100         char ch;
101         int i=0;
102         int s=0;
103         int y=0;
104         ch=a.charAt(i);
105         while(!(ch+"").equals("#") || !fuhao.peek().equals("#"))
106         {
107             if(ch==' ')//如果遇到字符为空,说明遇到数字
108             {
109                 String rn="";//用来记录数据
110                 while(true)
111                 {
112                     ch=a.charAt(++i);
113                     if(ch==' ')
114                     {
115                         break;
116                     }
117                     rn+=ch;
118                 }
119                 if((i+1)<a.length()){
120                     ch=a.charAt(++i);
121                 }
122                 num.push(rn);
123             }
124             else//遇到的是字符
125             {
126                 char comp=youxian(fuhao.peek(),ch+"");//比较两个字符的优先级
127                 if(comp=='='){//说明遇到右括号
128                     fuhao.pop();
129                     if((i+1)<a.length()){
130                         ch=a.charAt(++i);
131                     }
132                 }
133                 else if(comp=='>')//优先级高,弹出两个数和一个运算符,进行运算
134                 {
135                     String st1=num.pop();
136                     String st2=num.pop();
137                     String fuh1=fuhao.pop();
138                     char fuh2=fuh1.charAt(0);//将String类型转为char类型
139                     String []rs1=new String[2];
140                     rs1=yunsuan2(st2, st1, fuh1);
141                     if(rs1[1].equals("error"))//如果运算中有问题,就结束运算
142                     {
143                         return "error";
144                     }
145                     else
146                     {
147                         num.push(rs1[0]+"");//将两数结果压入栈中
148                     }
149                 }
150                 else//优先级比较低,把运算符压入栈中
151                 {
152                     fuhao.push(ch+"");
153                     if((i+1)<a.length())
154                     {
155                         ch=a.charAt(++i);
156                     }
157                 }
158             }
159         }
160         String rs=num.pop();
161         int wz=rs.indexOf("/");
162         if(wz!=-1)
163         {
164             String fb=rs.substring(0, wz);
165             String sb=rs.substring(wz+1,rs.length());
166             int fb1=Integer.parseInt(fb);
167             int sb1=Integer.parseInt(sb);
168             if(fb1>=sb1&&fb1%sb1==0)
169             {
170                 
171                 rs=(fb1/sb1)+"";
172             }
173             else if(fb1<sb1&&fb1%sb1!=0)
174             {
175                 int commondivisor=maxyue(fb1, sb1);
176                 fb1/=commondivisor;
177                 sb1/=commondivisor;
178                 rs=fb1+"/"+sb1;
179             }
180             else
181             {
182                 int commondivisor=maxyue(fb1, sb1);
183                 fb1/=commondivisor;
184                 sb1/=commondivisor;
185                 rs=(fb1/sb1)+"'"+(fb1%sb1)+"/"+sb1;
186             }
187         }
188         return rs;
189     }
190     public  static String[] tys(String fn,String sn,char c)//两个整数的运算
191     {
192         int a=Integer.parseInt(fn);
193         int b=Integer.parseInt(sn);
194         String []a1=new String [2];//a1[0]用来记录两数运算结果,a1[1]用来记录两数能否继续算下去
195         a1[0]=a1[1]="";
196         int d=0;//d用来短暂存取两数运算结果
197         int z=0;//除法中判断a1[0]是否需要加上d
198         if(c=='+')
199         {
200             d=a+b;
201         }
202         else if(c=='-')
203         {
204             if(a<b)
205             {
206                 a1[1]="error";
207                 return a1;
208             }
209             else
210             {
211                 d=a-b;
212             }
213         }
214         else if(c=='*')
215         {
216             d=a*b;
217         }
218         
219         else
220         {
221             if(a%b==0&&a>=b)
222             {
223                 d=a/b;
224             }
225             else
226             {
227                 z=1;
228                 a1[0]=a+"/"+b;
229             }
230         }
231         if(z==0)
232         {
233             a1[0] = d+"";
234         }
235         return a1;
236     }
237     public static String[] yunsuan2(String fn,String sn,String e)//两个数运算,分数,整数均可
238     {
239         String rs[]=new String[2];
240         rs[0]=rs[1]="";
241         int location1=fn.indexOf("/");
242         int location2=sn.indexOf("/");
243         if(location1==-1&&location2==-1)//两个整数的运算
244         {
245             rs=tys(fn, sn, e.charAt(0));
246         }
247         else{
248             int a=0;
249             int b=0;
250             int c=0;
251             int d=0;
252             if(location1!=-1&&location2!=-1)//两个数都为真分数
253             {
254                 String r1=fn.substring(0,location1);
255                 String r2=fn.substring(location1+1,fn.length());
256                 String r3=sn.substring(0,location2);
257                 String r4=sn.substring(location2+1,sn.length());
258                 a=Integer.parseInt(r1);
259                 b=Integer.parseInt(r2);
260                 c=Integer.parseInt(r3);
261                 d=Integer.parseInt(r4);
262             }
263             else
264             {
265                 if(location1==-1)
266                 {
267                     a=Integer.parseInt(fn);
268                     b=1;
269                     String r3=sn.substring(0,location2);
270                     String r4=sn.substring(location2+1,sn.length());
271                     c=Integer.parseInt(r3);
272                     d=Integer.parseInt(r4);
273                 }
274                 else
275                 {
276                     c=Integer.parseInt(sn);
277                     d=1;
278                     String r1=fn.substring(0,location1);
279                     String r2=fn.substring(location1+1,fn.length());
280                     a=Integer.parseInt(r1);
281                     b=Integer.parseInt(r2);
282                 }
283             }
284             int f=0,g=0,h=0,t=0;
285             if(e.equals("+"))
286             {
287                 if(b==d)
288                 {
289                     f=a+c;
290                     g=b;
291                 }
292                 else
293                 {
294                     g=b*d/maxyue(b,d);
295                     a=g/b*a;
296                     c=g/d*c;
297                     f=a+c;
298                 }
299             }
300             else if(e.equals("-"))
301             {
302                 if(b==d)
303                 {
304                     f=a-c;
305                     if(f<=0)
306                     {
307                         rs[1]="error";
308                         return rs;
309                     }
310                     g=b;
311                 }
312                 else
313                 {
314                     g=b*d/maxyue(b,d);
315                     a=g/b*a;
316                     c=g/d*c;
317                     f=a-c;
318                     if(f<0)
319                     {
320                         rs[1]="error";
321                         return rs;
322                     }
323                 }
324             }
325             else if(e.equals("*"))
326             {
327                 f=a*c;
328                 g=b*d;
329             }
330             else
331             {
332                 f=a*d;
333                 g=b*c;
334             }
335             rs[0]=f+"/"+g;
336             
337         }
338         
339         return rs;
340     }
341     public static String[] operationAndStatistical(int ch1,int ch2,int num,int scope) throws ClassNotFoundException, SQLException//对结果进行操作并统计题数
342     {
343         properFraction p =new properFraction();
344         integer i1= new integer();
345         String rs="";
346         int i=0;
347         String saveexcep[]=new String[2*num];
348         while(i<num)
349         {
350             int n=(int) (Math.random()*3+2);
351             if(ch1==1&&ch2==1)
352             {
353                 rs=p.properFractionExithk(n,scope);//产生含有括号的真分数表达式
354             }
355             else if(ch1==1&&ch2==2)
356             {
357                 rs=p.properFractionExit(n,scope);//产生不含括号的真分数表达式
358             }
359             else if(ch1==2&&ch2==1)
360             {
361                 rs=i1.generateExpressionkh(n,scope);//产生含括号的整数表达式
362             }
363             else 
364             {
365                 rs=i1.generationexception(n,scope);//产生不含括号的整数表达式
366             }
367             String judgers=jisuanbh(rs);//记录答案
368             
369             if(judgers.equals("error"))
370             {
371                 System.out.print("");
372             }
373             else
374             {
375                 saveexcep[i]=rs;
376                 saveexcep[i+num]=judgers; 
377                 i++;
378             }
379         }
380         return saveexcep;
381     }
382 
383 }
整数和真分数都需要用到的方法
 1 package util;
 2 
 3 public class solve {
 4     public boolean[] judgeIfTrue(String[] a,String[] b)//对传过来的两个数组进行比较,正确的数对应1返回
 5     {
 6         boolean judgers[] = new boolean [a.length];
 7         for(int i=0;i<a.length;i++)
 8         {
 9             judgers[i]=false;
10             if(a[i].equals(b[i]))
11             {
12                 judgers[i]=true;
13             }
14         }
15         
16         return judgers;
17     }
18     public int[] zongjie(boolean[] judeger)
19     {
20         
21         int[] statisticaltrue = new int[2];//用来计算正确和错误的题数
22         statisticaltrue[0]=0;
23         statisticaltrue[1]=0;//初始化
24          for(int i=0;i<judeger.length;i++)
25         {
26             if(judeger[i]==true)
27             {
28                 statisticaltrue[0]+=1;
29             }
30             else
31             {
32                 statisticaltrue[1]+=1;
33             }
34         }
35          return statisticaltrue;
36     }
37     public String[] qiq (boolean []judgers)
38     {
39         String []rs =new String[2];//分别用来记录正确和错误的题号
40         rs[0]="正确的题目题号如下:";
41         rs[1]="错误的题目题号如下:";
42         for(int i=0;i<judgers.length;i++)
43         {
44             if(judgers[i]==true)
45             {
46                 rs[0]+=(i+1)+" ";
47             }
48             else
49             {
50                 rs[1]+=(i+1)+" ";
51             }
52         }
53         return rs;
54     }
55     public String[]countexpression(String[]bds,String[]correctrs,String[]inputrs, boolean[]judgers)
56     {
57         String[]count1 = new String[judgers.length];
58         for(int i=0;i<judgers.length;i++)
59         {
60             if(judgers[i]==true)
61             {
62                 count1[i]="第"+(i+1)+"道题:	"+bds[i]+"="+correctrs[i]+"	"+"回答正确";
63                 
64             }
65             else
66             {
67                 count1[i]="第"+(i+1)+"道题:	"+bds[i]+"="+inputrs[i]+"	"+"回答错误,"+"正确答案是"+correctrs[i];
68 
69             }
70         }
71         return count1;
72     }
73 }
判断题目是否正确

4.结果截图:

(1)选择查看还是做题

(2)选择做题:

(3)做题:

(4)判断正确:

查询历史记录:

 

 这个查询界面做的还不算完善,后面会进行改进。。。

在其中加入了js的基本的判空

 

5.实验总结:通过对这个简单的程序的编写,能基本的使用jsp Javabean和servlet来实现一个最小的网页版的程序,在通过链接数据库,使用jdbc技术实现数据的存储和查询,

这是我对MVC框架有了一个更加深刻的认识,在通过加入js,是这个程序更加符合人们的需求,虽然我现在只是用了这个的皮毛,但是我以后越来越熟练地掌握,另外,我还准备

加上jquare和ajax来使界面更加合理,我会使我这个程序更加完善。

  6.合作感受:两个人一起写时跟一个写是不一样的,我比较熟悉整体的结构,他比较细心,再设计页面和找错误方面比较擅长,通过这次练习找到我们自己的不足之处,我们

收货到了更多。

                                                                          程序完成者:    王志伟    胡洋洋

原文地址:https://www.cnblogs.com/qingtianxt/p/6679771.html