要求说明:练习者自己选择是进行加法还是减法运算,之后输入进行多少以内的加法或减法运算,具体数值会由计算机随机产生,输入答案,计算机会根据输入的数据判断是否正确。
//整数加减法练习
#include <stdio.h>
int main(int argc, char **argv)
{
int a,b,c,sign,max;
char sign1;
printf("please select sign(1 or other,1:-,other:+):\n"); //输入数据赋值给sign,
scanf("%d",&sign);
printf("please select the max number(<10000):\n"); //输入数据赋值给max
scanf("%d",&max);
srand((unsigned long)time(0)); //系统时间设定种子
a = rand()%max; /产生小于max的随机数赋值给a
b = rand()%max; //产生小于max的随机数赋值给b
while((a<b)&&(sign == 1)) //选择减法运算时如果a小于b,则重新产生随机数
{
a = rand()%max;
b = rand()%max;
}
sign1 = (sign==1?'-':'+'); //将选择的运算符赋给sign1
printf("\n%d%c%d=",a,sign1,b);
scanf("%d",&c); //输入计算结果
if((sign == 1)&& (a-b == c) || (sign != 1)&& (a+b ==c)) //判断计算结果是否等于正确答案
{
printf("OK!\n"); //正确输出ok
}
else
{
printf("the result is wrong!\n"); //输出错误
}
return 0;
}
样例输入输出:
please select sign(1 or other,1:-,other:+):
1
please select the max number(<10000):
998
955-928=27
OK!