Skip to content

Commit

Permalink
chore: 更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
sd0ric4 committed Jul 11, 2024
1 parent 5c04338 commit 83efbd5
Show file tree
Hide file tree
Showing 6 changed files with 915 additions and 133 deletions.
130 changes: 0 additions & 130 deletions Docs/代码解释.md
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)`:比较用户答案和正确答案
###
Loading

0 comments on commit 83efbd5

Please sign in to comment.