软工第一次作业——小学四则运算
题目描述:
从《构建之法》第一章的 “程序” 例子出发,像阿超那样,花二十分钟写一个能自动生成小学四则运算题目的命令行 “软件”,满足以下需求:
- 除了整数以外,还要支持真分数的四则运算,真分数的运算,例如:1/6 + 1/8 = 7/24
- 运算符为 +, −, ×, ÷
- 并且要求能处理用户的输入,并判断对错,打分统计正确率。
- 要求能处理用户输入的真分数, 如 1/2, 5/12 等
- 使用 -n 参数控制生成题目的个数,例如执行下面命令将生成10个题目
Myapp.exe -n 10
需求分析:
- 能够实现生成整数和分数的式子并实现计算;
- 能够控制生成式子的个数;
- 能够实现判断对错并计算正确率;
设计实现:
- 通过if实现选择生成整数或分数的式子并计算;
- 显示正确率
代码说明:
#pragma warning(disable:4996) #include<stdio.h> #include<stdlib.h> #include<time.h> #include<windows.h> #include<string.h> using namespace std; #define OPERATOR 4 //获取随机运算符 char getopre() { char oper[OPERATOR] = { '+','-','*','/' }; srand((unsigned)time(NULL)); return oper[rand() % 4]; } //获取随机数 int random(double start, double end) { return (int)(start + (end - start)*rand() / (RAND_MAX + 1.0)); } //列式子 void equation() { int num1; int num2; int result; int answer; char oper; int right=0; float accuracy; int i, n; printf("请输入要生成的式子数:"); scanf("%d", &n); for (i = 0; i < n; i++) { srand((unsigned)time(NULL)); oper = getopre(); num1 = random(0, 20); num2 = random(1, 20); switch (oper) { case '+': result = num1 + num2; printf("%d+%d=\n", num1, num2); break; case '-': result = num1 - num2; printf("%d-%d=\n", num1, num2); break; case '*': result = num1*num2; printf("%d*%d=\n", num1, num2); break; case '/': num1 = num1*num2; result = num1 / num2; printf("%d÷%d=\n", num1, num2); break; } //printf("%d%c%d=\n", num1, oper, num2); printf("请输入你的答案:"); scanf("%d", &answer); if (result==answer) { printf("恭喜你答对了\n\n"); right++; } else { printf("对不起答错了\n"); printf("正确答案为:%d\n\n", result); } } accuracy = ((float)right /(float) n)*100; printf("正确率为百分之%1.2f\n", accuracy); } //真分数列式 void fraction() { int num1; int num2; int num3; int num4; int a, b; int answer1, answer2; float result; float answer; char oper; int right = 0; float accuracy; int i, n; printf("请输入要生成的式子数:"); scanf("%d", &n); for (i = 0; i < n; i++) { srand((unsigned)time(NULL)); oper = getopre(); num1 = random(0, 20); num2 = random(1, 20); num3 = random(0, 20); num4 = random(1, 20); switch (oper) { case '+': a = num1*num4+num3*num2; b = num2*num4; result = (float)a / (float)b; printf("%d/%d+%d/%d=\n", num1, num2, num3, num4); break; case '-': a = num1*num4 - num3*num2; b = num2*num4; result = (float)a / (float)b; printf("%d/%d-%d/%d=\n", num1, num2, num3, num4); break; case '*': a = num1*num3; b = num2*num4; result = (float)a / (float)b; printf("%d/%d*%d/%d=\n", num1, num2,num3, num4); break; case '/': a = num1*num4; b = num2*num3; result =(float) a /(float) b; printf("%d/%d÷%d/%d=\n", num1, num2, num3, num4); break; } printf("请输入你的答案:\n"); scanf("%d", &answer1); printf("/\n"); scanf("%d", &answer2); answer = (float)answer1 / (float)answer2; if (result==answer) { printf("恭喜你答对了\n\n"); right++; } else { printf("对不起答错了\n"); printf("正确答案为:%d/%d\n\n", a,b); } } accuracy = ((float)right / (float)n) * 100; printf("正确率为百分之%1.2f\n", accuracy); } //主函数 void main() { int choose; printf("输入1生成整数算式,其他生成分式算式:"); scanf("%d", &choose); if (choose == 1) { equation(); } else { fraction(); } }
测试运行:
PSP展示:
PSP2.1 | Personal Software Process Stages | Time (%) Senior Student(/hour) | Time (%)(/hour) |
· Planning | 计划 | 2 | 2 |
· Estimate | 估计这个任务需要多少时间 | 30 | 31 |
· Analysis | 需求分析 (包括学习新技术) | 1 | 1 |
· Coding Standard | 代码规范 | 0.5 | 0.5 |
· Design | 具体设计 | 2 | 1 |
· Coding | 具体编码 | 25 | 27 |
· Test | 测试(自我测试,修改代码,提交修改) | 1 | 1 |
Reporting | 报告 | 1 | 1 |
码云链接:https://git.coding.net/ras/work1.git