• 团队作业——四则运算网页版


    设计思路:1、输入,在初始界面输入选择以及数值范围,出题数量,打印方式

                 2、(1)整数运算分为有括号和无括号

                            是否有乘除法:在前面做出选择后,在下面只需设置运算符随机出数的范围在0-1之间还是0-3之间

          数值范围:即四则运算随机出数的范围在前域~后域

         加减有无负数:对随机生成的数字进行运算,如果进行加/减运算之后,有负数,则根据选择进行保留或舍弃

        有无括号:用随机数来进行选择在原来式子之前还是之后进行添加

         控制题目不能重复:将之前的题目存放在数组中,然后依次进行比较

        打印方式:根据用户输入要求一行输出几列后,利用取余的方法判断是否要换行输出

        带括号的计算是直接从最里层括号开始往外依次计算

         不带括号的计算则分为没有乘除法和有乘除法

          没有乘除法的是将运算式存为二维数组,将数值和运算符依次分别存放于两个数组中,然后按从左到右的顺序进行计算

        有乘除法的则需进一步统计,在哪有除法或者乘法,然后先进行这两个运算。

                     (2)分数运算,则需产生四个随机数,然后对其进行运算

                  3、输出则是运算式的形式

    input.jsp

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <form name="form1" method="post" action="index.jsp">
    11   <center>小学生的四则运算</center>
    12   请输入选择:1、整数运算  2、真分数运算 <input name="c" type="text" id="c"maxlength="5"><br>
    13   请输入选择:1 、有乘除法 2、无乘除法<input name="c1" type="text" id="c1" maxlength="5"><br>
    14   请输入数值范围的前域:<input name="c2" type="text" id="c2" maxlength="5"><br>
    15   请输入数值范围的后域:<input name="c3" type="text" id="c3" maxlength="5"><br> 
    16   请输入选择:1、除法有余数 2、除法无余数<input name="c4"type="text" id="c4"maxlength="5"><br>
    17    请输入要出题的题目数量:<input name="c5" type="text" id="c5"maxlength="5"><br>
    18    请输入在一行中输出几列运算式<input name="c6" type="text" id="c6" maxlength="5"><br>
    19  请输入选择: 1、有括号 2、无括号:<input name="c7" type="text" id="c7"maxlength="5"><br>
    20     <center><input type="submit"value="确定"></center>
    21 </body>
    22 </html>

    index.jsp

      1 <%@ page language="java" contentType="text/html; charset=utf-8"
      2     pageEncoding="utf-8"%>
      3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      4 <html>
      5 <head>
      6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      7 <title>Insert title here</title>
      8 </head>
      9 <body>
     10 <%@page import="java.util.Random"%>
     11 <%@page import="java.util.Scanner"%>
     12 <%@page import="javax.swing.JOptionPane"%>
     14  <% 
     15  String str1="";
     16  String j=request.getParameter("c");
     17  int c= Integer.valueOf(j);
     18  
     19  String j1=request.getParameter("c1");
     20  int c1=Integer.valueOf(j1);
     21  String j2=request.getParameter("c2");
     22  int c2=Integer.valueOf(j2);
     23  String j3=request.getParameter("c3");
     24  int c3=Integer.valueOf(j3);
     25  String j4=request.getParameter("c4");
     26  int c4=Integer.valueOf(j4);
     27  String j5=request.getParameter("c5");
     28  int c5=Integer.valueOf(j5);
     29  String j6=request.getParameter("c6");
     30  int c6=Integer.valueOf(j6);
     31  String j7=request.getParameter("c7");
     32  int c7=Integer.valueOf(j7);
     33   Random rand = new Random();
     34   int a=0,b=0,d=0,e=0;
     35   int x1,x2,y1,y2,z;
     36   int f=0;//符号
     37   String fu="";//符号
     38   int flag=0;
     39   int p=0,w=0;
     40   int g=0;//结果
     41   int y=0;//用户输入的结果
     42   int m=0;//用户正确题目数
     43   int h=1;//最大公约数
     44   if(c==1)
     45   {
     46        String []Repeat1=new String[1000];
     47       // String [][]Repeat2=new String[2*c5][10];
     48        for(int s=0;s<c5;s++)
     49        {
     50            //数值范围
     51            a=rand.nextInt(c3+1)%(c3-c2+1)+c2;
     52            b=rand.nextInt(c3+1)%(c3-c2+1)+c2;
     53            //符号
     54            if(c1==1)
     55            {
     56                 f=rand.nextInt(4);
     57            }
     58            if(c1==2)
     59            {
     60                f=rand.nextInt(2);
     61            }
     62            if(f==0)
     63            {
     64                fu="+";
     65                g=a+b;
     66                  Repeat1[s]=a+fu+b;
     67            }
     68            if(f==1)
     69            {
     70                fu="-";
     71                if(a>b)
     72                {
     73                    g=a-b;
     74                      Repeat1[s]=a+fu+b;
     75                }
     76                if(a<=b)
     77                {
     78                    g=b-a;
     79                      Repeat1[s]=b+fu+a;
     80                }
     81            }
     82            if(f==2)
     83            {
     84                fu="*";
     85                g=a*b;
     86                  Repeat1[s]=a+fu+b;
     87            }
     88            if(f==3)
     89            {
     90 
     91                if(c4==1)
     92                {
     93                    if(b!=0)
     94                    {
     95                          fu="/";
     96                          g=a/b;
     97                            Repeat1[s]=a+fu+b;
     98                    }
     99                    else
    100                    {
    101                        flag=1;
    102                    }
    103                }
    104                if(c4==2)
    105                {
    106                     if(a%b==0)
    107                     {
    108                         if(b!=0)
    109                         {
    110                             fu="/";
    111                             g=a/b;
    112                              Repeat1[s]=a+fu+b;
    113                         }
    114                         else
    115                         {
    116                             flag=1;
    117                         }
    118                     }
    119                     if(a%b!=0)
    120                     {
    121                         flag=1;
    122                     }
    123                 }
    124            }
    125 
    126            
    127            //出题长度
    128            w=rand.nextInt(3);
    129            if(c7==1)//有括号
    130            {
    131                for(int q=0;q<w;q++)
    132                {
    133                     d=rand.nextInt(c3+1)%(c3-c2+1)+c2;
    134                     p=rand.nextInt(2);
    135                 
    136                       
    137                         if(p==0)
    138                         {
    139                             //符号
    140                               if(c1==1)
    141                               {
    142                                    f=rand.nextInt(4);
    143                               }
    144                               if(c1==2)
    145                               {
    146                                   f=rand.nextInt(2);
    147                               }
    148                               if(f==0)
    149                               {
    150                                   fu="+";
    151                                   g=g+d;
    152                                  Repeat1[s]="("+Repeat1[s]+")"+fu+d+")";
    153                               }
    154                               if(f==1)
    155                               {
    156                                   fu="-";
    157                                   if(g>=d)
    158                                   {
    159                                      g=g-d;
    160                                       Repeat1[s]="("+Repeat1[s]+")"+fu+d+")";
    161                                   }
    162                                   if(g<d)
    163                                   {
    164                                       g=d-g;
    165                                      Repeat1[s]="("+d+fu+"("+Repeat1[s]+")";
    166                                   }
    167                                  // g=g-d;
    168                               }
    169                               if(f==2)
    170                               {
    171                                   fu="*";
    172                                   g=g*d;
    173                                  Repeat1[s]="("+Repeat1[s]+")"+fu+d+")";
    174                               }
    175                               if(f==3)
    176                               {
    177 
    178                                   if(c4==1)
    179                                   {
    180                                      if(d!=0)
    181                                       {
    182                                           fu="/";
    183                                           g=g/d;
    184                                       }
    185                                       else
    186                                       {
    187                                           flag=1;
    188                                       } 
    189                                   }
    190                                   if(c4==2)
    191                                   {
    192                                        if(a%d==0)
    193                                        {
    194                                           if(d!=0)
    195                                           {
    196                                               fu="/";
    197                                               g=g/d;
    198                                           }
    199                                           else
    200                                           {
    201                                               flag=1;
    202                                           } 
    203                                        }
    204                                        if(a%d!=0)
    205                                        {
    206                                            flag=1;
    207                                        }
    208                                       
    209                                    }
    210                                  Repeat1[s]="("+Repeat1[s]+")"+fu+d+")";
    211                               }
    212                              // if(g>d)
    213                             
    214                         }
    215                         if(p==1)
    216                         {
    217                             //符号
    218                               if(c1==1)
    219                               {
    220                                    f=rand.nextInt(4);
    221                               }
    222                               if(c1==2)
    223                               {
    224                                   f=rand.nextInt(2);
    225                               }
    226                               if(f==0)
    227                               {
    228                                   fu="+";
    229                                   g=d+g;
    230                                  Repeat1[s]="("+d+fu+"("+Repeat1[s]+")";
    231                               }
    232                               if(f==1)
    233                               {
    234                                   fu="-";
    235                                   if(g>d)
    236                                   {
    237                                       g=g-d;
    238                                      Repeat1[s]="("+Repeat1[s]+")"+fu+d+")";
    239                                   }
    240                                   if(g<=d)
    241                                   {
    242                                       g=d-g;
    243                                      Repeat1[s]="("+d+fu+"("+Repeat1[s]+")";
    244                                   }
    245                               }
    246                               if(f==2)
    247                               {
    248                                   fu="*";
    249                                   g=d*g;
    250                                  Repeat1[s]="("+d+fu+"("+Repeat1[s]+")";
    251                               }
    252                               if(f==3)
    253                               {
    254 
    255                                   if(c4==1)
    256                                   {
    257                                       if(g!=0)
    258                                       {
    259                                           fu="/";
    260                                           g=d/g;
    261                                       }
    262                                       else
    263                                       {
    264                                           flag=1;
    265                                       }
    266                                   }
    267                                   if(c4==2)
    268                                   {
    269                                        if(a%d==0)
    270                                        {
    271                                           if(g!=0)
    272                                           {
    273                                               fu="/";
    274                                               g=d/g;
    275                                           }
    276                                           else
    277                                           {
    278                                               flag=1;
    279                                           }
    280                                        }
    281                                        if(a%d!=0)
    282                                        {
    283                                            flag=1;
    284                                        }
    285                                    }
    286                                  Repeat1[s]="("+d+fu+"("+Repeat1[s]+")";
    287                               }
    288                         
    289                         }
    290                        
    291                                            
    292                          
    293                }     
    294            }
    295            if(c7==2)//无括号
    296            {
    297                for(int q=0;q<w;q++)
    298                {
    299                     d=rand.nextInt(c3+1)%(c3-c2+1)+c2;
    300                     p=rand.nextInt(2);
    301                 
    302                       //符号
    303                          if(c1==1)
    304                          {
    305                               f=rand.nextInt(4);
    306                          }
    307                          if(c1==2)
    308                          {
    309                              f=rand.nextInt(2);
    310                          }
    311                          if(f==0)
    312                          {
    313                              fu="+";
    314                          }
    315                          if(f==1)
    316                          {
    317                              fu="-";
    318                          }
    319                          if(f==2)
    320                          {
    321                              fu="*";
    322                          }
    323                          if(f==3)
    324                          {
    325 
    326                              if(c4==1)
    327                              {
    328                                  fu="/";
    329                                  
    330                              }
    331                              if(c4==2)
    332                              {
    333                                   if(a%d==0)
    334                                   {
    335                                       fu="/";
    336                                   }
    337                                   if(a%d!=0)
    338                                   {
    339                                       flag=1;
    340                                   }
    341                               }
    342                          }
    343                         if(p==0)
    344                         {
    345                             if(fu.equals("+")&&d==0)
    346                             {
    347                                 flag=1;
    348                             }
    349                             else
    350                             {
    351                                 Repeat1[s]=Repeat1[s]+fu+d;
    352                             }
    353                             
    354                         }
    355                         if(p==1)
    356                         {
    357                             Repeat1[s]=d+fu+Repeat1[s];
    358                         }
    359                         
    360                }
    361            }
    362           //判断重复
    363            String str="";//中间判等字符串
    364            StringBuffer stringBuffer = new StringBuffer (str); 
    365            str=str+stringBuffer.reverse(); 
    366            for(int w1=0;w1<w;w++)
    367            {
    368                if(w==0)//两位数
    369                {
    370                    if(Repeat1[s].equals(Repeat1[w1]))//简单判等
    371                    {
    372                         flag=1;
    373                    }     
    374                    if(Repeat1[s].equals(str))
    375                    {
    376                        flag=1;
    377                    }
    378                }
    379                else
    380                {
    381                    if(Repeat1[s].equals(Repeat1[w1]))//简单判等
    382                    {
    383                         flag=1;
    384                    }     
    385                }
    386            }
    387            //输出
    388            if(flag==1)
    389            {
    390                c5++;
    391            }
    392            else if(flag==0)
    393            {
    394               if((s+1)%c6==0)
    395               {
    396                   for(int k=0;k<10000000;k++)
    397                        {
    398                            String inputx=JOptionPane.showInputDialog(Repeat1[s]+"="+"请输入计算结果");
    399                            if(inputx!=null&&!inputx.equals(""))
    400                           {
    401                              y=Integer.parseInt(inputx);
    402                              break;
    403                           }     
    404                        }
    405                        if(y==g)
    406                          {
    407                             // System.out.println(Repeat1[s]+"="+y+"正确");    
    408                             str1=str1+Repeat1[s]+"="+y+"正确"+"<br>";
    409                              m++;
    410                          }
    411                          if(y!=g)
    412                          {
    413                              //System.out.println(Repeat1[s]+"="+d+"不正确");
    414                              str1=str1+Repeat1[s]+"="+y+"不正确"+"<br>";
    415                          }
    416               }
    417               else
    418               {
    419                   for(int k=0;k<10000000;k++)
    420                        {
    421                            String inputx=JOptionPane.showInputDialog(Repeat1[s]+"="+"请输入计算结果");
    422                            if(inputx!=null&&!inputx.equals(""))
    423                           {
    424                              y=Integer.parseInt(inputx);
    425                              break;
    426                           }     
    427                        }
    428                        if(y==g)
    429                          {
    430                              //System.out.print(Repeat1[s]+"="+y+"正确");    
    431                              str1=str1+Repeat1[s]+"="+y+"正确";
    432                              m++;
    433                          }
    434                          if(y!=g)
    435                          {
    436                              //System.out.print(Repeat1[s]+"="+y+"不正确");
    437                              str1=str1+Repeat1[s]+"="+y+"不正确";
    438                          }
    439               }
    440            }
    441        }
    442       // System.out.println("共"+c5+"道题"+"    "+m+"道题正确");
    443       str1=str1+"<br>"+"共"+c5+"道题"+"    "+m+"道题正确";
    444 }
    445   if(c==2)
    446   {
    447        String []Repeat1=new String[2*c5];
    448        String []Repeat2=new String[2*c5];
    449        for(int s=0;s<c5;s++)
    450        {
    451            int sx=s;
    452 
    453           //数值范围
    454              a=rand.nextInt(c3+1)%(c3-c2+1)+c2;
    455              b=rand.nextInt(c3+1)%(c3-c2+1)+c2;
    456              d=rand.nextInt(c3+1)%(c3-c2+1)+c2;
    457              e=rand.nextInt(c3+1)%(c3-c2+1)+c2;
    458              String s1="",s2="";//两个真分数的字符串
    459              if(b==0)
    460              {
    461                  flag=1;
    462              }
    463              else if(b!=0)
    464              { 
    465                  if(a<b)
    466                  {
    467                      int r = b % a;
    468                         while(r != 0){
    469                           b = a;
    470                           a= r;
    471                           r = b % a;
    472                           h=a;
    473                         }
    474                         
    475                      //h=f(Math.abs(a),Math.abs(b));
    476                      s1=(a/h)+"/"+(b/h);
    477                     
    478                  }
    479                  if(a>b)
    480                  {
    481                      int r = a % b;
    482                         while(r != 0){
    483                           a = b;
    484                           b = r;
    485                           r = a % b;
    486                           h=b;
    487                         }
    488                      //h=f(Math.abs(b),Math.abs(a));
    489                      s1=(b/h)+"/"+(a/h);
    490                  }
    491                  if(a==b)
    492                  {
    493                      b++;
    494                      int r = b % a;
    495                         while(r != 0){
    496                           b = a;
    497                           a= r;
    498                           r = b % a;
    499                           h=a;
    500                         }
    501                     // h=f(Math.abs(a-1),Math.abs(b));
    502                      s1=(a/h)+"/"+(b/h);
    503                  }
    504              }
    505              if(e==0) 
    506              {
    507                  flag=1;
    508              }
    509              else
    510              {
    511                  if(d<e)
    512                  {
    513                      int r = e % d;
    514                         while(r != 0){
    515                           e = d;
    516                           d = r;
    517                           r = e % d;
    518                           h=d;
    519                         }
    520                     // h=f(Math.abs(d),Math.abs(e));
    521                      s2=(d/h)+"/"+(e/h);
    522                  }
    523                  if(d>e)
    524                  {
    525                      int r = d % e;
    526                         while(r != 0){
    527                           d = e;
    528                           e= r;
    529                           r = d % e;
    530                           h=e;
    531                         }
    532                     // h=f(Math.abs(e),Math.abs(d));
    533                      s2=(e/h)+"/"+(d/h);
    534                  }
    535                  if(d==e)
    536                  {
    537                      e++;
    538                      int r = e% d;
    539                         while(r != 0){
    540                           e = d;
    541                           d = r;
    542                           r = d % e;
    543                           h=d;
    544                         }
    545                     // h=f(Math.abs(d-1),Math.abs(e));
    546                      s2=(d/h)+"/"+(e/h);
    547                  }
    548              }
    549             
    550               //符号
    551              f=rand.nextInt(4);
    552              if(f==0)
    553              {
    554                  fu="+";
    555              }
    556              if(f==1)
    557              {
    558                  fu="-";
    559              }
    560              if(f==2)
    561              {
    562                  fu="*"; 
    563              }
    564              if(f==3)
    565              {
    566                  if(d!=0)
    567                  {
    568                      fu="/";
    569                  }
    570                  if(d==0)
    571                  {
    572                      flag=1;
    573                  }
    574              }
    575              Repeat1[s]=s1+fu+s2;
    576              //String str1=s2+fu+s1;
    577              //判断重复
    578              int cc=0;
    579              for(int ss=0;ss<sx;ss++)
    580              {
    581                  if(Repeat1[s].equals(Repeat1[ss]))
    582                  {
    583                      flag=1;
    584                  }
    585                  else
    586                  {
    587                      flag=0;
    588                  }
    589                  
    590              }
    591              //输出
    592              Repeat2[s]="("+s1+")"+fu+"("+s2+")";
    593              if(flag==1)
    594              {
    595                   c5++;
    596              }
    597              else if(flag==0)
    598              {
    599                  if((s+1)%c6==0)
    600                  {
    601                      //System.out.println(Repeat2[s]+"=  ");
    602                      str1=str1+Repeat2[s]+"=  "+"<br>";
    603                  }
    604                  else
    605                  {
    606                     // System.out.print(Repeat2[s]+"=  ");
    607                      str1=str1+Repeat2[s]+"=  ";
    608                  }
    609              }
    610               
    611        }
    612   }
    613   
    614   %>
    615   <%=str1 %>
    616  <table>
    617  <tr>
    618  </tr>
    619  </table>
    620 </body>
    621 </html>



    运行结果:


    
    
    结果分析:无括号整数运算,以及分数运算都不能实现
     
  • 相关阅读:
    POJ 3419 Difference Is Beautiful (DP + 二分 + rmq)
    CodeForces 755C PolandBall and Forest (并查集)
    CodeForces 754D Fedor and coupons (优先队列)
    CodeForces 753C Interactive Bulls and Cows (Hard)
    CodeForces 754C Vladik and chat (DP+暴力)
    HDU 1996 汉诺塔VI (排列组合)
    HDU 1995 汉诺塔V (水题)
    HDU 2077 汉诺塔IV (递推)
    HDU 2064 汉诺塔III (递推)
    How Javascript works (Javascript工作原理) (二) 引擎,运行时,如何在 V8 引擎中书写最优代码的 5 条小技巧
  • 原文地址:https://www.cnblogs.com/muamu/p/5372518.html
Copyright © 2020-2023  润新知