zoukankan      html  css  js  c++  java
  • 软件工程个人作业03

    四则运算三

    设计思想:

    ①:在四则运算二的基础上,加入输入和计算

    ②:首先需要在每一个输出项的后面加入输入项并能提示用户输入数据,再通过系统自身计算的数值,来核对用户的输入是否正确,利用if条件判定输出,并在正确后利用计数来表示最后总计的正确数。

    ③:对于整数计算的话,整数加减的计算直接交给电脑就行了,也没有什么注意的项,主要是用数值传递随机数的值参与运算。乘除需要注意除法除数可能为0的所有情况,因为只要出就会出错。

    ④:对于分数计算,加减可通过通分使分母一致再进行计算,最后的约分需要求出公约数进行约分。乘除法更简单,乘法乘后约分就行了,然后除法转化为乘法来计算。

    ⑤:对于带括号的混合运算,则需要利用压栈的方法来实现,建立一个参与计算的数据栈,和一个运算符栈,将生成的算式作为字符串类型传入计算的方法中,通过设置优先级来实现计算的先后,如判断加减的优先级会返回false乘除则返回true,这样就可以设定计算的规则,从栈中每次调出两个数据进行计算,将结果压栈,循环执行,最后就能得到最终的答案返回就行了。

    源代码:

    package 加减乘除;
    
    import java.util.*;
    
    public class Arithmetic {
        static Operate oper=new Operate();
        static int count = 0;
        public static void main(String[] args) {
             Scanner input=new Scanner(System.in);
                System.out.println("请输入要输出多少道运算题");
                int h=input.nextInt();
                System.out.println("请输入你要计算数的最大值");
                int g=input.nextInt();
                System.out.println("请选择要使用的功能:");
                System.out.println("1.   整数加减法运算");
                System.out.println("2.   整数四则运算");
                System.out.println("3.   分数的加减运算");
                System.out.println("4.   分数的四则运算");
                System.out.println("5.   多数的四则运算(带括号)");
                Random random = new Random();
                char arr[] = {'+','-','*','/','('};
                int a=input.nextInt();
                switch(a)
                { 
                case 1:
                {
                     System.out.println("请选择减法是否有负数;0:无负数,1:有负数");
                     int f=input.nextInt();
                     int i;
                     switch(f)
                     {
                     case 0:
                     {
                         for(i=0;i<h;i++)
                         {
                             String str1=random.nextInt(g)+""+arr[random.nextInt(2)]+""+random.nextInt(g);
                              
                             int t1 = oper.caculate(str1+'#');
                             System.out.println(str1+'=');
                             System.out.println("请输入答案:");
                             int c = input.nextInt();
                             if(c!=t1)
                             {
                                 System.out.println("答案错误!");
                                 System.out.println("正确答案是:"+t1);
                             }
                             else
                             {
                                 System.out.println("答案正确!");
                                 count++;
                             }
                             if(i==h-1)
                             {
                                 System.out.println("共答对"+count+"道!");
                             }
                             
                         }
                         break;
                     }
                     case 1:
                     {
                         for(i=0;i<h;i++)
                         {
                             int c1=random.nextInt(g);
                             int c2=random.nextInt(g);
                             char w1=arr[random.nextInt(2)];
                             if(c1>c2){
                                 c1=random.nextInt(g);
                                 c2=random.nextInt(g);
                             }
                             String str2 = c1+""+w1+""+c2;
                             System.out.println(str2+"=");
                             int t2 = oper.caculate(str2+'#');
                             System.out.println("请输入答案:");
                             int c = input.nextInt();
                             if(c!=t2)
                             {
                                 System.out.println("答案错误!");
                                 System.out.println("正确答案是:"+t2);
                             }
                             else
                             {
                                 System.out.println("答案正确!");
                                 count++;
                             }
                             if(i==h-1)
                             {
                                 System.out.println("共答对"+count+"道!");
                             }
                         }
                         break;
                     }
                     default:
                         System.out.println("输入的数有误,请输入0或1");
                     }
                     break;
                }
                case 2:
                {
                    System.out.println("请选择要使用的功能:");
                    System.out.println("1.   整数无余数四则运算!");
                    System.out.println("3.   整数可有余数四则运算");
                    int f=input.nextInt();
                     switch(f)
                     {
                     case 1:
                     {
                         for(int i=0;i<h;i++)
                         {
                             String str3;
                             int u=random.nextInt(g);
                             int v=random.nextInt(g)+1;
                             char w=arr[random.nextInt(4)];
                             if(u%v==0 && w=='/')
                             {
                                 str3 =u+""+w+""+v;
                             }
                            else
                                str3 =u-(u%v)+""+w+""+v;
                             int t1 = oper.caculate(str3+'#');
                             System.out.println(str3+'=');
                             System.out.println("请输入答案:");
                             int c = input.nextInt();
                             if(c!=t1)
                             {
                                 System.out.println("答案错误!");
                                 System.out.println("正确答案是:"+t1);
                             }
                             else
                             {
                                 System.out.println("答案正确!");
                                 count++;
                             }
                             if(i==h-1)
                             {
                                 System.out.println("共答对"+count+"道!");
                             }
                         }
                         break;
                     }
                     case 2:
                     {
                         for(int i=0;i<h;i++)
                         {
                             int u=random.nextInt(g);
                             int v=random.nextInt(g)+1;
                             char w=arr[random.nextInt(4)];
                             String str3 =u+" "+w+" "+v;
                             int t1 = oper.caculate(str3+'#');
                             System.out.println(str3+'=');
                             System.out.println("请输入答案:");
                             int c = input.nextInt();
                             if(c!=t1)
                             {
                                 System.out.println("答案错误!");
                                 System.out.println("正确答案是:"+t1);
                             }
                             else
                             {
                                 System.out.println("答案正确!");
                                 count++;
                             }
                             if(i==h-1)
                             {
                                 System.out.println("共答对"+count+"道!");
                             }
                         }
                         break;
                     }
                     default:
                         System.out.print("输入的数有误,请输入1--2的整数");
                     }
                     break;
                }
                case 3:
                {
                    System.out.println("请选择减法是否有负数;0:无负数,1:有负数");
                     int f=input.nextInt();
                     switch(f)
                     {
                     case 0:
                     {
                         for(int i=0;i<h;i++)
                         {
                             int c1 =random.nextInt(g);
                             int c2 =random.nextInt(g)+1;
                             int c3 =random.nextInt(g);
                             int c4 =random.nextInt(g)+1;
                             char w =arr[random.nextInt(2)];
                             System.out.println(c1+"/"+c2+" "+w+" "+c3+"/"+c4+"=");
                             System.out.println("请输入答案:");
                             int t2,t3;
                             String t1=new String();
                             if(w=='+')
                             {
                                  t2 = c2*c4;
                                   t3 = c1*c4+c3*c2;
                                   t1 = t3+"/"+t2;
                             }
                             else
                             {
                                 t2 = c2*c4;
                                  t3 = c1*c4-c3*c2;
                                  t1 = t3+"/"+t2;
                             }
                             String c = input.next();
                            if(!c.equals(t1))
                             {
                                 System.out.println("答案错误!");
                                 System.out.println("正确答案是:"+t1);
                             }
                             else
                             {
                                 System.out.println("答案正确!");
                                 count++;
                             }
                             if(i==h-1)
                             {
                                 System.out.println("共答对"+count+"道!");
                             }
                         }
                         break;
                     }
                     case 1:
                     {
                         for(int i=0;i<h;i++)
                         {
                             int c1 =random.nextInt()%g;
                             int c2 =random.nextInt(g)+1;
                             int c3 =random.nextInt()%g;
                             int c4 =random.nextInt(g)+1;
                             char w =arr[random.nextInt(2)];
                             System.out.println(c1+"/"+c2+" "+w+" "+c3+"/"+c4+"=");
                             System.out.println("请输入答案:");
                             int t2,t3;
                             String t1=new String();
                             if(w=='+')
                             {
                                  t2 = c2*c4;
                                   t3 = c1*c4+c3*c2;
                                   t1 = t3+"/"+t2;
                             }
                             else
                             {
                                 t2 = c2*c4;
                                  t3 = c1*c4-c3*c2;
                                  t1 = t3+"/"+t2;
                             }
                             String c = input.next();
                            if(!c.equals(t1))
                             {
                                 System.out.println("答案错误!");
                                 System.out.println("正确答案是:"+t1);
                             }
                             else
                             {
                                 System.out.println("答案正确!");
                                 count++;
                             }
                             if(i==h-1)
                             {
                                 System.out.println("共答对"+count+"道!");
                             }
                         }
                         
                         break;
                     }
                     default:
                         System.out.println("输入的数有误,请输入0或1");
                     }
                    break;
                }
                case 4:
                {
                    System.out.println("请选择要使用的功能:");
                    System.out.println("1.   正分数四则运算!");
                    System.out.println("2.   可负分数四则运算!");
                    int f=input.nextInt();
                     switch(f)
                     {
                     case 1:
                     {
                         for(int i=0;i<h;i++)
                         {
                             int c1=random.nextInt(g);
                             int c2=random.nextInt(g)+1;
                             int c3=random.nextInt(g)+1;
                             int c4=random.nextInt(g)+1;
                             char w=arr[random.nextInt(4)];
                             System.out.println(c1+"/"+c2+" "+w+" "+c3+"/"+c4+"=");
                             System.out.println("请输入答案:");
                             int t2,t3;
                             String t1=new String();
                             if(w=='+')
                             {
                                  t2 = c2*c4;
                                   t3 = c1*c4+c3*c2;
                                   t1 = t3+"/"+t2;
                             }
                             else if(w=='-')
                             {
                                 t2 = c2*c4;
                                  t3 = c1*c4-c3*c2;
                                  t1 = t3+"/"+t2;
                             }
                             else if(w=='*')
                             {
                                 t2 = c2*c4;
                                  t3 = c1*c3;
                                  t1 = t3+"/"+t2;
                             }
                             else
                             {
                                 t2 = c2*c3;
                                  t3 = c1*c4;
                                  t1 = t3+"/"+t2;
                             }
                             String c = input.next();
                            if(!c.equals(t1))
                             {
                                 System.out.println("答案错误!");
                                 System.out.println("正确答案是:"+t1);
                             }
                             else
                             {
                                 System.out.println("答案正确!");
                                 count++;
                             }
                             if(i==h-1)
                             {
                                 System.out.println("共答对"+count+"道!");
                             }
                         }
                         break;
                     }
                     case 2:
                     {
                         for(int i=0;i<h;i++)
                         {
                             int c1=-random.nextInt()%g;
                             int c2=random.nextInt(g)+1;
                             int c3=-random.nextInt()%g+1;
                             int c4=random.nextInt(g)+1;
                             char w=arr[random.nextInt(4)];
                             System.out.println(c1+"/"+c2+" "+w+" "+c3+"/"+c4+"=");
                             System.out.println("请输入答案:");
                             int t2,t3;
                             String t1=new String();
                             if(w=='+')
                             {
                                  t2 = c2*c4;
                                   t3 = c1*c4+c3*c2;
                                   t1 = t3+"/"+t2;
                             }
                             else if(w=='-')
                             {
                                 t2 = c2*c4;
                                  t3 = c1*c4-c3*c2;
                                  t1 = t3+"/"+t2;
                             }
                             else if(w=='*')
                             {
                                 t2 = c2*c4;
                                  t3 = c1*c3;
                                  t1 = t3+"/"+t2;
                             }
                             else
                             {
                                 t2 = c2*c3;
                                  t3 = c1*c4;
                                  t1 = t3+"/"+t2;
                             }
                             String c = input.next();
                            if(!c.equals(t1))
                             {
                                 System.out.println("答案错误!");
                                 System.out.println("正确答案是:"+t1);
                             }
                             else
                             {
                                 System.out.println("答案正确!");
                                 count++;
                             }
                             if(i==h-1)
                             {
                                 System.out.println("共答对"+count+"道!");
                             }
                         }
                         break;
                     }
                     default:
                         System.out.print("输入的数有误,请输入1或2");
                     }
                     break;
                }
                case 5:
                {
                    for(int i=0;i<h;i++)
                    {
                        String str=new String();
                        int n =random.nextInt(9)+1;
                        Stack<Integer> S1 = new Stack<Integer>();
                        Stack<Character> S2 = new Stack<Character>();
                        for(int j=0;j<n+1;j++)
                        {    
                            char w=' ';
                            
                            int e= random.nextInt(g)+1;
                            if( w=='/'&&e==0)
                            {
                                e = random.nextInt(g)+1;
                            }
                            w = arr[random.nextInt(5)];
                            S1.push(e);
                            S2.push(w);
                        }
                        int k=0;
                        for(int j=0;j<n;j++)
                        {
                            char m =  S2.pop();
                            if(m=='(')
                            {
                                if(j>n-2)
                                    str =str+S1.pop()+""+arr[random.nextInt(4)];
                                else
                                {
                                    str =str+S1.pop()+""+arr[random.nextInt(4)]+""+m;
                                    k=k+1;
                                }
                            }
                            else
                                str =str+S1.pop()+""+ m;
                        }
                        if(k==0)
                        {
                            str =str+random.nextInt(g)+1;
                        }
                        else if(k==1)
                            str=str+random.nextInt(g)+1+")";
                        else if(k==2)
                            str=str+random.nextInt(g)+1+"))";
                        else if(k==3)
                            str=str+random.nextInt(g)+1+")))";
                        else
                            str=str+random.nextInt(g)+1+"))))";
                         int t1 = oper.caculate(str+'#');
                         System.out.println(str+'=');
                         System.out.println("请输入答案:");
                         int c = input.nextInt();
                         if(c!=t1)
                         {
                             System.out.println("答案错误!");
                             System.out.println("正确答案是:"+t1);
                         }
                         else
                         {
                             System.out.println("答案正确!");
                             count++;
                         }
                         if(i==h-1)
                         {
                             System.out.println("共答对"+count+"道!");
                         }
                    }
                    break;
                }
                default:
                     {
                         System.out.println("输入的数有误,请输入1--5的整数");
                     }
                }
        }
    
    }
     class Operate{   
          private Stack<Character> priStack = new Stack<Character>();// 操作符栈   
          private Stack<Integer> numStack = new Stack<Integer>();;// 操作数栈   
            
          public int caculate(String str) {   
            // 1.判断string当中有没有非法字符   
            String temp;// 用来临时存放读取的字符   
            // 2.循环开始解析字符串,当字符串解析完,且符号栈为空时,则计算完成   
            StringBuffer tempNum = new StringBuffer();// 用来临时存放数字字符串(当为多位数时)   
            StringBuffer string = new StringBuffer().append(str);// 用来保存,提高效率   
            
            while (string.length() != 0) {   
              temp = string.substring(0, 1);   
              string.delete(0, 1);   
              // 判断temp,当temp为操作符时   
              if (!isNum(temp)) {   
                // 1.此时的tempNum内即为需要操作的数,取出数,压栈,并且清空tempNum   
                if (!"".equals(tempNum.toString())) {   
                  // 当表达式的第一个符号为括号   
                  int num = Integer.parseInt(tempNum.toString());   
                  numStack.push(num); 
                  tempNum.delete(0, tempNum.length());   
                }   
                // 用当前取得的运算符与栈顶运算符比较优先级:若高于,则因为会先运算,放入栈顶;若等于,因为出现在后面,所以会后计算,所以栈顶元素出栈,取出操作数运算;   
                // 若小于,则同理,取出栈顶元素运算,将结果入操作数栈。   
            
                // 判断当前运算符与栈顶元素优先级,取出元素,进行计算(因为优先级可能小于栈顶元素,还小于第二个元素等等,需要用循环判断)   
                while (!compare(temp.charAt(0)) && (!priStack.empty())) {  
                  int a = (int) numStack.pop();// 第二个运算数   
                  int b = (int) numStack.pop();// 第一个运算数   
                  char ope = priStack.pop();   
                  int result = 0;// 运算结果   
                  switch (ope) {   
                  // 如果是加号或者减号,则   
                  case '+':   
                    result = b + a;   
                    // 将操作结果放入操作数栈   
                    numStack.push(result);   
                    break;   
                  case '-':   
                    result = b - a;   
                    // 将操作结果放入操作数栈   
                    numStack.push(result);   
                    break;   
                  case '*':   
                    result = b * a;   
                    // 将操作结果放入操作数栈   
                    numStack.push(result);   
                    break;   
                  case '/':   
                    result = b / a;// 将操作结果放入操作数栈   
                    numStack.push(result);   
                    break;   
                  }   
            
                }   
                // 判断当前运算符与栈顶元素优先级, 如果高,或者低于平,计算完后,将当前操作符号,放入操作符栈   
                if (temp.charAt(0) != '#') {   
                  priStack.push(new Character(temp.charAt(0)));   
                  if (temp.charAt(0) == ')') {// 当栈顶为'(',而当前元素为')'时,则是括号内以算完,去掉括号   
                    priStack.pop();   
                    priStack.pop();   
                  }   
                }   
              } else  
                // 当为非操作符时(数字)   
                tempNum = tempNum.append(temp);// 将读到的这一位数接到以读出的数后(当不是个位数的时候)   
            }   
            return numStack.pop();   
          }   
            
          private boolean isNum(String temp) {   
            return temp.matches("[0-9]");   
          }   
            
          /*  
           * 比较当前操作符与栈顶元素操作符优先级,如果比栈顶元素优先级高,则返回true,否则返回false  
           */  
          private boolean compare(char str) {   
            if (priStack.empty()) {   
              // 当为空时,显然 当前优先级最低,返回高   
              return true;   
            }   
            char last = (char) priStack.lastElement();   
            // 如果栈顶为'('显然,优先级最低,')'不可能为栈顶。   
            if (last == '(') {   
              return true;   
            }   
            switch (str) {   
            case '#':   
              return false;// 结束符   
            case '(':   
              // '('优先级最高,显然返回true   
              return true;   
            case ')':   
              // ')'优先级最低,   
              return false;   
            case '*': {   
              // '*/'优先级只比'+-'高   
              if (last == '+' || last == '-')   
                return true;   
              else  
                return false;   
            }   
            case '/': {   
              if (last == '+' || last == '-')   
                return true;   
              else  
                return false;   
            }   
              // '+-'为最低,一直返回false   
            case '+':   
              return false;   
            case '-':   
              return false;   
            }   
            return true;   
          }
    }

    结果截图:

    编程总结分析:去了上课时间,总共用了一天左右的世间,终于完成了那个计算的方法,但是还是有些bug不知道怎么解决,最大的就是那个字符传入时会出现负数的情况,由于入栈时缺少判定难以在数据进来完后进行计算,总是提示栈空的错误,做了很多改动,但是总是会出各种错误,最后还是没做好,看来在这上面还有很多的欠缺需要恶补一下,而且在用栈的过程中暴露的最大的缺点就是不能很熟练,总是磕磕绊绊还得去查些资料什么的,感觉还是需要继续找点资源的。

    PSP0级记录表:

    项目计划日志:

                  周活动总结表

                  姓名:高开拓                                            日期:2016/3/26

    日期任务 听课 编写程序 阅读文档 查询资料 日统计
    周日          
    周一 100   10 20 130
    周二   40 20 30 90
    周三     20 30 50
    周四   100 20 40 160
    周五   180 20 20 220
    周六   400 20 40 460
    周总计 100 720 110 180 1110

                  阶段时间和效率                                           周数:第四周

    总计 100 720 110 180 1110
    平均 100 120 20 30 180
    最大 100 400 20 40 460
    最小 100 40 10 20 50

                  以前的各周统计

    总计 200 1100 150 290 1740
    平均 100 550 75 145 870
    最大 100 720 110 180 1110
    最小 100 380 40 110 630

                  事间记录日志

                  学生:高开拓        日期:2016/3/26

                  教师:王建民        课程名:软件工程概论

    日期 开始时间 结束时间 中断时间 净时间 活动 备注
    3/21 8:00 9:50 10 100 上课 下课休息
      16:00 16:40 10 10+20 阅读文档+查资料 出去
    3/22 14:00 15:35 5 90 编程,看文档,上网查资料 厕所
    3/23 15:50 16:40   50 阅读文档和查资料  
    3/24 16:20 19:30 30 160 编,看,查 出去吃饭
    3/25 17:00 21:30 50 220 编程序查询资料 吃饭、转转
    3/26 9:00 18:41 121 460 写程序改bug修不足一直到写博客 吃饭等
                 
                 

     工作照:

  • 相关阅读:
    CentOS7 mysql5.7安装并配置主主同步
    CentOS7 jdk安装
    CentOS7 zabbix安装并实现其它服务器服务监控报警与自动恢复
    CentOS7 SkyWalking APM8.1.0 搭建与项目集成使用
    Centos7 搭建DNS服务器
    mybatis中collection association优化使用及多参数传递
    mysql常用配置注意项与sql优化
    Spring webFlux:坐等spring-boot-starter-data-mysql-reactive
    ElasticSearch 工具类封装(基于ElasticsearchTemplate)
    接口配置有效管理(获取配置文件内容3种常见方式)
  • 原文地址:https://www.cnblogs.com/kt97458/p/5323685.html
Copyright © 2011-2022 走看看