随机数加减法运算

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include

int main()
{
	int a = 0, b = 0, c = 0, max = 0, sign = 0;
	char signL;
	printf("请选择运算符:1->加法,2->减法\n");
	scanf("%d", &sign);
	signL = sign == 1 ? '+': '-';

	srand((unsigned long)time(0));

	printf("请输入最大数(小于10000)\n");
	scanf("%d", &max);
	a = rand() % max;
	b = rand() % max;

	while (a < b)
	{
		a = rand() % max;
		b = rand() % max;
	}
	printf("计算数据为:\n");
	if (1 == sign)
	{
		printf("%d %c %d = \n", a,signL, b);
	}
	else
	{
		printf("%d %c %d = \n", a,signL, b);
	}

	printf("请输入您计算的结果:\n");
	scanf("%d", &c);

	if ((sign == 1) && (a + b == c) || (sign == 2) && (a - b == c))
	{
		printf("恭喜您! 计算正确!\n");
		printf("正确结果为 %d %c %d = %d\n", a, signL, b, c);
	}
	else
	{
		printf("不好意思! 计算错误!\n");
		if (sign == 1)
		{
			c = a + b;
			printf("正确结果为 %d %c %d = %d\n", a, signL, b, c);
		}
		else
		{
			c = a - b;
			printf("正确结果为 %d %c %d = %d\n", a, signL, b, c);
		}
	}
	return 0;
}

功能描述:

1.选择加法或减法进行运算;

2.系统随机选出小于Max的2个数字进行加法或减法运算;

3.输入你计算的结果;

4.系统会判断你计算的正确或错误,并输出正确的计算结果。

你可能感兴趣的:(C语言编程,c语言)