• 四则运算网页版


    主要思路:在完成控制出题界面后,根据选择控制输出,将答案计算出来,连接到数据库,其中还涉及到最大公约数计算分数运算。

    源代码:

    <%@ 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=ISO-8859-1">
        <title>出题系统</title>
    </head>
    <body> 
            <h2>出题系统</h2> 
            <form action="check.jsp" method="post">
            请输入出题数量:<input type="text" name = "username"><br>
            请输入运算元数:   <input type="text" name="choose1"><br>
            输入数值范围:<input type="text" name="choose2"><br><hr>
       1.整数四则运算 2.真分数四则运算<br>
            选择:   有<input type="radio" name="choose"value=1>   无<input type="radio" name="choose"value=2><br>
            有无乘除:   有<input type="radio" name="choose3"value=1>  无<input type="radio" name="choose3"value=2><br>
            有无负数:   有<input type="radio" name="choose4"value=1>  无<input type="radio" name="choose4"value=2><br>
            有无余数:   有<input type="radio" name="choose6"value=1>  无<input type="radio" name="choose6"value=2><br>    
            有无括号:   有<input type="radio" name="choose7"value=1>  无<input type="radio" name="choose7"value=2><br>
            是否显示答案:   有<input type="radio" name="choose5"value=1>  无<input type="radio" name="choose5"value=2><br>
            <input type="submit" value="出题">
            </form>
    </body>
    </html>
    

      Yunsuan.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%@   page   import= "size.com.* "%>
    <!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>
    <%
        JspWriter mout=pageContext.getOut();
    String zh = request.getParameter("shuliang");
    String zhh = request.getParameter("choose");
    String Min=request.getParameter("xiao");
    String Max=request.getParameter("da");
    String shu = request.getParameter("geshu");
    String chu = request.getParameter("chengchu");
    String hao=request.getParameter("kuohao");
    int x=Integer.parseInt(zh);
    int y=Integer.parseInt(zhh);
    int min=Integer.parseInt(Min);
    int max=Integer.parseInt(Max);
    int z=Integer.parseInt(shu);
    int xx=Integer.parseInt(chu);
    int t=Integer.parseInt(hao);
    int m,n,j,jj;
    char []ch=new char[4];
    int a1,b1;
    ch[0]='+';
    ch[1]='-';
    ch[2]='*';
    ch[3]='/';
    if(y==1)
    {
        int []M=new int[10];
        String []N=new String[10];
        for(int ii=1;ii<=x;ii++)
        {
            mout.print("30道题目");
            mout.print("<br>");
            String []A=new String[30];
            a1=(int)(Math.random()*(z-1)+1);
            b1=a1+1;
            for(int i=0;i<30;i++)
            {
                A[i]="";
                for(int o=0;o<z;o++)
                {
                    M[o]=(int)(Math.random()*max+min);
                    if(xx==1)
                j=(int)(Math.random()*4+1);
                    else
                        j=(int)(Math.random()*2+1);
            if(o==z-1)
                A[i]=A[i]+M[o];
            else
                A[i]=A[i]+M[o]+ch[j-1];
                }
                for(int k=0;k<i;k++)
                {
                    if(A[i].equals(A[k]))
                    {
                        i--;
                        break;
                    }
                }
            }
            String[] B=new String[30];
            for(int i=0;i<30;i++)
            {
                B[i]=A[i];
            }
            if(t==1)
            {
                for(int iii=0;iii<30;iii++)
                {
                    String ss=A[iii];
                    int num =z-1;
                    int length = ss.length();
                    int[] in = new int[num];
                    int i=0;
                    for(int j1=0;j1<ss.length();j1++)
                    {
                        if(ss.charAt(j1)=='+'||ss.charAt(j1)=='/'||ss.charAt(j1)=='-'||ss.charAt(j1)=='*')
                        {
                            in[i] = j1;
                            i++;
                        }
                    }
         
                    /*for(int m1=0;m1<num;m1++)
                    {
                         mout.print("<br>");
                    }*/
                    StringBuffer temp = new StringBuffer(ss);
                    int former = (int)(Math.random()*length+1);
                    int end = (int)(Math.random()*length+1);
                    while(num<=2&&former !=1&&end != length)
                    {
                       former = (int)(Math.random()*length+1);
                       end = (int)(Math.random()*length+1);
                    }
                    if(former == 1)
                    {
                       temp.insert(former-1, '(');
                       int next = (int)(Math.random()*num);
                       while(next == 0)
                           next = (int)(Math.random()*num);
                       temp.insert(in[next]+1, ')');
                       end--;
                    }
                    if(former >1&&end <length&&num>2)
                    {
                        int first = (int)(Math.random()*num);//括起来的第一个数
                        int next = (int)(Math.random()*num);//括起来的最后一个数
                        while(next-first<=1)
                        {
                             first = (int)(Math.random()*num);//括起来的第一个数
                             next = (int)(Math.random()*num);//括起来的最后一个数
                        }
     
                        temp.insert(in[first]+1, '(');
                        temp.insert(in[next]+1, ')');
                   }
         
                   if(end == length)
                   {
                        temp.insert(end, ')');
                        int first = (int)(Math.random()*num);
                        while(first == num-1)
                            first = (int)(Math.random()*num);
                        temp.insert(in[first]+1, '(');
                   }
                   ss = temp.toString();
                   A[iii]=ss;
                }
            }
            double[] K=new double[30];
             double[] C=new double[30];
            for(int i=0;i<30;i++)
            {
                if(t==1)
                {
                    while(A[i].indexOf('(')!=-1)
                    {
                        int left=0;
                        int right=A[i].length();
                        char op;
                        for(int i4=0;i4<A[i].length();i4++)
                        {
                            if(A[i].charAt(i4)=='(')
                            {
                                left=i4;
                            }
                            if(A[i].charAt(i4)==')')
                            {
                                right=i4;
                                break;
                            }
                        }
                        String strq=A[i].substring(left+1,right);
                        mout.print("<br>");
                        mout.print(strq);
                        mout.print("<br>");
                        /* C[i]=YL.fun1(strq);
                        B[i]=A[i].substring(0,left-1)+String.valueOf(C[i])+A[i].substring(right+1,A[i].length()-1);*/
                    }
                    /*K[i]=YL.fun1(B[i]);
                    mout.print("<br>");
                    mout.print(C[i]);
                    mout.print("<br>");*/
                }
                else
                    K[i]=YL.fun1(B[i]);
            }
            for(int i=0;i<30;i++)
            {
                mout.print((i+1)+". "+A[i]);
                mout.print("<br>");
            }
            mout.print("答案:");
            mout.print("<br>");
            for(int i=0;i<30;i++)
            {
                mout.print((i+1)+"、"+K[i]);
                mout.print("<br>");
            }
            mout.print("-"+ii+"-");
            mout.print("<br><br><br>");
        }
    }
    if(y==2)
    {
        for(int ii=1;ii<=x;ii++)
        {
            int a,b,c,d;
            String aa,bb;
            mout.print("30道题目");
            mout.print("<br>");
            String []B=new String[30];
            for(int i=0;i<30;i++)
            {
                a=(int)(Math.random()*100+1);
                b=(int)(Math.random()*100+1);
                if(a>b)
                    aa=b+"/"+a;
                else if(a<b)
                    aa=a+"/"+b;
                else
                    aa="1";
                c=(int)(Math.random()*100+1);
                d=(int)(Math.random()*100+1);
                if(c>d)
                    bb=d+"/"+c;
                else if(c<d)
                    bb=c+"/"+d;
                else
                    bb="1";
                j=(int)(Math.random()*4+1);
                B[i]=aa+" "+ch[j-1]+" "+bb+"=";
                for(int k=0;k<i;k++)
                {
                    if(B[i].equals(B[k]))
                    {
                        i--;
                        break;
                    }
                }
            }
            for(int i=0;i<30;i++)
            {
                mout.print((i+1)+". "+B[i]);
                mout.print("<br>");
            }
            mout.print("-"+ii+"-");
            mout.print("<br><br><br>");
        mout.print("-"+ii+"-");
        mout.print("<br><br><br>");
        }
    }
    %>
    </body>
    </html><br><br><br><br>
    

      

     Caculate.class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    package demo;
     
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Stack;
    public class Caculate {
        /*public static void main(String[] args) {
        // TODO Auto-generated method stub
     
        String str1="22+4*(2+3)";
        double result=fun2(str1);
        System.out.println(result);
        }*/
        public static Map pro=new HashMap();
        public static void init()
        {
            pro.put('+'1);
            pro.put('-'1);
            pro.put('*'2);
            pro.put('÷'2);
        }
        public static int getIndex(String str)
        {
           int index1=(str.indexOf('+')==-1?str.length():str.indexOf('+'));
           int index2=(str.indexOf('-')==-1?str.length():str.indexOf('-'));
           int index3=(str.indexOf('*')==-1?str.length():str.indexOf('*'));
           int index4=(str.indexOf('÷')==-1?str.length():str.indexOf('÷'));
           int index=index1<index2?index1:index2;
           index=index<index3?index:index3;
           index=index<index4?index:index4;
           return index;
        }
        public static double cal(char op,double num1,double num2)
        {
           switch(op)
           {
           case '+':
           return num1+num2;
           case '-':
           return num1-num2;
           case '*':
           return num1*num2;
           default:
           return num1%num2;
           }
        }
        public static double fun1(String str)
        {
           init();
           Stack st1=new Stack();
           Stack st2=new Stack();
           int fop=0;
           while(str.length()>0)
           {
              int index=getIndex(str);
              st1.push(Double.parseDouble(str.substring(0,index)));
              if(index!=str.length())
              {
                 char op=str.charAt(index);
                 str=str.substring(index+1);
                 while(true)
                 {
                    if((int)pro.get(op)>fop)
                    {
                       st2.push(op);
                       fop=(int)pro.get(op);
                       break;
                    }
                     else
                     {
                        double num2= (double) st1.pop();
                        double num1=(double) st1.pop();
                        double result=cal((char)st2.pop(),num1,num2);
                        st1.push(result);
                        if(st2.size()==0)
                        {
                             st2.push(op);
                             fop=(int)pro.get(op);
                             break;
                        }
                        char cop=(char) st2.pop();
                        fop=(int)pro.get(cop);
                        st2.push(cop);
                       }
               }
              }
               else
               {
                  break;
               }
           }
            while(st2.size()!=0)
            {
                double num2=(double) st1.pop();
                double num1=(double) st1.pop();
                char op=(char) st2.pop();
                st1.push(cal(op,num1,num2));
            }
        double result=(double) st1.pop();
        return result;
        }
        public static double fun2(String str)
        {
        while(str.indexOf('(')!=-1)
        {
        int left=0;
        int right=str.length();
        char op;
        for(int i=0;i<str.length();i++)
        {
        if(str.charAt(i)=='(')
        {
        left=i;
        }
        if(str.charAt(i)==')')
        {
        right=i;
        break;
        }
        }
        str=str.substring(0,left)+fun1(str.substring(left+1,right))+str.substring(right+1);
        }
     
        return fun1(str);
     
    }
        }

      fencal.class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    package demo;
     
    public class fencal {
     
        public static String fenmu(String ss,int n,int m)
        {//n 几元,m 有无括号
            ss = '='+ss+'=';
            String result="";
            String cal = "";
            int num = n*2+1;
            if(m == 1)
            {
                num = num + 2;
            }
            int length = ss.length();
            int[] in = new int[num];//操作符下标数组
            String[] str = new String[n];//分母数组
            String[] str1 = new String[n];//分子数组
             
            int i=0;//操作符计数器
            int j=0;//字符串计数器
            int z=0;//分母数组计数器
            double fm=1;
             
            for(j=0;j<length;j++)
            {
                if(ss.charAt(j)=='+'||ss.charAt(j)=='÷'||ss.charAt(j)=='-'||ss.charAt(j)=='*'||ss.charAt(j)=='('||ss.charAt(j)==')'||ss.charAt(j)=='/'||ss.charAt(j)=='=')
                {
                    in[i] = j;
                    i++;
                }
            }
             
            /*for(i=0;i<num;i++)
                System.out.println(in[i]);*/
     
            for(i=0;i<num;i++)
            {
                 
                if(ss.charAt(in[i]) == '/')
                {
                    str[z] = ss.substring(in[i]+1, in[i+1]);
                    z++;
                }
            }
             
            /*for(z=0;z<n;z++)
                System.out.println(str[z]);*/
             
            for(z=0;z<n;z++)
            {
                double fmm = Double.parseDouble(str[z]);
                fm = fm*fmm;
            }
             
            System.out.println("分母为"+fm);
             
            return ""+fm;
             
        }
     
        public static String shizi(String ss,int n,int m)
        {
            String shizi = "";
            ss = '='+ss+'=';
            String result="";
            String cal = "";
            int num = n*2+1;
            if(m == 1)
            {
                num = num + 2;
            }
            int length = ss.length();
            int[] in = new int[num];//操作符下标数组
            String[] str = new String[n];//分母数组
            String[] str1 = new String[n];//分子数组
             
            int i=0;//操作符计数器
            int j=0;//字符串计数器
            int z=0;//分母数组计数器
            double fm=1;
             
            for(j=0;j<length;j++)
            {
                if(ss.charAt(j)=='+'||ss.charAt(j)=='÷'||ss.charAt(j)=='-'||ss.charAt(j)=='*'||ss.charAt(j)=='('||ss.charAt(j)==')'||ss.charAt(j)=='/'||ss.charAt(j)=='=')
                {
                    in[i] = j;
                    i++;
                }
            }
             
            for(i=0;i<num;i++)
                System.out.println(in[i]);
     
            for(i=0;i<num;i++)
            {
                if(ss.charAt(in[i]) == '/')
                {
                    str[z] = ss.substring(in[i]+1, in[i+1]);
                    z++;
                }
            }
             
            /*for(z=0;z<n;z++)
                System.out.println(str[z]);*/
             
            for(z=0;z<n;z++)
            {
                double fmm = Double.parseDouble(str[z]);
                fm = fm*fmm;
            }
             
            System.out.println("分母为"+fm);
             
            z=0;
            for(i=0;i<num;i++)
            {
                if(ss.charAt(in[i]) == '/')
                {
                    str1[z] = ss.substring(in[i-1]+1, in[i]);
                    z++;
                }
            }
             
            for(z=0;z<n;z++)
            {
                double fmm = Double.parseDouble(str[z]);
                double fzz = Double.parseDouble(str1[z]);
                double fz = fm/fmm*fzz;
                 
                str1[z] = ""+fz;
            }
             
             
            for(z=0;z<n;z++)
                System.out.println("分子"+str1[z]);
             
            z=0;
            int numm = n-1;
            if(m == 1)
                numm = numm +2;
            String[] str2 =new String[numm];
            for(i=0;i<num;i++)
            {
                if(ss.charAt(in[i])!='='&&ss.charAt(in[i])!='/')
                {
                    str2[z] = ""+ss.charAt(in[i]);
                    z++;
                }
            }
             
            /*for(z=0;z<numm;z++)
                System.out.println(str2[z]);*/
             
            if(m == 1)
            {
                 
                for(i=0;i<numm;i++)
                {
                    if(str2[i].equals("(")&&i>0)
                    {
                        j=i-1;
                        str2[j] = str2[j] + str2[i];
                        break;
                    }
                }
                 
                for(j=j+1;j<numm-1;j++)
                    str2[j]=str2[j+1];
            }
             
            if(m == 1)
            {
                for(i=0;i<numm;i++)
                {
                     
                    if(str2[i].equals(")")&&i<numm-1)
                    {System.out.println("zou");
                        j=i+1;
                        str2[i] = str2[i]+str2[j];
                        break;
                    }
                }
                 
                for(;j<numm-1;j++)
                    str2[j]=str2[j+1];
            }
             
            for(z=0;z<numm;z++)
                System.out.println(str2[z]);
             
             
            if(str2[0].equals("("))
            {
                for(i=0;i<n-1;i++)
                {
                    shizi = shizi + str2[i]+""+ str1[i];
                }
                shizi = shizi + str2[i]+str1[n-1];
            }
            else if(str2[numm-1].equals(")"))
            {
                for(i=0;i<n-1;i++)
                {
                    shizi = shizi + str1[i]+ ""+ str2[i];
                }
                shizi = shizi +str1[n-1]+str2[i];
            }
            else
            {
                for(i=0;i<n-1;i++)
                {
                    shizi = shizi + str1[i]+""+ str2[i];
                }
                 
                shizi = shizi + str1[n-1];
            }
             
             
            return shizi;
        }
     
        public static int getGongYueShu(int a, int b){
            int t = 0;
            if(a<b){
                t =a;
                a=b;
                b=t;
                }
            int c=a%b;
            if(c==0)
            {
                return b;
            }
            else
                return getGongYueShu(a,c);
            }
     
     
         
    }

      Action.class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package demo;
     
    import java.sql.*;
     
    public class Action {
        public void add(int num,String str1,String str2) throws SQLException{
             Connection conn  = Main.getConn();  //获取数据库连接
             Statement st = conn.createStatement();
             String sql = "insert into calTable([number],[question],[answer]) values('"+ num +"','" + str1 + "','" + str2 + "')";
             st.executeUpdate(sql);
             st.close();
             conn.close();
     }
    }

      Main.class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    package demo;
     
    import java.sql.*;
     
    public class Main {
        public static Connection getConn(){
      
          String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
          Connection dbConn = null;
          String dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Size";
          String userName="sa";
          String userPwd="123456";
         try
        {
            Class.forName(driverName);
            System.out.println("加载驱动成功!");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("加载驱动失败!");
        }
        try{
             dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
              
                System.out.println("连接数据库成功!");
        }catch(Exception e)
        {
            e.printStackTrace();
            System.out.print("SQL Server连接失败!");
        }
        return dbConn;
        }
     }

      

    截图:

     总结:这次四则运算的实现是我和于莅翔同学一起完成的 ,在我们的共同努力下,将此次作业逐步完善并修改,虽然其中有些不足,但也看到了结对编程的好处,以后会更加努力。

  • 相关阅读:
    Leveldb Advanced
    loadrunner生成随机数
    用strtok函数分割字符串
    loadrunner关联及web_reg_save_param方法浅析
    Linux中find用法
    在LoadRunner中从数组类型的参数随机取值的方法
    LoadRunner可以把关联取值当作检查点来使用
    Oracle特殊字符转义:&amp;和&#39;
    LoadRunner 11 error:Cannot initialize driver dll
    用SecureCRT在windows和CentOS间上传下载文件
  • 原文地址:https://www.cnblogs.com/jokerr/p/6687048.html
Copyright © 2020-2023  润新知