zoukankan      html  css  js  c++  java
  • 结对四则运算04—网页版

    1.实验要求:

     

    2.实验思路:使用jsp Javabean和servlet来实现,Javabean定义实体类,定义能根据设置的参数产生出对应的方法,jsp页面用来让用户选择参数,做题和查看历史记录,servlet根据jsp传的数据进行响应和处理。

    首先有一个选择界面的jsp,如果选择做题就跳转到设置参数的jsp,设置好参数后,传到servlet,servlet根据传过来的参数产生题目,存储好以后,跳转到显示题目的jsp页面,用户可以输入答案,当交卷的时候会提交到判断的servlet,servlet根据传过的答案和正确答案进行比较,并输出做对和做错的题号,然后输出每道题和每道题的判断结果;如果选择的是查看历史记录,就会查看以前所做的题目。

    3.实验代码:

    //选择做题还是查询历史记录

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
            <h1>请选择是做题还是查询历史记录</h1>
            <hr>
            <br>
            <br>
            <br>
            <a href="setParam.jsp"><input type="button" value="开始做题"></a>  
            <a href="selectServlet"><input type="button" value="查询历史记录"></a>
    </body>
    </html>
    

    //设置参数,比如是否要括号,做真分数还是整数等等

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <style>
        span {
                color:blue;
             }
    
    </style>
    <script type="text/javascript">
        $(function()
            {
            $('#formbackground').height($(window).height());
            $('#formbackground').width($(window).width());
            }
        );
    </script>
    <script type="text/javascript">
        function $(id)
        {
            return document.getElementById(id);
        }
        function check() {
            var num=$("num").value;
            var scope=$("scope").value;
            $("numinfo").innerHTML="";
            $("scopeinfo").innerHTML="";
            if(num=="")
                {
                    $("numinfo").innerHTML="题数不能为空,请输入题数";
                    $("num").focus();
                    return false;
                }
            if(scope=="")
                {
                    $("scopeinfo").innerHTML="取值范围不能为空,请输入范围";
                    $("scope").focus();
                    return false;
                }
            return true;
        }
    </script>
    <title>Insert title here</title>
    </head>
    <body>
    <h1>请选择做题的类型</h1>
        <br>
            <form action="setParamServlet" method="post" onsubmit="return check()">
                <table>
                    <tr>
                        <td>类型:</td>
                        <td>整数<input type="radio" name="type" value="2" checked="checked"></td>  
                        <td>真分数<input type="radio" name="type" value="1"></td>
                    </tr>
                    <tr>
                        <td>题数:</td>
                        <td><input type="text" name="num" id="num"><span id="numinfo"></span></td>
                    </tr>
                    <tr>
                        <td>取值范围:</td>
                        <td><input type="text" name="scope" id="scope"><span id="scopeinfo"></span></td>
                    </tr>
                    <tr> 
                        <td>是否含有括号</td>
                        <td>无<input type="radio" name="isBracket" value="2" checked="checked"></td>
                        <td>有<input type="radio" name="isBracket" value="1"></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="确认">  <input type="reset" value="取消"></td>
                    </tr>
                </table>
            </form>
    </body>
    </html>
    
    设置参数jsp
    

    //产生题目的servlet

    package servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.SQLException;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import util.publicUse;
    
    /**
     * Servlet implementation class setParamServlet
     */
    @WebServlet("/setParamServlet")
    public class setParamServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public setParamServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see Servlet#init(ServletConfig)
         */
        public void init(ServletConfig config) throws ServletException {
            // TODO Auto-generated method stub
        }
    
        /**
         * @see Servlet#destroy()
         */
        public void destroy() {
            // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doPost(request, response);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
    
            // TODO Auto-generated method stub
            publicUse P = new publicUse();
            response.setContentType("text/html;charset=utf-8");
            String type;//题目类型
            int num;//题数
            int scope;//范围
            String isBracket;//是否含有括号
            type=request.getParameter("type");
            num=Integer.parseInt(request.getParameter("num"));
            scope=Integer.parseInt(request.getParameter("scope"));
            isBracket=request.getParameter("isBracket");
            PrintWriter out = response.getWriter();
            System.out.println(type+"	"+num+"	"+scope+"	"+isBracket);
            int choose1=Integer.parseInt(type);
            int choose2=Integer.parseInt(isBracket);
            try {
    
                String rs[]=new String[2*num];
                rs=P.operationAndStatistical(choose1, choose2, num,scope);
                String []bds = new String[num];//存取表达式
                String []rs1 = new String[num];//存取结果
                for(int i=0;i<num;i++)
                {
                    bds[i] = rs[i];
                    rs1[i] = rs[i + num];
                }
                
                request.getSession().setAttribute("bds", bds);
                request.getSession().setAttribute("rs1", rs1);
                request.getSession().setAttribute("num", num);
                request.getRequestDispatcher("show.jsp").forward(request, response);
    //            response.sendRedirect(request.getContextPath()+"/show.jsp");
                
            } catch (ClassNotFoundException | SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        
        }
    
    }
    
    产生题目的servlet
    

    //显示题目

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    <script type="text/javascript">
        function $(id) {
            return document.getElementById(id);
        }
        function check(){
            var result = document.getElementsByName('result');
            for(var i = 0;i < result.length;i++)
            {
                if(result[i].value=="")
                {
                    $("" + i).innerHTML = "请输入答案";
                    result[i].focus();
                    return false;
                }
                else{
                    $("" + i).innerHTML ="";
                    result[i].focus();
                }
            }
            function init(){
            }
            return true;
        }
    </script>
    </head>
    <body>
    
        <h1>请在此处答题:</h1>
        <hr>
        <%
        
        %>
        <form action="judgeServlet" method="post" onsubmit="return check()">
            <table>
            <%
            int num=(Integer)request.getSession().getAttribute("num");
            String []a=(String[])request.getSession().getAttribute("bds");
            for(int i=0;i<num;i++)
            {
                %><tr>
                    <td>请作答第<%=i+1 %>道题:</td>
                    <td><%=a[i] %></td>
                    <td><input type="text" name="result" /><span id=<%=i %>></span></td>
                    
                </tr>
                
                <%
            }
                %>
                <tr>
                    <td><input type="submit" value="交卷"></td>
                </tr>
            </table>
        </form>
    </body>
    </html>
    
    做题页面jsp

    package servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.SQLException;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import entity.data;
    import jdbc.insertsj;
    import util.solve;
    
    /**
     * Servlet implementation class judgeServlet
     */
    @WebServlet("/judgeServlet")
    public class judgeServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public judgeServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see Servlet#init(ServletConfig)
         */
        public void init(ServletConfig config) throws ServletException {
            // TODO Auto-generated method stub
        }
    
        /**
         * @see Servlet#destroy()
         */
        public void destroy() {
            // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doPost(request, response);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            String []bds =(String[]) request.getSession().getAttribute("bds");//获取表达式
            String []rs =(String[]) request.getSession().getAttribute("rs1");//获取正确结果
            String []inputrs = request.getParameterValues("result");//获取输入的结果
            solve s = new solve();
            boolean[]sz = s.judgeIfTrue(rs, inputrs);//判断是否正确
            int[] idnum = new int[rs.length];//存取题号
            int[]count = s.zongjie(sz);
            String[]qiq = s.qiq(sz);
            String[]count1 = s.countexpression(bds, rs, inputrs, sz);
             out.println("正确的题数"+count[0]+"<br>");
             out.println("错误的题数"+count[1]+"<br>");
             out.println(qiq[0]+"<br>");
             out.println(qiq[1]+"<br>");
             for(int i=0;i<rs.length;i++)
             {
                 idnum[i]=i+1;
                 out.println(count1[i]+"<br>");
             }
             data []a = new data[rs.length];
             for(int i=0;i<rs.length;i++)
             {
                 a[i] = new data();
                 a[i].setId(idnum[i]);
                 a[i].setTitleexception(bds[i]);
                 a[i].setResult(rs[i]);
                 a[i].setInputrs(inputrs[i]);
                 a[i].setIftrue(sz[i]);
             }
            
             insertsj out1 =new insertsj();
             try {
                out1.cunchu(a);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // response.sendRedirect(request.getContextPath()+"/showResult.jsp");
            // request.getRequestDispatcher("showResult.jsp").forward(request, response);
             out.print("<a href='choose.jsp'>返回</a>");
             
        }
    
    }
    
    判断是否正确,并将结果输出的servlet和和讲题目存档
    package servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.SQLException;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import entity.data;
    import jdbc.insertsj;
    import util.solve;
    
    /**
     * Servlet implementation class judgeServlet
     */
    @WebServlet("/judgeServlet")
    public class judgeServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public judgeServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see Servlet#init(ServletConfig)
         */
        public void init(ServletConfig config) throws ServletException {
            // TODO Auto-generated method stub
        }
    
        /**
         * @see Servlet#destroy()
         */
        public void destroy() {
            // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doPost(request, response);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            String []bds =(String[]) request.getSession().getAttribute("bds");//获取表达式
            String []rs =(String[]) request.getSession().getAttribute("rs1");//获取正确结果
            String []inputrs = request.getParameterValues("result");//获取输入的结果
            solve s = new solve();
            boolean[]sz = s.judgeIfTrue(rs, inputrs);//判断是否正确
            int[] idnum = new int[rs.length];//存取题号
            int[]count = s.zongjie(sz);
            String[]qiq = s.qiq(sz);
            String[]count1 = s.countexpression(bds, rs, inputrs, sz);
             out.println("正确的题数"+count[0]+"<br>");
             out.println("错误的题数"+count[1]+"<br>");
             out.println(qiq[0]+"<br>");
             out.println(qiq[1]+"<br>");
             for(int i=0;i<rs.length;i++)
             {
                 idnum[i]=i+1;
                 out.println(count1[i]+"<br>");
             }
             data []a = new data[rs.length];
             for(int i=0;i<rs.length;i++)
             {
                 a[i] = new data();
                 a[i].setId(idnum[i]);
                 a[i].setTitleexception(bds[i]);
                 a[i].setResult(rs[i]);
                 a[i].setInputrs(inputrs[i]);
                 a[i].setIftrue(sz[i]);
             }
            
             insertsj out1 =new insertsj();
             try {
                out1.cunchu(a);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // response.sendRedirect(request.getContextPath()+"/showResult.jsp");
            // request.getRequestDispatcher("showResult.jsp").forward(request, response);
             out.print("<a href='choose.jsp'>返回</a>");
             
        }
    
    }
    
    判断是否正确,并将结果输出的servlet和和讲题目存档
    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h1>历史记录页面</h1>
        <%
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            String s=(String)request.getSession().getAttribute("s1");
            String []s1 = s.split(s);
            if(s1.length==0)
            {
                %>你还没有历史记录<%
            }
            else
            {
                for(int i=0;i<s1.length;i++)
                {
                    out.print(s1[i] + "<br>");
                }
            }
        %>
        <a href="choose.jsp">返回</a>
    </body>
    </html>
    
    显示做题记录的jsp

      

    我把需要用的方法和用调用的参数都已经分好类:

    如下图:

     entity用来存取实体类,也就是数据库对应的那些参数,一开始写了三个,最后只有一个用上了,内容如下:

    package entity;
    
    public class data {
        private int id;//题号
        private String    titleexception;//表达式
        private String result;//正确结果
        private String inputrs;//输入结果
        private boolean iftrue;//判断对错
        public data()
        {
            
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getTitleexception() {
            return titleexception;
        }
        public void setTitleexception(String titleexception) {
            this.titleexception = titleexception;
        }
        public String getResult() {
            return result;
        }
        public void setResult(String result) {
            this.result = result;
        }
        public String getInputrs() {
            return inputrs;
        }
        public void setInputrs(String inputrs) {
            this.inputrs = inputrs;
        }
        public boolean isIftrue() {
            return iftrue;
        }
        public void setIftrue(boolean iftrue) {
            this.iftrue = iftrue;
        }
        
        
    }
    
    entity

     第二个是jdbc,也就是连接数据库,查询和插入的方法:

    package jdbc;
    
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.Statement;
    
    import entity.data;
    
    public class insertsj {
        public void cunchu(data[]a) throws SQLException
        {
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try
            {
                conn = (Connection) JdbcUtils.getConnection();
                String sql = "insert into wzw3 (idnum,titleexception,result,inputrs,iftrue) values(?,?,?,?,?)";
                
                ps = conn.prepareStatement(sql);
                for(int k=0;k<a.length;k++)
                {
                    ps.setInt(1, a[k].getId());
                    ps.setString(2, a[k].getTitleexception());
                    ps.setString(3, a[k].getResult());
                    ps.setString(4, a[k].getInputrs());
                    ps.setBoolean(5, a[k].isIftrue());
                    ps.executeUpdate();
                }
            }
            finally 
            {
                JdbcUtils.free(rs, ps, conn);
            }
            
        }
        
    }
    
    插入的
    package jdbc;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public final class JdbcUtils {
        private static String url = "jdbc:mysql://localhost:3306/wzw1";
        private static String user = "wzw1";
        private static String password = "121203";
        
        private JdbcUtils()
        {}
        
        static {
            try{
                Class.forName("com.mysql.jdbc.Driver");
            }catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        
        public static Connection getConnection() throws SQLException
        {
            return DriverManager.getConnection(url,user,password);
        }
        
        public static void free(ResultSet rs, Statement ps, com.mysql.jdbc.Connection conn) throws SQLException {
            // TODO Auto-generated method stub
    
            try{
                if(rs!=null)
                {
                    rs.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
            finally {
                try{
                    if(ps!=null)
                    ps.close();
                }
                catch(SQLException e)
                    {
                        e.printStackTrace();
                    }
                    finally {
                        if(conn!=null)
                        {
                            conn.close();
                        }
                    }
                    }
                
        }
    
        public static void free(ResultSet rs, PreparedStatement ps, com.mysql.jdbc.Connection conn) throws SQLException {
            // TODO Auto-generated method stub
    
            try{
                if(rs!=null)
                {
                    rs.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
            finally {
                try{
                    if(ps!=null)
                    ps.close();
                }
                catch(SQLException e)
                    {
                        e.printStackTrace();
                    }
                    finally {
                        if(conn!=null)
                        {
                            conn.close();
                        }
                    }
                    }
                
        }
    }
    
    jdbc连接的工具类
    package jdbc;
    
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.mysql.jdbc.Connection;
    
    public class select {
        public static String selectbyIdTime() throws SQLException
        {
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try{
                conn = (Connection) JdbcUtils.getConnection();
                String sql="select * from wzw3";
                ps = conn.prepareStatement(sql);
                rs=ps.executeQuery();
                String a1 = "";
                while(rs.next())
                {
                    a1+=rs.getInt("idnum")+rs.getString("titleexception")+rs.getString("result")+rs.getString("inputrs")+rs.getBoolean("iftrue") + "#";
                }
                return a1;
            }
            finally 
            {
                JdbcUtils.free(rs, ps, conn);
            }
        }
    }
    
    查询

    第三个是用来相应的servlet,在上面都已经展示过了

    第四个是产生题目的方法,跟上一次写的差不多,但是因为太多,我把分成三个Java文件,还有一个是判断所做的体是否正确的方法:

    package util;
    
    public class integer {
        //整数运算
        public static String generateExpressionkh(int num,int scope)//产生带括号的整数表达式
        {
            publicUse u = new publicUse();
            int a1[]=new int[num];
            int a2[]=new int[num-1];
            int a3[]=new int[num];
            String[]a5=new String[num];
            String[] a4={"+","-","*","/"};
            for(int i=0;i<num;i++)
            {
                a1[i]=(int) (Math.random()*(scope-1)+1);
            }
            for(int i=0;i<num-1;i++)
            {
                a2[i]=(int) (Math.random()*4);
            }
            a3=u.chansheng(num);
            for(int i=0;i<num;i++)
            {
                a5[i]="";
                if(a3[i]<0)
                {
                    int c=0-a3[i];
                    for(int j=0;j<c;j++)
                    {
                        a5[i]+=")";
                    }
                }
                else
                {
                    for(int j=0;j<a3[i];j++)
                    {
                        a5[i]+="(";
                    }
                }
            }
            String t="";
            for(int i=0;i<num-1;i++)
            {
                if(a3[i]>0)
                {
                    t+=a5[i]+" "+a1[i]+" "+a4[a2[i]];
                }
                else
                {
                    t+=" "+a1[i]+" "+a5[i]+a4[a2[i]];
                }
            }
            if(a3[num-1]>0)
            {
                t+=a5[num-1]+" "+a1[num-1]+" ";
            }
            else
            {
                t+=" "+a1[num-1]+" "+a5[num-1];
            }
            return t;
        }
        public static String generationexception(int num,int scope)//产生不带括号的表达式
        {
            int a1[]=new int[num];
            int a2[]=new int[num-1];
            int a3[]=new int[num];
            String[] a4={"+","-","*","/"};
            for(int i=0;i<num;i++)
            {
                a1[i]=(int) (Math.random()*(scope-1)+1);
            }
            for(int i=0;i<num-1;i++)
            {
                a2[i]=(int) (Math.random()*4);
            }
            String t="";
            for(int i=0;i<num-1;i++)
            {
                if(a3[i]>0)
                {
                    t+=" "+a1[i]+" "+a4[a2[i]];
                }
                else
                {
                    t+=" "+a1[i]+" "+a4[a2[i]];
                }
            }
            if(a3[num-1]>0)
            {
                t+=" "+a1[num-1]+" ";
            }
            else
            {
                t+=" "+a1[num-1]+" ";
            }
            return t;
        }
        
    }
    
    产生整数的题目
    package util;
    
    public class properFraction {
    
        //真分数运算
        public static String properFractionExit(int num,int scope)//产生不含括号含有真分数的表达式
        {
            publicUse u =new publicUse();
            
            int []r1=new int[2*num];//接受产生的数值
            int []r2=new int[num-1];//接受符号
            String[]r3={"+","-","*","/"};
            
            String rs="";//接受含括号的和不含括号的表达式
            char ch='z';
            while(ch=='z')
            {
                int i=0;
                for(;i<2*num;i++)
                {
                    r1[i]=(int) (Math.random()*(scope-1)+1);
                }
                ch='y';
                int j=0;
                while(j<2*num)
                {
                    if(r1[j]>=r1[j+1])
                    {
                        ch='z';
                        break;
                    }
                    j++;
                    j++;
                }
            }
            for(int i=0;i<num-1;i++)
            {
                r2[i]=(int) (Math.random()*4);
            }
            int j=0;
            while(j<2*num-2)
            {
                
                int commondivisor=u.maxyue(r1[j], r1[j+1]);
                r1[j]/=commondivisor;
                r1[j+1]/=commondivisor;
                if(r1[j]==r1[j+1])
                {
                    rs+=" "+1+" ";
                }
                else
                {
                    rs+=" "+r1[j]+"/"+r1[j+1]+" "+r3[r2[(j+1)/2]];
                }
                j++;
                j++;
            }
            int commondivisor1=u.maxyue(r1[2*num-2],r1[2*num-1]);
            r1[2*num-2]/=commondivisor1;
            r1[2*num-1]/=commondivisor1;
            if(r1[2*num-2]==r1[2*num-1])
            {
                rs+=" "+1+" ";
            }
            else
            {
                rs+=" "+r1[2*num-2]+"/"+r1[2*num-1]+" ";
            }
            return rs;
        }
        public static String properFractionExithk(int num,int scope)//产生含括号含有真分数的表达式
        {
            publicUse u =new publicUse();
            
            int []r1=new int[2*num];//接受产生的数值
            int []r2=new int[num-1];//接受符号
            String[]r3={"+","-","*","/"};
            int []r4=new int[num];//接受随机产生的括号
            String []r5=new String[2*num];//将接受的括号的个数,转成字符串
            String rs="";//接受含括号的和不含括号的表达式
            r4=u.chansheng(num);
            char ch='z';
            
            //产生数值
            while(ch=='z')
            {
                
                for(int i=0;i<2*num;i++)
                {
                    r1[i]=(int) (Math.random()*(scope-1)+1);
                }
                ch='y';
                int j=0;
                while(j<2*num)
                {
                    if(r1[j]>=r1[j+1])
                    {
                        ch='z';
                        break;
                    }
                    j++;
                    j++;
                }
            }
            
            
            //产生符号
            for(int i=0;i<num-1;i++)
            {
                r2[i]=(int) (Math.random()*4);
            }
            
            
            //产生括号的数组
            for(int i=0;i<2*num;i++)
            {
                r5[i]="";
                if(i%2==0)
                {
                    if(r4[i/2]>0)
                    {
                        for(int j=0;j<r4[i/2];j++)
                        {
                            r5[i]+="(";
                        }
                    }
                    else if(r4[i/2]<0)
                    {
                        for(int j=0;j<0-r4[i/2];j++)
                        {
                            r5[i]+=")";
                        }
                    }
                }
            }
            
            //添加到一个String类型的表达式中
            int j=0;
            while(j<2*num-2)
            {
                int commondivisor=u.maxyue(r1[j], r1[j+1]);
                r1[j]/=commondivisor;
                r1[j+1]/=commondivisor;
                    if(r5[j].equals(""))
                    {
                        rs+=" "+r1[j]+"/"+r1[j+1]+" "+r3[r2[(j+1)/2]];
                    }
                    else if(r5[j].substring(0, 1).equals("("))
                    {
                        rs+=r5[j]+" "+r1[j]+"/"+r1[j+1]+" "+r3[r2[(j+1)/2]];
                    }
                    else
                    {
                        rs+=" "+r1[j]+"/"+r1[j+1]+" "+r5[j]+r3[r2[(j+1)/2]];
                    }
                
                j++;
                j++;
            }
            //算最后一个数
            int commondivisor1=u.maxyue(r1[2*num-2],r1[2*num-1]);
            r1[2*num-2]/=commondivisor1;
            r1[2*num-1]/=commondivisor1;
            
                if(r5[2*num-2].equals(""))
                {
                    rs+=" "+r1[2*num-2]+"/"+r1[2*num-1]+" ";
                }
                else if(r5[2*num-2].substring(0, 1).equals("("))
                {
                    rs+=r5[2*num-2]+" "+r1[2*num-2]+"/"+r1[2*num-1]+" ";
                }
                else
                {
                    rs+=" "+r1[2*num-2]+"/"+r1[2*num-1]+" "+r5[2*num-2];
                }
            return rs;
        }
        
    }
    
    产生真分书类型的题目
    package util;
    
    import java.sql.SQLException;
    import java.util.Date;
    import java.util.Random;
    import java.util.Scanner;
    import java.util.Stack;
    
    public class publicUse {
    //    public static void main(String[] args) throws ClassNotFoundException, SQLException
    //    {
    //        
    //        System.out.println("请选择题目类型:1.真分数。2整数");
    //        int choose1=sc.nextInt();
    //        System.out.println("请输入产生题的个数");
    //        int num=sc.nextInt();
    //        System.out.println("请输入取值范围");
    //        int scope=sc.nextInt();
    //        System.out.println("请选择题目类型中是否有括号:1.有。2.没有");
    //        int choose2=sc.nextInt();
    //        String rs[]=new String[2*num];
    //        rs=operationAndStatistical(choose1, choose2, num,scope);
    //        for(int i=0;i<num;i++)
    //        {
    //            System.out.println(rs[i]+"="+rs[i+num]);
    //        }
    //    }
        
        public static Scanner sc=new Scanner(System.in);
        
        //真分数和整数都需要用到
        public static int maxyue(int y,int x)//最大公约数
        {
            int r=y;
            while(r!=0)
            {
                r=x%y;
                x=y;
                y=r;
            }
            return x;
        }
        public     static char youxian(String f,String s)//计算两个符号的优先级
        {
            char a1[][]={
                    {'>','>','<','<','<','>','>'},
                    {'>','>','<','<','<','>','>'},
                    {'>','>','>','>','<','>','>'},
                    {'>','>','>','>','<','>','>'},
                    {'<','<','<','<','<','=',' '},
                    {'>','>','>','>',' ','>','>'},
                    {'<','<','<','<','<',' ','='}
            };
            String a="+-*/()#";
            int a11=a.indexOf(f);
            int a12=a.indexOf(s);
            return a1[a11][a12];
        }
        static int [] chansheng(int num)//随机产生括号
        {
            int []b=new int[num];
            for(int i=0;i<b.length;i++)
            {
                b[i]=0;
            }
            Random rd=new Random();
            for(int i=2;i<num;i++)
            {
                for(int j=0;j<num-i+1;j++)
                {
                    int t=rd.nextInt(2);
                    if(t==1)
                    {
                        if(b[j]>=0&&b[j+i-1]<=0)
                        {
                            int c=0;
                            for(int k=j;k<j+i;k++)
                            {
                                c+=b[k];
                            }
                            if(c==0)
                            {
                                b[j]++;
                                b[j+i-1]--;
                            }
                        }
                        
                    }
                }
            }
            return b;
        }
            //运算
        public     static String jisuanbh(String a)//表达式的运算
        {
            Stack <String>num=new Stack <String>();
            Stack <String>fuhao=new Stack<String>();
            a+="#";
            fuhao.push("#");
            char ch;
            int i=0;
            int s=0;
            int y=0;
            ch=a.charAt(i);
            while(!(ch+"").equals("#") || !fuhao.peek().equals("#"))
            {
                if(ch==' ')//如果遇到字符为空,说明遇到数字
                {
                    String rn="";//用来记录数据
                    while(true)
                    {
                        ch=a.charAt(++i);
                        if(ch==' ')
                        {
                            break;
                        }
                        rn+=ch;
                    }
                    if((i+1)<a.length()){
                        ch=a.charAt(++i);
                    }
                    num.push(rn);
                }
                else//遇到的是字符
                {
                    char comp=youxian(fuhao.peek(),ch+"");//比较两个字符的优先级
                    if(comp=='='){//说明遇到右括号
                        fuhao.pop();
                        if((i+1)<a.length()){
                            ch=a.charAt(++i);
                        }
                    }
                    else if(comp=='>')//优先级高,弹出两个数和一个运算符,进行运算
                    {
                        String st1=num.pop();
                        String st2=num.pop();
                        String fuh1=fuhao.pop();
                        char fuh2=fuh1.charAt(0);//将String类型转为char类型
                        String []rs1=new String[2];
                        rs1=yunsuan2(st2, st1, fuh1);
                        if(rs1[1].equals("error"))//如果运算中有问题,就结束运算
                        {
                            return "error";
                        }
                        else
                        {
                            num.push(rs1[0]+"");//将两数结果压入栈中
                        }
                    }
                    else//优先级比较低,把运算符压入栈中
                    {
                        fuhao.push(ch+"");
                        if((i+1)<a.length())
                        {
                            ch=a.charAt(++i);
                        }
                    }
                }
            }
            String rs=num.pop();
            int wz=rs.indexOf("/");
            if(wz!=-1)
            {
                String fb=rs.substring(0, wz);
                String sb=rs.substring(wz+1,rs.length());
                int fb1=Integer.parseInt(fb);
                int sb1=Integer.parseInt(sb);
                if(fb1>=sb1&&fb1%sb1==0)
                {
                    
                    rs=(fb1/sb1)+"";
                }
                else if(fb1<sb1&&fb1%sb1!=0)
                {
                    int commondivisor=maxyue(fb1, sb1);
                    fb1/=commondivisor;
                    sb1/=commondivisor;
                    rs=fb1+"/"+sb1;
                }
                else
                {
                    int commondivisor=maxyue(fb1, sb1);
                    fb1/=commondivisor;
                    sb1/=commondivisor;
                    rs=(fb1/sb1)+"'"+(fb1%sb1)+"/"+sb1;
                }
            }
            return rs;
        }
        public  static String[] tys(String fn,String sn,char c)//两个整数的运算
        {
            int a=Integer.parseInt(fn);
            int b=Integer.parseInt(sn);
            String []a1=new String [2];//a1[0]用来记录两数运算结果,a1[1]用来记录两数能否继续算下去
            a1[0]=a1[1]="";
            int d=0;//d用来短暂存取两数运算结果
            int z=0;//除法中判断a1[0]是否需要加上d
            if(c=='+')
            {
                d=a+b;
            }
            else if(c=='-')
            {
                if(a<b)
                {
                    a1[1]="error";
                    return a1;
                }
                else
                {
                    d=a-b;
                }
            }
            else if(c=='*')
            {
                d=a*b;
            }
            
            else
            {
                if(a%b==0&&a>=b)
                {
                    d=a/b;
                }
                else
                {
                    z=1;
                    a1[0]=a+"/"+b;
                }
            }
            if(z==0)
            {
                a1[0] = d+"";
            }
            return a1;
        }
        public static String[] yunsuan2(String fn,String sn,String e)//两个数运算,分数,整数均可
        {
            String rs[]=new String[2];
            rs[0]=rs[1]="";
            int location1=fn.indexOf("/");
            int location2=sn.indexOf("/");
            if(location1==-1&&location2==-1)//两个整数的运算
            {
                rs=tys(fn, sn, e.charAt(0));
            }
            else{
                int a=0;
                int b=0;
                int c=0;
                int d=0;
                if(location1!=-1&&location2!=-1)//两个数都为真分数
                {
                    String r1=fn.substring(0,location1);
                    String r2=fn.substring(location1+1,fn.length());
                    String r3=sn.substring(0,location2);
                    String r4=sn.substring(location2+1,sn.length());
                    a=Integer.parseInt(r1);
                    b=Integer.parseInt(r2);
                    c=Integer.parseInt(r3);
                    d=Integer.parseInt(r4);
                }
                else
                {
                    if(location1==-1)
                    {
                        a=Integer.parseInt(fn);
                        b=1;
                        String r3=sn.substring(0,location2);
                        String r4=sn.substring(location2+1,sn.length());
                        c=Integer.parseInt(r3);
                        d=Integer.parseInt(r4);
                    }
                    else
                    {
                        c=Integer.parseInt(sn);
                        d=1;
                        String r1=fn.substring(0,location1);
                        String r2=fn.substring(location1+1,fn.length());
                        a=Integer.parseInt(r1);
                        b=Integer.parseInt(r2);
                    }
                }
                int f=0,g=0,h=0,t=0;
                if(e.equals("+"))
                {
                    if(b==d)
                    {
                        f=a+c;
                        g=b;
                    }
                    else
                    {
                        g=b*d/maxyue(b,d);
                        a=g/b*a;
                        c=g/d*c;
                        f=a+c;
                    }
                }
                else if(e.equals("-"))
                {
                    if(b==d)
                    {
                        f=a-c;
                        if(f<=0)
                        {
                            rs[1]="error";
                            return rs;
                        }
                        g=b;
                    }
                    else
                    {
                        g=b*d/maxyue(b,d);
                        a=g/b*a;
                        c=g/d*c;
                        f=a-c;
                        if(f<0)
                        {
                            rs[1]="error";
                            return rs;
                        }
                    }
                }
                else if(e.equals("*"))
                {
                    f=a*c;
                    g=b*d;
                }
                else
                {
                    f=a*d;
                    g=b*c;
                }
                rs[0]=f+"/"+g;
                
            }
            
            return rs;
        }
        public static String[] operationAndStatistical(int ch1,int ch2,int num,int scope) throws ClassNotFoundException, SQLException//对结果进行操作并统计题数
        {
            properFraction p =new properFraction();
            integer i1= new integer();
            String rs="";
            int i=0;
            String saveexcep[]=new String[2*num];
            while(i<num)
            {
                int n=(int) (Math.random()*3+2);
                if(ch1==1&&ch2==1)
                {
                    rs=p.properFractionExithk(n,scope);//产生含有括号的真分数表达式
                }
                else if(ch1==1&&ch2==2)
                {
                    rs=p.properFractionExit(n,scope);//产生不含括号的真分数表达式
                }
                else if(ch1==2&&ch2==1)
                {
                    rs=i1.generateExpressionkh(n,scope);//产生含括号的整数表达式
                }
                else 
                {
                    rs=i1.generationexception(n,scope);//产生不含括号的整数表达式
                }
                String judgers=jisuanbh(rs);//记录答案
                
                if(judgers.equals("error"))
                {
                    System.out.print("");
                }
                else
                {
                    saveexcep[i]=rs;
                    saveexcep[i+num]=judgers; 
                    i++;
                }
            }
            return saveexcep;
        }
    
    }
    
    整数和真分数都需要用到的方法
    package util;
    
    public class solve {
        public boolean[] judgeIfTrue(String[] a,String[] b)//对传过来的两个数组进行比较,正确的数对应1返回
        {
            boolean judgers[] = new boolean [a.length];
            for(int i=0;i<a.length;i++)
            {
                judgers[i]=false;
                if(a[i].equals(b[i]))
                {
                    judgers[i]=true;
                }
            }
            
            return judgers;
        }
        public int[] zongjie(boolean[] judeger)
        {
            
            int[] statisticaltrue = new int[2];//用来计算正确和错误的题数
            statisticaltrue[0]=0;
            statisticaltrue[1]=0;//初始化
             for(int i=0;i<judeger.length;i++)
            {
                if(judeger[i]==true)
                {
                    statisticaltrue[0]+=1;
                }
                else
                {
                    statisticaltrue[1]+=1;
                }
            }
             return statisticaltrue;
        }
        public String[] qiq (boolean []judgers)
        {
            String []rs =new String[2];//分别用来记录正确和错误的题号
            rs[0]="正确的题目题号如下:";
            rs[1]="错误的题目题号如下:";
            for(int i=0;i<judgers.length;i++)
            {
                if(judgers[i]==true)
                {
                    rs[0]+=(i+1)+" ";
                }
                else
                {
                    rs[1]+=(i+1)+" ";
                }
            }
            return rs;
        }
        public String[]countexpression(String[]bds,String[]correctrs,String[]inputrs, boolean[]judgers)
        {
            String[]count1 = new String[judgers.length];
            for(int i=0;i<judgers.length;i++)
            {
                if(judgers[i]==true)
                {
                    count1[i]="第"+(i+1)+"道题:	"+bds[i]+"="+correctrs[i]+"	"+"回答正确";
                    
                }
                else
                {
                    count1[i]="第"+(i+1)+"道题:	"+bds[i]+"="+inputrs[i]+"	"+"回答错误,"+"正确答案是"+correctrs[i];
    
                }
            }
            return count1;
        }
    }
    
    判断题目是否正确

    4.结果截图:

    (1)选择查看还是做题

    (2)选择做题:

    (3)做题:

    (4)判断正确:

    查询历史记录:

     

     这个查询界面做的还不算完善,后面会进行改进。。。

    在其中加入了js的基本的判空

     

    5.实验总结:通过对这个简单的程序的编写,能基本的使用jsp Javabean和servlet来实现一个最小的网页版的程序,在通过链接数据库,使用jdbc技术实现数据的存储和查询,

    这是我对MVC框架有了一个更加深刻的认识,在通过加入js,是这个程序更加符合人们的需求,虽然我现在只是用了这个的皮毛,但是我以后越来越熟练地掌握,另外,我还准备

    加上jquare和ajax来使界面更加合理,我会使我这个程序更加完善。

    6.合作感受:两个人一起写时跟一个写是不一样的,我比较熟悉整体的结构,他比较细心,再设计页面和找错误方面比较擅长,通过这次练习找到我们自己的不足之处,我们

    收货到了更多。

                                                                              程序完成者:    王志伟    胡洋洋

  • 相关阅读:
    Python3.6学习笔记(四)
    Python3.6学习笔记(三)
    Python3.6学习笔记(二)
    Python 3.6学习笔记(一)
    iOS:解决UITextView自适应高度粘贴大量文字导致显示不全的问题
    iOS:苹果内购实践
    iOS:类似于网易云音乐的刷新条目显示弹框
    iOS:练习题中如何用技术去实现一个连线题
    iOS:NSFileHandle和NSFileManger的使用
    iOS:使用莱文斯坦距离算法计算两串字符串的相似度
  • 原文地址:https://www.cnblogs.com/huyangyang/p/6683803.html
Copyright © 2011-2022 走看看