zoukankan      html  css  js  c++  java
  • 作业四:个人项目-小学四则运算之JAVA版

    作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2186

    代码所在的github远程仓库的地址:https://github.com/lyh-2333/lyhthree

      github仓库文件:项目文件(Operations.zip);代码文件(operstions.txt);可执行文件(public.zip)。


    一、需求分析

      任何编程语言都可以,命令行程序接受一个数字输入,然后输出相应数目的四则运算题目和答案。例如输入数字是 30, 那就输出 30 道题目和答案。 运算式子必须至少有两个运算符,运算数字是在 100 之内的正整数,答案不能是负数。 如:23 - 3 * 4 = 11。

      统计用户共答对多少道题。

    二、功能设计

       1.根据用户要求的题目数n,循环n次输出随机的四则运算。

       2.判断用户输入值是否正确,并给出正确题目数。

       3.运算答案不能小于0,小于0就重新在后台出题目。

    三、详细设计

       随机产生三个100以内的正整数和四个分别用“1”-“4”代表+,-,*,/运算符号,通过对四则运算中乘除和加减的优先级别判断:先乘除后加减,在计算函数中遍历运算符数组中乘除符号后进行优先计算,之后再对乘除计算之后的运算数进行相加减运算。

      遇到当随机产生的正整数运算题计算结果为负数时,递归重新生成运算题,直至结果是大于零时所产生的题目打印在控制台上,用户输入答案,答案与结果匹配正确即累加正确题数,错误的输出打印正确结果,当全部运算题做完后输出统计结果。

    代码实现:

      1)主要运算

     1   public void operation(int n){
     2         int op1,op2;
     3         for (int i = 0; i < n; i++) {
     4             num1 = new Random().nextInt(100)+1;
     5             num2 = new Random().nextInt(100)+1;
     6             num3 = new Random().nextInt(100)+1;
     7             op1=new Random().nextInt(4)+1;//用1~4随机生成的整数来代表加、减、乘、除四个运算符
     8             op2=new Random().nextInt(4)+1;
     9             //整数1代表加法
    10             if(op1 == 1){
    11                 String str = num1 + "+" + num2;//str为输出提示语句(公式)
    12                 if(op2 == 1){
    13                     result = add(num1,num2);
    14                     result = add(result,num3);
    15                     str = str + "+" + num3;/*++*/
    16                 }
    17                 else if(op2 == 2){
    18                     result = add(num1,num2);
    19                     //while循环确保此result值大于等于num3,从而保证最终答案不为负数
    20                     while(result < num3){
    21                         num3 = new Random().nextInt(100)+1;
    22                     }
    23                     result = sub(result,num3);
    24                     str = str + "-" + num3;/*+-*/
    25                 }
    26                 /*乘法除法做优先级处理*/
    27                 else{
    28                     str = pri(op1,op2);
    29                 }
    30                 print(str);
    31             }
    32             //整数2代表减法
    33             else if(op1 == 2){
    34 //                String str = num1 + "-" + num2;
    35                 String str = "";
    36                 if(op2 == 1){
    37                     result = sub(num1,num2);
    38                     //while循环确保此result的绝对值小于等于num3的绝对值,从而保证最终答案不为负数
    39                     while(Math.abs(result) > Math.abs(num3)){
    40                         num3 = new Random().nextInt(100)+1;
    41                     }
    42                     result = add(result,num3);
    43                     str = num1 + "-" + num2 + "+" + num3;
    44                 }
    45                 else if(op2 == 2){
    46                     //while循环确保result的值为正数(因为如果这一步result为负数,下一步的result值一定为负数(由于num3一定为正数))
    47                     while(num1 < num2){//算法不优先,把num1的取值范围缩小了
    48                         num1 = new Random().nextInt(100)+1;
    49                     }
    50                     result = sub(num1,num2);
    51 //                    str = num1 + "-" + num2;
    52                     //while循环确保result大于等于num3
    53                     while(result < num3){
    54                         num3 = new Random().nextInt(100)+1;
    55                     }
    56                     result = sub(result,num3);
    57                 }
    58                 /*乘法除法做优先级处理*/
    59                 else{
    60                     str = pri(op1,op2);
    61                 }
    62                 print(str);
    63             }
    64             //整数3代表从乘法
    65             else if(op1 == 3){
    66                 String str = num1 + "*" + num2+"*" + num3;
    67                 result = mul(num1,num2);
    68                 result = mul(result,num3);
    69 //                str = operation2(op1,op2,str);
    70                 print(str);
    71             }
    72             //整数4代表除法
    73             else if(op1 == 4){
    74                 String str = num1 + "÷" + num2;
    75                 result = div(num1,num2);
    76                 str = operation2(op1,op2,str);
    77                 print(str);
    78             }
    79             else {
    80                 System.out.println("出错了。。。。");
    81             }
    82         }
    83         System.out.println(n+"题中"+"您一共答对了"+mark+"题。");
    84     }

      2)对先进行加(减)法再进行乘(除)法的算式进行优先级判断和处理

     1   public String pri(int op1,int op2){
     2         String str = "";
     3         /*乘法*/
     4         if(op2 == 3){
     5             result = mul(num2,num3);
     6             str = comPri(op1,op2,str);
     7             str = str + "*" + num3;
     8         }
     9         /*除法*/
    10         if(op2 == 4){
    11             result = div(num2,num3);
    12             str = comPri(op1,op2,str);
    13             str = str + "÷" + num3;
    14         }
    15         return str;
    16     }
    17     public String comPri(int op1,int op2,String str){
    18         if(op1 == 1){
    19             result = add(num1,result);
    20             str = num1 + "+" + num2;
    21         }
    22         if(op1 == 2){
    23             //while循环确保最终答案不为负数
    24             while(num1 < result){
    25                 num2 = new Random().nextInt(100)+1;
    26                 num3 = new Random().nextInt(100)+1;
    27                 result = mul(num2,num3);
    28             }
    29             result = sub(num1,result);
    30             str = num1 + "-" + num2;
    31         }
    32         return str;
    33     }

      3)对除法中的运算进行分析

     1   public String operation2(int op1,int op2,String str){
     2         if(op2 == 1){
     3             result = add(result,num3);
     4             return str + "+" + num3;
     5         }
     6         /*先除后减*/
     7         else if(op2 == 2){
     8             //while循环确保最终答案不为负数
     9             while(result < num3){
    10                 num1 = new Random().nextInt(100)+1;
    11                 num2 = new Random().nextInt(100)+1;/*重新生成num1,num2*/
    12                 result = div(num1,num2);
    13                 str = num1 + "÷" + num2;
    14             }
    15             result = sub(result,num3);
    16             return str + "-" + num3;
    17         }
    18         else if(op2 == 3){
    19             result = mul(result,num3);
    20             return str + "*" + num3;
    21         }
    22         else{
    23             /*还未做被除数和除数不为零的操作*/
    24             result = div(result,num3);
    25             return str + "÷" + num3;
    26         }
    27     }

      4)输出代码

     1   public void print(String str){
     2 
     3         Scanner s = new Scanner(System.in);
     4         System.out.print(str + "=");
     5         int input = s.nextInt();
     6         if(input == result){
     7             mark=mark+1;
     8             System.out.println("回答正确!");
     9 //            System.out.println("已答对"+mark+"题。");
    10         }
    11         else {
    12             System.out.println("回答错误!正确答案为:" + str + "=" +result);
    13         }
    14     }

    四、运行结果

       

    五、问题及解决办法

       对于如何做到答案不为负数的问题。会出现负数的情况只有:在减法中减数大于被减数。所以我在有减法的运算式中对其减数和被减数进行了对比处理;

      从上面的运行结果不难看出,对于除法的运算还是未能做到完全正确。其中的问题来源于:我把结果result定义为了int类型,因此我的整个程序都只是做整数部分的运算。

      我的程序中还未做到的处理是:被除数不为零的操作。

    六、过程耗时估计与统计表

    PSP2.1 Personal Software Process Stages 预计时间(分钟) 实际时间(分钟)
    Planning 计划  20  20
    · Estimate 估计这个任务需要多少时间  180 160
    Development 开发  120  150
    · Analysis 需求分析 (包括学习新技术)  20  20
    · Design Spec 生成设计文档  10  20
    · Design Review 设计复审  10  10
    · Coding Standard 代码规范  10  10
    · Design 具体设计  30  30
    · Coding 具体编码  120  150
    · Code Review 代码复审  30  20
    · Test 测试(自我测试,修改代码,提交修改)  60  120
    Reporting 报告  30  30
    · 测试报告  10  10
    · 计算工作量  10  10
    · 并提出过程改进计划  10  10
  • 相关阅读:
    (转)Java 调用 C++ (Java 调用 dll)
    用Gvim建立IDE编程环境 (Windows篇)
    (转)python调取C/C++的dll生成方法
    C/C++协程的实现方式总结
    时钟周期,机器周期,指令周期,总线周期
    (转)MongoDB和Redis区别
    linux cpu占用100%排查
    (转)linux 打开文件数 too many open files 解决方法
    Python下载网页图片
    python抓网页数据【ref:http://www.1point3acres.com/bbs/thread-83337-1-1.html】
  • 原文地址:https://www.cnblogs.com/linxiLYH/p/9781374.html
Copyright © 2011-2022 走看看