软件工程个人作业03

 四则运算3

学生写的程序必须能判定用户的输入答案是否正确;

程序必须能处理四种运算的混合算式;

连续的减法和除法,应该遵守左结合的规定;

连续除法要打括号,否则会引起歧义。

小组成员:王艺霖、李杨

工作照片:

设计思想:

分别用不同的函数来表示不同的功能,最后在主函数中调用所需要的数据

1、用SIZE来确定数目打印题数

2、用SecondOperation来判定是否有乘除法

3、用Negative判定加减有无负数

4、用Remainder判定除法有无余数

5、再分别用四个函数表示加减乘除这四个运算

6、再用一个生成函数通过传递调用之前的函数所传递的数值来实现算式的随机产生

7、通过一个数组来储存算式再用for循环来排除重复的(若重复,则 i-1,退出此循环,重新再大循环一次)

8、通过一个for循环来计算答对和答错的题目数目

源程序代码:

import java.util.*;
public class Operation3 {
    public static int SIZE(int size)//定制数量
    {
        int q;
        q=size;
        return q;
    }
    
    public static int SecondOperation(String p1)//是否有乘除法
    {
        int q = 4;
        if(p1.equals("Y"))
        {
            q=4;
        } 
        if(p1.equals("N"))
        {
            q=2;
        }
        return q;
    }
    
    public static String Negative(String p2)//加减有无负数
    {
        String q;
        q=p2;
        return q;
    }
    
    public static String Remainder(String p3)//除法有无余数
    {
        String q;
        q=p3;
        return q;
    }
    
    public static int Max(int max)//最大数
    {
        int m;
        m=max;
        return m;
    }
    public static int Min(int min)//最小数
    {
        int m;
        m=min;
        return m;
    }
    public static String Compare(int q1,int w1,int q2,int w2,int c)//答案
    {
        String n=new String();
        int q3=1,w3=1;
        if(w1==-99999)
        {
            w1=1;
        }
        if(w2==-99999)
        {
            w2=1;
        }
        
        if(c==0)//加法运算
        {
            w3=w1*w2;
            q3=q1*w2+q2*w2;
        }
        if(c==1)//减法运算
        {
            w3=w1*w2;
            q3=q1*w2-q2*w1;
        }
        if(c==2)//乘法运算
        {
            w3=w1*w2;
            q3=q1*q2;
        }
        if(c==3)//除法运算
        {
            int nd=q2;
            q2=w2;
            w2=nd;        
            w3=w1*w2;
            q3=q1*q2;
        }   
        //化简
        int z1=Math.abs(q3),z2=Math.abs(w3);
        int chushu=2;
        if(z1>z2)
        {
            while(z2>=chushu)
            {
                if(z1%chushu==0&&z2%chushu==0)
                {z1=z1/chushu;z2=z2/chushu;}
                else
                {chushu++;}
            }
            
        }
        else if(z1<z2)
        {
            while(z1>=chushu)
            {
                if(z1%chushu==0&&z2%chushu==0)
                {z1=z1/chushu;z2=z2/chushu;}
                else
                {chushu++;}
            }
        }
        else
        {
            z1=1;z2=1;
        };
        if(q3<0)
        {
            q3=0-z1;
        }
        else 
        {
            q3=z1;
        }
        if(w3<0)
        {
            w3=0-z2;
        }
        else 
        {
            w3=z2;
        }
        //化简      
        if(w3==1)
        {
            n=q3+"";
        }
        else if(w3==-1)
        {
            q3=0-q3;
            n=q3+"";
        }
        else
        {
            if((q3<0&&w3<0)||(q3>0&&w3<0))
            {
                q3=0-q3;
                w3=0-w3;
            }
            n=q3+"/"+w3+"";
        }
        return n;
    }
    public static void Display(int SIZE,int SecondOperation,String Negative,String Remainder,int Max,int Min)//算式计算
    {
        String Again[][]=new String[SIZE][1];//用数组来存储表达式
        int RightNum=0;//正确的数目
        int WrongNum=0;//错误的数目
        for(int i=0;i<SIZE;i++)//重复次数,用以确定算式多少
        {        
            int cha=Max-Min;
            int c,q1=1,w1=1,q2=1,w2=1;
            String s1=new String();
            String s2=new String();
            String equation=new String();       
            String symbol=new String();//符号判定
            c=(int)(Math.random()*SecondOperation);
            if(c==0) symbol="+";
            if(c==1) symbol="-";
            if(c==2) symbol="*";
            if(c==3) symbol="/";     
            for(int j=0;j<2;j++)//两次循环,第一次为第一个数字,第二次为第二个数字
            {
                int n1 =-99999,n2=-99999;//用于后面是否为分数的判定
                int s=(int)(Math.random()*2);//随机数判定整数或分数,0整数,1分数
                if(s==0)//整数
                {
                    if(Negative.equals("N"))
                    {
                        if(c==0||c==1)
                        {
                            while(n1<0)
                            {
                                n1=(int)(Min+Math.random()*(cha+1));//随机产生从min到max之间的数
                            }
                        }
                        else
                        {
                            n1=(int)(Min+Math.random()*(cha+1));
                        }
                    }
                    if(Negative.equals("Y"))
                    {
                        n1=(int)(Min+Math.random()*(cha+1));
                    }
                }
                if(s==1)//分数
                {
                    n1=(int)(Min+Math.random()*(cha+1));//随机产生从min到max之间的数
                    n2=(int)(Min+Math.random()*(cha+1));
                        if(Negative.equals("N"))
                        {
                            if(c==0||c==1)
                            {
                                while(n1<=0||n2<=0)
                                {
                                    n1=(int)(Min+Math.random()*(cha+1));
                                    n2=(int)(Min+Math.random()*(cha+1));
                                }
                            }
                            else
                            {
                                while(n1==0||n2==0)//分母不能为零
                                {
                                    n1=(int)(Min+Math.random()*(cha+1));
                                    n2=(int)(Min+Math.random()*(cha+1));
                                }
                            }
                        }
                        if(Negative.equals("Y"))
                        {
                            while(n1==0||n2==0)//分母不能为零
                            {
                                n1=(int)(Min+Math.random()*(cha+1));
                                n2=(int)(Min+Math.random()*(cha+1));
                            }
                        }
                    int z1=Math.abs(n1),z2=Math.abs(n2);//取n1和n2的绝对值,以便化简输出
                    int chushu=2;
                    if(z1>z2)//化简
                    {
                        while(z2>=chushu)
                        {
                            if(z1%chushu==0&&z2%chushu==0)
                            {
                                z1=z1/chushu;z2=z2/chushu;
                            }
                            else
                            {
                                chushu++;
                            }
                        }                    
                    }
                    else if(z1<z2)
                    {
                        while(z1>=chushu)
                        {
                            if(z1%chushu==0&&z2%chushu==0)
                            {
                                z1=z1/chushu;z2=z2/chushu;
                            }
                            else
                            {
                                chushu++;
                            }
                        }
                    }
                    else
                    {
                        z1=1;z2=1;
                    };
                    if(n1<0)//去掉绝对值,返回原来的数          
                    {
                        n1=0-z1;
                    }
                    else 
                    {
                        n1=z1;
                    }
                    if(n2<0)
                    {
                        n2=0-z2;
                    }
                    else 
                    {
                        n2=z2;
                    }
                }
                if(j==0)//第一个数字
                {
                    q1=n1;w1=n2;
                    if(w1==-1&&q1<0)
                    {
                        q1=Math.abs(q1);
                    }
                    if(w1==-1&&q1>0)
                    {
                        q1=0-q1;
                    }
                    if(w1>-99999)//如果存在分母,则为分数
                    {
                        if(Math.abs(w1)!=1)
                        {
                            if(q1<0&&w1<0)
                            {
                                q1=Math.abs(q1);w1=Math.abs(w1);
                            }
                            if(w1<0)
                            {
                                s1="("+q1+"/("+w1+"))"+"";
                            }
                            else
                            {
                                s1="("+q1+"/"+w1+")"+"";
                            }
                        }
                        if(Math.abs(w1)==1)//为整数
                        {
                            if(q1>=0)
                            {
                                s1=q1+"";
                            }
                            if(q1<0)
                            {
                                s1="("+q1+")"+"";
                            }
                        }
                    }
                    
                    else//否则为整数
                    {
                        if(q1>=0)
                        {
                            s1=q1+"";
                        }
                        if(q1<0)
                        {
                            s1="("+q1+")"+"";
                        }
                    }
                }
                if(j==1)//第二个数字
                {
                    q2=n1;w2=n2;
                    if(c==3)//当为除法时,除数不能为0,
                    {
                        while(q2==0)
                        {
                            q2=(int)(Min+Math.random()*(cha+1));
                        }//分子或整数不能为0
                    }
                    if(w2==-1&&q2<0)
                    {
                        q2=Math.abs(q2);
                    }
                    if(w2==-1&&q2>0)
                    {
                        q2=0-q2;
                    }
                    if(w2>-99999)//如果存在分母,则为分数
                    {
                        if(Math.abs(w2)!=1)
                        {
                            if(q2<0&&w2<0)
                            {
                                q2=Math.abs(q2);w2=Math.abs(w2);
                            }
                            if(w2<0)
                            {
                                s2="("+q2+"/("+w2+"))"+"";
                            }
                            else
                            {
                                s2="("+q2+"/"+w2+")"+"";
                            }
                        }
                        else
                        {
                            if(q2<0)
                            {
                                s2="("+q2+")"+"";
                            }
                            if(q2>0)
                            {
                                s2=q2+"";
                            }
                        }
                    }
                    else//否则为整数
                    {
                        if(q2<0)
                        {
                            s2="("+q2+")"+"";
                        }
                        if(q2>0)
                        {
                            s2=q2+"";
                        }
                    }
                    if(c==3&&Remainder.equals("N"))//除法无余数
                    {
                        q1=q2*n1;w1=w2;
                        if(w1%n1==0)
                        {
                            w1=w1/n1;q1=q1/n1;
                        }
                        if(w1>-99999)//如果存在分母,则为分数
                        {
                            if(Math.abs(w1)!=1)
                            {s1="("+q1+"/"+w1+")"+"";}
                            else//否则为整数
                            {
                                s1=q1+"";
                            }
                        }
                        else//否则为整数
                        {
                            s1=q1+"";
                        }
                        
                    }
                }
            }
            equation=equation+s1+symbol+s2+"=";//表达式
            Again[i][0]=equation;
            for(int k=0;k<(i+1);k++)//避免重复
            {
                if(Again[i][0].equals(Again[k][0]))
                {
                    k--;break;
                }
            }
            System.out.println(equation);
            String Daan=Compare(q1,w1,q2,w2,c);           
            @SuppressWarnings("resource")
            Scanner sc=new Scanner(System.in);
            String Answer=sc.next();
            if(Answer.equals(Daan))
            {
                System.out.println("恭喜你!回答正确");//回答正确
                RightNum++;
            }
            else
            {
                System.out.println("回答错误,其正确答案为:"+Daan);//回答错误
                WrongNum++;
            }
        }
        System.out.println("你一共回答了"+SIZE+"道题,其中正确:"+RightNum+"道,错误:"+WrongNum+"道");
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String p3="Y";
        @SuppressWarnings("resource")
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入定制数量:");
        int size=sc.nextInt();
        System.out.println("是否有乘除法,有(Y),没有(N)");
        String p1=sc.next();
        System.out.println("加减有无负数,有(Y),没有(N)");
        String p2=sc.next();
        if(p1.equals("Y"))
        {
            System.out.println("除法有无余数,有(Y),没有(N)");
            p3=sc.next();
        }
        System.out.println("请输入数值范围最大值:");
        int max=sc.nextInt();
        System.out.println("请输入数值范围最小值:");
        int min=sc.nextInt();        
        int a=SIZE(size);
        int b=SecondOperation(p1) ;
        String c=Negative(p2);
        String d=Remainder(p3);
        int e=Max(max);
        int f=Min(min);
        
        Display(a,b,c,d,e,f);//函数的调用,实现
    }

}

运行结果截图:

编程总结分析:

这次编程过程中产生很多错误,每次认为好了,结果一运行输入答案就出错,特别是有负号的运算和分数带负号的除法。总之,需要考虑的东西很多很多,遇到有错误就重新修改。

  周活动总结表

  姓名:王艺霖     日期 2016/3/26

  听课 编写代码 阅读课本 准备考试     日总计
周日              
周一 100 55 30       185
周二   22 30       52
周三   34 30       64
周四   45         45
周五   62         62
周六   220         120
周总计  100 438  90        628

                                                                    

  时间记录日志

  学生:王艺霖  日期:2016/3/26

  教师:王建民  课程:软件工程

 日期  开始时间 结束时间   中断时间  净时间 活动   备注  C U
3/21  8:00  10:00 20  100 上课 下课    
   19:00  20:30 10  80 编程 休息    
3/22  15:00  17:00 20  100 编程 /看书/讨论 休息    
3/23  15:00  17:30 20  130 编程 //讨论 休息    
3/24 19:00 20:30 10  80 编程/看书/讨论 休息    
3/25  9:00  10:30 10  80 编程 休息    
  14:00 16:30 30 120 编程 休息    

  缺陷记录日志

  学生:王艺霖

  日期:2016/3/26

  教员:王建民

  程序号:四则运算3

 日期  编号  类型  引入阶段  排除阶段  修复时间 修复阶缺陷
 3/23  1    编码   编译   20  
 除法的结果负号的判定

 

 

 

原文地址:https://www.cnblogs.com/mengqimoli/p/5322480.html