四则运算
| 博客班级 | 软件工程 |
|---|---|
| 作业要求 | 作业要求 |
| 作业目标 | 能够精准高效完成四则运算并输出结果;迅速理解需求制定功能 |
| 学号 | 3180701216 |
目录
* 一.作业信息
* 二.作业要求
* 三.代码提交与截图
* 四.个人小结
二.作业要求
写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
- 除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
- 程序要求能处理用户的输入,判断对错,累积分数
- 程序支持可以由用户自行选择加、减、乘、除运算
- 使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目
计算公约数:
public static int GCD(int m, int n) {//辗转相除法
while (m % n != 0) {
int t = m % n;
m = n;
n = t;
}
return n;
}
约分:
public static String Reduction(int m, int n) //约分
{
String t;
if (n == 1) {
t = m + "";
} else {
t = (m) + "" + "/" + (n) + "";
}
return t;
}
加法运算:
public static String add(Fraction a, Fraction b) //加法
{
int m = a.getNumerator() *b.getDenominator() + a.getDenominator() * b.getNumerator();
int n = a.getDenominator() * b.getDenominator();
int t = Fraction.GCD(m,n);
return Fraction.Reduction(m / t, n / t);
}
减法运算:
public static String sub(Fraction a, Fraction b)//减法
{
int m = a.getNumerator() * b.getDenominator() - a.getDenominator() * b.getNumerator();
int n = a.getDenominator() * b.getDenominator();
int t = Fraction.GCD(m,n);
return Fraction.Reduction(m / t, n / t);
}
乘法运算:
public static String mul(Fraction a, Fraction b)//乘法
{
int m = a.getNumerator() * b.getNumerator();
int n = a.getDenominator() * b.getDenominator();
int t = Fraction.GCD(m,n);
return Fraction.Reduction(m / t, n / t);
}
除法部分 :
public static String div(Fraction a, Fraction b)//除法
{
int m = a.getNumerator() * b.getDenominator();
int n = a.getDenominator() * b.getNumerator();
int t = Fraction.GCD(m,n);
return Fraction.Reduction(m / t, n / t);
}
验证对错,如果错误显示正确答案:
public static boolean compare(String m, String n) //比较输入结果与答案
{
if (m.equals(n))
{System.out.println("恭喜你,回答正确!");
return true;}
else
{
System.out.println("回答错误,再接再厉,正确答案是:" + n);
return false;
}
}
统计正确率、记时:
public class Test {
@SuppressWarnings("resource")
public static void main(String[] args) {
Fraction f1 = new Fraction();
Fraction f2 = new Fraction();
int operator;
int right=0,wrong=0;
String answer;
Scanner s1 = new Scanner(System.in);
System.out.println("请输入想要完成的题目数 :");
int i = s1.nextInt();
long begin = System.currentTimeMillis();
...
long end = System.currentTimeMillis();
long hour = (end - begin)/3600000;
long minute = (end - begin-hour36000000)/60000;
long second = (end - begin-hour36000000-minute*60000)/1000;
double total = (double)right+wrong;
System.out.println("\n您已完成练习!");
System.out.println("你所用的时间是:" + hour+"小时"+minute+"分钟"+ second+"秒");
System.out.println('\n'+"一共有"+i+"题"+" ,"+"做对"+right+"题"+" ,"+"做错"+wrong+"题");
System.out.printf("正确率:%f",right/total *100 );
System.out.println("%");
}
}
作业小结
| psp2.1 | 任务内容 | 计划完成需要的时间(min) | 实际完成需要的时间(min) |
|---|---|---|---|
| Planning | 计划 | 2000 | 250 |
| Estimate | 估计这个任务需要多少时间,并规划大致工作步骤 | 20 | 40 |
| Development | 开发 | 10 | 30 |
| Analysis | 需求分析(包括学习新技术) | 10 | 10 |
| Design Spec | 生成设计文档 | 10 | 20 |
| Design Review | 设计复审 | 10 | 20 |
| Coding Standard | 代码规范 | 10 | 10 |
| Design | 具体设计 | 20 | 30 |
| Coding | 具体编码 | 30 | 20 |
| Code Review | 代码复审 | 20 | 30 |
| Test | 测试(自我测试,修改代码,提交修改) | 20 | 30 |
| Reporting | 报告 | 10 | 20 |
| Test Report | 测试报告 | 10 | 15 |
| Size Measurement | 计算工作量 | 20 | 20 |
| Postmortem & Process Improvement Plan | 事后总结,并提出过程改进计划 | 10 | 20 |
| 2.心得体会 | |||
| 通过网上查找资料和请教同学完成了这学期第一个程序。这次写程序让我重新回顾了以前学过的知识点,虽然程序过于简单,功能不够完善,我相信我以后的努力能弥补现在知识的空缺. |
浙公网安备 33010602011771号