求c语言题答案 计算机自动出四则运算计算题,要求自动出0-10之间的四则运算题,并批改结果

关键问题:1.如何让程序每次执行的时候都出不同的题目? 运算符的生成:用编码0-3表示“+、-、×、/”,四个运算符。因此题目的生成就是生成0-3之间的随机数。 2. 使用随机数生成器rand() 能随机生成0到RAND_MAX之间的整型数 。 RAND_MAX在stdlib.h中定义,不大于双字节整数的最大值32767 产生[0,b-1] 之间的随机数:magic = rand()%b; 产生[a,a+b-1] 之间的随机数:magic = rand()%b+a; 3.改变随机数的种子
设置种子的函数srand : srand (种子) 如何让程序每次执行时选择的种子都不一样呢? 选择系统时间为种子:time(NULL) 取当前的系统时间,需要包含头文件time.h。 4.程序结构生成题目
srand (time(NULL))
magic = rand()%4;
switch(题目类型)
{ case 0:显示题目,输入和的值,判断正确与否// 加法
case 1:显示题目,输入差的值,判断正确与否//减法
case 2:显示题目,输入积的值,判断正确与否//乘法
case 3:显示题目,输入商和余数的值,判断正确与否//除法
}

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int main()
{
int type;
int left, right;
float result;
srand(unsigned(time(NULL)));
while(1)
{
type = rand() % 4;
left = rand() % 10;
right = 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:
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:
printf("%d / %d = ?\n", left, right);
if(right == 0)
break;
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;
}
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-03-15
简单,没分,不做
第2个回答  2013-03-17
一个学校的啊 我也正找答案呢 我大一测绘
相似回答