用c语言编写一个完整的程序解决下面的问题 问题描述:将单项选择题考试的题目和标准答案制作成数据文件,在考试人选择答题后显示题目,在答题人答题完毕后自动与标准答案比对并给出评判结果。实现功能:1、有良好的人机交互功能提示用户下一步的操作。 2、在用户答题完毕后自动显示下一道题目 3、答题完毕后显示答题的总信息和标准答案的比对及评分结果 4、信息都需要保存在文件中
时间: 2023-03-31 12:01:15 浏览: 174
以下是一个用 C 语言编写的程序,可以解决上述问题:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_QUESTIONS 100 // 最大题目数量
#define MAX_OPTIONS 4 // 每个题目的最大选项数量
#define MAX_ANSWER_LENGTH 10 // 每个答案的最大长度
// 题目结构体
typedef struct {
char question[100]; // 题目
char options[MAX_OPTIONS][100]; // 选项
int correct_option; // 正确选项
} Question;
// 答题记录结构体
typedef struct {
int question_number; // 题目编号
int selected_option; // 选择的选项
int is_correct; // 是否正确
} AnswerRecord;
// 读取题目和答案文件
void read_questions_and_answers(char *filename, Question *questions, char **answers, int *num_questions) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("无法打开文件 %s\n", filename);
exit(1);
}
char line[100];
int question_index = ;
while (fgets(line, sizeof(line), fp) != NULL) {
// 读取题目
strcpy(questions[question_index].question, line);
// 读取选项
for (int i = ; i < MAX_OPTIONS; i++) {
fgets(line, sizeof(line), fp);
strcpy(questions[question_index].options[i], line);
}
// 读取正确选项
fgets(line, sizeof(line), fp);
questions[question_index].correct_option = atoi(line);
// 读取答案
fgets(line, sizeof(line), fp);
answers[question_index] = strdup(line);
// 去掉答案末尾的换行符
answers[question_index][strlen(answers[question_index]) - 1] = '\';
question_index++;
}
*num_questions = question_index;
fclose(fp);
}
// 显示题目
void display_question(Question *question) {
printf("%s", question->question);
for (int i = ; i < MAX_OPTIONS; i++) {
printf("%d. %s", i + 1, question->options[i]);
}
}
// 显示答题结果
void display_result(AnswerRecord *records, char **answers, int num_questions) {
int num_correct = ;
for (int i = ; i < num_questions; i++) {
printf("第%d题:", records[i].question_number + 1);
if (records[i].is_correct) {
printf("正确\n");
num_correct++;
} else {
printf("错误,正确答案是:%s\n", answers[records[i].question_number]);
}
}
printf("答题结束,共%d题,%d题正确,得分:%d分\n", num_questions, num_correct, num_correct * 100 / num_questions);
}
int main() {
Question questions[MAX_QUESTIONS];
char *answers[MAX_QUESTIONS];
int num_questions;
read_questions_and_answers("questions.txt", questions, answers, &num_questions);
AnswerRecord records[MAX_QUESTIONS];
int current_question = ;
while (current_question < num_questions) {
display_question(&questions[current_question]);
int selected_option;
printf("请选择答案(1-%d):", MAX_OPTIONS);
scanf("%d", &selected_option);
records[current_question].question_number = current_question;
records[current_question].selected_option = selected_option;
records[current_question].is_correct = (selected_option == questions[current_question].correct_option);
current_question++;
}
display_result(records, answers, num_questions);
// 保存答题记录到文件
FILE *fp = fopen("records.txt", "w");
if (fp == NULL) {
printf("无法打开文件 records.txt\n");
exit(1);
}
for (int i = ; i < num_questions; i++) {
fprintf(fp, "%d,%d,%d\n", records[i].question_number, records[i].selected_option, records[i].is_correct);
}
fclose(fp);
return ;
}
阅读全文