四则运算

一、摘要

  作业地址:https://edu.cnblogs.com/campus/nenu/2016CS/homework/2266

  git仓库地址:https://git.coding.net/kefei101/f4.git

  结对成员:田佳欣  https://www.cnblogs.com/tianjx687/p/9933776.html

二、需求分析

  通过对题目功能一、功能二、功能三的分析,我们共提取出以下6个需求:

  1、对输入格式的判断,以及路径、提示功能的实现;

  2、分别实现整数运算、整数小数运算;

  3、分别实现不含括号的四则运算、含括号的四则运算;

  4、对运算结果的处理分别实现保留三位小数、日常化;

  5、分别实现答题计算、输出算式结果到指定txt文件;

  6、分别实现表达式可重复的四则运算、表达式不重复的四则运算。

三、解题思路

  我们共设计了6个类,如图:

软件工程 week  04

    Creat类:负责随机产生N条3个运算符,4个运算数的四则运算的式子。

    Calculator类:负责计算答案。

    MakeFile类:负责运算结果输出至控制台上,且保存到指定位置的txt文件中。

    Symbol类:负责将产生的代表运算符的数字转换成对应的运算符。

         (0:“+”;1:“-”;2:“*”;3:“/”)

    Result类:负责处理不同功能要求的输出结果格式。

    Main类:主类,负责接收控制台输入,并判断其输入格式,应实现的功能。

     6个类的相互调用关系为:

  软件工程 week  04

    比较重要的函数有:

      Main类:main():判断输入格式以及路径、提示功能的实现,主要是逻辑。

        Create类:createProblem(int n,int no):随机产生N个运算数为整数,不带括号的四则运算式子;createSm(int n):随机产生N个运算数为整数小数均可的,带有括号的四则运算式子。(n表示n个式子,no表示功能一二的选择)   

        Calculator类:algorithm(int[] var):计算运算数为整数,不带括号的式子具体计算算法;bracketsAlgo(int[] var):计算运算数整数小数均可 ,带括号的式子计提计算算法。(var[]代表将要存放的运算符)  

      MakeFile类:creatFile(int n,String filename):将N个四则运算式子输出并存放到指定文件夹。(n表示n个式子,filename表示指定txt文件路径)

      Result类:treat(double res):实现不同功能对对输出结果的要求。

四、测试运行  

  1、输入格式错误: 

软件工程 week  04

软件工程 week  04

  2、功能一、功能二

软件工程 week  04软件工程 week  04

  3、功能三

软件工程 week  04

软件工程 week  04

五、重要代码展示

  1、主方法main()测试:由于有足够的前期基础,逻辑这块没有多大难度,唯一有点难度的,就是会使用库方法获取项目路径。

 1     /**
 2      * Main主函数
 3      * @param args args
 4      */
 5     public static void main(String[] args){
 6 
 7         if(args.length==0){
 8             System.out.println("请进入小程序所在文件夹,并以正确的格式输入。");
 9             System.exit(1); //结束运行
10         } else if (args.length == 2) {
11             if (args[0].equals("-n")) {
12                 if (args[1].matches("^[0-9]*$")) {
13                     int num = Integer.parseInt(args[1]);
14                     if (num < 0) {
15                         System.out.println("题目数量必须是 正整数。");
16                         System.exit(1); //结束运行
17                     }else{
18                         Create create = new Create();
19                         create.createProblem(num, 1);
20                     }
21                 } else {
22                     System.out.println("题目数量必须是 正整数。");
23                     System.exit(1); //结束运行
24                 }
25             } else if (args[0].equals("-c")) {
26                 if (args[1].matches("^[0-9]*$")) {
27                     int num = Integer.parseInt(args[1]);
28                     if (num > 100 || num < 1) {
29                         System.out.println("题目数量范围为1到100之间的正整数。");
30                         System.exit(1); //结束运行
31                     }else {
32                         Create create = new Create();
33                         create.createProblem(num, 2);
34                     }
35                 } else {
36                     System.out.println("题目数量范围为1到100之间的正整数。");
37                     System.exit(1); //结束运行
38                 }
39             } else {
40                 System.out.println("输入格式错误!");
41                 System.exit(1); //结束运行
42             }
43         }
44         //f4 -c 题目总数 -f txt文件路径
45         else if (args.length == 4) {
46             String fileName = args[3];
47             if (args[0].equals("-c") && args[2].equals("-f")) {
48                 if (args[1].matches("^[0-9]*$")) {
49                     int num = Integer.parseInt(args[1]);
50                     if (num > 100 || num < 1) {
51                         System.out.println("题目数量范围为1到100之间的正整数。");
52                         System.exit(1); //结束运行
53                     }else {
54                         MakeFile makeFile = new MakeFile();
55                         makeFile.creatFile(num, fileName);
56                     }
57                 } else {
58                     System.out.println("题目数量范围为1到100之间的正整数。");
59                     System.exit(1); //结束运行
60                 }
61             } else {
62                 System.out.println("输入格式错误!");
63                 System.exit(1); //结束运行
64             }
65         } else {
66             System.out.println("输入格式错误!");
67             System.exit(1); //结束运行
68         }
69     }
main

相关文章:

  • 2021-07-14
  • 2021-12-21
  • 2022-01-15
  • 2021-12-08
  • 2021-07-29
  • 2021-06-11
猜你喜欢
  • 2022-01-01
  • 2021-09-05
  • 2022-01-29
  • 2022-12-23
  • 2022-02-20
  • 2021-12-02
相关资源
相似解决方案