我给熊孩子们设计了一个简单的C程序,随机生成1~100的四则运算,让他们从小就练好算术。 首先是通过包含"time.h"这个头文件,来获取时间,并且以时间作为种子,随机生成1~100的随机数字,代码如下

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

int main(){
     
     time_t t;
     srand((unsigned)time(&t);
     int a = rand() % 100 + 1; //用a来接受随机生成的数字,+1是为了将从1~100开始
     return 0;
}

接下就是用随机生成四则运算的代码

int index = rand() % 4;

if(index == 0){
     printf("%d + %d =",a,b);
     scanf("%f",&studentsolution);
     solution = a + b;
}
else if(index == 1){
       if ((a - b) < 0) {       //如果学过负数的话,可以将这个if去掉
		i--;
		continue;
	}
	printf("%d - %d =", a, b);
	scanf("%f",&studentSolution);
	solution = a - b;
}
else if(index == 2){
       printf("%d * %d = ", a, b);
       scanf("%f", &studentSolution);
       solution = a * b;
}
else if(index ==3){
        if ((a % b) != 0) {     //学过小数同样可以把这个if去掉
		i--;
		continue;
	}
	printf("%d / %d = ", a, b);
	scanf("%f", &studentSolution);
	solution = a / b;
}
else {
	i--;
	continue;
}

然后让我们添加一点点的细节,完整的代码如下

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

typedef struct info {
	int studentNo;
	char name[30];
	char exercise[30];
	float solution;
	float studentSolution;
}STUINFO;
STUINFO record[100];

void generateExercise();
void marking();
void evaluation(float score);
float MAX = 20;  //生成多少道题目

int main() {
	int i=0;
	printf("请输入您的学号:\n");
	scanf_s("%d", &record[i].studentNo);
	printf("请输入您的姓名:\n");
	scanf_s("%s", record[i].name,sizeof(record[i].name));
	printf("欢迎使用小学生辅助教学系统\n");
	printf("您的学号:%d\t您的姓名:%s\n", record[i].studentNo, record[i].name);
	generateExercise();

	return 0;
}
//生成四则题目
void generateExercise() {
	time_t t;
	srand((unsigned)time(&t));
	printf("请输入正确的答案:\n");
	
	for (int i = 0; i < MAX; i++) {
		int a = rand() % 100 + 1;
		int b = rand() % 100 + 1;
		int index = rand() % 4;
		if (index == 0) {
			sprintf(record[i].exercise, "%d + %d = ", a, b);
			printf("%s", record[i].exercise);
			scanf_s("%f", &record[i].studentSolution);
			record[i].solution = a + b;
		}
		else if (index == 1) {
			if ((a - b) < 0) {
				i--;
				continue;
			}
			sprintf(record[i].exercise, "%d - %d = ", a, b);
			printf("%s", record[i].exercise);
			scanf_s("%f", &record[i].studentSolution);
			record[i].solution = a - b;
		}
		else if (index == 2) {
			sprintf(record[i].exercise, "%d * %d = ", a, b);
			printf("%s", record[i].exercise);
			scanf_s("%f", &record[i].studentSolution);
			record[i].solution = a * b;
		}
		else if (index == 3) {
			if ((a % b) != 0) {
				i--;
				continue;
			}
			sprintf(record[i].exercise, "%d / %d = ", a, b);
			printf("%s", record[i].exercise);
			scanf_s("%f", &record[i].studentSolution);
			record[i].solution = a / b;
		}
		else {
			i--;
			continue;
		}
	}
	marking();
}
//计算分数
void marking() {
	float score=0;
	for (int i = 0; i < MAX; i++) {
		if (record[i].solution == record[i].studentSolution) {
			score += 10/MAX;
		}
	}
	evaluation(score);
}
//评价
void evaluation(float score) {
	float sum = score * 10;
	printf("你的总分是:%.2f\n", sum);
	if (sum >= 90 && sum <= 100) {
		printf("你是学霸\n");
	}
	else if (sum >= 80 && sum <= 89) {
		printf("努力一下你就是学霸\n");
	}
	else {
		printf("加油!加油!\n");
	}
}

  至此代码就完成啦,如果还可以有修改的地方,欢迎指出。