今天终于把周一布置的课堂测试完成了,说真的,有些困难。
代码如下:
<%@ page import="java.util.Random" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.Scanner" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html lang="ch"> <%! Random ra=new Random(); static class tools { public static char getOperator() { char operator = 0; Random ran = new Random(); int i = ran.nextInt(4); switch (i) { case 0: operator = '+'; break; case 1: operator = '-'; break; case 2: operator = '*'; break; case 3: operator = '/'; break; } return operator; }//获得运算符 public static int getNumber(int max) { int number = 0; Random ran = new Random(); number = ran.nextInt(max + 1); return number; }//获得随机数 //根据运算符个数随机得到子节点个数 public static boolean[] getChildPlace(int num) { int d = 0; int size = 0, j = 1; while (num >= (int) Math.pow(2, j)) { j++; } d = (int) Math.pow(2, j) - 1 - num; size = (int) Math.pow(2, j - 1); boolean[] k = new boolean[size]; for (int i = 0; i < size; i++) { k[i] = true; } for (int i = 0; i < d; i++) { Random ran = new Random(); int f = ran.nextInt(size); while (k[f] == false) { f = ran.nextInt(size); } k[f] = false; } return k; } public static int getTextClass() { Scanner sc = new Scanner(System.in); System.out.print("请选择:"); int num = sc.nextInt(); return num; }//选择年级 public static int judgeTextdanger() { Scanner sc = new Scanner(System.in); System.out.print("请选择是否带乘除(1.是 2.否):"); int tb = sc.nextInt(); return tb; }//判断是否带乘除 public static int judgeTextKuo() { Scanner sc = new Scanner(System.in); System.out.print("请选择是否带括号(1.是 2.否):"); int tb = sc.nextInt(); return tb; }//判断是否带括号(四年级) public static int getTextNum() { Scanner sc = new Scanner(System.in); System.out.print("请输入做题数量:"); int num = sc.nextInt(); return num; }//获得做题数量 public static int getTextMax(int k) {//k为几年级 Scanner sc = new Scanner(System.in); if (k == 2) System.out.print("请选择数值范围(1-100):"); else if (k == 3) System.out.print("请选择数值范围(1-1000):"); else System.out.print("请选择数值范围(1-1000):"); int tb = sc.nextInt(); return tb; }//获得操作数范围 public static int getTextOperator(int k) {//k为年级,3/4 Scanner sc = new Scanner(System.in); if (k == 3) System.out.print("请选择操作数(2-4):"); else System.out.print("请选择操作数(2-5):"); int tb = sc.nextInt(); return tb; }//获得操作数的数量 //判断是否完成题目 public static int judgeFinish() { Scanner sc = new Scanner(System.in); System.out.print("是否在文件内完成题目(1.是/2.否):"); int k = sc.nextInt(); return k; } } static class Tree { private String str; private Tree lchild; private Tree rchild; public Tree(String str){ this.str=str; } public Tree(String str,Tree lchild,Tree rchild){ this.str = str; this.rchild = rchild; this.lchild = lchild; } public void setChild(Tree lchild, Tree rchild){ this.lchild = lchild; this.rchild = rchild; } public String getStr() { return str; } public void setStr(String str) { this.str = str; } public Tree getRchild() { return rchild; } public void setRchild(Tree rchild) { this.rchild = rchild; } public Tree getLchild() { return lchild; } public void setLchild(Tree lchild) { this.lchild = lchild; } //判断是否有左右孩子 public boolean hasChild(){ if(lchild == null && rchild == null) return false; else return true; } //获取结果,并检验减法和除法的情况是否合理 public String getResult(){ Random ra=new Random(); if(hasChild()){ switch(str){ case "+": return String.valueOf(Integer.parseInt(getLchild().getResult()) + Integer.parseInt(getRchild().getResult())); case "-": if(Integer.parseInt(getLchild().getResult())<Integer.parseInt(getRchild().getResult())){ int t=ra.nextInt(2); if(t==0) str="+"; else str="*"; return this.getResult(); } return String.valueOf(Integer.parseInt(getLchild().getResult()) - Integer.parseInt(getRchild().getResult())); case "*": return String.valueOf(Integer.parseInt(getLchild().getResult()) * Integer.parseInt(getRchild().getResult())); case "/": if(getRchild().getResult().equals("0")){ while(str.equals("/")){ str = String.valueOf(tools.getOperator()); } return this.getResult(); } else if(Integer.parseInt(getLchild().getResult()) % Integer.parseInt(getRchild().getResult()) != 0){ while(str.equals("/")){ str = String.valueOf(tools.getOperator()); } return this.getResult(); } else return String.valueOf(Integer.parseInt(getLchild().getResult()) / Integer.parseInt(getRchild().getResult())); } } return str; } //加括号 public String toString(){ String Lstr = "", Rstr = "", Str = ""; if(hasChild()){ if(getRchild().hasChild()){//在运算符为-,*,/时需要添加括号,其余不进行添加括号操作 if(str.equals("/")){ Rstr = getRchild().toString(); } else if(str.equals("*") || str.equals("-")){ if(getRchild().str.equals("+") || getRchild().str.equals("-")){ Rstr = getRchild().toString(); } else{ Rstr = getRchild().toString().substring(1, getRchild().toString().length()-1); } } else{ Rstr = getRchild().toString().substring(1, getRchild().toString().length()-1); } } else{ Rstr = getRchild().str; } if(getLchild().hasChild()){//和右孩子同理,但此时减号不用添加括号 if(str.equals("*") || str.equals("/")){ if(getLchild().str.equals("+") || getLchild().str.equals("-")){ Lstr = getLchild().toString(); } else{ Lstr = getLchild().toString().substring(1, getLchild().toString().length()-1); } } else{ Lstr = getLchild().toString().substring(1, getLchild().toString().length()-1); } } else{ Lstr = getLchild().str; } Str = "(" + Lstr + str + Rstr + ")"; } else{ //若没有孩子,说明是数字节点,直接返回数字 Str = str; } return Str; } } static class doTree { private Tree root; private int num; private ArrayList<Tree> arr = new ArrayList<Tree>(); public doTree(int num){ this.num=num; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public Tree getRoot() { return root; } public void setRoot(Tree root) { this.root = root; } //验证表达式是否正确并进行计算 public String TestProblem(){ return root.getResult(); } //获取最终的式子(需在验证式子正确后进行) public String toString(){ String str= root.toString(); str=str.substring(1,str.length()-1); return str; } //计算二叉树的深度 public int Deepth(){ int n=this.num; int deep=2; while(n/2>0){ deep++; n/=2; } return deep; } //创建树 public void CreateTree(int Max){ Tree lchild, rchild, lnode, rnode; if(num == 1){ lchild = new Tree(String.valueOf(tools.getNumber(Max)), null, null); rchild = new Tree(String.valueOf(tools.getNumber(Max)), null, null); root = new Tree(String.valueOf(tools.getOperator()), lchild, rchild); } else{ int num1 = 0; int n = Deepth() - 3; boolean[] place = tools.getChildPlace(num); root = new Tree(String.valueOf(tools.getOperator()), null, null); arr.add(root); for(int i = 0; i < n; i++){ for(int j = 0; j < (int)Math.pow(2, i); j++, num1++){ lchild = new Tree(String.valueOf(tools.getOperator()), null, null); rchild = new Tree(String.valueOf(tools.getOperator()), null, null); arr.get(j + num1).setChild(lchild, rchild); arr.add(lchild); arr.add(rchild); } } for(int i = 0; i < place.length; i++){ if(place[i]){ lnode = new Tree(String.valueOf(tools.getNumber(10)), null, null); rnode = new Tree(String.valueOf(tools.getNumber(10)), null, null); if(i%2 == 0){ lchild = new Tree(String.valueOf(tools.getOperator()), lnode, rnode); arr.add(lchild); arr.get(num1).setLchild(lchild); } else{ rchild = new Tree(String.valueOf(tools.getOperator()), lnode, rnode); arr.add(rchild); arr.get(num1).setRchild(rchild); } } else{ if(i%2 == 0){ lchild = new Tree(String.valueOf(tools.getNumber(10)), null, null); arr.get(num1).setLchild(lchild); } else{ rchild = new Tree(String.valueOf(tools.getNumber(10)), null, null); arr.get(num1).setRchild(rchild); } } num1 = num1 + i%2; } } } } String[] result=new String[10]; String[] answer=new String[10]; %> <head> <title>四则运算</title> </head> <body> <div id="container"> <form action="putin.jsp" method="post" onSubmit="return check(this)"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500" > <tr> <th colspan=2>四则运算题目</th> </tr> <td align="center" style="color: orangered">题目 </td> <td align="center" style="color: orangered">答案</td> <script type="text/javascript"> var sysSecond = parseInt('121'); var interValObj = window.setInterval(setRemainTime, 1000); function setRemainTime() { if (sysSecond > 0) { sysSecond -= 1; var second = Math.floor(sysSecond % 60); // 计算秒 var minite = Math.floor((sysSecond / 60) % 60); //计算分 var timeHtml = "<li><span>"+minite+"</span>分</li>"; timeHtml+="<li><span>"+second+"</span>秒</li>"; try { document.getElementById("counter").innerHTML = timeHtml; } catch(e){}} else { window.clearInterval(interValObj);} }</script> <div text-align="center" id="counter" style="margin-left: auto;margin-right: auto; width:120px; height:50px; background-color: cyan;">倒计时开始</div> <% for(int i=0;i<10;i++){ int k= ra.nextInt(2); doTree t=new doTree(k+1); t.CreateTree(100); answer[i]=t.TestProblem(); result[i]=t.toString()+"="; pageContext.setAttribute("q",result[i]); pageContext.setAttribute("p",answer[i]); %> <tr> <td align="center" style="color: orangered"><input type="hidden" name="timu" value="${q}"/>${q} </td> <td><input type="text" style="background-color: cornsilk" name="key"/></td> <input type="hidden" name="answer" value="${p}"/> </tr> <% } %> <%-- <tr> <th colspan=2><img src="yanzherngm.jsp"></th> </tr> --%> <tr align="center"> <td colspan="5"> <input type="submit" style="background-color: aqua" value="保存" /> <input type="button" onclick="window.location.reload();" style="background-color: aqua" value="重置" /> </td> </tr> </table> </form> </div> </body> </html>
<%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Statement" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.*" %> <%@ page import="java.util.Objects" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String timu[]=request.getParameterValues("timu"); String key[]= request.getParameterValues("key"); String answer[]= request.getParameterValues("answer"); for (int i = 0; i < timu.length; i++) { System.out.println(timu[i]); } for (int i = 0; i < timu.length; i++) { System.out.println(key[i]); } for (int i = 0; i < timu.length; i++) { System.out.println(answer[i]); } try { //1.注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); // SQL语句,使用我自己的test数据库下的 boss 表 for(int i=0;i<10;i++) { String sql = "insert into test.user(题目,提交答案,正确答案) value (?,?,?)"; PreparedStatement pstmt; pstmt = connection.prepareStatement(sql); pstmt.setString(1, timu[i]); pstmt.setString(2, key[i]); pstmt.setString(3, answer[i]); pstmt.executeUpdate(); if(!Objects.equals(key[i],answer[i])) { String sql2 = "insert into test.project(错题题目,答案) value (?,?)"; PreparedStatement pstmt2; pstmt2 = connection.prepareStatement(sql2); pstmt2.setString(1, timu[i]); pstmt2.setString(2, answer[i]); pstmt2.executeUpdate(); } } connection.close(); } catch (Exception e) { System.out.println(e); } //ResultSet resultSet = statement.executeQuery(sql); //resultSet.close(); //statement.executeUpdate("delete from user where name='n'"); %> <html> <body> <form action="index.jsp" method="post" onSubmit="return check(this)"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500" > <tr> <th colspan=2>错误题目</th> </tr> <td align="center" style="color: orangered">题目 </td> <td align="center" style="color: orangered">提交的答案</td> <% for(int i=0;i<10;i++){ if(!key[i].equals(answer[i])){ pageContext.setAttribute("q",timu[i]); pageContext.setAttribute("p",key[i]); %> <tr> <td align="center" style="color: orangered"><input type="hidden" name="timu" value="${q}"/>${q} </td> <td><input type="text" style="background-color: cornsilk" name="key" value="${p}"/></td> </tr> <% } } %> <%-- <tr> <th colspan=2><img src="yanzherngm.jsp"></th> </tr> --%> <tr align="center"> <td colspan="5"> <input type="submit" onclick="window.location.reload();" style="background-color: aqua" value="继续做题" /> </td> </tr> </table> </form> </body> </html>
两个数据库表:
标签:String,int,18,2023.10,Tree,new,rchild,public From: https://www.cnblogs.com/jiajiayu/p/17773530.html