随机产生n到四则运算并记录答对数量 过程及分析

随机产生n到四则运算并记录答对数量 过程及分析,第1张

随机产生n到四则运算并记录答对数量 过程及分析
#include
#include
#include
int free_num()        //随机产生十以内的数字
{
	int num;
	num = rand() % 10 + 1;
	return num;
}
char free_word()   //随机产生一个运算符
{
	int i = 0; char word = 'w'; int num = 0;
	i = rand() %4 + 1;
	switch (i)
	{
	case(1):word = '+'; return word;
	case(2):word = '-'; return word;
	case(3):word = '*'; return word;
	case(4):word = '/'; return word;
	}

}
int main()
{
	srand((unsigned)time(NULL));
	int a, b; char c; int ans = 0; int n = 0; int j = 0; int ret = 0;
	int ok = 0;
	printf("请输入你想回答的题数:n");
	scanf_s("%d", &j);
	while (n < j)
	{
		a = free_num();
		b = free_num();
		c = free_word();
		
		printf("%d %c %d", a, c, b);
		switch (c)
		{
		case '+':ans = a + b; break;
		case '-':ans = a - b; break;
		case '*':ans = a * b;  break;
		case '/':ans = a / b;  break;
		} 
		printf("n");
		printf("请回答你的答案:");
		scanf_s("%d", &ret);
		if (ret == ans)
		{
			printf("you are rightn");
			ok++;
		}
		else
			printf("you are non");
		n++;
	}
	printf("你回答正确的题数是:");
	printf("%d", ok);
	return 0;
}

有用就点个赞呗 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5657937.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存