个人作业1——四则运算题目生成程序(基于java)

项目代码:

https://git.coding.net/YJh_/11.git

题目要求:

1 除了整数以外,还要支持真分数的四则运算,真分数的运算,例如:1/6 + 1/8 = 7/24
2 运算符为 +, −, ×, ÷
3 并且要求能处理用户的输入,并判断对错,打分统计正确率。
4 要求能处理用户输入的真分数, 如 1/2, 5/125 使用 -n 参数控制生成题目的个数,例如执行下面命令将生成10个题目
6 Myapp.exe -n 10

 

程序设计:

  主要功能:

  1. 真分数的四则运算
  2. 按照用户输入的题数出题
  3. 能得出用户的正确率

  设计的主要函数以及属性:

  主要算法代码:

public static int commonDivisor(int x, int y) // 计算2个数的最大公约数。按绝对值计算。
        {
            if (x == 0 || y == 0) {
                return 1;
            }
            int x1;
            int y1;

            x1 = (Math.abs(x) > Math.abs(y)) ? Math.abs(x) : Math.abs(y); // 使x1>y1.
            y1 = (Math.abs(x) > Math.abs(y)) ? Math.abs(y) : Math.abs(x);
            int z = 1;
            while (z != 0) {
                z = x1 % y1;
                x1 = y1;
                y1 = z;
            }
            return x1;
        }

   主要四则运算方法:

    Calculator add(Calculator r) { // 加法运算
        int a = r.getNumerator();
        int b = r.getDenominator();
        int newNumerator = numerator * b + denominator * a;
        int newDenominator = denominator * b;
        
        int maxCommon = commonDivisor(newNumerator, newDenominator);
        Calculator result = new Calculator(newNumerator/ maxCommon, newDenominator/ maxCommon);
        return result;
    }

    Calculator sub(Calculator r) { // 减法运算
        int a = r.getNumerator();
        int b = r.getDenominator();
        int newNumerator = numerator * b - denominator * a;
        int newDenominator = denominator * b;
        
        int maxCommon = commonDivisor(newNumerator, newDenominator);
        Calculator result = new Calculator(newNumerator/ maxCommon, newDenominator/ maxCommon);
        return result;
    }

    Calculator muti(Calculator r) { // 乘法运算
        int a = r.getNumerator();
        int b = r.getDenominator();
        int newNumerator = numerator * a;
        int newDenominator = denominator * b;
        
        int maxCommon = commonDivisor(newNumerator, newDenominator);
        Calculator result = new Calculator(newNumerator/ maxCommon, newDenominator/ maxCommon);
        return result;
    }

    Calculator div(Calculator r) { // 除法运算
        int a = r.getNumerator();
        int b = r.getDenominator();
        int newNumerator = numerator * b;
        int newDenominator = denominator * a;
        
        int maxCommon = commonDivisor(newNumerator, newDenominator);
        Calculator result = new Calculator(newNumerator/ maxCommon, newDenominator/ maxCommon);
        return result;
    }

 

程序测试:

 

项目总结:

   由于java学得并不好,重新回去看了java笔记学习,也上网查了一些资料,包括如何提交代码到码市,装egit的时候,因为下载的eclipse是中文版的,都不大懂,安装了一直出差错,所以前前后后花费了挺多的时间了,请教了班级里的一些比较厉害的同学,做出了这个程序。从中深刻了解到,平时要多多实践,熟能生巧。纸上得来终觉浅,绝知此事要躬行。

PSP表格

PSP2.1 Personal Software Process Stages Time (%) Senior Student Time (%)
Planning 计划 8 15
· Estimate 估计这个任务需要多少时间 8 15
Development 开发 20 15
· Analysis 需求分析 (包括学习新技术) 1 1
· Design Spec 生成设计文档 1 1
· Design Review 设计复审 0 0
· Coding Standard 代码规范 10 7
· Design 具体设计 8 9
· Coding 具体编码 6 6
· Code Review 代码复审 1 1
· Test 测试(自我测试,修改代码,提交修改) 3 3
Reporting 报告 10 10
·Test Report 测试报告 8 7
· Size Measurement 计算工作量 2 2
·Postmortem & Process Improvement Plan 并提出过程改进计划 0 0
posted @ 2017-03-07 13:09  叶华琴  阅读(213)  评论(0编辑  收藏  举报