题目:完成一个基于控制台的四则运算程序,实现一个自动生成小学四则运算题目的命令行程序
从《构建之法》第一章的 “程序” 例子出发,像阿超那样,花二十分钟写一个能自动生成小学四则运算题目的命令行 “软件”。
一、程序设计及实现
a.需求分析:
1.四则运算是小学数学计算教学中的难点内容,也是学生必须熟练掌握的内容之一。因而设计两个数的四则运算题库,能了解并提高学生对四则运算的掌握程度
2.小学四则运算以两个不超过100的整数,以及两个分数的四则运算为主。
b.功能设计:
1.基本功能
- 除了整数以外,还要支持真分数的四则运算,真分数的运算,例如:1/6 + 1/8 = 7/24
- 运算符为 +, −, ×, ÷
- 并且要求能处理用户的输入,并判断对错,打分统计正确率
- 要求能处理用户输入的真分数, 如 1/2, 5/12 等
2.部分代码说明:
求公约数:
int gcd(int x, int y)
{
int temp;
while (x%y!=0)
{
temp = x%y;
x = y;
y = temp;
}
return y;
}
生成随机数:
up = 1 + rand() % 10;
down = 1 + rand() % 10;
up1 = 1 + rand() % 10;
down1 = 1 + rand() % 10;
left = 1 + rand() % 100;
right = 1 + rand() % 100;
type = 1 + rand() % 8;
整数加减乘除:
switch (type)
{
case 1:
cout << left;
cout << "+";
cout << right;
cout << "=?" << endl;
a[i] = left + right;
e[i] = 1;
break;
case 2:
cout << left;
cout << "-";
cout << right;
cout << "=?" << endl;
a[i] = left - right;
e[i] = 2;
break;
case 3:
cout << left;
cout << "*";
cout << right;
cout << "=?" << endl;
a[i] = left*right;
e[i] = 3;
break;
case 4:
cout << left;
cout << "÷";
cout << right;
cout << "=?" << endl;
result = left / right;
result = ((double)((int)((result + 0.005) * 100))) / 100;
a[i] = result;
e[i] = 4;
break;
}
分数的生成及加法(减乘除同理)
int gn;
if (up < down)
{
printf("%g/%g", up, down);
cout << "+";
if (up1 < down1)
{
printf("%g/%g=?
", up1, down1);
result1 = up*down1 + up1*down;
result2 = down1*down;
gn = gcd(result1, result2);
result1 = result1 / gn;
result2 = result2 / gn;
std::to_string(result1);
std::to_string(result2);
g[i] = result1 + '/' + result2;
}
else
{
printf("%g/%g=?
", down1, up1);
result1 = up*up1 + down1*down;
result2 = up1*down1;
gn = gcd(result1, result2);
result1 = result1 / gn;
result2 = result2 / gn;
std::to_string(result1);
std::to_string(result2);
g[i] = result1 + '/' + result2;
}
}
else
{
printf("%g/%g", down, up);
cout << "+";
if (up1 < down1)
{
printf("%g/%g=?
", up1, down1);
result1 = down1*down + up1*up;
result2 = up*down1;
gn = gcd(result1, result2);
result1 = result1 / gn;
result2 = result2 / gn;
std::to_string(result1);
std::to_string(result2);
g[i] = result1 + '/' + result2;
}
else
{
printf("%g/%g=?
", down1, up1);
result1 = down*up1 + down1*up;
result2 = up*up1;
gn = gcd(result1, result2);
result1 = result1 / gn;
result2 = result2 / gn;
std::to_string(result1);
std::to_string(result2);
g[i] = result1 + '/' + result2;
}
}
e[i] = 5;
break;
输入答案并判断对错正确率:
cout << "请输入答案" << endl;
for (i = 0; i < n; i++)
{
if (e[i] < 5)
{
cin >> b[i];
if (b[i] == a[i])
count += 1;
}
if (e[i] >= 5)
{
cin >> f[i];
if (f[i].compare(g[i]) == 0)
count += 1;
else
continue;
}
}
double rate = count/ n;
cout << "正确率:";
cout << rate;
c.运行结果截图:
2.展示psp:
PSP2.1
|
Personal Software Process Stages
|
Time (m) Senior Student
|
Time (m)
|
Planning
|
计划
|
2h
|
1.5h
|
· Estimate
|
估计这个任务需要多少时间
|
8h
|
10h
|
Development
|
开发
|
--
|
--
|
· Analysis
|
需求分析 (包括学习新技术)
|
10min
|
10min
|
· Design Spec
|
生成设计文档
|
20min
|
15min
|
· Design Review
|
设计复审
|
--
|
--
|
· Coding Standard
|
代码规范
|
5min
|
8min
|
· Design
|
具体设计
|
40min
|
50min
|
· Coding
|
具体编码
|
6h
|
7h
|
· Code Review
|
代码复审
|
1h
|
1.5h
|
· Test
|
测试(自我测试,修改代码,提交修改)
|
3h
|
3h
|
Reporting
|
报告
|
30min
|
25min
|
·
|
测试报告
|
--
|
--
|
·
|
计算工作量
|
5min
|
5min
|
·
|
并提出过程改进计划
|
--
|
--
|
开发软件:vs2013
三、小结:
第一次做软件工程的作业 ,编程水平有限导致编写代码花费时间长,很多地方不完善。
码云地址:https://coding.net/u/linzhao/p/sizeyunsuan/git/tree/master/