码迷,mamicode.com
首页 > 其他好文 > 详细

四则运算三

时间:2018-01-25 10:51:25      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:attribute   elf   mvc   eof   个数   show   key   操作   ack   

 

 

一、程序设计思路

在详细的查看了观察《冀教版数学二年级上册口算练习》文档之后,可以发现:1.算式中每个数不能超过100,运算结果不能超过100。2.运算结果必须得是正整数,除法不能出现余数。3.乘法算式的范围在9 X 9乘法表内,除法算式同样。4.可以出现一部分混合运算,但仅限于加减法而且不带括号优先级。

根据上述规则,可以采用如下方式产生算式:

首先可以明确,在一定范围内,随机数的产生结果不会有大规模的重复现象,查重很耗费时间。

操作符通过1-4的随机数产生,一个数对应一个操作符。

对于加法算式,第一个操作数opre1直接在1-99内生成,第二个操作数在100-opre1到1之间生成。

对于减法算式,第一个操作数opre1直接在1-99内生成,第二个操作数在opre1到1之间生成。

对于乘法,两个操作数均在1-9内产生。

对于除法,先产生两个1-9之间的随机数opre1、opre2,然后让opre1 = opre1*opre2,opre2不变,opre1作为算式的答案。

对于三个操作数的混合运算,原理同上。

由于要改成网页版,所以按照MVC的框架模式来划分:

视图(View)有三个界面,首页获取小孩儿输入的题目数量(用JavaScript脚本检验输入数据的合法性),将这个题目数量发送到题目展示界面proshow.jsp,这个界面会使用GradeTwo中产生算式的静态方法,并产生答案。在这个界面上要输入计算结果,点击提交后将答案发送到Servlet中判断结果。Servlet处理后会将结果信息发送到result.jsp中显示。

模型(Model)就是GradeTwo这个类,这个类中有好几个静态变量的集合,分别储存算式、答案、结果。在每次重新产生算式时,三个集合清空。这个类中还有产生表达式的方法、判断答案对错的方法,供外界调用。

控制(Control)就是Servlet,主要就是把proshow.jsp传递过来的结果进行验证,并把结果集传递给result.jsp中显示,没有其他的流程了。

二、源代码

 

  1 import java.util.ArrayList;
  2 import java.util.List;
  3 import java.util.Random;
  4 import java.util.Set;
  5 import java.util.Vector;
  6 
  7 public class GradeTwo {
  8     private static String opera = "+-*/";
  9     public static Vector<String> pro = new Vector<String>();
 10     public static Vector<String> key = new Vector<String>();
 11     //public static Vector<String> key1 = new Vector<String>();
 12     public static String[]result1;
 13     public static int[]end = new int[3];
 14     public static void solve(String[]list) {
 15         result1 = new String[list.length];
 16         for(int i = 0;i<list.length;++i)
 17             result1[i]= list[i];
 18     }
 19     public static void clear() {
 20         pro.clear();
 21         key.clear();
 22     }
 23     public static int isInteger(int c1, int c2, char opera) {
 24         int result = 0;
 25         int result1 = 0;
 26         switch (opera) {
 27         case ‘+‘:
 28             result = c1 + c2;
 29             break;
 30         case ‘-‘:
 31             result = c1 - c2;
 32             break;
 33         case ‘*‘:
 34             result = c1 * c2;
 35             break;
 36         case ‘/‘:
 37             result = c1 / c2;
 38             result1 = c1 % c2;
 39             break;
 40         }
 41         if (result >= 0 && result <= 100 && result1 == 0)
 42             return result;
 43         else
 44             return -1;
 45     }
 46 
 47     public static String getPoly() {
 48         String poly = "";
 49 //        Random random = new Random();
 50 //        
 51 //        int c1;
 52 //        int c2;
 53 //        int result;
 54 //        char operac;
 55 //        boolean flag = true;
 56 //        do {
 57 //            c1 = random.nextInt(100) + 1;
 58 //            c2 = random.nextInt(100) + 1;
 59 //            operac = opera.charAt(random.nextInt(4));
 60 //            if (operac == ‘*‘)
 61 //                poly = c1 + " × " + c2 + " = ";
 62 //            if (operac == ‘/‘)
 63 //                poly = c1 + " ÷ " + c2 + " = ";
 64 //            if (operac == ‘+‘)
 65 //                poly = c1 + " + " + c2 + " = ";
 66 //            if (operac == ‘-‘)
 67 //                poly = c1 + " - " + c2 + " = ";
 68 //            /*for (int i = 0;i<pro.size();++i) {
 69 //                if (poly.equals(pro.get(i))) {
 70 //                    flag = false;
 71 //                    break;
 72 //                }
 73 //            }*/
 74 //        } while (!flag || (result = isInteger(c1, c2, operac)) < 0);
 75 //        pro.add(poly);
 76 //        key.add(String.valueOf(result));
 77         Random random = new Random();
 78         //产生0.9的两位运算,0.1的三位运算
 79         char operate;
 80         char operate1;
 81         int operand1 = 0;
 82         int operand2 = 0;
 83         int operand3 = 0;
 84         int result;
 85         int percent = random.nextInt(100)+1;
 86         if(percent<=90) {
 87             operate = opera.charAt(random.nextInt(4));
 88             switch(operate) {
 89                case ‘+‘:
 90                    operand1 = random.nextInt(100);
 91                    operand2 = random.nextInt(100-operand1);
 92                    poly = operand1+" + "+operand2+" = ";
 93                    break;
 94                case ‘-‘:
 95                    operand1 = random.nextInt(100)+1;
 96                    operand2 = random.nextInt(operand1);
 97                    poly = operand1+" - "+operand2+ " = ";
 98                    break;
 99                case ‘*‘:
100                    operand1 = random.nextInt(9)+1;
101                    operand2 = random.nextInt(9)+1;
102                    poly = operand1+" × "+operand2+ " = ";
103                    break;
104                case ‘/‘:
105                    operand1 = random.nextInt(9)+1;
106                    operand2 = random.nextInt(9)+1;
107                    operand1 = operand1*operand2;
108                    poly = operand1+" ÷ "+operand2+ " = ";
109                    break;
110             }
111             result = isInteger(operand1, operand2, operate);
112             key.add(String.valueOf(result));
113         }else {
114             operate = opera.charAt(random.nextInt(2));
115             operate1 = opera.charAt(random.nextInt(2));
116             if(operate==‘+‘) {
117                 operand1 = random.nextInt(100);
118                 operand2 = random.nextInt(100-operand1);
119                 if(operate1==‘+‘) {
120                     operand3 = random.nextInt(100-operand1-operand2);
121                     poly = operand1+" + "+operand2+" + "+operand3+" = ";
122                 }else {
123                     operand3 = random.nextInt(operand1+operand2);
124                     poly = operand1+" + "+operand2+" - "+operand3+" = ";
125                 }
126             }else {
127                 operand1 = random.nextInt(100)+1;
128                 operand2 = random.nextInt(operand1);
129                 if(operate1==‘+‘) {
130                     operand3 = random.nextInt(100-operand1+operand2);
131                     poly = operand1+" - "+operand2+" + "+operand3+" = ";
132                 }else {
133                     operand3 = random.nextInt(operand1-operand2);
134                     poly = operand1+" - "+operand2+" - "+operand3+" = ";
135                 }
136             }
137             result = isInteger(operand1, operand2, operate);
138             result = isInteger(result, operand3, operate1);
139             key.add(String.valueOf(result));
140         }
141         pro.add(poly);
142         return poly;
143     }
144     public static void End(String[]list){
145         //int[]result = new int[3];
146         end[0] = 0;
147         end[1] = 0;
148         end[2] = 0;
149         int len = list.length;
150         for(int i = 0;i<len;++i) {
151             if(key.get(i).equals(list[i]))end[0]++;
152             else {
153                 if(list[i].equals(""))end[2]++;
154                 else end[1]++;
155             }
156         }
157         return;
158     }
159     public static void main(String[]args) {
160               Scanner s = new Scanner(System.in);
161             System.out.print("输入题目数量:");
162             int num = s.nextInt();
163             String str = null;
164             for(int i = 1;i<=num;++i) {
165                 str = GradeTwo.getPoly();
166                 System.out.println(str);
167             }
168     }
169 }

 

 1 import CC.GradeTwo;
 2 import javax.servlet.http.HttpServlet;
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 
 8 
 9 
10 public class GradeServlet extends HttpServlet{
11     public void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
12         String method = req.getParameter("method");
13         if(method.equals("end"))end(req,resp); 
14     }
15     public void end(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
16            String[]keys = req.getParameterValues("list");
17            //req.setAttribute("result",GradeTwo.end(keys));
18            GradeTwo.End(keys);
19            for(int i = 0;i<keys.length;++i)
20                if(keys[i].equals(""))keys[i] = "未答";
21            GradeTwo.solve(keys);
22            //req.setAttribute("result1",keys);
23            //req.getRequestDispatcher("").forward(req, resp);
24            resp.sendRedirect("../result.jsp");
25            return;
26     }
27 }
 1 import javax.servlet.http.HttpServlet;
 2 import java.io.IOException;
 3 
 4 import javax.servlet.Filter;
 5 import javax.servlet.FilterChain;
 6 import javax.servlet.FilterConfig;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.ServletRequest;
 9 import javax.servlet.ServletResponse;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import javax.servlet.http.HttpSession;
13 
14 public class KeyFilter implements Filter{
15     protected FilterConfig filterConfig;
16     @Override
17     public void destroy() {
18         // TODO 自动生成的方法存根
19         filterConfig = null;
20     }
21 
22     @Override
23     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
24             throws IOException, ServletException {
25         // TODO 自动生成的方法存根
26           HttpServletRequest req = (HttpServletRequest)request;
27           HttpServletResponse resp = (HttpServletResponse)response;
28           HttpSession session = req.getSession();
29           String flag = (String)session.getAttribute("online");
30           String reqURL = req.getServletPath();
31           if(!reqURL.equals("/index.jsp")&&flag==null) {
32               resp.sendRedirect("index.jsp");
33               return;
34           }
35           chain.doFilter(request,response);
36     }
37 
38     @Override
39     public void init(FilterConfig filterConfig) throws ServletException {
40         // TODO 自动生成的方法存根
41          this.filterConfig = filterConfig;
42     }
43      
44 }
 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 2 <html>
 3 <head>
 4 <%@ page language="java" contentType="text/html; charset=UTF-8"
 5     pageEncoding="UTF-8"%>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>小学二年级算式计算</title>
 8 <style type="text/css">
 9 body{
10     font-family:微软雅黑;
11     background-repeat: no-repeat;
12     background-size:100%;
13 } 
14 #t1{
15 position:absolute;
16 width:400px;
17 height:260px;
18 left:0;
19 right:0;
20 top:0;
21 bottom:0;
22 margin:auto;
23 color:#FFFFFF;
24 background-color:#000000;
25 opacity:0.4;
26 box-shadow:0px 0px 10px #777777;
27 border-radius:10px 10px 10px 10px;
28 }
29 #t2{
30 position:absolute;
31 width:400px;
32 height:25px;
33 top:15px;
34 font-size:25px;
35 }
36 #t3{
37 position:absolute;
38 width:400px;
39 height:180px;
40 top:70px;
41 }
42 </style>
43 
44 </head>
45 <script type="text/javascript">
46 function check(form){
47     str = form.num.value;
48     if(str==""){
49         alert("题目数量不能为空!");
50         return false;
51     }
52     for(i=0;i<str.length;++i)
53         if(str[i]<0||str[i]>9){
54             alert("请输入合法的数字!");
55             return false;
56         }
57     num = parseInt(str);
58     if(num<=0||num>60){
59         alert("题目数量请固定在1到60之间");
60         return false;
61     }
62     return true;
63 }
64 
65 </script>
66 <div align="center"><%@ include file="./music.jsp" %></div>
67 <body background="timg.jpg">
68 <div id = "t1">
69 <div id="t2" align="center">小学二年级算式计算</div>
70 <div id="t3" align="center">
71 <p align="center">本页仅供二年级的小同学测试使用,100以内加减乘除算式计算题,每道题限时8秒钟。</p>
72 <%session.setAttribute("online", "true"); %>
73 <form name="form1" action="proshow.jsp" method="post" onsubmit="return check(form1)">
74      <p><label>输入题目数量:</label><label><input type="text" name="num"></label></p>
75      <p><label ><input type="submit" value="确定"></label></p>
76 </form>
77 </div>
78 </div>
79 
80 </body>
81 </html>
 1 <%@page import="CC.GradeTwo" %>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4 <head>
 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 6 <script type="text/javascript" src=JS/jquery.min.js charset=“GBK”></script>
 7 <script type="text/javascript" src=JS/jrj6out.js charset=“GBK”></script>
 8 <script type="text/javascript" src=JS/prefixfree.min.js charset=“GBK”></script>
 9 <script type="text/javascript" src=JS/three.min.js charset=“GBK”></script>
10 <script type="text/javascript" src=JS/TweenMax.min.js charset=“GBK”></script>
11 
12 <link href="CSS/style.css" rel="stylesheet">
13 <%@ page language="java" contentType="text/html; charset=UTF-8"
14     pageEncoding="UTF-8"%>
15 <meta http-equiv="refresh" content="text/html; charset=UTF-8">
16 <script type="text/javascript">
17       var num = <%=request.getParameter("num")%>
18       var maxtime = 8*parseInt(num);
19       function CountDowm(){
20           if(maxtime>=0){
21               minutes = Math.floor(maxtime/60);
22               seconds = Math.floor(maxtime%60);
23               if(seconds>=10)
24                   msg = "剩余时间:"+minutes+":"+seconds;
25               else
26                   msg = "剩余时间:"+minutes+":0"+seconds;
27               document.all["timer"].innerHTML = msg;
28               --maxtime;
29           }
30           else{
31               document.forms("form1").submit();
32           }
33       }
34       timer = setInterval("CountDowm()",1000);
35 </script>
36 <style>
37     form{
38         background-color: rgba(255,255,255,0);
39     }
40     .wrapper{
41         display: absolute;
42         top:0;
43         right:0;
44         width:100%;
45     }
46 </style>
47 <title>小学二年级算式计算</title>
48 </head>
49 <%
50      String str = null;
51      int num = Integer.parseInt(request.getParameter("num"));
52 %>
53 <body>
54 
55 <%
56     request.setCharacterEncoding("utf-8");
57 %>
58 
59 <div class="intro-container">
60 <div style="overflow: auto">
61 <jsp:include flush="true" page="inc.jsp">
62    <jsp:param name="color" value="blue" />
63    <jsp:param name="title" value="小学二年级" />
64    <jsp:param name="body" value="数学口算题" />
65 </jsp:include>
66 <%@ include file="./music.jsp" %>
67 <div id="timer"></div>
68 <form name="form1" action="${pageContext.request.contextPath}/Servlet/GradeServlet?method=end" target="_self" method="post">
69       <table align="center" style="color:white">
70       <tr><th colspan="3"><%=num %>题,注意用时</th></tr>
71           <%    GradeTwo.clear();
72                 for(int i = 1;i<=num;++i)      {    
73                     str = GradeTwo.getPoly();
74 
75       %>
76       <tr><td><%=i %></td><td><%=str %></td><td><input type="text" name="list"></td></tr>
77       <%
78                 }
79       %>
80       <tr><td colspan="3" align="center"><input type="submit" value="提交"></td></tr>
81       </table>
82 </form>
83 </div>
84 </div>
85 <script type="text/javascript" src=JS/index.js charset=“GBK”></script>
86 </body>
87 </html>
 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 2 <html>
 3 <head>
 4 <%@page import="CC.GradeTwo" %>
 5 <%@ page language="java" contentType="text/html; charset=UTF-8"
 6     pageEncoding="UTF-8"%>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <script type="text/javascript" src=JS/jquery.min.js charset=“GBK”></script>
 9 <script type="text/javascript" src=JS/jrj6out.js charset=“GBK”></script>
10 <script type="text/javascript" src=JS/prefixfree.min.js charset=“GBK”></script>
11 <script type="text/javascript" src=JS/three.min.js charset=“GBK”></script>
12 <script type="text/javascript" src=JS/TweenMax.min.js charset=“GBK”></script>
13 
14 <link href="CSS/style.css" rel="stylesheet">
15 <title>答案</title>
16 </head>
17 <body>
18 <div class="intro-container">
19 <div style="overflow: auto">
20 <%@ include file="./music.jsp" %>
21 <h3 align="center" style="color:red">答题结果</h3>
22 <%
23    int[]end = GradeTwo.end;
24    String str[] = GradeTwo.result1;
25 %>
26 
27 <table align="center" border="1">
28     <tr><th>答对:</th><td><%=end[0] %></td></tr>
29     <tr><th>答错:</th><td><%=end[1] %></td></tr>
30     <tr><th>未答:</th><td><%=end[2] %></td></tr>
31 </table><p>
32 <p align="center" style="color:red">答题情况</p>
33 <table align="center" border="1">
34    <tr><td>题目</td><td>提交答案</td><td>正确答案</td></tr>
35    <%   
36        String color = "";
37        int num = GradeTwo.key.size();
38        for(int i = 0;i<num;++i){
39           if(GradeTwo.key.get(i).equals(str[i])){
40    %>
41        <tr><td><%=GradeTwo.pro.get(i) %></td><td style="color:green"><%=str[i] %></td><td><%=GradeTwo.key.get(i) %></td></tr>
42    <%}
43           else{
44    %>
45    <tr><td><%=GradeTwo.pro.get(i) %></td><td style="color:red"><%=str[i] %></td><td><%=GradeTwo.key.get(i) %></td></tr>
46    
47    <%
48    }
49        }
50    %>
51 </table>
52 <div align="center"><input type="button" value="再来一次" onclick="window.location.href=‘<%=request.getContextPath()%>/index.jsp‘" /></div>
53 </div>
54 </div>
55 <script type="text/javascript" src=JS/index.js charset=“GBK”></script>
56 </body>
57 </html>

三、验证截图

 

技术分享图片

技术分享图片

技术分享图片

 

 

四则运算三

标签:attribute   elf   mvc   eof   个数   show   key   操作   ack   

原文地址:https://www.cnblogs.com/messi2017/p/8349726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!