@Adrian
我是魔鬼,来折磨小学生了,这又你做不完的题,不要哭,先来个1000题,静静心。
给祖国的花朵施施肥
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(void)
{
int count = 100;
FILE * quiz_file;
FILE * result_file;
quiz_file = fopen("D:\\class/exam.txt", "w");
result_file = fopen("D:\\class/result.txt", "w");
if (quiz_file == NULL || result_file == NULL)
{
puts("Error,couldn't open files!");
getchar();
getchar();
return 1;
}
while (count > 0)
{
char str[20];
int first, second;
char str2[3], str4[3];
int result = 0;
first = rand() % 20 +1;
second = rand() % 20 + 1;
sprintf(str, "%d", first);
sprintf(str2, "%d", second);
switch (rand() % 2)
{
case 0:
strcat(str, "+");
strcat(str, str2);
result = first + second;
if (result < 0 || result > 20)
{
continue;
}
break;
case 1:
strcat(str, "-");
strcat(str, str2);
result = first - second;
if (result < 0 || result > 20)
{
continue;
}
break;
default:
break;
}
strcat(str, "=");
sprintf(str4, "%d", result);
fputs(str, quiz_file);
fputc('\n', quiz_file);
strcat(str, str4);
fputs(str, result_file);
fputc('\n', result_file);
count--;
}
fclose(quiz_file);
fclose(result_file);
return 0;
}
操作步骤:
- 复制代码到编译器
- 根据代码中指定的路径创建文件夹(例如:
在D:盘中创建class文件夹) - 执行代码
- 在class文件夹下生成:exam.txt(试题)和result.txt(有答案的试题)