小学生运算练习程序
题目:
制作一个小学生运算练习程序,随即产生10题两位数含有加减乘运算的数学题,判断正误,且给出最终的分数。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
# define N 10 //10个题目
int main()
{
int i,right=0,error=0;
int a,b,op,result,input;
char ops;
srand(time(NULL));
printf("请计算下列算术题的结果:\n");
for(i=1;i<=N;i++)
{
printf("第%-2d题:",i);
a=rand()%100; //题目要求100以内的两个数+-*
b=rand()%100;
op=rand()%3;
switch(op)
{
case 0:ops='+'; result=a+b; break;
case 1:ops='-'; if(a>=b) result=a-b;
else result=b-a; break;
case 2:ops='*'; result=a*b; break;
default: break;
}
if(a<b&&ops=='-')
printf("%d %c %d=",b,ops,a);
else printf("%d %c %d=",a,ops,b);
scanf("%d",&input);
if(result==input)
{
printf("答案正确!\n");
right=right+1;
}
else
{
printf("答案错误!\n");
error=error+1;
}
}
printf("练习结果:你做对了%d题,做错了%d题。\n",right,error);
return 0;
}