用C语言设计单项选择题标准化考试系统设计代码 (1)用文件保存试题库。(每个试题包括题干、4个备选答案、标准答案) (2)试题录入:可随时增加试题到试题库中 (3)试题的修改和删除 (4)试题抽取:每次从试题库中可以随机抽出N道题且不重复(N由键盘输入) (5)答题:用户可实现输入自己的答案 (6)自动判卷:系统可根据用户答案与标准答案的对比实现判卷并给出成绩。
时间: 2024-01-03 07:05:16 浏览: 197
以下是一个简单的单项选择题标准化考试系统设计代码,使用了C语言和文件操作:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_QUESTIONS 1000
#define MAX_QUESTION_LEN 100
#define MAX_ANSWER_LEN 20
struct question {
char prompt[MAX_QUESTION_LEN];
char answer[4][MAX_ANSWER_LEN];
int correct_answer;
};
int num_questions = 0;
struct question questions[MAX_QUESTIONS];
void load_questions() {
FILE *file = fopen("questions.txt", "r");
if (file == NULL) {
printf("Cannot open questions file.\n");
return;
}
while (!feof(file) && num_questions < MAX_QUESTIONS) {
struct question *q = &questions[num_questions];
fgets(q->prompt, MAX_QUESTION_LEN, file);
for (int i = 0; i < 4; i++) {
fgets(q->answer[i], MAX_ANSWER_LEN, file);
}
fscanf(file, "%d\n", &q->correct_answer);
num_questions++;
}
fclose(file);
}
void save_questions() {
FILE *file = fopen("questions.txt", "w");
if (file == NULL) {
printf("Cannot open questions file.\n");
return;
}
for (int i = 0; i < num_questions; i++) {
struct question *q = &questions[i];
fprintf(file, "%s", q->prompt);
for (int j = 0; j < 4; j++) {
fprintf(file, "%s", q->answer[j]);
}
fprintf(file, "%d\n", q->correct_answer);
}
fclose(file);
}
void add_question() {
if (num_questions >= MAX_QUESTIONS) {
printf("Cannot add more questions.\n");
return;
}
struct question *q = &questions[num_questions];
printf("Enter question prompt:\n");
fgets(q->prompt, MAX_QUESTION_LEN, stdin);
for (int i = 0; i < 4; i++) {
printf("Enter answer %d:\n", i + 1);
fgets(q->answer[i], MAX_ANSWER_LEN, stdin);
}
printf("Enter correct answer (1-4):\n");
scanf("%d", &q->correct_answer);
getchar(); // consume newline
num_questions++;
}
void modify_question() {
printf("Enter question number to modify (1-%d):\n", num_questions);
int n;
scanf("%d", &n);
if (n < 1 || n > num_questions) {
printf("Invalid question number.\n");
return;
}
struct question *q = &questions[n - 1];
printf("Current prompt: %s", q->prompt);
printf("Enter new prompt (or press enter to keep current):\n");
fgets(q->prompt, MAX_QUESTION_LEN, stdin);
for (int i = 0; i < 4; i++) {
printf("Current answer %d: %s", i + 1, q->answer[i]);
printf("Enter new answer %d (or press enter to keep current):\n", i + 1);
fgets(q->answer[i], MAX_ANSWER_LEN, stdin);
}
printf("Current correct answer: %d\n", q->correct_answer);
printf("Enter new correct answer (1-4, or 0 to keep current):\n");
int new_correct_answer;
scanf("%d", &new_correct_answer);
if (new_correct_answer >= 1 && new_correct_answer <= 4) {
q->correct_answer = new_correct_answer;
}
getchar(); // consume newline
}
void delete_question() {
printf("Enter question number to delete (1-%d):\n", num_questions);
int n;
scanf("%d", &n);
if (n < 1 || n > num_questions) {
printf("Invalid question number.\n");
return;
}
for (int i = n - 1; i < num_questions - 1; i++) {
questions[i] = questions[i + 1];
}
num_questions--;
}
void shuffle_questions() {
srand(time(NULL));
for (int i = 0; i < num_questions; i++) {
int j = rand() % num_questions;
struct question temp = questions[i];
questions[i] = questions[j];
questions[j] = temp;
}
}
void take_test() {
int num_questions_to_ask;
printf("Enter number of questions to ask:\n");
scanf("%d", &num_questions_to_ask);
if (num_questions_to_ask < 1 || num_questions_to_ask > num_questions) {
printf("Invalid number of questions.\n");
return;
}
shuffle_questions();
int score = 0;
for (int i = 0; i < num_questions_to_ask; i++) {
struct question *q = &questions[i];
printf("%d. %s", i + 1, q->prompt);
for (int j = 0; j < 4; j++) {
printf("%d. %s", j + 1, q->answer[j]);
}
int answer;
scanf("%d", &answer);
if (answer == q->correct_answer) {
score++;
}
getchar(); // consume newline
}
printf("You got %d out of %d questions correct.\n", score, num_questions_to_ask);
}
int main() {
load_questions();
while (1) {
printf("Enter a command:\n");
printf("1. Add question\n");
printf("2. Modify question\n");
printf("3. Delete question\n");
printf("4. Take test\n");
printf("5. Quit\n");
int choice;
scanf("%d", &choice);
getchar(); // consume newline
switch (choice) {
case 1:
add_question();
save_questions();
break;
case 2:
modify_question();
save_questions();
break;
case 3:
delete_question();
save_questions();
break;
case 4:
take_test();
break;
case 5:
return 0;
default:
printf("Invalid command.\n");
}
}
}
```
代码中使用了一个结构体 `question` 来保存每个试题的题干、备选答案和标准答案,以及一个数组 `questions` 来保存所有试题。在程序启动时,从文件中加载试题库;在程序运行时,用户可以添加、修改、删除试题,或者进行答题,程序会自动判卷并给出成绩。在抽取试题时,程序会随机打乱试题库的顺序,然后从中选择指定数量的试题。
阅读全文