package tool; import java.util.*; import java.util.List; import java.util.Scanner; public class formula { public void runing(int numtotal,int formtotal,int numrange,boolean muland,boolean negnum) { Scanner in=new Scanner(System.in); System.out.println("请输入 one fromula use time:"); RandomFormual B=new RandomFormual (numtotal,formtotal,numrange, muland,negnum); List<String> test = new ArrayList<String>(); List<String> fals = new ArrayList<String>(); List<Integer> fals_anws = new ArrayList<Integer>(); test=B.getFormulas(); double correctrat=0; List<Integer> anwser = B.getAnswer0sList(test); while(true) { do { int all=test.size(); correctrat=0; if(all==0) break; System.out.println(test.size());//test use for(int i=0;i<test.size();i++) System.out.println(i+1+". "+test.get(i)+"=");//test use System.out.println("=");//test use for(int i=0;i<test.size();i++) { System.out.print(i+1+". "+test.get(i)+"= "); System.out.print(" 答案:"); if(anwser.get(i)==in.nextInt()) { System.out.println(" 答案 is:(tset use)"+anwser.get(i)); correctrat++; test.remove(i); anwser.remove(i); } fals=test; fals_anws.addAll(anwser); } System.out.println("是做下一套题还是做错题 ture or false"); if(in.nextBoolean()) { break; } System.out.println(correctrat/all); System.out.println("全对了吗,有没有超时,没通过继续吧"); }while((correctrat/=test.size())!=1); System.out.println("好吧,你过了。"); System.out.println("是否继续 true or false"); if(in.nextBoolean()); break; } System.out.println("是否查看错题加答案 true or false"); if(in.nextBoolean()); for(int i=0;i<fals.size();i++) System.out.println(i+". "+fals.get(i)+"="+fals_anws.get(i)); System.out.println("是否根据错题进行答题 true or false"); if(in.nextBoolean()) { do { for(int i=0;i<fals.size();i++) System.out.println(i+1+". "+fals.get(i)+"=");//test use System.out.println("=");//test use for(int i=0;i<test.size();i++) { System.out.print(i+1+". "+fals.get(i)+"= "); if(fals_anws.get(i)==in.nextInt()) { fals.remove(i); fals_anws.remove(i); } } System.out.println("是做下一套题还是做错题 ture or false"); if(in.nextBoolean()) { break; } }while(true); } } }
package tool; import java.util.*; public class RandomFormual { public int numberTotal;//运算操作数 public int formulaTotal;//运算式数量 public int numberRange;//数值范围 public boolean includeMulAndDiv;//是否包含乘除 public boolean includeNegNum;//是否包含负数 public RandomFormual() { this.numberTotal = 2; this.formulaTotal = 30; this.numberRange = 100; this.includeMulAndDiv = false; this.includeNegNum = false; } public RandomFormual(int numberTotal, int formulaTotal, int numberRange, boolean includeMulAndDiv, boolean includeNegNum) { this.numberTotal = numberTotal; this.formulaTotal = formulaTotal; this.numberRange = numberRange; this.includeMulAndDiv = includeMulAndDiv; this.includeNegNum = includeNegNum; } // 获取随机数 public int getRandomNumber() { Random rand = new Random(); if (this.includeNegNum) { return (rand.nextInt(this.numberRange) + 1) * (rand.nextDouble() > 0.5 ? 1 : -1); } else { return rand.nextInt(this.numberRange) + 1; } } //得到一个随机的运算符 public String getRandomOperator() { Random rand = new Random(); String[] operations = { "+", "-", "*", "/" }; return operations[rand.nextInt((this.includeMulAndDiv == true) ? 4 : 2)]; } // 生成算式 public String generateFormula() { String formula = ""; for (int i = 0; i < this.numberTotal; i++) { if (i >= this.numberTotal - 1) { formula += random_kuohao(this.getRandomNumber())+")"; continue; } formula +="("+ random_kuohao(this.getRandomNumber()) + " " + this.getRandomOperator() + " "; } return formula; } //生成算式集合 public List<String> getFormulas() { List<String> set = new ArrayList<String>(); set.add(this.generateFormula()); String str="/"; while (set.size() <= this.formulaTotal){ for(int i=0;i<set.size();i++) { String formula=this.generateFormula(); if(str.equals(formula)) if(this.getAnswer(formula)%1!=0) continue; if(!formula.equalsIgnoreCase(set.get(i))) { set.add(formula); break; } } } return set; } //若负数,加括号 public String random_kuohao(int num) { if (this.getRandomNumber()%2==0) { return "(" + num ; } return +num+")" ; } //生成算式结果 public int getAnswer(String formula) { int length = 0; String[] formulaArr = formula.split(" "); String operators = "+-*/"; Stack<Integer> opNumbers = new Stack<Integer>(); Stack<String> opOperators = new Stack<String>(); opOperators.add("#"); while (length < formulaArr.length) { String op = formulaArr[length++]; if (operators.indexOf(op) > -1) { String sign = opOperators.peek(); int priority = compare(op, sign); if (priority >= 0) { opNumbers.add(compute(opOperators, opNumbers)); opOperators.add(op); } else { opOperators.add(op); } continue; } opNumbers.add(Integer.parseInt(op.replace("(", "").replace(")", ""))); } while (opOperators.peek() != "#") { opNumbers.add(compute(opOperators, opNumbers)); } return opNumbers.pop(); } //比较运算优先级 public int compare(String operator1, String operator2) { int res = 0; switch (operator1) { case "+": case "-": if (operator2.equals("+") || operator2.equals("-") || operator2.equals("*") || operator2.equals("/")) { res = 1; } else { res = -1; } break; case "*": case "/": if (operator2.equals("*") || operator2.equals("/")) { res = 1; } else { res = -1; } break; } return res; } //算式求值 public int compute(Stack<String> opOperators, Stack<Integer> opNumbers) { int num2 = opNumbers.pop(); int num1 = opNumbers.pop(); String _op = opOperators.pop(); int result = 0; switch (_op) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": result = num1 / num2; break; } return result; } //生成算式结果数组 public int[] getAnswers(List<String> set) { int[] arr = new int[set.size()]; int i = 0; for (String str : set) { arr[i++] = getAnswer(str); } return arr; } public List<Integer> getAnswer0sList(List<String> set) { List<Integer> arr = new ArrayList<Integer>(); for (String str : set) { arr.add( getAnswer(str)); } return arr; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="tool.*" %> <%@ page import="java.util.*" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>好了吗</title> </head> <body> <p> <font size="4">请输入你的选择:<br></font> <font size="2"><input type="checkbox" name="one" value="1"></font> <font size="2">1.自定义<br></font> <font size="2"><input type="checkbox" name="two" value="2"></font> <font size="2">2.二年级程度<br></font> <font size="2"><input type="checkbox" name="three" value="3"></font> <font size="2">3.三年级程度<br></font> <font size="2"><input type="checkbox" name="four" value="4"></font> <font size="2">4.四年级程度<br></font> </p> <form action="" method="get" name="form" id="form"> <font size="2"><input type="number" name="choice" min="1" max="4" value=NULL> <input type="submit" value="确定" name="submit"></font> </form> <% String str = request.getParameter("choice"); int r=0; if(str != null) { r = Integer.parseInt(str); out.print("test use : "+r+"<br/>"); } else r = 0; if(r==1) { request.getRequestDispatcher("/byHemself.jsp").forward(request, response); } else if(r==2) { request.getRequestDispatcher("/two.jsp").forward(request, response); out.print("测试用输出选择:"+r+"<br/>"); } else if(r==3) { request.getRequestDispatcher("/three.jsp").forward(request, response); out.print("测试用输出选择:"+r+"<br/>"); } else if(r==4) { request.getRequestDispatcher("/four.jsp").forward(request, response); out.print("测试用输出选择:"+r+"<br/>"); } else { out.println(" "); } %> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="tool.*" %> <%@ page import="java.util.*" %> <%@ page import="java.io.*" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta charset="UTF-8"> <title>自选</title> </head> <body> <p> <font size="2">请输入运算操作数 </font> <form action="" method="get" name="form" id="form"> <font size="2"><input type="text" name="data1" value="2"> <input type="submit" value="确定" name="submit"></font> </form> <br/> <font size="2"> 请输入运算式数量</font> <form action="" method="get" name="form" id="form"> <font size="2"><input type="text" name="data2" value="30"> <input type="submit" value="确定" name="submit"></font> </form> <br/> <font size="2"> 请输入数值范围</font> <form action="" method="get" name="form" id="form"> <font size="2"><input type="text" name="data3" value="100"> <input type="submit" value="确定" name="submit"></font> </form> <br/> <font size="2"> 请输入是否包含乘除(0/1)</font> <form action="" method="get" name="form" id="form"> <font size="2"><input type="text" name="data4" value="0"> <input type="submit" value="确定" name="submit"></font> </form> <br/> <font size="2"> 请输入是否包含负数(0/1)</font> <form action="" method="get" name="form" id="form"> <font size="2"><input type="text" name="data5" value="0"> <input type="submit" value="确定" name="submit"></font> </form> <% %> <% String str1 = request.getParameter("data1"); int r1=0; if(str1 != null) { r1 = Integer.parseInt(str1); out.print("test use : "+r1+"<br/>"); } else r1 = 2; String str2 = request.getParameter("data2"); int r2=0; if(str2 != null) { r2 = Integer.parseInt(str2); out.print("test use : "+r2+"<br/>"); } else r2 = 30; String str3 = request.getParameter("data3"); int r3=0; if(str3 != null) { r3 = Integer.parseInt(str3); out.print("test use : "+r3+"<br/>"); } else r3 = 100; String str4 = request.getParameter("data4"); int r4=0; if(str4 != null) { r4 = Integer.parseInt(str4); out.print("test use : "+r4+"<br/>"); } else r4 = 0; String str5 = request.getParameter("data5"); int r5=0; if(str5 != null) { r5 = Integer.parseInt(str5); out.print("test use : "+r5+"<br/>"); } else r5 = 0; boolean muland=false;//是否包含乘除 boolean negnum=false;//是否包含负数 if(r4==1) muland=true; if(r5==1) negnum=true; %> <% /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ RandomFormual run=new RandomFormual(r1,r2,r3,muland,negnum); List<String> formuals=run.getFormulas(); List<Integer> anwsers=run.getAnswer0sList(formuals); List<String> fals = new ArrayList<String>(); List<Integer> fals_anws = new ArrayList<Integer>(); double correctrat=0; %> <%!boolean cho=true; int temp; %> <% do { int all=formuals.size(); correctrat=0; if(all==0) break; out.println(formuals.size());//test use for(int i=0;i<formuals.size();i++) out.println(i+1+". "+formuals.get(i)+"=");//test use out.println("="+"<br/>");//test use for(int i=0;i<formuals.size();i++) { out.print(i+1+". "+formuals.get(i)+"= "+"<br/>"); out.print(" 答案:"+"<br/>"); PrintWriter pw=response.getWriter(); pw.write("<script language='javascript'>function prom1() {var name = prompt(); if (name){alert(name);temp=name;}}</script>"); if(anwsers.get(i)==temp) { out.println(" 答案 is:(tset use)"+anwsers.get(i)+"</br>"); correctrat++; formuals.remove(i); anwsers.remove(i); } fals=formuals; fals_anws.addAll(anwsers); } out.println("是做下一套题还是做错题 ture or false"+"<br/>"); PrintWriter pw2=response.getWriter(); pw2.write("<script language='javascript'>function firm() {cho=confirm}</script>"); if(cho) { break; } out.println(correctrat/all); out.println("全对了吗,有没有超时,没通过继续吧"+"<br/>"); }while((correctrat/=formuals.size())!=1); out.println("好吧,你过了。"+"<br/>"); out.println("是否继续 true or false"+"<br/>"); out.println("是否查看错题加答案 true or false"+"<br/>"); PrintWriter pw4=response.getWriter(); pw4.write("<script language='javascript'>function firm() {cho=confirm}</script>"); if(cho); for(int i=0;i<fals.size();i++) out.println(i+". "+fals.get(i)+"="+fals_anws.get(i)+"<br/>"); out.println("是否根据错题进行答题 true or false"+"<br/>"); PrintWriter pw5=response.getWriter(); pw5.write("<script language='javascript'>function firm() {cho=confirm}</script>"); if(cho) { do { for(int i=0;i<fals.size();i++) out.println(i+1+". "+fals.get(i)+"=");//test use out.println("=");//test use for(int i=0;i<formuals.size();i++) { out.print(i+1+". "+fals.get(i)+"= "+"<br/>"); PrintWriter pw6=response.getWriter(); pw6.write("<script language='javascript'>function prom() {var name = prompt(); if (name){alert(name);temp=name;}}</script>"); if(fals_anws.get(i)==temp) { fals.remove(i); fals_anws.remove(i); } } out.println("是做下一套题还是做错题 ture or false"+"<br/>"); PrintWriter pw7=response.getWriter(); pw7.write("<script language='javascript'>function firm() {cho=confirm}</script>"); if(cho) { break; } }while(true); } List<String> loop1=new ArrayList<String>(); Iterator lp1=loop1.iterator(); while(lp1.hasNext()){ %> <%=lp1.next() %> <br> <% } %> <p> <input type="submit" name="Submit2" value="提交" onclick="firm()" /> </p> <p> <input type="submit" name="Submit3" value="提交" onclick="prom()" /> </p> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="tool.*" %> <%@ page import="java.util.*" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>二年级</title> </head> <body> <p> <font size="2"> 请输入运算式数量</font> <form action="" method="get" name="form" id="form"> <font size="2"><input type="text" name="data2" value="30"> <input type="submit" value="确定" name="submit"></font> </form> <br/> </p> </body> </html>