软件工程概论作业-03

1.题目要求:

    

2.设计思想:

先将表达式求得的值放入数组,然后将用户输入的值与数组中的值进行比较,确定结果是否正确。在有多个运算符和括号的时候,可用用栈确定运算的优先级。用递归的思想计算表达式的值1.在进行不加括号的运算时,可以通过产生的表达式直接求得正确值,然后跟用户输入的值进行比较;2.有括号的运算时,我们可以通过循环求得每次循环所得值,并放入数组中;再通过判断循环后的运算符,来决定数组值之间的加减乘除,以求得最后正确值,然后跟用户输入的值进行比较。

3.程序代码:

package demo;
import java.util.Scanner;
import java.util.ArrayList;
public class Test {
    public static void main(String[] args){
        int N,i,N1,N2;
        int sum=0;
        String str;
        String hh;    
        Scanner in=new Scanner(System.in);    
        change fra=new change();
        System.out.println("请输入运算题的数目:");
        N=in.nextInt();
        System.out.println("请选择是否需要分数运算(Y/N):");
        hh=in.next();
        if(hh.equals("Y"))//分数运算
        {
            for(i=0;i<N;i++)
            {            
                int a,b,c,d;
                int s1,s2;
                a=(int) (Math.random()*100);
                b=(int) (Math.random()*99+1);
                //分母不等于0
                c=(int) (Math.random()*100);
                d=(int) (Math.random()*99+1);
                int h=(int)(Math.random()*4);
                int aa=0,bb=0,cc=0,dd=0;
                //为了产生分数,分子也不可为零
                if(a==0)
                    a=(int) (Math.random()*99+1);
                if(c==0)
                    c=(int) (Math.random()*99+1);
                //化简分数
                int j;
                j=fra.change_(a,b);
                aa=a/j;
                bb=b/j;
                //化简分数
                int p;
                p=fra.change_(c,d);
                cc=c/p;
                dd=d/p;
                int j1;
                int s11,s22;
                String str1;
                if(h==0)
                {   
                    //化简后,分母等于1时,直接输出分子
                    if(bb!=1&&dd!=1)
                        System.out.println(aa+"/"+bb+" + "+cc+"/"+dd+"=");
                        
                    else if(bb==1&&dd!=1)
                        System.out.println(aa+" + "+cc+"/"+dd+"=");
                    
                    else if(bb!=1&&dd==1)
                        System.out.println(aa+"/"+bb+" + "+cc+"=");
                        
                    else
                        System.out.println(aa+"+"+cc+"=");
                    //分数加法计算
                      s1=aa*dd+bb*cc;
                      s2=bb*dd;
                      j1=fra.change_(s1,s2);
                      s11=s1/j1;
                      s22=s2/j1;
                      str1=s11+"/"+s22;
                      str=in.next();
                      if(fra.judge_FRA(str1,str)==1)
                      {
                         sum++;
                      }
                                 
                }
                else if(h==1)
                {
                    //不能产生负数
                    int t1,t2;
                    if((a/b-c/d)<0)
                    {
                        t1=aa;
                        aa=cc;
                        cc=t1;
                        t2=bb;
                        bb=dd;
                        dd=t2;
                    }
                    
                    //化简后,分母等于1时,直接输出分子    
                    if(bb!=1&&dd!=1)
                        System.out.println(aa+"/"+bb+" - "+cc+"/"+dd+"=");                        
                    else if(bb==1&&dd!=1)
                        System.out.println(aa+" - "+cc+"/"+dd+"=");
                    else if(bb!=1&&dd==1)
                        System.out.println(aa+"/"+bb+" - "+cc+"=");
                    else
                        System.out.println(aa+"-"+cc+"=");
                    //分数减法计算
                    s1=aa*dd-bb*cc;
                    s2=bb*dd;
                    j1=fra.change_(s1,s2);
                    s11=s1/j1;
                    s22=s2/j1;
                    str1=s11+"/"+s22;
                    str=in.next();
                    if(fra.judge_FRA(str1,str)==1)
                    {
                        sum++;
                    }
                    
                }
                else if(h==2)
                {
                    
                    //化简后,分母等于1时,直接输出分子
                    if(bb!=1&&dd!=1)
                        System.out.println(aa+"/"+bb+" * "+cc+"/"+dd+"=");

                    else if(bb==1&&dd!=1)
                        System.out.println(aa+" * "+cc+"/"+dd+"=");
             
                    else if(bb!=1&&dd==1)
                        System.out.println(aa+"/"+bb+" * "+cc+"=");
                        
                    else
                        System.out.println(aa+"*"+cc+"=");
                    //分数乘法计算
                    s1=aa*cc;
                    s2=bb*dd;
                    j1=fra.change_(s1,s2);
                    s11=s1/j1;
                    s22=s2/j1;
                    str1=s11+"/"+s22;
                    str=in.next();
                    if(fra.judge_FRA(str1,str)==1)
                    {
                        sum++;
                    }
                    
                }
                else
                { 
                    
                    //化简后,分母等于1时,直接输出分子
                    if(bb!=1&&dd!=1)
                        System.out.println("("+aa+"/"+bb+")"+" / "+"("+cc+"/"+dd+")"+"=");
                     
                    else if(bb==1&&dd!=1)
                        System.out.println(aa+" / "+"("+cc+"/"+dd+")"+"=");
                        
                    else if(bb!=1&&dd==1)
                        System.out.println("("+aa+"/"+bb+")"+" / "+cc+"=");
                        
                    else
                        System.out.println(aa+"/"+cc+"=");
                    //分数除法计算
                    s1=aa*dd;
                    s2=bb*cc;
                    j1=fra.change_(s1,s2);
                    s11=s1/j1;
                    s22=s2/j1;
                    str1=s11+"/"+s22;
                    str=in.next();
                    if(fra.judge_FRA(str1,str)==1)
                    {
                        sum++;
                    }
                    
                }
            }
        }
        else if(hh.equals("N"))//整数运算
        {
            System.out.println("请选择是否需要产生括号的运算题(Y/N):");
            String str1,str2,str3;
            double strr2;
            str=in.next();
            double strr1=0;
            double ss;
            if(str.equals("Y"))
            {
               
            }
            
            else if(str.equals("N"))//不需要产生括号
            {
                System.out.println("请输入数值范围:");
                N2=in.nextInt();
                System.out.println("请选择是否有无乘除法的运算题(Y/N):");
                str1=in.next();
                if(str1.equals("Y"))//有乘除法
                {                
                    System.out.println("请选择是否有无余数的运算题(Y/N):");
                    str2=in.next();    
                    
                    if(str2.equals("Y"))//需要有余数
                    {
                        System.out.println("请选择是否有负数(Y/N):");
                        str3=in.next();
                        for(i=0;i<N;i++)
                        {
                            int a,b,c,c1,h,h2;
                            a=(int) (Math.random()*N2);
                            b=(int) (Math.random()*N2);
                            h=(int) (Math.random()*4);
                            //控制加减运算符
                            c=(int) (Math.random()*(N2-1)+1);
                            c1=(int) (Math.random()*10+1);
                            //控制倍数
                            
                            if(str3.equals("Y"))//有负数
                            {
                                if(h==0)//加法
                                {
                                    h2=(int) (Math.random()*2);
                                    //控制有无负数
                                    if(h2==0)
                                    {
                                        System.out.println(a+"+"+b+"=");
                                        double s;
                                        s=a+b;
                                        strr2=in.nextDouble();
                                        if(fra.judge(s,strr2)==1)
                                        {
                                            sum++;
                                        }
                                        
                                    }
                                    else if(h2==1)
                                    {
                                        System.out.println("-"+a+"+"+b+"=");
                                        double s;
                                        s=-a+b;
                                        strr2=in.nextDouble();
                                        if(fra.judge(s,strr2)==1)
                                        {
                                            sum++;
                                        }
                                        
                                    }
                                }
                                if(h==1)//减法
                                {
                                    h2=(int) (Math.random()*2);
                                    //控制有无负数
                                    if(h2==0)
                                    {
                                        System.out.println(a+"-"+b+"=");
                                        double s;
                                        s=a-b;
                                        strr2=in.nextDouble();
                                        if(fra.judge(s,strr2)==1)
                                        {
                                            sum++;
                                        }
                                    }
                                    else if(h2==1)
                                    {
                                        System.out.println("-"+a+"-"+b+"=");
                                        double s;
                                        s=-a-b;
                                        strr2=in.nextDouble();
                                        if(fra.judge(s,strr2)==1)
                                        {
                                            sum++;
                                        }
                                    }
                                }
                            }
                            else if(str3.equals("N"))//无负数
                            {
                                if(h==0)
                                {
                                    System.out.println(a+"+"+b+"=");
                                    double s;
                                    s=a+b;
                                    strr2=in.nextDouble();
                                    if(fra.judge(s,strr2)==1)
                                    {
                                        sum++;
                                    }
                                    
                                }
                                if(h==1)
                                {
                                    System.out.println(a+"-"+b+"=");
                                    double s;
                                    s=a-b;
                                    strr2=in.nextDouble();
                                    if(fra.judge(s,strr2)==1)
                                    {
                                        sum++;
                                    }
                                    
                                }
                            }
                            if(h==2)
                            {
                                System.out.println(a+"*"+b+"=");
                                double s;
                                s=a*b;
                                strr2=in.nextDouble();
                                if(fra.judge(s,strr2)==1)
                                {
                                    sum++;
                                }
                                
                            }
                            if(h==3)
                            {
                                int d,k;                    
                                k=(int) (Math.random()*(c-1)+1);
                                //必须产生余数
                                d=c*c1+k;
                                System.out.println(d+"/"+c+"=");
                                double s;
                                s=d/c;
                                strr2=in.nextDouble();
                                if(fra.judge(s,strr2)==1)
                                {
                                    sum++;
                                }
                                
                            }
                        }
                    }
                    else if(str2.equals("N"))//不需要产生余数
                    {
                        System.out.println("请选择是否有负数(Y/N):");
                        str3=in.next();
                        for(i=0;i<N;i++)
                        {
                            int a,b,c,c1,h,h2;
                            a=(int) (Math.random()*N2);
                            b=(int) (Math.random()*N2);
                            h=(int) (Math.random()*4);
                            //控制加减运算符
                            c=(int) (Math.random()*(N2-1)+1);
                            c1=(int) (Math.random()*10+1);
                            //控制倍数
                            
                            if(str3.equals("Y"))//有负数
                            {
                                if(h==0)//加法
                                {
                                    h2=(int) (Math.random()*2);
                                    //控制有无负数
                                    if(h2==0)
                                    {
                                        System.out.println(a+"+"+b+"=");
                                        double s;
                                        s=a+b;
                                        strr2=in.nextDouble();
                                        if(fra.judge(s,strr2)==1)
                                        {
                                            sum++;
                                        }
                                        
                                    }
                                    else if(h2==1)
                                    {
                                        System.out.println("-"+a+"+"+b+"=");
                                        double s;
                                        s=-a+b;
                                        strr2=in.nextDouble();
                                        if(fra.judge(s,strr2)==1)
                                        {
                                            sum++;
                                        }
                                        
                                    }
                                }
                                if(h==1)//减法
                                {
                                    h2=(int) (Math.random()*2);
                                    //控制有无负数
                                    if(h2==0)
                                    {
                                        System.out.println(a+"-"+b+"=");
                                        double s;
                                        s=a-b;
                                        strr2=in.nextDouble();
                                        if(fra.judge(s,strr2)==1)
                                        {
                                            sum++;
                                        }
                                    }
                                    else if(h2==1)
                                    {
                                        System.out.println("-"+a+"-"+b+"=");
                                        double s;
                                        s=-a-b;
                                        strr2=in.nextDouble();
                                        if(fra.judge(s,strr2)==1)
                                        {
                                            sum++;
                                        }
                                    }
                                }
                            }
                            else if(str3.equals("N"))//无负数
                            {
                                if(h==0)
                                {
                                    System.out.println(a+"+"+b+"=");
                                    double s;
                                    s=a+b;
                                    strr2=in.nextDouble();
                                    if(fra.judge(s,strr2)==1)
                                    {
                                        sum++;
                                    }
                                }
                                if(h==1)
                                {
                                    System.out.println(a+"-"+b+"=");
                                    double s;
                                    s=a-b;
                                    strr2=in.nextDouble();
                                    if(fra.judge(s,strr2)==1)
                                    {
                                        sum++;
                                    }
                                }
                            }
                            if(h==2)//乘法
                            {
                                System.out.println(a+"*"+b+"=");
                                double s;
                                s=a*b;
                                strr2=in.nextDouble();
                                if(fra.judge(s,strr2)==1)
                                {
                                    sum++;
                                }
                            }
                            if(h==3)//除法,不产生余数
                            {
                                int d;            
                                //不能产生余数
                                d=c*c1;
                                System.out.println(d+"/"+c+"=");
                                double s;
                                s=d/c;
                                strr2=in.nextDouble();
                                if(fra.judge(s,strr2)==1)
                                {
                                    sum++;
                                }
                                
                            }
                        }
                    }
                }
                        
                else if(str1.equals("N"))//没有乘除法
                {
                    int a,b,h,h2;                        
                    System.out.println("请选择是否有负数(Y/N):");
                    str3=in.next();
                    for(i=0;i<N;i++)
                    {
                        a=(int) (Math.random()*N2);
                        b=(int) (Math.random()*N2);
                        h=(int) (Math.random()*2);
                        //控制运算符
                        
                        if(str3.equals("Y"))//有负数
                        {
                            if(h==0)//加法
                            {
                                h2=(int) (Math.random()*2);
                                //控制有无负数
                                if(h2==0)
                                {
                                    System.out.println(a+"+"+b+"=");
                                    double s;
                                    s=a+b;
                                    strr2=in.nextDouble();
                                    if(fra.judge(s,strr2)==1)
                                    {
                                        sum++;
                                    }
                                }
                                else if(h2==1)
                                {
                                    System.out.println("-"+a+"+"+b+"=");
                                    double s;
                                    s=-a+b;
                                    strr2=in.nextDouble();
                                    if(fra.judge(s,strr2)==1)
                                    {
                                        sum++;
                                    }
                                }
                            }
                            if(h==1)//减法
                            {
                                h2=(int) (Math.random()*2);
                                //控制有无负数
                                if(h2==0)
                                {
                                    System.out.println(a+"-"+b+"=");
                                    double s;
                                    s=a-b;
                                    strr2=in.nextDouble();
                                    if(fra.judge(s,strr2)==1)
                                    {
                                        sum++;
                                    }
                                }
                                else if(h2==1)
                                {
                                    System.out.println("-"+a+"-"+b+"=");
                                    double s;
                                    s=-a-b;
                                    strr2=in.nextDouble();
                                    if(fra.judge(s,strr2)==1)
                                    {
                                        sum++;
                                    }
                                }
                            }
                        }
                        else if(str3.equals("N"))//无负数
                        {
                            if(h==0)
                            {
                                System.out.println(a+"+"+b+"=");
                                double s;
                                s=a+b;
                                strr2=in.nextDouble();
                                if(fra.judge(s,strr2)==1)
                                {
                                    sum++;
                                }
                            }
                            if(h==1)
                            {
                                System.out.println(a+"-"+b+"=");
                                double s;
                                s=a-b;
                                strr2=in.nextDouble();
                                if(fra.judge(s,strr2)==1)
                                {
                                    sum++;
                                }
                            }
                        }
                    }
                }
            }
        }
        in.close();
    }
}
class change{
    Scanner in=new Scanner(System.in);
    int change_(int a,int b)//化简分数
    {
      int r1;
      if(a>b)
      {
          r1=b;
      }
      else
          r1=a;
      int k=0;
      for(int j=1;j<=r1;j++)
        {
          if(a%j==0&&b%j==0)
          {
              k=j;
           }          
        }
        return k;
    }
  int  judge_FRA(String str1,String str)//分数运算判断对错,计算正确次数
  {
      int ju;
      if(str.equals(str1))
      {
          System.out.println("正确");
          ju=1;
      }
      else
      {
          System.out.println("错误");
          ju=0;
      }
      return ju;
  }
  int judge(double s,double ss)//整数判断对错,计算正确次数
  {
      int ju;
      if(s==ss)
      {
          System.out.println("正确");
          ju=1;
      }
      else
      {
          System.out.println("错误");
          ju=0;
      }
      return ju;
  }
}

 4.运行结果截图:

 

psp表格:

PSP2.1

Personal Software Process Stages

Time

Planning

计划

 

  · Estimate

  · 估计这个任务需要多少时间

 8小时

Development

开发

 

  · Analysis

  · 需求分析 (包括学习新技术)

栈的应用

用过栈来确定运算的优先级

  · Design Spec

  · 生成设计文档

 

  · Design Review

  · 设计复审 (和同事审核设计文档)

 

  · Coding Standard

  · 代码规范 (为目前的开发制定合适的规范)

 

  · Design

  · 具体设计

 

  · Coding

  · 具体编码

 

  · Code Review

  · 代码复审

 

  · Test

  · 测试(自我测试,修改代码,提交修改)

 

Reporting

报告

 

  · Test Report

  · 测试报告

 

  · Size Measurement

  · 计算工作量

 

  · Postmortem & Process Improvement Plan

  · 事后总结, 并提出过程改进计划

 对于栈还不是很熟悉

 

合计

 

项目计划总结:

        任务

日期

上课

编写程序

阅读书籍

3.13

2小时课程

 

 

3.14

 

思考如何输入结果和连接数据库,以及如何连接数据库

 

3.15

 

思考如何输入结果和连接数据库,以及如何连接数据库

 

3.16

 

 

阅读《梦断代码》

3.17

 

 

 

时间记录日志:

日期

开始时间

结束时间

中断时间

活动

备注

3.13

14:00

15:50

10min

上课

 

3.14

 

 

 

 

 

3.15

19:00

20:30

 

编程

 

3.16

 

 

 

阅读

 

3.17

19:00

 

 

编程

 

3.18

 

 

 

写博客

 

缺陷记录日志:

日期

编号

类型

引入阶段

排除阶段

修复时间

修复缺陷

描述

3.15

1

思维逻辑

设计

设计

5min

 

对于结果对错,先将表达式结果算出来存入数组,然后与输入的结果比较

 

 

 

 

 

 

 

 
posted @ 2017-03-17 16:03  康杨  阅读(164)  评论(0编辑  收藏  举报