-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
915 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1 @@ | ||
# 代码解释 | ||
|
||
## 考生功能 | ||
|
||
### 读取题库 | ||
|
||
```c | ||
/** | ||
* 读取指定文件夹下的题库 | ||
*/ | ||
void read_question_bank(char *path) | ||
{ | ||
DIR *dir; | ||
struct dirent *ptr; | ||
dir = opendir(path); | ||
printf("正在读取题库...\n"); | ||
if (readdir(dir) != NULL) | ||
{ | ||
printf("题库列表:\n"); | ||
} | ||
|
||
while ((ptr = readdir(dir)) != NULL) | ||
{ | ||
if (ptr->d_type == 8) | ||
{ | ||
char *filename = ptr->d_name; | ||
char *name = filename; | ||
// 添加颜色 | ||
printf("\033[34m%s\033[0m\n", name); | ||
} | ||
} | ||
closedir(dir); | ||
} | ||
``` | ||
#### 说明 | ||
- 读取指定文件夹下的题库 | ||
- 题库列表以蓝色显示 | ||
- `ptr->d_type == 8` 表示文件类型为文件 | ||
- `\033[34m%s\033[0m` 表示颜色为蓝色 | ||
#### 参数说明 | ||
- `char *path`:题库路径 | ||
### 选择题库 | ||
```c | ||
/** | ||
* 选择题库 | ||
* @param filename 文件名 | ||
*/ | ||
void choose_question_bank(char *filename) | ||
{ | ||
question_bank = load_question_bank(filename); | ||
if (question_bank == NULL) | ||
{ | ||
printf("题库加载失败。\n"); | ||
exit(1); | ||
} | ||
} | ||
``` | ||
|
||
#### 说明 | ||
|
||
- 选择题库 | ||
- 调用 `load_question_bank` 函数加载题库 | ||
- 如果题库加载失败,程序退出 | ||
|
||
#### 参数说明 | ||
|
||
- `char *filename`:题库文件名 | ||
|
||
|
||
### 比较答案 | ||
|
||
```c | ||
/** | ||
* 比较用户答案并给出评分 | ||
* @param question 题目 | ||
* @param user_answer 用户答案 | ||
*/ | ||
void compare_answers_and_score(cJSON *question, const char *user_answer) | ||
{ | ||
const char *correct_answer = cJSON_GetObjectItem(question, "correctAnswer")->valuestring; | ||
char upper_case_answer[strlen(user_answer) + 1]; | ||
|
||
for (int i = 0; i < strlen(user_answer); i++) | ||
{ | ||
if (user_answer[i] >= 'a' && user_answer[i] <= 'z') | ||
{ | ||
upper_case_answer[i] = user_answer[i] - 32; | ||
} | ||
else | ||
{ | ||
upper_case_answer[i] = user_answer[i]; | ||
} | ||
} | ||
upper_case_answer[strlen(user_answer)] = '\0'; | ||
|
||
if (strcmp(correct_answer, upper_case_answer) == 0) | ||
{ | ||
printf("\033[32m回答正确!\033[0m\n\n"); | ||
} | ||
else | ||
{ | ||
printf("\033[31m回答错误!\033[0m 正确答案是:\033[32m%s\033[0m\n\n", correct_answer); | ||
} | ||
} | ||
``` | ||
#### 说明 | ||
- 比较用户答案 | ||
- 将用户答案转换为大写 | ||
- 如果用户答案与正确答案相同,输出“回答正确!” | ||
- 如果用户答案与正确答案不同,输出“回答错误!” | ||
- 如果用户答案与正确答案不同,输出正确答案 | ||
#### 参数说明 | ||
- `cJSON *question`:题目 | ||
- `const char *user_answer`:用户答案 | ||
- `const char *correct_answer`:正确答案 | ||
- `char upper_case_answer[strlen(user_answer) + 1]`:用户答案的大写形式 | ||
- `int i`:循环变量 | ||
- `strlen(user_answer)`:用户答案的长度 | ||
- `strcmp(correct_answer, upper_case_answer)`:比较用户答案和正确答案 | ||
### |
Oops, something went wrong.