博客班级 | 班级链接 |
---|---|
作业要求 | 作业链接 |
作业目标 | 培养编程能力和学用markdown |
学号 | 3180701104 |
题目要求
写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
2)程序要求能处理用户的输入,判断对错,累积分数
3)程序支持可以由用户自行选择加、减、乘、除运算
4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目
代码
加法题目生成及运算
void add(int num,fraction fraans[],int ans[])
{
int a, b, up1, down1, up2, down2,flag=0,i;
整数部分
for (i = 0; i <=num / 2; i++)
{
unsigned int times = (unsigned int)time(0);
srand(times * (i + 1));
a = rand() % 10 + 1;
b = rand() % 10 + 1;
ans[i] = a + b;
printf("%d.%d+%d=
", i+1, a, b);
}
分数部分
for (i = num/2+1; i < num; i++)
{
unsigned int times = (unsigned int)time(0);
srand(times * (i + 1));
up1 = rand() % 10 + 1;
down1 = rand() % 10 + 1;
up2 = rand() % 10 + 1;
down2 = rand() % 10 + 1;
printf("%d.", i + 1);
yuefen(up1, down1,flag);
printf("+");
yuefen(up2, down2,flag);
printf("=");
printf("
");
fraans[i].up = up1 * down2 + up2 * down1;
fraans[i].down = down1 * down2;
flag = 1;
yuefen(fraans[i].up, fraans[i].down,flag);
flag = 0;
}
}
减法题目生成及运算
void subtract(int num, fraction fraans[], int ans[])
{
int a, b, up1, down1, up2, down2,flag=0,i;
for (i = 0; i <= num / 2; i++)
{
unsigned int times = (unsigned int)time(0);
srand(times * (i + 1));
a = rand() % 10 + 1;
b = rand() % 10 + 1;
ans[i] = a - b;
printf("%d.%d-%d=
", i, a, b);
}
for (i = num / 2 + 1; i < num; i++)
{
unsigned int times = (unsigned int)time(0);
srand(times * (i + 1));
up1 = rand() % 10 + 1;
down1 = rand() % 10 + 1;
up2 = rand() % 10 + 1;
down2 = rand() % 10 + 1;
printf("%d.", i + 1);
yuefen(up1, down1, flag);
printf("-");
yuefen(up2, down2, flag);
printf("
");
fraans[i].up = up1 * down2 - up2 * down1;
fraans[i].down = down1 * down2;
flag = 1;
yuefen(fraans[i].up, fraans[i].down, flag);
flag = 0;
}
}
乘法题目生成及运算
void multiply(int num, fraction fraans[], int ans[])
{
int a, b, up1, down1, up2, down2,flag = 0,i;
for (i = 0; i <= num / 2; i++)
{
unsigned int times = (unsigned int)time(0);
srand(times * (i + 1));
a = rand() % 10 + 1;
b = rand() % 10 + 1;
ans[i] = a * b;
printf("%d.%d*%d=
", i+1, a, b);
}
for (i = num / 2 + 1; i < num; i++)
{
unsigned int times = (unsigned int)time(0);
srand(times * (i + 1));
up1 = rand() % 10 + 1;
down1 = rand() % 10 + 1;
up2 = rand() % 10 + 1;
down2 = rand() % 10 + 1;
printf("%d.", i + 1);
yuefen(up1, down1, flag);
printf("*");
yuefen(up2, down2, flag);
printf("
");
fraans[i].up = up1 * up2;
fraans[i].down = down1 * down2;
flag = 1;
yuefen(fraans[i].up, fraans[i].down, flag);
flag = 0;
}
}
除法题目生成及运算
void divide(int num, fraction fraans[])
{
int up1, down1, up2, down2, flag = 0;
for (int i = 0; i < num; i++)
{
unsigned int times = (unsigned int)time(0);
srand(times * (i + 1));
up1 = rand() % 10 + 1;
down1 = rand() % 10 + 1;
up2 = rand() % 10 + 1;
down2 = rand() % 10 + 1;
printf("%d. ", i + 1);
yuefen(up1, down1, flag);
printf(" / ");
yuefen(up2, down2, flag);
printf("
");
fraans[i].up = up1 * down2;
fraans[i].down = down1 * up2;
flag = 1;
yuefen(fraans[i].up, fraans[i].down, flag);
flag = 0;
}
}
主函数
int main()
{
int m=1;
fraction fraans[400], userfraans[400];
int ans[100] = { 0 }, userans[100] = { 0 },i;
int num, symbol, grade;
printf("*****欢迎使用四则运算生成程序*****
");
printf("* 1.加法运算 *
");
printf("* 2.减法运算 *
");
printf("* 3.乘法运算 *
");
printf("* 4.除法运算 *
");
printf("* 0.退出 *
");
printf("**********************************
");
while(m)
{
label: printf("
请选择符号0、1、2、3、4:");
scanf("%d", &symbol);
grade=0;//用symbol标志用户选择的符号,1234对应加减乘除;
switch (symbol)//对应选择不同的算法
{
case 0:m=0;break;
case 1:
printf("请输入题目数量(最少为2,最大100):");
scanf("%d", &num);add(num, fraans, ans); break;
case 2:
printf("请输入题目数量(最少为2,最大100):");
scanf("%d", &num);subtract(num, fraans, ans); break;
case 3:
printf("请输入题目数量(最少为2,最大100):");
scanf("%d", &num);multiply(num, fraans, ans); break;
case 4:
printf("请输入题目数量(最少为2,最大100):");
scanf("%d", &num);divide(num, fraans); break;
default:
printf("请重新输入:");
goto label;
}
if(m!=0)
{
if (symbol != 4)//当用户选择的运算不为除法时,分别对整数和分数答案进行保存
{
for ( i = 0; i <= num / 2; i++)
{
printf("第%d道题的答案为:", i + 1);
scanf("%d", &userans[i]);
}
for ( i = num / 2 + 1; i < num; i++)
{
printf("第%d道题的答案为:", i + 1);
if (fraans[i].down == 1)
{
scanf("%d", &userfraans[i].up);
userfraans[i].down = 1;
}
else
{
scanf("%d/%d", &userfraans[i].up, &userfraans[i].down);
}
}
}
else//当用户选择为出发时,对用户输入的答案进行保存
{
for ( i = 0; i < num; i++)
{
printf("第%d道题的答案为:", i + 1);
if (fraans[i].down == 1)
{
scanf("%d", &userfraans[i].up);
userfraans[i].down = 1;
}
else
{
scanf("%d/%d", &userfraans[i].up, &userfraans[i].down);
}
}
}
计数得分
for ( i = 0; i <= num / 2; i++)
{
if (userans[i] == ans[i])grade++;
}
for ( i = num / 2 + 1; i < num; i++)
{
if (userfraans[i].up == fraans[i].up && userfraans[i].down == fraans[i].down)grade++;
}
printf("grade=%d
", grade);
}
}
}
运行界面
小结
psp表格
psp2.1 | 任务内容 | 计划需要完成的时间(min | 实际完成需要的时间(min) |
---|---|---|---|
Planning | 计划 | 240 | 360 |
Estimate | 估计这个任务需要多少时间,并规划大致工作步骤 | 20 | 20 |
Development | 开发 | 20 | 15 |
Analysis | 需求分析(包括学习新技术) | 30 | 30 |
Design Spec | 生成设计文档 | 40 | 30 |
Design Review | 设计复审 | 30 | 30 |
Coding Standard | 代码规范 | 20 | 15 |
Design | 具体设计 | 20 | 10 |
Coding | 具体编码 | 100 | 120 |
Code Review | 代码复审 | 10 | 20 |
Test | 测试(自我测试,修改代码,提交修改) | 10 | 15 |
Reporting | 报告 | 20 | 20 |
Test Report | 测试报告 | 20 | 15 |
Size Measurement | 计算工作量 | 20 | 10 |
Postmortem & Process Improvement Plan | 事后总结,并提出过程改进计划 | 20 | 20 |
心得体会
通过本次作业主要学习了Markdown语法编辑,也在编写过程中发现了许多不足,希望后面能提高代码能力。