个人作业-四则运算题目生成程序
一.作业规范要求
博客班级 | 计算机18级 |
---|---|
作业要求 | 四则运算生成 |
作业目标 | 用工程化的思想完成本题 |
学号 | 3180701128 |
二.题目要求
写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
- 除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
- 程序要求能处理用户的输入,判断对错,累积分数
- 程序支持可以由用户自行选择加、减、乘、除运算
- 使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目
三.代码提交
加法函数:
int addit(int topic_num) //加法函数
{
//分子分母
int left_molecule, left_denominator, right_molecule, right_denominator;
int common_factor;//分子分母最大公约数
//保证随机性
srand((unsigned)time(NULL));
for(int i = 0; i < topic_num; i++)
{
Flag[i] = rand()%2;
if (!Flag[i])
{
//整数运算
left_molecule = rand()%100;
right_molecule = rand()%100;
Anwser_molecule[i] = left_molecule + right_molecule;
printf("第%d题:%d + %d\n", i+1, left_molecule, right_molecule);
}
else
{
//分数运算
left_denominator = rand()%100+1;
left_molecule = rand()%left_denominator+1;
common_factor = func(left_denominator, left_molecule);
left_denominator = left_denominator/common_factor;
left_molecule = left_molecule/common_factor;
if (left_denominator==left_molecule)
{
left_denominator++;
}
right_denominator = rand()%100+1;
right_molecule = rand()%right_denominator+1;
common_factor = func(right_denominator, right_molecule);
right_denominator = right_denominator/common_factor;
right_molecule = right_molecule/common_factor;
if(right_denominator==right_molecule)
{
right_denominator++;
}
Anwser_denominator[i] = left_denominator*right_denominator;
Anwser_molecule[i] = left_molecule*right_denominator + left_denominator*right_molecule;
common_factor = func(Anwser_denominator[i], Anwser_molecule[i]);
Anwser_denominator[i] = Anwser_denominator[i]/common_factor;
Anwser_molecule[i] = Anwser_molecule[i]/common_factor;
printf("第%d题:%d/%d + %d/%d\n", i+1, left_molecule, left_denominator, right_molecule, right_denominator);
}
}
correct(topic_num);
anwser(topic_num);
return 0;
}
减法函数:
int subtract(int topic_num) //减法函数
{
//分子分母
int left_molecule, left_denominator, right_molecule, right_denominator;
int common_factor;//分子分母最大公约数
//保证随机性
srand((unsigned)time(NULL));
for(int i = 0; i < topic_num; i++)
{
Flag[i] = rand()%2;
if (!Flag[i])
{
//整数运算
left_molecule = rand()%100;
right_molecule = rand()%100;
Anwser_molecule[i] = left_molecule - right_molecule;
printf("第%d题:%d - %d\n", i+1, left_molecule, right_molecule);
}
else
{
//分数运算
left_denominator = rand()%100+1;
left_molecule = rand()%left_denominator+1;
common_factor = func(left_denominator, left_molecule);
left_denominator = left_denominator/common_factor;
left_molecule = left_molecule/common_factor;
if (left_denominator==left_molecule)
{
left_denominator++;
}
right_denominator = rand()%100+1;
right_molecule = rand()%right_denominator+1;
common_factor = func(right_denominator, right_molecule);
right_denominator = right_denominator/common_factor;
right_molecule = right_molecule/common_factor;
if(right_denominator==right_molecule)
{
right_denominator++;
}
Anwser_denominator[i] = left_denominator*right_denominator;
Anwser_molecule[i] = left_molecule*right_denominator - left_denominator*right_molecule;
common_factor = func(Anwser_denominator[i], Anwser_molecule[i]);
Anwser_denominator[i] = Anwser_denominator[i]/common_factor;
Anwser_molecule[i] = Anwser_molecule[i]/common_factor;
printf("第%d题:%d/%d - %d/%d\n", i+1, left_molecule, left_denominator, right_molecule, right_denominator);
}
}
correct(topic_num);
anwser(topic_num);
return 0;
}
乘法函数:
int multiplicat(int topic_num) //乘法函数
{
//分子分母
int left_molecule, left_denominator, right_molecule, right_denominator;
int common_factor;//分子分母最大公约数
//保证随机性
srand((unsigned)time(NULL));
for(int i = 0; i < topic_num; i++)
{
Flag[i] = rand()%2;
if (!Flag[i])
{
//整数运算
left_molecule = rand()%100;
right_molecule = rand()%100;
Anwser_molecule[i] = left_molecule * right_molecule;
printf("第%d题:%d * %d\n", i+1, left_molecule, right_molecule);
}
else
{
//分数运算
left_denominator = rand()%100+1;
left_molecule = rand()%left_denominator+1;
common_factor = func(left_denominator, left_molecule);
left_denominator = left_denominator/common_factor;
left_molecule = left_molecule/common_factor;
if (left_denominator==left_molecule)
{
left_denominator++;
}
right_denominator = rand()%100+1;
right_molecule = rand()%right_denominator+1;
common_factor = func(right_denominator, right_molecule);
right_denominator = right_denominator/common_factor;
right_molecule = right_molecule/common_factor;
if(right_denominator==right_molecule)
{
right_denominator++;
}
Anwser_denominator[i] = left_denominator*right_denominator;
Anwser_molecule[i] = left_molecule*right_molecule;
common_factor = func(Anwser_denominator[i], Anwser_molecule[i]);
Anwser_denominator[i] = Anwser_denominator[i]/common_factor;
Anwser_molecule[i] = Anwser_molecule[i]/common_factor;
printf("第%d题:%d/%d * %d/%d\n", i+1, left_molecule, left_denominator, right_molecule, right_denominator);
}
}
correct(topic_num);
anwser(topic_num);
return 0;
}
除法函数:
除法结果保留两位小数
int division(int topic_num) //除法函数
{
int score=0;
float left_num, right_num;
float anwser[10000];
float num_anwser;
srand((unsigned)time(NULL));
for (int i = 0; i < topic_num; i++)
{
left_num = rand()%100;
right_num = rand()%100+1;
anwser[i] = (int)((left_num/right_num*100)+0.5)/100.0;
printf("第%d题:%.0f / %.0f\n", i+1, left_num, right_num);
}
for (int i = 0; i < topic_num; i++)
{
printf("\n输入第%d题答案(保留小数点后两位):", i+1);
cin >> num_anwser;
if(num_anwser==anwser[i])
{
printf("答对了!!!加一分\n");
score++;
}
else
{
printf("答错了!!!正确答案是:%.2f\n", anwser[i]);
}
}
printf("满分%d分,您一共得分:%d\n", topic_num, score);
printf("\n标准答案如下:\n");
for (int i = 0; i < topic_num; i++)
{
printf("第%d题:", i+1);
cout << anwser[i] <<endl;
}
return 0;
}
两数最大公约数:
int func(int num_m, int num_n)//找出分子分母的最大公约数(辗转相除法)
{
int tmp;
num_m = abs(num_m);
num_n = abs(num_n);
if(num_m == num_n)
{
return num_m;
}
if(num_m < num_n)
{
tmp = num_m;
num_m = num_n;
num_n = tmp;
}
while (num_m % num_n != 0)
{
tmp = num_m;
num_m = num_n;
num_n = tmp % num_n;
}
return num_n;
}
用户输入答案:
int correct(int topic_num)//检验用户输入的答案
{
int score=0, anwser_molecule, anwser_denominator;
string anwser, str_buffer;
for(int i = 0; i < topic_num; i++)
{
printf("\n输入第%d题答案:", i+1);
cin >> anwser;
if(Flag[i])
{
for (int j = 0; anwser[j]; j++)
{
if(anwser[j]!='/')
{
str_buffer = str_buffer + anwser[j];
}
else
{
anwser_molecule = atoi(str_buffer.c_str());
str_buffer.clear();
}
}
anwser_denominator = atoi(str_buffer.c_str());
str_buffer.clear();
if (anwser_molecule==Anwser_molecule[i] && anwser_denominator==Anwser_denominator[i])
{
printf("答对了!!!加一分\n");
score++;
}
else
{
printf("答错了!!!正确答案应该是:%d/%d\n", Anwser_molecule[i], Anwser_denominator[i]);
}
}
else if (Anwser_molecule[i]==atoi(anwser.c_str()))
{
printf("答对了!!!加一分\n");
score++;
}
else
{
printf("答错了!!!正确答案应该是:%d\n", Anwser_molecule[i]);
}
}
printf("\n满分%d分,您一共得分:%d\n",topic_num, score);
return 0;
}
源码:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stdio.h>
#include <cstring>
using namespace std;
int func(int num_m, int num_n);
int anwser(int topic_num);
int correct(int topic_num);
int addit(int topic_num);
int subtract(int topic_num);
int multiplicat(int topic_num);
int division(int topic_num);
int Anwser_molecule[10000], Anwser_denominator[10000];//答案中的分子分母
int Flag[10000];//决定整数或者分数运算标志
int main()
{
//题目个数
int topic_num, ch;
topic_num = 10;
while (1)
{
printf("-----四则运算-----\n");
printf("0.退出题库\n");
printf("1.加法运算\n");
printf("2.减法运算\n");
printf("3.乘法运算\n");
printf("4.除法运算\n");
printf("选择您想要的运算题单(输入标号):");
cin >> ch;
switch(ch)
{
case 1:
printf("输入题目个数:");
cin >> topic_num;
printf("-----试题-----\n");
addit(topic_num);//加法
break;
case 2:
printf("输入题目个数:");
cin >> topic_num;
printf("-----试题-----\n");
subtract(topic_num);//减法
break;
case 3:
printf("输入题目个数:");
cin >> topic_num;
printf("-----试题-----\n");
multiplicat(topic_num);//乘法
break;
case 4:
printf("输入题目个数:");
cin >> topic_num;
printf("-----试题-----\n");
division(topic_num);//除法
break;
case 0:
system("pause");
return 0;
}
}
system("pause");
return 0;
}
int func(int num_m, int num_n)//找出分子分母的最大公约数(辗转相除法)
{
int tmp;
num_m = abs(num_m);
num_n = abs(num_n);
if(num_m == num_n)
{
return num_m;
}
if(num_m < num_n)
{
tmp = num_m;
num_m = num_n;
num_n = tmp;
}
while (num_m % num_n != 0)
{
tmp = num_m;
num_m = num_n;
num_n = tmp % num_n;
}
return num_n;
}
int anwser(int topic_num)
{
//测试运算结果
printf("答案如下:\n");
for(int i = 0; i < topic_num; i++)
{
if (!Flag[i])
{
printf("第%d题:%d\n",i+1, Anwser_molecule[i]);
}
else
{
printf("第%d题:%d/%d\n",i+1, Anwser_molecule[i], Anwser_denominator[i]);
}
}
return 0;
}
int correct(int topic_num)//检验用户输入的答案
{
int score=0, anwser_molecule, anwser_denominator;
string anwser, str_buffer;
for(int i = 0; i < topic_num; i++)
{
printf("\n输入第%d题答案:", i+1);
cin >> anwser;
if(Flag[i])
{
for (int j = 0; anwser[j]; j++)
{
if(anwser[j]!='/')
{
str_buffer = str_buffer + anwser[j];
}
else
{
anwser_molecule = atoi(str_buffer.c_str());
str_buffer.clear();
}
}
anwser_denominator = atoi(str_buffer.c_str());
str_buffer.clear();
if (anwser_molecule==Anwser_molecule[i] && anwser_denominator==Anwser_denominator[i])
{
printf("答对了!!!加一分\n");
score++;
}
else
{
printf("答错了!!!正确答案应该是:%d/%d\n", Anwser_molecule[i], Anwser_denominator[i]);
}
}
else if (Anwser_molecule[i]==atoi(anwser.c_str()))
{
printf("答对了!!!加一分\n");
score++;
}
else
{
printf("答错了!!!正确答案应该是:%d\n", Anwser_molecule[i]);
}
}
printf("\n满分%d分,您一共得分:%d\n",topic_num, score);
return 0;
}
int addit(int topic_num) //加法函数
{
//分子分母
int left_molecule, left_denominator, right_molecule, right_denominator;
int common_factor;//分子分母最大公约数
//保证随机性
srand((unsigned)time(NULL));
for(int i = 0; i < topic_num; i++)
{
Flag[i] = rand()%2;
if (!Flag[i])
{
//整数运算
left_molecule = rand()%100;
right_molecule = rand()%100;
Anwser_molecule[i] = left_molecule + right_molecule;
printf("第%d题:%d + %d\n", i+1, left_molecule, right_molecule);
}
else
{
//分数运算
left_denominator = rand()%100+1;
left_molecule = rand()%left_denominator+1;
common_factor = func(left_denominator, left_molecule);
left_denominator = left_denominator/common_factor;
left_molecule = left_molecule/common_factor;
if (left_denominator==left_molecule)
{
left_denominator++;
}
right_denominator = rand()%100+1;
right_molecule = rand()%right_denominator+1;
common_factor = func(right_denominator, right_molecule);
right_denominator = right_denominator/common_factor;
right_molecule = right_molecule/common_factor;
if(right_denominator==right_molecule)
{
right_denominator++;
}
Anwser_denominator[i] = left_denominator*right_denominator;
Anwser_molecule[i] = left_molecule*right_denominator + left_denominator*right_molecule;
common_factor = func(Anwser_denominator[i], Anwser_molecule[i]);
Anwser_denominator[i] = Anwser_denominator[i]/common_factor;
Anwser_molecule[i] = Anwser_molecule[i]/common_factor;
printf("第%d题:%d/%d + %d/%d\n", i+1, left_molecule, left_denominator, right_molecule, right_denominator);
}
}
correct(topic_num);
anwser(topic_num);
return 0;
}
int subtract(int topic_num) //减法函数
{
//分子分母
int left_molecule, left_denominator, right_molecule, right_denominator;
int common_factor;//分子分母最大公约数
//保证随机性
srand((unsigned)time(NULL));
for(int i = 0; i < topic_num; i++)
{
Flag[i] = rand()%2;
if (!Flag[i])
{
//整数运算
left_molecule = rand()%100;
right_molecule = rand()%100;
Anwser_molecule[i] = left_molecule - right_molecule;
printf("第%d题:%d - %d\n", i+1, left_molecule, right_molecule);
}
else
{
//分数运算
left_denominator = rand()%100+1;
left_molecule = rand()%left_denominator+1;
common_factor = func(left_denominator, left_molecule);
left_denominator = left_denominator/common_factor;
left_molecule = left_molecule/common_factor;
if (left_denominator==left_molecule)
{
left_denominator++;
}
right_denominator = rand()%100+1;
right_molecule = rand()%right_denominator+1;
common_factor = func(right_denominator, right_molecule);
right_denominator = right_denominator/common_factor;
right_molecule = right_molecule/common_factor;
if(right_denominator==right_molecule)
{
right_denominator++;
}
Anwser_denominator[i] = left_denominator*right_denominator;
Anwser_molecule[i] = left_molecule*right_denominator - left_denominator*right_molecule;
common_factor = func(Anwser_denominator[i], Anwser_molecule[i]);
Anwser_denominator[i] = Anwser_denominator[i]/common_factor;
Anwser_molecule[i] = Anwser_molecule[i]/common_factor;
printf("第%d题:%d/%d - %d/%d\n", i+1, left_molecule, left_denominator, right_molecule, right_denominator);
}
}
correct(topic_num);
anwser(topic_num);
return 0;
}
int multiplicat(int topic_num) //乘法函数
{
//分子分母
int left_molecule, left_denominator, right_molecule, right_denominator;
int common_factor;//分子分母最大公约数
//保证随机性
srand((unsigned)time(NULL));
for(int i = 0; i < topic_num; i++)
{
Flag[i] = rand()%2;
if (!Flag[i])
{
//整数运算
left_molecule = rand()%100;
right_molecule = rand()%100;
Anwser_molecule[i] = left_molecule * right_molecule;
printf("第%d题:%d * %d\n", i+1, left_molecule, right_molecule);
}
else
{
//分数运算
left_denominator = rand()%100+1;
left_molecule = rand()%left_denominator+1;
common_factor = func(left_denominator, left_molecule);
left_denominator = left_denominator/common_factor;
left_molecule = left_molecule/common_factor;
if (left_denominator==left_molecule)
{
left_denominator++;
}
right_denominator = rand()%100+1;
right_molecule = rand()%right_denominator+1;
common_factor = func(right_denominator, right_molecule);
right_denominator = right_denominator/common_factor;
right_molecule = right_molecule/common_factor;
if(right_denominator==right_molecule)
{
right_denominator++;
}
Anwser_denominator[i] = left_denominator*right_denominator;
Anwser_molecule[i] = left_molecule*right_molecule;
common_factor = func(Anwser_denominator[i], Anwser_molecule[i]);
Anwser_denominator[i] = Anwser_denominator[i]/common_factor;
Anwser_molecule[i] = Anwser_molecule[i]/common_factor;
printf("第%d题:%d/%d * %d/%d\n", i+1, left_molecule, left_denominator, right_molecule, right_denominator);
}
}
correct(topic_num);
anwser(topic_num);
return 0;
}
int division(int topic_num) //除法函数
{
int score=0;
float left_num, right_num;
float anwser[10000];
float num_anwser;
srand((unsigned)time(NULL));
for (int i = 0; i < topic_num; i++)
{
left_num = rand()%100;
right_num = rand()%100+1;
anwser[i] = (int)((left_num/right_num*100)+0.5)/100.0;
printf("第%d题:%.0f / %.0f\n", i+1, left_num, right_num);
}
for (int i = 0; i < topic_num; i++)
{
printf("\n输入第%d题答案(保留小数点后两位):", i+1);
cin >> num_anwser;
if(num_anwser==anwser[i])
{
printf("答对了!!!加一分\n");
score++;
}
else
{
printf("答错了!!!正确答案是:%.2f\n", anwser[i]);
}
}
printf("满分%d分,您一共得分:%d\n", topic_num, score);
printf("\n标准答案如下:\n");
for (int i = 0; i < topic_num; i++)
{
printf("第%d题:", i+1);
cout << anwser[i] <<endl;
}
return 0;
}
运行截图
1.加法运算
2.减法运算
3.乘法运算
4.除法运算
5.退出
四.个人小结
1) psp表格
psp | 任务内容 | 计划完成需要的时间(min) | 实际完成需要的时间(min) |
---|---|---|---|
Planning | 计划 | 20 | 16 |
Estimate | 估计这个任务需要多少时间,并规划大致工作步骤 | 20 | 23 |
Development | 开发 | 300 | 280 |
Analysis | 需求分析(包括学习新技术) | 24 | 20 |
Design Spec | 生成设计文档 | 10 | 5 |
Design Review | 设计复审 | 10 | 5 |
Coding Standard | 代码规范 | 5 | 3 |
Design | 具体设计 | 20 | 15 |
Coding | 具体编码 | 60 | 58 |
Code Review | 代码复审 | 5 | 10 |
Test | 测试(自我测试,修改代码,提交修改) | 20 | 15 |
Reporting | 报告 | 10 | 8 |
Test Report | 测试报告 | 5 | 3 |
Size Measurement | 计算工作量 | 3 | 2 |
Postmortem & Process Improvement Plan | 事后总结,并提出过程改进计划 | 6 | 5 |
2) |
- 用软件工程的思想来设计此项目以实现开发的工程化,可以提高其可维护性;
- 由于先生成所有题目再进行答题的设计逻辑导致如果题目太多可能对用户不太友好。