码迷,mamicode.com
首页 > 其他好文 > 详细

软件工程结对作业02

时间:2016-04-09 21:52:57      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

input.jsp

<%@ 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>
<form name="form1" method="post" action="index.jsp">
  <center>小学生的四则运算</center>
  请输入选择:1、整数运算  2、真分数运算 <input name="c" type="text" id="c"maxlength="5"><br>
  请输入选择:1 、有乘除法 2、无乘除法<input name="c1" type="text" id="c1" maxlength="5"><br>
  请输入数值范围的前域:<input name="c2" type="text" id="c2" maxlength="5"><br>
  请输入数值范围的后域:<input name="c3" type="text" id="c3" maxlength="5"><br> 
  请输入选择:1、除法有余数 2、除法无余数<input name="c4"type="text" id="c4"maxlength="5"><br>
   请输入要出题的题目数量:<input name="c5" type="text" id="c5"maxlength="5"><br>
   请输入在一行中输出几列运算式<input name="c6" type="text" id="c6" maxlength="5"><br>
 请输入选择: 1、有括号 2、无括号:<input name="c7" type="text" id="c7"maxlength="5"><br>
    <center><input type="submit"value="确定"></center>
</body>
</html>

index.jsp

<%@ 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>
<%@page import="java.util.Random"%>
<%@page import="java.util.Scanner"%>
<%@page import="javax.swing.JOptionPane"%>
 <% 
 String str1="";
 String j=request.getParameter("c");
 int c= Integer.valueOf(j);
 
 String j1=request.getParameter("c1");
 int c1=Integer.valueOf(j1);
 String j2=request.getParameter("c2");
 int c2=Integer.valueOf(j2);
 String j3=request.getParameter("c3");
 int c3=Integer.valueOf(j3);
 String j4=request.getParameter("c4");
 int c4=Integer.valueOf(j4);
 String j5=request.getParameter("c5");
 int c5=Integer.valueOf(j5);
 String j6=request.getParameter("c6");
 int c6=Integer.valueOf(j6);
 String j7=request.getParameter("c7");
 int c7=Integer.valueOf(j7);
  Random rand = new Random();
  int a=0,b=0,d=0,e=0;
  int x1,x2,y1,y2,z;
  int f=0;//符号
  String fu="";//符号
  int flag=0;
  int p=0,w=0;
  int g=0;//结果
  int y=0;//用户输入的结果
  int m=0;//用户正确题目数
  int h=1;//最大公约数
  if(c==1)
  {
       String []Repeat1=new String[1000];
      // String [][]Repeat2=new String[2*c5][10];
       for(int s=0;s<c5;s++)
       {
           //数值范围
           a=rand.nextInt(c3+1)%(c3-c2+1)+c2;
           b=rand.nextInt(c3+1)%(c3-c2+1)+c2;
           //符号
           if(c1==1)
           {
                f=rand.nextInt(4);
           }
           if(c1==2)
           {
               f=rand.nextInt(2);
           }
           if(f==0)
           {
               fu="+";
               g=a+b;
                 Repeat1[s]=a+fu+b;
           }
           if(f==1)
           {
               fu="-";
               if(a>b)
               {
                   g=a-b;
                     Repeat1[s]=a+fu+b;
               }
               if(a<=b)
               {
                   g=b-a;
                     Repeat1[s]=b+fu+a;
               }
           }
           if(f==2)
           {
               fu="*";
               g=a*b;
                 Repeat1[s]=a+fu+b;
           }
           if(f==3)
           {

               if(c4==1)
               {
                   if(b!=0)
                   {
                         fu="/";
                         g=a/b;
                           Repeat1[s]=a+fu+b;
                   }
                   else
                   {
                       flag=1;
                   }
               }
               if(c4==2)
               {
                    if(a%b==0)
                    {
                        if(b!=0)
                        {
                            fu="/";
                            g=a/b;
                             Repeat1[s]=a+fu+b;
                        }
                        else
                        {
                            flag=1;
                        }
                    }
                    if(a%b!=0)
                    {
                        flag=1;
                    }
                }
           }

           
           //出题长度
           w=rand.nextInt(3);
           if(c7==1)//有括号
           {
               for(int q=0;q<w;q++)
               {
                    d=rand.nextInt(c3+1)%(c3-c2+1)+c2;
                    p=rand.nextInt(2);
                
                      
                        if(p==0)
                        {
                            //符号
                              if(c1==1)
                              {
                                   f=rand.nextInt(4);
                              }
                              if(c1==2)
                              {
                                  f=rand.nextInt(2);
                              }
                              if(f==0)
                              {
                                  fu="+";
                                  g=g+d;
                                 Repeat1[s]="("+Repeat1[s]+")"+fu+d+")";
                              }
                              if(f==1)
                              {
                                  fu="-";
                                  if(g>=d)
                                  {
                                     g=g-d;
                                      Repeat1[s]="("+Repeat1[s]+")"+fu+d+")";
                                  }
                                  if(g<d)
                                  {
                                      g=d-g;
                                     Repeat1[s]="("+d+fu+"("+Repeat1[s]+")";
                                  }
                                 // g=g-d;
                              }
                              if(f==2)
                              {
                                  fu="*";
                                  g=g*d;
                                 Repeat1[s]="("+Repeat1[s]+")"+fu+d+")";
                              }
                              if(f==3)
                              {

                                  if(c4==1)
                                  {
                                     if(d!=0)
                                      {
                                          fu="/";
                                          g=g/d;
                                      }
                                      else
                                      {
                                          flag=1;
                                      } 
                                  }
                                  if(c4==2)
                                  {
                                       if(a%d==0)
                                       {
                                          if(d!=0)
                                          {
                                              fu="/";
                                              g=g/d;
                                          }
                                          else
                                          {
                                              flag=1;
                                          } 
                                       }
                                       if(a%d!=0)
                                       {
                                           flag=1;
                                       }
                                      
                                   }
                                 Repeat1[s]="("+Repeat1[s]+")"+fu+d+")";
                              }
                             // if(g>d)
                            
                        }
                        if(p==1)
                        {
                            //符号
                              if(c1==1)
                              {
                                   f=rand.nextInt(4);
                              }
                              if(c1==2)
                              {
                                  f=rand.nextInt(2);
                              }
                              if(f==0)
                              {
                                  fu="+";
                                  g=d+g;
                                 Repeat1[s]="("+d+fu+"("+Repeat1[s]+")";
                              }
                              if(f==1)
                              {
                                  fu="-";
                                  if(g>d)
                                  {
                                      g=g-d;
                                     Repeat1[s]="("+Repeat1[s]+")"+fu+d+")";
                                  }
                                  if(g<=d)
                                  {
                                      g=d-g;
                                     Repeat1[s]="("+d+fu+"("+Repeat1[s]+")";
                                  }
                              }
                              if(f==2)
                              {
                                  fu="*";
                                  g=d*g;
                                 Repeat1[s]="("+d+fu+"("+Repeat1[s]+")";
                              }
                              if(f==3)
                              {

                                  if(c4==1)
                                  {
                                      if(g!=0)
                                      {
                                          fu="/";
                                          g=d/g;
                                      }
                                      else
                                      {
                                          flag=1;
                                      }
                                  }
                                  if(c4==2)
                                  {
                                       if(a%d==0)
                                       {
                                          if(g!=0)
                                          {
                                              fu="/";
                                              g=d/g;
                                          }
                                          else
                                          {
                                              flag=1;
                                          }
                                       }
                                       if(a%d!=0)
                                       {
                                           flag=1;
                                       }
                                   }
                                 Repeat1[s]="("+d+fu+"("+Repeat1[s]+")";
                              }
                        
                        }
                       
                                           
                         
               }     
           }
           if(c7==2)//无括号
           {
               for(int q=0;q<w;q++)
               {
                    d=rand.nextInt(c3+1)%(c3-c2+1)+c2;
                    p=rand.nextInt(2);
                
                      //符号
                         if(c1==1)
                         {
                              f=rand.nextInt(4);
                         }
                         if(c1==2)
                         {
                             f=rand.nextInt(2);
                         }
                         if(f==0)
                         {
                             fu="+";
                         }
                         if(f==1)
                         {
                             fu="-";
                         }
                         if(f==2)
                         {
                             fu="*";
                         }
                         if(f==3)
                         {

                             if(c4==1)
                             {
                                 fu="/";
                                 
                             }
                             if(c4==2)
                             {
                                  if(a%d==0)
                                  {
                                      fu="/";
                                  }
                                  if(a%d!=0)
                                  {
                                      flag=1;
                                  }
                              }
                         }
                        if(p==0)
                        {
                            if(fu.equals("+")&&d==0)
                            {
                                flag=1;
                            }
                            else
                            {
                                Repeat1[s]=Repeat1[s]+fu+d;
                            }
                            
                        }
                        if(p==1)
                        {
                            Repeat1[s]=d+fu+Repeat1[s];
                        }
                        
               }
           }
          //判断重复
           String str="";//中间判等字符串
           StringBuffer stringBuffer = new StringBuffer (str); 
           str=str+stringBuffer.reverse(); 
           for(int w1=0;w1<w;w++)
           {
               if(w==0)//两位数
               {
                   if(Repeat1[s].equals(Repeat1[w1]))//简单判等
                   {
                        flag=1;
                   }     
                   if(Repeat1[s].equals(str))
                   {
                       flag=1;
                   }
               }
               else
               {
                   if(Repeat1[s].equals(Repeat1[w1]))//简单判等
                   {
                        flag=1;
                   }     
               }
           }
           //输出
           if(flag==1)
           {
               c5++;
           }
           else if(flag==0)
           {
              if((s+1)%c6==0)
              {
                  for(int k=0;k<10000000;k++)
                       {
                           String inputx=JOptionPane.showInputDialog(Repeat1[s]+"="+"请输入计算结果");
                           if(inputx!=null&&!inputx.equals(""))
                          {
                             y=Integer.parseInt(inputx);
                             break;
                          }     
                       }
                       if(y==g)
                         {
                            // System.out.println(Repeat1[s]+"="+y+"正确");    
                            str1=str1+Repeat1[s]+"="+y+"正确"+"<br>";
                             m++;
                         }
                         if(y!=g)
                         {
                             //System.out.println(Repeat1[s]+"="+d+"不正确");
                             str1=str1+Repeat1[s]+"="+y+"不正确"+"<br>";
                         }
              }
              else
              {
                  for(int k=0;k<10000000;k++)
                       {
                           String inputx=JOptionPane.showInputDialog(Repeat1[s]+"="+"请输入计算结果");
                           if(inputx!=null&&!inputx.equals(""))
                          {
                             y=Integer.parseInt(inputx);
                             break;
                          }     
                       }
                       if(y==g)
                         {
                             //System.out.print(Repeat1[s]+"="+y+"正确");    
                             str1=str1+Repeat1[s]+"="+y+"正确";
                             m++;
                         }
                         if(y!=g)
                         {
                             //System.out.print(Repeat1[s]+"="+y+"不正确");
                             str1=str1+Repeat1[s]+"="+y+"不正确";
                         }
              }
           }
       }
      // System.out.println("共"+c5+"道题"+"    "+m+"道题正确");
      str1=str1+"<br>"+"共"+c5+"道题"+"    "+m+"道题正确";
}
  if(c==2)
  {
       String []Repeat1=new String[2*c5];
       String []Repeat2=new String[2*c5];
       for(int s=0;s<c5;s++)
       {
           int sx=s;

          //数值范围
             a=rand.nextInt(c3+1)%(c3-c2+1)+c2;
             b=rand.nextInt(c3+1)%(c3-c2+1)+c2;
             d=rand.nextInt(c3+1)%(c3-c2+1)+c2;
             e=rand.nextInt(c3+1)%(c3-c2+1)+c2;
             String s1="",s2="";//两个真分数的字符串
             if(b==0)
             {
                 flag=1;
             }
             else if(b!=0)
             { 
                 if(a<b)
                 {
                     int r = b % a;
                        while(r != 0){
                          b = a;
                          a= r;
                          r = b % a;
                          h=a;
                        }
                        
                     //h=f(Math.abs(a),Math.abs(b));
                     s1=(a/h)+"/"+(b/h);
                    
                 }
                 if(a>b)
                 {
                     int r = a % b;
                        while(r != 0){
                          a = b;
                          b = r;
                          r = a % b;
                          h=b;
                        }
                     //h=f(Math.abs(b),Math.abs(a));
                     s1=(b/h)+"/"+(a/h);
                 }
                 if(a==b)
                 {
                     b++;
                     int r = b % a;
                        while(r != 0){
                          b = a;
                          a= r;
                          r = b % a;
                          h=a;
                        }
                    // h=f(Math.abs(a-1),Math.abs(b));
                     s1=(a/h)+"/"+(b/h);
                 }
             }
             if(e==0) 
             {
                 flag=1;
             }
             else
             {
                 if(d<e)
                 {
                     int r = e % d;
                        while(r != 0){
                          e = d;
                          d = r;
                          r = e % d;
                          h=d;
                        }
                    // h=f(Math.abs(d),Math.abs(e));
                     s2=(d/h)+"/"+(e/h);
                 }
                 if(d>e)
                 {
                     int r = d % e;
                        while(r != 0){
                          d = e;
                          e= r;
                          r = d % e;
                          h=e;
                        }
                    // h=f(Math.abs(e),Math.abs(d));
                     s2=(e/h)+"/"+(d/h);
                 }
                 if(d==e)
                 {
                     e++;
                     int r = e% d;
                        while(r != 0){
                          e = d;
                          d = r;
                          r = d % e;
                          h=d;
                        }
                    // h=f(Math.abs(d-1),Math.abs(e));
                     s2=(d/h)+"/"+(e/h);
                 }
             }
            
              //符号
             f=rand.nextInt(4);
             if(f==0)
             {
                 fu="+";
             }
             if(f==1)
             {
                 fu="-";
             }
             if(f==2)
             {
                 fu="*"; 
             }
             if(f==3)
             {
                 if(d!=0)
                 {
                     fu="/";
                 }
                 if(d==0)
                 {
                     flag=1;
                 }
             }
             Repeat1[s]=s1+fu+s2;
             //String str1=s2+fu+s1;
             //判断重复
             int cc=0;
             for(int ss=0;ss<sx;ss++)
             {
                 if(Repeat1[s].equals(Repeat1[ss]))
                 {
                     flag=1;
                 }
                 else
                 {
                     flag=0;
                 }
                 
             }
             //输出
             Repeat2[s]="("+s1+")"+fu+"("+s2+")";
             if(flag==1)
             {
                  c5++;
             }
             else if(flag==0)
             {
                 if((s+1)%c6==0)
                 {
                     //System.out.println(Repeat2[s]+"=  ");
                     str1=str1+Repeat2[s]+"=  "+"<br>";
                 }
    
             else
                 {
                    // System.out.print(Repeat2[s]+"=  ");
                     str1=str1+Repeat2[s]+"=  ";
                 }
             }
              
       }
  }
  
  %>
  <%=str1 %>
 <table>
 <tr>
 </tr>
 </table>
</body>
</html>



运行结果:

技术分享

技术分享

技术分享

技术分享

结果分析:无括号整数运算,以及分数运算都不能实现

软件工程结对作业02

标签:

原文地址:http://www.cnblogs.com/qianxia/p/5372734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!