四则运算生成器
1.代码来源:自己编写
2.运行环境:VC++6.0
3.编程语言:c语言
4.bug:未发现
5.当前功能:可以生成0-10的四则运算的题,在每道题做完后判断正确性和给出正确答案。
5.功能扩展的方向:增加复合运算。
6.gitbub代码地址:https://github.com/selfTboke/project1
7.实现
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
{
int type;
int left, right,center;
float result;
srand(unsigned(time(NULL)));
while(1)
{
//清屏
system("cls");
//生成随机数值
type = rand() % 7;
left = rand() % 10;
right = rand() % 10;
center = rand() % 10;
switch(type)
{
case 0:
printf("%d + %d = ?\n", left, right);
scanf("%f", &result);
if(left + right == result)
printf("right!\n");
else
printf("wrong!The result is %d\n", left + right);
break;
case 1:
//保证被减数大于减数
if(left < right)
break;
printf("%d - %d = ?\n", left, right);
scanf("%f", &result);
if(left - right == result)
printf("right!\n");
else
printf("wrong!The result is %d\n", left - right);
break;
case 2:
printf("%d * %d = ?\n", left, right);
scanf("%f", &result);
if(left * right == result)
printf("right!\n");
else
printf("wrong!The result is %d\n", left * right);
break;
case 3:
//判断除数是否为0
if(right == 0)
break;
printf("%d / %d = ?\n", left, right);
printf("除不尽时请保留三位小数:");
scanf("%f", &result);
if(fabs(float(left) / right - result) <= 0.001 )
printf("right!\n");
else
printf("wrong!The result is %.3f\n", float(left) / right);
break;
case 4:
printf("%d + %d + %d = ?\n",left,center,right);
scanf("%f",&result);
if(left+center+right == result)
printf("right!\n");
else
printf("wrong!The result is %d\n", left+center+right);
break;
case 5:
if(left<(center+right))
break;
printf("%d - %d - %d = ?\n",left,center,right);
scanf("%f",&result);
if(left-center-right == result)
printf("right!\n");
else
printf("wrong!The result is %d\n", left-center-right);
break;
case 6:
if((left+center)<right)
break;
printf("%d + %d - %d = ?\n",left,center,right);
scanf("%f",&result);
if(left+center-right == result)
else
printf("wrong!The result is %d\n", left+center-right);
break;
}
//防止闪退
system("pause");
}
return 0;
}