您的位置:首页 > 其它

基于控制台的四则运算

2017-03-06 20:26 351 查看
Coding地址:https://coding.net/u/Julyyun/p/homework/git

一、题目简介

除了整数以外,还要支持真分数的四则运算,真分数的运算,例如:1/6 + 1/8 = 7/24

运算符为 +, −, ×, ÷

并且要求能处理用户的输入,并判断对错,打分统计正确率。

要求能处理用户输入的真分数, 如 1/2, 5/12 等

使用 -n 参数控制生成题目的个数,例如执行下面命令将生成10个题目Myapp.exe -n 10

二、需求分析

编写一段程序,能够输出小学四则运算的题目,并且支持真分数的操作,同时能够对用户输入的答案判定正确与否,给出成绩。

三、功能设计

1用户能自定义此次题目的数量;

2能够输出四则运算题目,并支持真分数的运算;

3能对答案进行判定,并给出正确答案。

四、代码说明

.整数部分


void zhengshu(int m, int a[])
{
for (int p = 0; p<m; p++)
{
int i = (int)rand() % 50;
int j = (int)rand() % 50;
int k = (int)rand() % 100 / 25;
switch (k)
{
case 0:
int o;
cout << i << "+" << j << "=";
a[p] = i + j;
cin >> o;
if ((i + j) == o)
cout << "答案正确" << endl;
else
cout << "答案错误" << endl;
break;
case 1:
int q;
cout << i << "-" << j << "=";
a[p] = i - j;
cin >> q;
if ((i - j) == q)
cout << "答案正确" << endl;
else
cout << "答案错误" << endl;
break;
case 2:
int r;
cout << i << "*" << j << "=";
a[p] = i*j;
cin >> r;
if ((i * j) == r)
cout << "答案正确" << endl;
else
cout << "答案错误" << endl;
break;
case 3:
int s;
a[p] = i / j;
cout << i << "/" << j << "=";
cin >> s;
if ((i / j) == s)
cout << "答案正确" << endl;
else
cout << "答案错误" << endl;
break;
}
}
}


.真分数部分

void fenshu(int m, int a[][2])
{
for (int p = 0; p<m; p++)
{
int ti,tj;
int i = (int)rand() %10;
int j = (int)rand() %10;
while (j == 0 || i >= j)
{
i = (int)rand() %10;
j = (int)rand() %10;
}
int x = (int)rand() %10;
int y = (int)rand() %10;
while (y == 0 || x >= y)
{
x = (int)rand() %10;
y = (int)rand() %10;
}
int k = (int)rand() % 100 / 25;
switch (k)
{
case 0:
cout << "(" << i << "/" << j << ")" << "+" << "(" << x << "/" << y << ")" << "=";
a[p][0] = i*y + x*j;
a[p][1] = j*y;
scanf("%d/%d",&ti,&tj);
if(a[p][0]==ti && a[p][1]==tj){
cout << "答案正确" << endl;
}
else{
cout << "答案错误" << endl;
}
break;
case 1:
cout << "(" << i << "/" << j << ")" << "-" << "(" << x << "/" << y << ")" << "=";
a[p][0] = i*y - x*j;
a[p][1] = j*y;
scanf("%d/%d",&ti,&tj);
if(a[p][0]==ti && a[p][1]==tj){
cout << "答案正确" << endl;
}
else{
cout << "答案错误" << endl;
}
break;
case 2:
cout << "(" << i << "/" << j << ")" << "*" << "(" << x << "/" << y << ")" << "=";
a[p][0] = i*x;
a[p][1] = j*y;
scanf("%d/%d",&ti,&tj);
if(a[p][0]==ti && a[p][1]==tj){
cout << "答案正确" << endl;
}
else{
cout << "答案错误" << endl;
}
break;
case 3:
cout << "(" << i << "/" << j << ")" << "/" << "(" << x << "/" << y << ")" << "=";
a[p][0] = i*y;
a[p][1] = j*x;
scanf("%d/%d",&ti,&tj);
if(a[p][0]==ti && a[p][1]==tj){
cout << "答案正确" << endl;
}
else{
cout << "答案错误" << endl;
}
}
}


五、测试运行





PSP表格

PSP2.1Personal Software Process StagesTime (%) Senior StudentTime (%)
Planning计划86
· Estimate估计这个任务需要多少时间150160
Development开发8070
· Analysis需求分析 (包括学习新技术)2520
· Design Spec生成设计文档55
· Design Review设计复审55
· Coding Standard代码规范33
· Design具体设计3020
· Coding具体编码80100
· Code Review代码复审75
· Test测试(自我测试,修改代码,提交修改)1520
Reporting报告1015
·测试报告32
·计算工作量21
·并提出过程改进计划33
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: