问题描述:《 要求实现的是一一个供 2年级小学生进行数学测验用的测试软件,并能够给出测试结果。每道题 目只能是两个1100之间的运算数进行加、减、乘或除中的某种运算。 一 功能要求: J 1)功能菜单包括(1) 输入学生信息: (2) 开始测试: (3) 查看测验结果;← 2)输入学生信息里要求用户在测试前首先输入姓名和年龄: 3)开始测试时,任意生成10道数学题: 4)每次随机给出一一道题后,提示用户输入其计算结果,当用户输入完成后给出下一道题目并提示输入,如此下去;← 5)当用户完成10道题后,按每题10分的标准输出用户最后得分,同时把所有题目、标准答案及用户答案显示出来;← 6)将学生的姓名和年龄,以及上述输出的内容全部保存成文件。← 7)采用菜单界面,文本或图形方式均可: 4 )学生可自行添加新功能模块,视情况额外加分。4 9)要求能通过文件保存信息,程序支持信息导入和保存<
时间: 2024-02-19 18:01:33 浏览: 17
好的,以下是一个基本的实现,您可以根据需要进行修改和完善:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 定义题目数量
#define QUESTION_NUM 10
// 定义题目类型
typedef enum {
ADDITION, // 加法
SUBTRACTION, // 减法
MULTIPLICATION, // 乘法
DIVISION, // 除法
} QuestionType;
// 学生信息结构体
typedef struct {
char name[20];
int age;
} Student;
// 题目结构体
typedef struct {
int num1;
int num2;
QuestionType type;
int answer;
int correct;
} Question;
// 生成随机数
int rand_int(int min, int max) {
return rand() % (max - min + 1) + min;
}
// 生成随机题目
Question generate_question() {
Question question;
question.num1 = rand_int(100, 1100);
question.num2 = rand_int(100, 1100);
int type_num = rand_int(0, 3);
switch (type_num) {
case 0:
question.type = ADDITION;
question.answer = question.num1 + question.num2;
break;
case 1:
question.type = SUBTRACTION;
question.answer = question.num1 - question.num2;
break;
case 2:
question.type = MULTIPLICATION;
question.answer = question.num1 * question.num2;
break;
case 3:
question.type = DIVISION;
question.answer = question.num1 / question.num2;
break;
default:
break;
}
question.correct = 0;
return question;
}
// 显示题目
void show_question(Question question) {
printf("请计算 %d", question.num1);
switch (question.type) {
case ADDITION:
printf(" + %d = ", question.num2);
break;
case SUBTRACTION:
printf(" - %d = ", question.num2);
break;
case MULTIPLICATION:
printf(" * %d = ", question.num2);
break;
case DIVISION:
printf(" / %d = ", question.num2);
break;
default:
break;
}
}
// 保存学生信息和测试结果
void save_result(Student student, Question *questions, int score) {
FILE *fp;
fp = fopen("result.txt", "w");
fprintf(fp, "学生姓名:%s\n", student.name);
fprintf(fp, "学生年龄:%d\n", student.age);
fprintf(fp, "得分:%d\n", score);
for (int i = 0; i < QUESTION_NUM; i++) {
fprintf(fp, "第%d题:", i + 1);
fprintf(fp, "%d", questions[i].num1);
switch (questions[i].type) {
case ADDITION:
fprintf(fp, " + %d", questions[i].num2);
break;
case SUBTRACTION:
fprintf(fp, " - %d", questions[i].num2);
break;
case MULTIPLICATION:
fprintf(fp, " * %d", questions[i].num2);
break;
case DIVISION:
fprintf(fp, " / %d", questions[i].num2);
break;
default:
break;
}
fprintf(fp, " = %d,", questions[i].answer);
fprintf(fp, "回答:%d,", questions[i].correct);
if (questions[i].correct == 1) {
fprintf(fp, "正确\n");
} else {
fprintf(fp, "错误\n");
}
}
fclose(fp);
}
// 主函数
int main() {
// 初始化随机数种子
srand(time(NULL));
Student student;
int choice;
int score = 0;
Question questions[QUESTION_NUM];
// 输入学生信息
printf("请输入学生姓名:");
scanf("%s", student.name);
printf("请输入学生年龄:");
scanf("%d", &student.age);
while (1) {
// 显示菜单
printf("\n请选择功能:\n");
printf("1. 输入学生信息\n");
printf("2. 开始测试\n");
printf("3. 查看测验结果\n");
printf("4. 退出程序\n");
printf("请输入数字选择功能:");
scanf("%d", &choice);
switch (choice) {
case 1: // 输入学生信息
printf("请输入学生姓名:");
scanf("%s", student.name);
printf("请输入学生年龄:");
scanf("%d", &student.age);
break;
case 2: // 开始测试
// 生成题目
for (int i = 0; i < QUESTION_NUM; i++) {
questions[i] = generate_question();
}
// 提示输入答案
for (int i = 0; i < QUESTION_NUM; i++) {
show_question(questions[i]);
int answer;
scanf("%d", &answer);
if (answer == questions[i].answer) {
printf("回答正确!\n");
questions[i].correct = 1;
score += 10;
} else {
printf("回答错误,正确答案是 %d!\n", questions[i].answer);
}
}
// 显示得分
printf("测试结束,您的得分是:%d\n", score);
// 保存结果
save_result(student, questions, score);
// 重置得分
score = 0;
break;
case 3: // 查看测验结果
// 读取文件并显示
FILE *fp;
char ch;
fp = fopen("result.txt", "r");
if (fp == NULL) {
printf("无法打开文件!\n");
} else {
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
fclose(fp);
}
break;
case 4: // 退出程序
printf("程序已退出。\n");
return 0;
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
return 0;
}
```
注:此代码只是一个简单的实现,还有很多地方需要完善和优化,如输入校验、错误处理、界面美化等。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)