用c语言程序设计--个对单项选择题的自动阅卷系统。功能要求1用文件保存试题库。 每个试题包括题干、4个备选答案、标准答案2试题录入可随时增加试题到试题库中3试题抽取:每次从试题库中可以随机抽出N道题N由键盘输入4答题:用户可实现输入自己的答案5自动判卷系统可根据用户答案与标准答案的对比实现判卷并给出成绩6试题删除
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>
#define MAX_QUESTION 1000 //最大试题数 #define MAX_OPTION 4 //最大备选答案数
typedef struct { char question[100]; //题干 char option[MAX_OPTION][50]; //备选答案 int answer; //标准答案 } Question;
Question question_bank[MAX_QUESTION]; //试题库 int question_count = 0; //试题数
void save_question_bank(); void load_question_bank(); void add_question(); void delete_question(); void extract_questions(); void answer_questions(); void judge_answers();
int main() { int choice; load_question_bank(); //程序启动时加载试题库
while (1) {
printf("\n请选择操作:\n");
printf("1. 试题录入\n");
printf("2. 试题删除\n");
printf("3. 试题抽取\n");
printf("4. 答题\n");
printf("5. 退出\n");
printf("请选择操作编号:");
scanf("%d", &choice);
getchar(); //吃掉回车符
switch (choice) {
case 1:
add_question();
break;
case 2:
delete_question();
break;
case 3:
extract_questions();
break;
case 4:
answer_questions();
break;
case 5:
save_question_bank(); //退出程序前保存试题库
return 0;
default:
printf("输入错误,请重新选择操作。\n");
}
}
}
//保存试题库到文件 void save_question_bank() { FILE *fp; int i, j;
fp = fopen("question_bank.txt", "w");
if (fp == NULL) {
printf("文件打开失败。\n");
return;
}
fprintf(fp, "%d\n", question_count); //先写入试题数
for (i = 0; i < question_count; i++) {
fprintf(fp, "%s\n", question_bank[i].question); //写入题干
for (j = 0; j < MAX_OPTION; j++) {
fprintf(fp, "%s\n", question_bank[i].option[j]); //写入备选答案
}
fprintf(fp, "%d\n", question_bank[i].answer); //写入标准答案
}
fclose(fp);
printf("试题库已保存到文件。\n");
}
//从文件中加载试题库 void load_question_bank() { FILE *fp; int i, j;
fp = fopen("question_bank.txt", "r");
if (fp == NULL) {
printf("文件不存在,将创建新的试题库。\n");
return;
}
fscanf(fp, "%d\n", &question_count); //读取试题数
for (i = 0; i < question_count; i++) {
fgets(question_bank[i].question, 100, fp); //读取题干
question_bank[i].question[strlen(question_bank[i].question) - 1] = '\0'; //去掉字符串末尾的换行符
for (j = 0; j < MAX_OPTION; j++) {
fgets(question_bank[i].option[j], 50, fp); //读取备选答案
question_bank[i].option[j][strlen(question_bank[i].option[j]) - 1] = '\0'; //去掉字符串末尾的换行符
}
fscanf(fp, "%d\n", &question_bank[i].answer); //读取标准答案
}
fclose(fp);
printf("试题库已加载。\n");
}
//录入试题 void add_question() { char confirm; int i;
printf("请输入题干:\n");
fgets(question_bank[question_count].question, 100, stdin);
question_bank[question_count].question[strlen(question_bank[question_count].question) - 1] = '\0'; //去掉字符串末尾的换行符
for (i = 0; i < MAX_OPTION; i++) {
printf("请输入备选答案%d:\n", i + 1);
fgets(question_bank[question_count].option[i], 50, stdin);
question_bank[question_count].option[i][strlen(question_bank[question_count].option[i]) - 1] = '\0'; //去掉字符串末尾的换行符
}
printf("请输入标准答案编号:");
scanf("%d", &question_bank[question_count].answer);
getchar(); //吃掉回车符
printf("请确认是否保存该试题?(y/n)");
scanf("%c", &confirm);
getchar(); //吃掉回车符
if (confirm == 'y' || confirm == 'Y') {
question_count++;
printf("试题已保存。\n");
} else {
printf("试题未保存。\n");
}
}
//删除试题 void delete_question() { int i, id;
printf("请输入要删除的试题编号:");
scanf("%d", &id);
getchar(); //吃掉回车符
if (id < 1 || id > question_count) {
printf("试题编号无效。\n");
return;
}
printf("确认要删除以下试题吗?\n");
printf("%s\n", question_bank[id - 1].question);
for (i = 0; i < MAX_OPTION; i++) {
printf("%s\n", question_bank[id - 1].option[i]);
}
printf("标准答案编号:%d\n", question_bank[id - 1].answer);
printf("(y/n)");
if (getchar() == 'y') {
for (i = id - 1; i < question_count - 1; i++) {
memcpy(&question_bank[i], &question_bank[i + 1], sizeof(Question)); //将后面的试题往前移动
}
question_count--;
printf("试题已删除。\n");
} else {
printf("试题未删除。\n");
}
}
//抽取试题 void extract_questions() { int count, i, j; int index[MAX_QUESTION];
if (question_count == 0) {
printf("试题库为空。\n");
return;
}
printf("请输入要抽取的试题数:");
scanf("%d", &count);
if (count > question_count || count < 1) {
printf("试题数无效。\n");
return;
}
//生成不重复的随机数
srand(time(NULL));
for (i = 0; i < count; i++) {
index[i] = rand() % question_count;
for (j = 0; j < i; j++) {
if (index[j] == index[i]) {
i--;
break;
}
}
}
//输出抽取的试题
printf("下面是抽取的试题:\n");
for (i = 0; i < count; i++) {
printf("第%d题:\n", i + 1);
printf("%s\n", question_bank[index[i]].question);
for (j = 0; j < MAX_OPTION; j++) {
printf("%c. %s\n", 'A' + j, question_bank[index[i]].option[j]);
}
}
}
//答题 void answer_questions() { int count, i, answer; int index[MAX_QUESTION];
if (question_count == 0) {
printf("试题库为空。\n");
return;
}
printf("请输入要答题的试题数:");
scanf("%d", &count);
if (count > question_count || count < 1) {
printf("试题数无效。\n");
return;
}
//生成不重复的随机数
srand(time(NULL));
for (i = 0; i < count; i++) {
index[i] = rand() % question_count;
for (int j = 0; j < i; j++) {
if (index[j] == index[i]) {
i--;
break;
}
}
}
//逐个回答试题
for (i = 0; i < count; i++) {
printf("第%d题:\n", i + 1);
printf("%s\n", question_bank[index[i]].question);
for (int j = 0; j < MAX_OPTION; j++) {
printf("%c. %s\n", 'A' + j, question_bank[index[i]].option[j]);
}
printf("请输入答案编号:");
scanf("%d", &answer);
if (answer == question_bank[index[i]].answer) {
printf("答案正确。\n");
} else {
printf("答案错误,正确答案为%c。\n", 'A' + question_bank[index[i]].answer);
}
}
}
//自动判卷 void judge_answers() { // TODO
原文地址: http://www.cveoy.top/t/topic/hjvg 著作权归作者所有。请勿转载和采集!