首页 > 其他分享 >2023.10.18

2023.10.18

时间:2023-10-18 22:36:20浏览次数:37  
标签:String int 18 2023.10 Tree new rchild public

今天终于把周一布置的课堂测试完成了,说真的,有些困难。

代码如下:

<%@ 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

相关文章

  • 2023/10/18 下午一家小公司 晚上上海微电子 然后南京小公司
    每日经历:早上 感性:好像现在越来越难起床了分析:手机电脑看得太多了精神衰弱  早睡可能有所缓解下午 感性:又是两个小时的车程自己好像不想准备了一样去了小公司  四周无人一层楼拥有甚至几家公司 我的青春绝对不可能奉献给这种小公司理性:小公司做业务......
  • 10.18 动手动脑
    AboutException.java运行结果 运行结果看出这里是程序发生异常,被0除的算术运算,这个错误发生后程序会结束,不会向下运行Error类称为错误类,它表示Java运行时产生的系统内部错误或资源耗尽的错误,是比较严重的,仅靠修改程序本身是不能恢复执行的,如系统崩溃,虚拟机错误等。CatchWho.......
  • 大二打卡(10.18)
    今天做了什么:背了英语笔记,少背了一页,结果就导致老师听写的时候有不会的,还得背,不能再像这次一样,临时抱佛脚突击背诵三四十分钟今天遇到什么问题:英语还得背,建民测试还得搞明天怎么做:上完一天的课,晚上熬一宿把建民测试搞完......
  • 23.10.18(常用Java异常处理情况整合)
    在JAVA项目中,异常处理是一项非常重要的任务。合理处理异常能够提高程序的稳定性和可靠性,保证程序的正常运行。下面是关于JAVA项目中常用的异常处理情况的总结:1.空指针异常(NullPointerException):在使用一个空对象的成员变量或方法时会抛出该异常。可以通过判断对象是否为空来避免......
  • 10.18(随机出题Web页面)
    今天完成了javaweb的出题系统,比较简陋jsp<%@pagecontentType="text/html;charset=UTF-8"language="java"%><!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>添加信息</title>&l......
  • 日常记录--2023-10月18日--周三
    日程:今天只有上午有课,7点起床,吃了个早饭去上课,早上第一节数据结构,学习了队列,还讲了相关应用。中午午休一个小时,下午起来干了点别的,完善了之前的代码,晚上7-9点听了下代码随想路,学了会javaweb。学了什么:可恶的Javaweb,复习了数据结构。PS:不想学习,想要成为月饼盒;......
  • 10.18闲话
    今天没模拟赛,爽爽爽......
  • 23.10.18 Java当中的异常处理
    Java当中的异常处理在Java中,异常是指在程序执行期间发生的错误或异常情况,可以分为两种类型:受检异常(CheckedException)和非受检异常(UncheckedException)。受检异常:受检异常是指需要在代码中显式处理的异常,通常继承自Exception类的子类。例如,IOException和SQLException是受检异......
  • 比赛经验(10/18)
     (1)有些比赛问题可以用公式直接计算,仔细辨别,不要盲目计算 (2)注重时间复杂度的分析,估算时间,选择合适的算法或者其他解题方式   时间复杂度 (3)对于出错的题目先跳过,一直提交可能会蒙圈,返回来再写时可能会清晰些......
  • HO引擎近况20231018
    放完假就开始忙现在又开始晚上9点下班了,一直到现在才想起来写博客公司搬家了。搬到了原来公司附近的写字楼里面工作环境变了,算是有好有坏吧好处是当初排座位的时候我要了一个靠过道的位置,现在看来还真的不错能把小柜子放到桌子外面的过道上一部分,还能放两个小柜子不靠窗户所......