一、作业信息
博客班级 | https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18 |
---|---|
作业要求 | https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18/homework/11377 |
作业目标 | 编写一个可以自动生成支持整数和真分数的四则运算题目的程序 |
学号 | 3180701205 |
二、题目要求
写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
2)程序要求能处理用户的输入,判断对错,累积分数
3)程序支持可以由用户自行选择加、减、乘、除运算
4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目
三:代码提交与运行截图
1:源代码
主函数:
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
int score=0;
void main()
{
int choise;
int con=0;
printf("
四则运算题目生成
");
while(1)
{
printf("请选择:
");
printf(" 1.加法运算\n");
printf(" 2.减法运算\n");
printf(" 3.乘法运算\n");
printf(" 4.除法运算\n");
if(con==0)
scanf("%d",&choise);
switch(choise)
{
case 1:
jia();
break;
case 2:
jian();
break;
case 3:
cheng();
break;
case 4:
chu();
break;
}
printf("
1.继续运算
");
printf("
2.重新选择
");
printf("
3.退出运算
");
scanf("%d",&con);
if(con==1)
con=1;
else if(con==2)
con=0;
else if(con==3)
break;
else
printf("抱歉!,你输入的指令有误!请重新输入!
");
}
printf("您的得分是 %d 分
",score);
}
加法:
void jia()//加法运算
{
int a,b,c;
srand((int)time(0));
a=rand()%100;
b=rand()%100;
printf("请回答:
%d + %d = ",a,b);
scanf("%d",&c);
if(a+b==c)
{
printf("回答正确!
");
score++;
}
else
{
printf("回答错误,正确答案是 %d
",a+b);
}
}
减法:
void jian()//减法运算
{
int a,b,c;
srand((int)time(0));
a=rand()%100;
b=rand()%100;
printf("请回答:
%d - %d = ",a,b);
scanf("%d",&c);
if(a-b==c)
{
printf("回答正确!
");
score++;
}
else
{
printf("回答错误,正确答案是 %d
",a-b);
}
}
乘法:
void cheng()//乘法运算
{
int a,b,c;
srand((int)time(0));
a=rand()%100;
b=rand()%100;
printf("请回答:
%d * %d = ",a,b);
scanf("%d",&c);
if(a*b==c)
{
printf("回答正确!
");
score++;
}
else
{
printf("回答错误,正确答案是 %d
",a*b);
}
}
除法:
void chu()//除法运算
{
int a,b,c;
srand((int)time(0));
a=rand()%100;
b=rand()%100;
printf("请回答:
%d / %d = ",a,b);
scanf("%d",&c);
if(a/b==c)
{
printf("回答正确!
");
score++;
}
else
{
printf("回答错误,正确答案是 %d
",a/b);
}
}
运行截图: