C语言大作业小学生随机出题测试计分

公众号:爱写bug

#include 
#include 
#include 

int test();
int checknum(int a, int b);
int checkresult(int x);

int a = -1, b, c, x;
static int k = 0, grade;

void main() {
	printf("请作答下列十道题,回车键开始开始作答!!!");
	getchar();
	do {
		int i = test();
		if (i == -1) {
			break;
		}
		else if (checkresult(i) == 0) {
			a = -1;
		}
	} while (k);
	getchar(); getchar(); getchar();
}

int test() {
	srand((unsigned int)time(NULL));
	while (checknum(a, b) == 0)
	{
		a = rand() % 51;
		b = rand() % 51;
		c = rand() % 2;
	}
	k++;
	if (k == 11) {
		printf("答题结束,您本次测试总分是 %d ,谢谢", grade);
		return -1;
	}
	else if (c == 0) {
		printf("第%d题:  %d+%d=", k, a, b);
		return a + b;
	}
	else if (c == 1) {
		printf("第%d题:  %d-%d=", k, a, b);
		return a - b;
	}
	return -1;
}
int checknum(int a, int b) {
	if ((a + b) > 50 || (a - b) < 0 || a > 50 || b > 50 || a < 0 || b < 0) {
		return 0;
	}
	return 1;
}
int checkresult(int result) {
	int i;
	for (i = 1; i++; i <= 3) {
		scanf_s("%d", &x);
		if (x != result) {
			if (i == 4) {
				if (c == 0) {
					printf("三次回答错误,正确答案是%d,请回答下一题\n", a + b);
				}
				else {
					printf("三次回答错误,正确答案是%d,请回答下一题\n", a - b);
				}
				return 0;
			}
			printf("回答错误,请重新计算并输入结果:");
		}
		else {
			switch (i) {
			case 2:
				grade += 10;
				break;
			case 3:
				grade += 7;
				break;
			case 4:
				grade += 5;
				break;
			default:
				grade += 0;
				break;
			}
			return 0;
		}
	}
	return 0;
}

C语言大作业小学生随机出题测试计分_第1张图片

你可能感兴趣的:(C)