四则运算三

Posted messi2017

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算三相关的知识,希望对你有一定的参考价值。

 

 

一、程序设计思路

在详细的查看了观察《冀教版数学二年级上册口算练习》文档之后,可以发现: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     日常Geetest滑动验证码(三代canvas版)处理小结(以B站登录验证为例)

四则运算

Android 逆向加壳技术简介 ( 动态加载 | 第一代加壳技术 - DEX 整体加固 | 第二代加壳技术 - 函数抽取 | 第三代加壳技术 - VMP / Dex2C | 动态库加壳技术 )

获取 badarith,[erlang,'+',[error,0],[],同时使用 Erlang 片段在 TSUNG 中执行算术运算

python实战演练计算器

pbootcms对接微信扫码登录代码核心片段和步骤(前后端)