用·c语言设计·一个程序在磁盘上创建一个test.txt文件,使用记事本打开后先写入40道单选题和相应的选项和正确答案。再编写一个单项选择题标准化考试系统,该系统功能要求: A、用文件保存试题库。(每个试题包括题干、4个备选答案、标准答案)。 B、试题录入、修改和删除。 C、试题抽取:每次从试题库中可以随机抽出N道题(N由键盘输入)。 D、考生答题:用户抽取N道试题进行答题,键盘输入每道题的答案。 E、自动判卷:系统可根据用户答案与标准答案的对比实现判卷并给出成绩。 系统操作过程中能够正确读取和更新试题文
时间: 2023-12-16 19:05:45 浏览: 66
题库,保证数据的有效性和完整性。
以下是一个用C语言设计的程序,实现上述功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_QUESTIONS 100 // 最大试题数量
#define MAX_OPTIONS 4 // 每道题最大备选答案数量
#define MAX_LINE_LENGTH 1024 // 文件中每行最大长度
// 定义试题结构体
struct Question {
char content[MAX_LINE_LENGTH]; // 题干
char options[MAX_OPTIONS][MAX_LINE_LENGTH]; // 备选答案
int answer; // 标准答案
};
// 定义试题库结构体
struct QuestionBank {
int count; // 试题数量
struct Question questions[MAX_QUESTIONS]; // 试题数组
};
// 读取试题库文件,返回试题库结构体
struct QuestionBank read_question_bank() {
struct QuestionBank bank = {0};
FILE *fp = fopen("question_bank.txt", "r");
if (fp == NULL) {
printf("Failed to open question bank file\n");
return bank;
}
char line[MAX_LINE_LENGTH];
while (fgets(line, MAX_LINE_LENGTH, fp)) {
if (bank.count >= MAX_QUESTIONS) {
printf("Question bank is full\n");
break;
}
struct Question *q = &bank.questions[bank.count++];
sscanf(line, "%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%d",
q->content, q->options[0], q->options[1], q->options[2], q->options[3], &q->answer);
}
fclose(fp);
printf("Read %d questions from question bank\n", bank.count);
return bank;
}
// 保存试题库到文件
void save_question_bank(struct QuestionBank *bank) {
FILE *fp = fopen("question_bank.txt", "w");
if (fp == NULL) {
printf("Failed to open question bank file\n");
return;
}
for (int i = 0; i < bank->count; i++) {
struct Question *q = &bank->questions[i];
fprintf(fp, "%s\t%s\t%s\t%s\t%s\t%d\n",
q->content, q->options[0], q->options[1], q->options[2], q->options[3], q->answer);
}
fclose(fp);
printf("Saved %d questions to question bank\n", bank->count);
}
// 录入一个试题
void add_question(struct QuestionBank *bank) {
if (bank->count >= MAX_QUESTIONS) {
printf("Question bank is full\n");
return;
}
struct Question *q = &bank->questions[bank->count++];
printf("Enter question content:\n");
scanf("%s", q->content);
printf("Enter option A:\n");
scanf("%s", q->options[0]);
printf("Enter option B:\n");
scanf("%s", q->options[1]);
printf("Enter option C:\n");
scanf("%s", q->options[2]);
printf("Enter option D:\n");
scanf("%s", q->options[3]);
printf("Enter the number of the correct answer (1-4):\n");
scanf("%d", &q->answer);
save_question_bank(bank);
}
// 修改一个试题
void edit_question(struct QuestionBank *bank) {
printf("Enter the number of the question to edit (1-%d):\n", bank->count);
int index;
scanf("%d", &index);
if (index < 1 || index > bank->count) {
printf("Invalid question number\n");
return;
}
struct Question *q = &bank->questions[index - 1];
printf("Enter new question content (current: %s):\n", q->content);
scanf("%s", q->content);
printf("Enter new option A (current: %s):\n", q->options[0]);
scanf("%s", q->options[0]);
printf("Enter new option B (current: %s):\n", q->options[1]);
scanf("%s", q->options[1]);
printf("Enter new option C (current: %s):\n", q->options[2]);
scanf("%s", q->options[2]);
printf("Enter new option D (current: %s):\n", q->options[3]);
scanf("%s", q->options[3]);
printf("Enter the number of the correct answer (1-4, current: %d):\n", q->answer);
scanf("%d", &q->answer);
save_question_bank(bank);
}
// 删除一个试题
void delete_question(struct QuestionBank *bank) {
printf("Enter the number of the question to delete (1-%d):\n", bank->count);
int index;
scanf("%d", &index);
if (index < 1 || index > bank->count) {
printf("Invalid question number\n");
return;
}
for (int i = index; i < bank->count; i++) {
bank->questions[i - 1] = bank->questions[i];
}
bank->count--;
save_question_bank(bank);
}
// 抽取N道试题
void extract_questions(struct QuestionBank *bank, int n, struct Question *questions) {
if (n > bank->count) {
printf("Not enough questions in the bank\n");
return;
}
// 打乱试题顺序
for (int i = 0; i < bank->count; i++) {
int j = rand() % (i + 1);
struct Question temp = bank->questions[i];
bank->questions[i] = bank->questions[j];
bank->questions[j] = temp;
}
// 复制前n道试题
for (int i = 0; i < n; i++) {
questions[i] = bank->questions[i];
}
}
// 显示一道试题
void show_question(struct Question *q) {
printf("%s\n", q->content);
for (int i = 0; i < MAX_OPTIONS; i++) {
printf("%c. %s\n", 'A' + i, q->options[i]);
}
}
// 显示N道试题,并返回用户的答案数组
int *take_exam(struct Question *questions, int n) {
int *answers = (int *) malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
show_question(&questions[i]);
printf("Enter your answer (A-D):\n");
char answer;
scanf(" %c", &answer);
answers[i] = answer - 'A' + 1;
}
return answers;
}
// 自动判卷,返回分数
int grade_exam(struct Question *questions, int *answers, int n) {
int score = 0;
for (int i = 0; i < n; i++) {
if (answers[i] == questions[i].answer) {
score++;
}
}
return score * 100 / n;
}
int main() {
srand((unsigned int) time(NULL));
struct QuestionBank bank = read_question_bank();
while (1) {
printf("\n1. Add question\n");
printf("2. Edit question\n");
printf("3. Delete question\n");
printf("4. Take exam\n");
printf("5. Exit\n");
int option;
scanf("%d", &option);
switch (option) {
case 1:
add_question(&bank);
break;
case 2:
edit_question(&bank);
break;
case 3:
delete_question(&bank);
break;
case 4:
printf("Enter the number of questions to take:\n");
int n;
scanf("%d", &n);
struct Question questions[n];
extract_questions(&bank, n, questions);
int *answers = take_exam(questions, n);
int score = grade_exam(questions, answers, n);
printf("Your score is %d%%\n", score);
free(answers);
break;
case 5:
return 0;
default:
printf("Invalid option\n");
break;
}
}
}
```
该程序实现了以下功能:
1. 从文件中读取试题库,保存到结构体中。
2. 可以录入、修改和删除试题,并将修改后的试题保存到文件中。
3. 可以从试题库中随机抽取N道试题,在屏幕上显示试题并接受用户答案,自动判卷并给出分数。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)