From 34cde60f4d30c12a7ee5841c8f854452d641d78a Mon Sep 17 00:00:00 2001 From: luxin Date: Fri, 22 Oct 2021 18:21:36 +0800 Subject: [PATCH] add 7 exercises --- .../config.json" | 16 +- .../structures.c" | 27 ++ .../structures.json" | 6 + .../structures.md" | 111 +++++++ .../config.json" | 4 +- .../structures_array.c" | 35 +++ .../structures_array.json" | 6 + .../structures_array.md" | 154 ++++++++++ .../config.json" | 16 +- .../structures_pointers.c" | 23 ++ .../structures_pointers.json" | 6 + .../structures_pointers.md" | 115 ++++++++ .../config.json" | 2 +- .../structures_n_func.c" | 40 +++ .../structures_n_func.json" | 6 + .../structures_n_func.md" | 180 ++++++++++++ .../config.json" | 16 +- .../linked_list.c" | 63 ++++ .../linked_list.json" | 6 + .../linked_list.md" | 272 ++++++++++++++++++ .../config.json" | 9 - .../config.json" | 11 + .../unions.c" | 42 +++ .../unions.json" | 6 + .../unions.md" | 184 ++++++++++++ .../config.json" | 11 + .../enum.c" | 36 +++ .../enum.json" | 6 + .../enum.md" | 166 +++++++++++ .../config.json" | 9 + 30 files changed, 1552 insertions(+), 32 deletions(-) create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/structures.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/structures.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/structures.md" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/structures_array.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/structures_array.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/structures_array.md" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/structures_pointers.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/structures_pointers.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/structures_pointers.md" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/structures_n_func.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/structures_n_func.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/structures_n_func.md" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/linked_list.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/linked_list.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/linked_list.md" delete mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223/config.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/config.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/unions.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/unions.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/unions.md" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/config.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/enum.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/enum.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/enum.md" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/config.json" diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/config.json" index 304857b..e12534c 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/config.json" @@ -1,9 +1,11 @@ { - "node_id": "569d5e11c4fc5de7844053d9a733c5e8", - "keywords": [ - "结构体", - "C语言" - ], - "children": [], - "export": [] + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "结构体", + "C语言" + ], + "children": [], + "export": [ + "structures.json" + ] } \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/structures.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/structures.c" new file mode 100644 index 0000000..f1c0145 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/structures.c" @@ -0,0 +1,27 @@ +#include + +struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} + +int main() +{ + struct Student stu; + stu.name = "张三"; + stu.id = 1001; + stu.age = 16; + stu.group = 'A'; + stu.score = 95.50; + + printf("========== 学生基本信息 ==========\n"); + printf("姓名:%s\n学号:%d\n年龄:%d\n所在小组:%c\n成绩:%.2f\n", + stu.name, stu.id, stu.age, stu.group, stu.score); + printf("==================================\n"); + + return 0; +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/structures.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/structures.json" new file mode 100644 index 0000000..5d07fd4 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/structures.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "卢昕", + "source": "structures.md", + "exercise_id":"51df13a16d96417bb5dbe6476c1ec05f" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/structures.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/structures.md" new file mode 100644 index 0000000..b5cdcba --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/1.\347\273\223\346\236\204\344\275\223\347\256\200\344\273\213/structures.md" @@ -0,0 +1,111 @@ +# 结构体的定义与使用 + +定义一个学生结构体,并实例化一个学生对象,保存一个学生的基本信息,最后输出该学生信息。请选出错误答案。 + +## 答案 +```c +#include + +struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} + +int main() +{ + struct Student stu; + stu.name = "张三"; + stu.id = 1001; + stu.age = 16; + stu.group = 'A'; + stu.score = 95.50; + + printf("========== 学生基本信息 ==========\n"); + printf("姓名:%s\n学号:%d\n年龄:%d\n所在小组:%c\n成绩:%.2f\n", + stu.name, stu.id, stu.age, stu.group, stu.score); + printf("==================================\n"); + + return 0; +} +``` + +## 选项 +### A +```c +#include + +struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; +}; + +int main() +{ + struct Student stu = {"张三", 1001, 16, 'A', 95.50}; + + printf("========== 学生基本信息 ==========\n"); + printf("姓名:%s\n学号:%d\n年龄:%d\n所在小组:%c\n成绩:%.2f\n", + stu.name, stu.id, stu.age, stu.group, stu.score); + printf("==================================\n"); + + return 0; +} +``` + +### B +```c +#include + +struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} stu = {"张三", 1001, 16, 'A', 95.50}; + +int main() +{ + printf("========== 学生基本信息 ==========\n"); + printf("姓名:%s\n学号:%d\n年龄:%d\n所在小组:%c\n成绩:%.2f\n", + stu.name, stu.id, stu.age, stu.group, stu.score); + printf("==================================\n"); + + return 0; +} +``` + +### C +```c +#include + +typedef struct +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} Student; + +int main() +{ + Student stu = {"张三", 1001, 16, 'A', 95.50}; + + printf("========== 学生基本信息 ==========\n"); + printf("姓名:%s\n学号:%d\n年龄:%d\n所在小组:%c\n成绩:%.2f\n", + stu.name, stu.id, stu.age, stu.group, stu.score); + printf("==================================\n"); + + return 0; +} +``` diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/config.json" index 304857b..a5a73f2 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/config.json" @@ -5,5 +5,7 @@ "C语言" ], "children": [], - "export": [] + "export": [ + "structures_array.json" + ] } \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/structures_array.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/structures_array.c" new file mode 100644 index 0000000..e3777fd --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/structures_array.c" @@ -0,0 +1,35 @@ +#include +#define NUM_STR 3 + +struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} cls[NUM_STR]; + +int main() +{ + cls = { + {"张三", 1001, 16, 'A', 95.50}, + {"李四", 1002, 15, 'A', 90.00}, + {"王五", 1003, 16, 'B', 80.50} + }; + + float total = 0, average = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (int i = 0; i < NUM_STR; ++i) { + printf("%s\t%d\t%d\t%c\t%.2f\n", + cls[i].name, cls[i].id, cls[i].age, cls[i].group, cls[i].score); + total += cls[i].score; + } + printf("============================================\n"); + average = total / NUM_STR; + printf("班级平均成绩:%.2f", average); + + return 0; +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/structures_array.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/structures_array.json" new file mode 100644 index 0000000..f21355f --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/structures_array.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "卢昕", + "source": "structures_array.md", + "exercise_id":"ce458884b396456d8b9993004272720e" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/structures_array.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/structures_array.md" new file mode 100644 index 0000000..a546b6b --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/2.\347\273\223\346\236\204\344\275\223\346\225\260\347\273\204/structures_array.md" @@ -0,0 +1,154 @@ +# 结构体数组 + +定义一个学生结构体,并实例化一个结构体数组,用于保存一个班级所有学生的基本信息,最后输出所有学生基本信息,以及班级的学生平均成绩。请选出错误答案。 + +## 答案 +```c +#include +#define NUM_STR 3 + +struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} cls[NUM_STR]; + +int main() +{ + cls = { + {"张三", 1001, 16, 'A', 95.50}, + {"李四", 1002, 15, 'A', 90.00}, + {"王五", 1003, 16, 'B', 80.50} + }; + + float total = 0, average = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (int i = 0; i < NUM_STR; ++i) { + printf("%s\t%d\t%d\t%c\t%.2f\n", + cls[i].name, cls[i].id, cls[i].age, cls[i].group, cls[i].score); + total += cls[i].score; + } + printf("============================================\n"); + average = total / NUM_STR; + printf("班级平均成绩:%.2f", average); + + return 0; +} +``` + +## 选项 +### A +```c +#include + +struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} cls[] = { + {"张三", 1001, 16, 'A', 95.50}, + {"李四", 1002, 15, 'A', 90.00}, + {"王五", 1003, 16, 'B', 80.50} +}; + +int main() +{ + size_t i, num_stu = sizeof(cls) / sizeof(struct Student); + float total = 0, average = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_stu; ++i) { + printf("%s\t%d\t%d\t%c\t%.2f\n", + cls[i].name, cls[i].id, cls[i].age, cls[i].group, cls[i].score); + total += cls[i].score; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); + + return 0; +} +``` + +### B +```c +#include + +struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} cls[] = { + "张三", 1001, 16, 'A', 95.50, + "李四", 1002, 15, 'A', 90.00, + "王五", 1003, 16, 'B', 80.50 +}; + +int main() +{ + size_t i, num_stu = sizeof(cls) / sizeof(struct Student); + float total = 0, average = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_stu; ++i) { + printf("%s\t%d\t%d\t%c\t%.2f\n", + cls[i].name, cls[i].id, cls[i].age, cls[i].group, cls[i].score); + total += cls[i].score; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); + + return 0; +} +``` + +### C +```c +#include + +struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} cls[] = { + {.name = "张三", .group = 'A', .age = 16, .score = 95.50, .id = 1001}, + {.age = 15, .score = 90.00, .group = 'A', .id = 1002, .name = "李四"}, + {.group = 'B', .age = 16, .name = "王五", .score = 80.50, .id = 1003} +}; + +int main() +{ + size_t i, num_stu = sizeof(cls) / sizeof(struct Student); + float total = 0, average = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_stu; ++i) { + printf("%s\t%d\t%d\t%c\t%.2f\n", + cls[i].name, cls[i].id, cls[i].age, cls[i].group, cls[i].score); + total += cls[i].score; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); + + return 0; +} +``` diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/config.json" index 304857b..3eb7bbd 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/config.json" @@ -1,9 +1,11 @@ { - "node_id": "569d5e11c4fc5de7844053d9a733c5e8", - "keywords": [ - "结构体", - "C语言" - ], - "children": [], - "export": [] + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "结构体", + "C语言" + ], + "children": [], + "export": [ + "structures_pointers.json" + ] } \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/structures_pointers.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/structures_pointers.c" new file mode 100644 index 0000000..278c18d --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/structures_pointers.c" @@ -0,0 +1,23 @@ +#include + +typedef struct +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} Student; + +int main() +{ + Student stu = {"张三", 1001, 16, 'A', 95.50}; + Student *stu_prt = &stu; + + printf("========== 学生基本信息 ==========\n"); + printf("姓名:%s\n学号:%d\n年龄:%d\n所在小组:%c\n成绩:%.2f\n", + *stu_prt.name, *stu_prt.id, *stu_prt.age, *stu_prt.group, *stu_prt.score); + printf("==================================\n"); + + return 0; +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/structures_pointers.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/structures_pointers.json" new file mode 100644 index 0000000..0f97398 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/structures_pointers.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "卢昕", + "source": "structures_pointers.md", + "exercise_id":"5fe2bafbe9974e62bf3544e50aefa3cc" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/structures_pointers.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/structures_pointers.md" new file mode 100644 index 0000000..8520059 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/3.\347\273\223\346\236\204\344\275\223\346\214\207\351\222\210/structures_pointers.md" @@ -0,0 +1,115 @@ +# 结构体指针 + +定义一个学生结构体,并实例化一个学生对象,保存一个学生的基本信息,最后使用结构体指针访问和输出该学生信息。请选出错误答案。 + + +## 答案 +```c +#include + +typedef struct +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} Student; + +int main() +{ + Student stu = {"张三", 1001, 16, 'A', 95.50}; + Student *stu_prt = &stu; + + printf("========== 学生基本信息 ==========\n"); + printf("姓名:%s\n学号:%d\n年龄:%d\n所在小组:%c\n成绩:%.2f\n", + *stu_prt.name, *stu_prt.id, *stu_prt.age, *stu_prt.group, *stu_prt.score); + printf("==================================\n"); + + return 0; +} +``` + +## 选项 +### A +```c +#include + +typedef struct +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} Student; + +int main() +{ + Student stu = {"张三", 1001, 16, 'A', 95.50}; + Student *stu_prt = &stu; + + printf("========== 学生基本信息 ==========\n"); + printf("姓名:%s\n学号:%d\n年龄:%d\n所在小组:%c\n成绩:%.2f\n", + stu_prt->name, stu_prt->id, stu_prt->age, stu_prt->group, stu_prt->score); + printf("==================================\n"); + + return 0; +} +``` + +### B +```c +#include + +struct +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} stu = {"张三", 1001, 16, 'A', 95.50}, *stu_prt = &stu; + +int main() +{ + printf("========== 学生基本信息 ==========\n"); + printf("姓名:%s\n学号:%d\n年龄:%d\n所在小组:%c\n成绩:%.2f\n", + stu_prt->name, stu_prt->id, stu_prt->age, stu_prt->group, stu_prt->score); + printf("==================================\n"); + + return 0; +} +``` + +### C +```c +#include + +typedef struct +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} Student; + +int main() +{ + Student stu; + Student *stu_prt = &stu; + stu_prt->name = "张三"; + stu_prt->id = 1001; + stu_prt->age = 16; + stu_prt->group = 'A'; + stu_prt->score = 95.50; + + printf("========== 学生基本信息 ==========\n"); + printf("姓名:%s\n学号:%d\n年龄:%d\n所在小组:%c\n成绩:%.2f\n", + stu_prt->name, stu_prt->id, stu_prt->age, stu_prt->group, stu_prt->score); + printf("==================================\n"); + + return 0; +} +``` diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/config.json" index 304857b..21eb34d 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/config.json" @@ -5,5 +5,5 @@ "C语言" ], "children": [], - "export": [] + "export": ["structures_n_func.json"] } \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/structures_n_func.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/structures_n_func.c" new file mode 100644 index 0000000..b971a27 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/structures_n_func.c" @@ -0,0 +1,40 @@ +#include + +typedef struct +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} Student; + +void print_students(Student stu) +{ + printf("%s\t%d\t%d\t%c\t%.2f\n", + stu.name, stu.id, stu.age, stu.group, stu.score); +} + +int main() +{ + Student cls[] = { + {"张三", 1001, 16, 'A', 95.50}, + {"李四", 1002, 15, 'A', 90.00}, + {"王五", 1003, 16, 'B', 80.50} + }; + + size_t i, num_stu = sizeof(cls) / sizeof(Student); + float total = 0, average = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_stu; ++i) { + print_students(cls[i]); + total += cls[i].score; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); + + return 0; +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/structures_n_func.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/structures_n_func.json" new file mode 100644 index 0000000..7af7acb --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/structures_n_func.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "卢昕", + "source": "structures_n_func.md", + "exercise_id":"d05a0cfc3bd6483b9de9d0c97cc0ace8" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/structures_n_func.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/structures_n_func.md" new file mode 100644 index 0000000..3984a94 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/4.\347\273\223\346\236\204\344\275\223\344\270\216\345\207\275\346\225\260/structures_n_func.md" @@ -0,0 +1,180 @@ +# 结构体与函数 + +定义一个学生结构体,并实例化一个结构体数组,用于保存一个班级所有学生的基本信息,最后输出所有学生基本信息,以及班级的学生平均成绩。请选出正确答案。 + +## 答案 +```c +#include + +typedef struct +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} Student; + +void print_students(Student stu) +{ + printf("%s\t%d\t%d\t%c\t%.2f\n", + stu.name, stu.id, stu.age, stu.group, stu.score); +} + +int main() +{ + Student cls[] = { + {"张三", 1001, 16, 'A', 95.50}, + {"李四", 1002, 15, 'A', 90.00}, + {"王五", 1003, 16, 'B', 80.50} + }; + + size_t i, num_stu = sizeof(cls) / sizeof(Student); + float total = 0, average = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_stu; ++i) { + print_students(cls[i]); + total += cls[i].score; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); + + return 0; +} +``` + +## 选项 +### A +```c +#include + +typedef struct +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} Student; + +void print_students(Student *stu) +{ + printf("%s\t%d\t%d\t%c\t%.2f\n", + stu->name, stu->id, stu->age, stu->group, stu->score); +} + +int main() +{ + Student cls[] = { + {"张三", 1001, 16, 'A', 95.50}, + {"李四", 1002, 15, 'A', 90.00}, + {"王五", 1003, 16, 'B', 80.50} + }; + + size_t i, num_stu = sizeof(cls) / sizeof(Student); + float total = 0, average = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_stu; ++i) { + print_students(cls[i]); + total += cls[i].score; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); + + return 0; +} +``` + +### B +```c +#include + +typedef struct +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} Student; + +void print_students(Student *stu) +{ + printf("%s\t%d\t%d\t%c\t%.2f\n", + *stu.name, *stu.id, *stu.age, *stu.group, *stu.score); +} + +int main() +{ + Student cls[] = { + {"张三", 1001, 16, 'A', 95.50}, + {"李四", 1002, 15, 'A', 90.00}, + {"王五", 1003, 16, 'B', 80.50} + }; + + size_t i, num_stu = sizeof(cls) / sizeof(Student); + float total = 0, average = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_stu; ++i) { + print_students(&cls[i]); + total += cls[i].score; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); + + return 0; +} +``` + +### C +```c +#include + +typedef struct +{ + char *name; + int id; + unsigned int age; + char group; + float score; +} Student; + +void print_students(Student stu) +{ + printf("%s\t%d\t%d\t%c\t%.2f\n", + stu->name, stu->id, stu->age, stu->group, stu->score); +} + +int main() +{ + Student cls[] = { + {"张三", 1001, 16, 'A', 95.50}, + {"李四", 1002, 15, 'A', 90.00}, + {"王五", 1003, 16, 'B', 80.50} + }; + + size_t i, num_stu = sizeof(cls) / sizeof(Student); + float total = 0, average = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_stu; ++i) { + print_students(cls[i]); + total += cls[i].score; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); + + return 0; +} +``` diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/config.json" index 304857b..44d7cc1 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/config.json" @@ -1,9 +1,11 @@ { - "node_id": "569d5e11c4fc5de7844053d9a733c5e8", - "keywords": [ - "结构体", - "C语言" - ], - "children": [], - "export": [] + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "结构体", + "C语言" + ], + "children": [], + "export": [ + "linked_list.json" + ] } \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/linked_list.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/linked_list.c" new file mode 100644 index 0000000..72a8e57 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/linked_list.c" @@ -0,0 +1,63 @@ +#include +#include + +typedef struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; + struct Student *next; +} Student, *Class; + +Class init_class() +{ + Class head; + head = (Student *)malloc(sizeof(Student)); + if (!head) + return NULL; + head->next = NULL; + return head; +} + +void insert_student(Class cls, char *name, int id, unsigned int age, char group, float score) +{ + Class p; + p = (Student *)malloc(sizeof(Student)); + p->name = name, p->id = id, p->age = age, p->group = group, p->score = score; + p->next = cls->next; + cls->next = p; +} + +void print_class(const Class cls) +{ + Class prt = NULL; + float total = 0, average = 0; + int num_stu = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (prt = cls->next; prt; prt = prt->next) + { + printf("%s\t%d\t%d\t%c\t%.2f\n", + prt->name, prt->id, prt->age, prt->group, prt->score); + total += prt->score; + ++num_stu; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); +} + +int main() +{ + Class cls = init_class(); + insert_student(cls, "王五", 1003, 16, 'B', 80.50); + insert_student(cls, "李四", 1002, 15, 'A', 90.00); + insert_student(cls, "张三", 1001, 16, 'A', 95.50); + + print_class(cls); + + return 0; +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/linked_list.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/linked_list.json" new file mode 100644 index 0000000..f6f5fdb --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/linked_list.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "卢昕", + "source": "linked_list.md", + "exercise_id":"af79a964ebfa4116a79cd623357bee1d" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/linked_list.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/linked_list.md" new file mode 100644 index 0000000..238a560 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/1.\347\273\223\346\236\204\344\275\223/5.\351\223\276\345\274\217\347\273\223\346\236\204/linked_list.md" @@ -0,0 +1,272 @@ +# 链式结构 + +定义一个学生结构体,并创建一个链表用于保存一个班级所有学生的基本信息,最后输出所有学生基本信息,以及班级的学生平均成绩。请选出正确答案。 + +## 答案 +```c +#include +#include + +typedef struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; + struct Student *next; +} Student, *Class; + +Class init_class() +{ + Class head; + head = (Student *)malloc(sizeof(Student)); + if (!head) + return NULL; + head->next = NULL; + return head; +} + +void insert_student(Class cls, char *name, int id, unsigned int age, char group, float score) +{ + Class p; + p = (Student *)malloc(sizeof(Student)); + p->name = name, p->id = id, p->age = age, p->group = group, p->score = score; + p->next = cls->next; + cls->next = p; +} + +void print_class(const Class cls) +{ + Class prt = NULL; + float total = 0, average = 0; + int num_stu = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (prt = cls->next; prt; prt = prt->next) + { + printf("%s\t%d\t%d\t%c\t%.2f\n", + prt->name, prt->id, prt->age, prt->group, prt->score); + total += prt->score; + ++num_stu; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); +} + +int main() +{ + Class cls = init_class(); + insert_student(cls, "王五", 1003, 16, 'B', 80.50); + insert_student(cls, "李四", 1002, 15, 'A', 90.00); + insert_student(cls, "张三", 1001, 16, 'A', 95.50); + + print_class(cls); + + return 0; +} +``` + +## 选项 +### A +```c +#include +#include + +typedef struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; + struct Student *next; +} Student, *Class; + +Class init_class() +{ + Class head; + head = (Student *)malloc(sizeof(Student)); + if (!head) + return NULL; + head->next = NULL; + return head; +} + +void insert_student(Class cls, char *name, int id, unsigned int age, char group, float score) +{ + Class p; + p = (Student *)malloc(sizeof(Student)); + p->name = name, p->id = id, p->age = age, p->group = group, p->score = score; + p->next = cls->next; + cls->next = p; +} + +void print_class(const Class cls) +{ + Class prt = NULL; + float total = 0, average = 0; + int num_stu = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (prt = cls; prt; prt = prt->next) + { + printf("%s\t%d\t%d\t%c\t%.2f\n", + prt->name, prt->id, prt->age, prt->group, prt->score); + total += prt->score; + ++num_stu; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); +} + +int main() +{ + Class cls = init_class(); + insert_student(cls, "王五", 1003, 16, 'B', 80.50); + insert_student(cls, "李四", 1002, 15, 'A', 90.00); + insert_student(cls, "张三", 1001, 16, 'A', 95.50); + + print_class(cls); + + return 0; +} +``` + +### B +```c +#include +#include + +typedef struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; + struct Student *next; +} Student, *Class; + +Class init_class() +{ + Class head; + head = (Student *)malloc(sizeof(Student)); + if (!head) + return NULL; + head->next = NULL; + return head; +} + +void insert_student(Class cls, char *name, int id, unsigned int age, char group, float score) +{ + Class p, q; + p = (Student *)malloc(sizeof(Student)); + p->name = name, p->id = id, p->age = age, p->group = group, p->score = score; + cls->next = p; + p->next = cls->next; +} + +void print_class(const Class cls) +{ + Class prt = NULL; + float total = 0, average = 0; + int num_stu = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (prt = cls->next; prt; prt = prt->next) + { + printf("%s\t%d\t%d\t%c\t%.2f\n", + prt->name, prt->id, prt->age, prt->group, prt->score); + total += prt->score; + ++num_stu; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); +} + +int main() +{ + Class cls = init_class(); + insert_student(cls, "王五", 1003, 16, 'B', 80.50); + insert_student(cls, "李四", 1002, 15, 'A', 90.00); + insert_student(cls, "张三", 1001, 16, 'A', 95.50); + + print_class(cls); + + return 0; +} +``` + +### C +```c +#include +#include + +typedef struct Student +{ + char *name; + int id; + unsigned int age; + char group; + float score; + struct Student *next; +} Student, *Class; + +Class init_class() +{ + Class head; + head = (Student *)malloc(sizeof(Student)); + if (!head) + return NULL; + head->next = NULL; + return head; +} + +void insert_student(Class cls, char *name, int id, unsigned int age, char group, float score) +{ + Class p; + p = (Student *)malloc(sizeof(Student)); + p->name = name, p->id = id, p->age = age, p->group = group, p->score = score; + p->next = cls->next; + cls->next = p; +} + +void print_class(const Class cls) +{ + Class prt = NULL; + float total = 0, average = 0; + int num_stu = 0; + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号\t年龄\t小组\t成绩\n"); + printf("--------------------------------------------\n"); + for (prt = (*cls).next; prt; ++prt) + { + printf("%s\t%d\t%d\t%c\t%.2f\n", + prt->name, prt->id, prt->age, prt->group, prt->score); + total += prt->score; + ++num_stu; + } + printf("============================================\n"); + average = total / num_stu; + printf("班级平均成绩:%.2f", average); +} + +int main() +{ + Class cls = init_class(); + insert_student(cls, "王五", 1003, 16, 'B', 80.50); + insert_student(cls, "李四", 1002, 15, 'A', 90.00); + insert_student(cls, "张三", 1001, 16, 'A', 95.50); + + print_class(cls); + + return 0; +} +``` diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223/config.json" deleted file mode 100644 index 304857b..0000000 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223/config.json" +++ /dev/null @@ -1,9 +0,0 @@ -{ - "node_id": "569d5e11c4fc5de7844053d9a733c5e8", - "keywords": [ - "结构体", - "C语言" - ], - "children": [], - "export": [] -} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/config.json" new file mode 100644 index 0000000..60778a7 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/config.json" @@ -0,0 +1,11 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "联合体", + "共用体" + ], + "children": [], + "export": [ + "unions.json" + ] + } \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/unions.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/unions.c" new file mode 100644 index 0000000..eccc07b --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/unions.c" @@ -0,0 +1,42 @@ +#include +#include +#define STR_LEN 20 + +typedef struct +{ + char *name; + int id; + unsigned int age; + char profession[STR_LEN]; + union + { + float score; + char course[STR_LEN]; + }un; +}Person; + +int main() +{ + Person persons[] = { + {.name = "张三", .id = 1001, .age = 16, .profession = "学生", .un.score = 95.50}, + {.name = "李四", .id = 1002, .age = 15, .profession = "学生", .un.score = 90.00}, + {.name = "王五", .id = 2001, .age = 26, .profession = "教师", .un.course = "语文"} + }; + + size_t i, num_per = sizeof(persons) / sizeof(Person); + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号/工号\t年龄\t身份\t成绩/学科\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_per; ++i) { + Person per = persons[i]; + if (strcmp(per.profession, "学生") == 0) + printf("%s\t%d\t\t%d\t%s\t%.2f\n", + per.name, per.id, per.age, per.profession, per.un.score); + else + printf("%s\t%d\t\t%d\t%s\t%s\n", + per.name, per.id, per.age, per.profession, per.un.course); + } + printf("============================================\n"); + + return 0; +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/unions.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/unions.json" new file mode 100644 index 0000000..87e3e57 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/unions.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "卢昕", + "source": "unions.md", + "exercise_id":"73886fb5ce5c4b1e9db67223b260ee72" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/unions.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/unions.md" new file mode 100644 index 0000000..b5af0da --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/1.\350\201\224\345\220\210\344\275\223/unions.md" @@ -0,0 +1,184 @@ +# 联合体 + +定义一个师生信息结构体,并实例化一个结构体数组,用于保存师生的基本信息,其中 **un** 字段用于保存学生的成绩或者教师的教学科目,最后输出所有人员信息。请选出正确答案。 + +## 答案 +```c +#include +#include +#define STR_LEN 20 + +typedef struct +{ + char *name; + int id; + unsigned int age; + char profession[STR_LEN]; + union + { + float score; + char course[STR_LEN]; + }un; +}Person; + +int main() +{ + Person persons[] = { + {.name = "张三", .id = 1001, .age = 16, .profession = "学生", .un.score = 95.50}, + {.name = "李四", .id = 1002, .age = 15, .profession = "学生", .un.score = 90.00}, + {.name = "王五", .id = 2001, .age = 26, .profession = "教师", .un.course = "语文"} + }; + + size_t i, num_per = sizeof(persons) / sizeof(Person); + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号/工号\t年龄\t身份\t成绩/学科\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_per; ++i) { + Person per = persons[i]; + if (strcmp(per.profession, "学生") == 0) + printf("%s\t%d\t\t%d\t%s\t%.2f\n", + per.name, per.id, per.age, per.profession, per.un.score); + else + printf("%s\t%d\t\t%d\t%s\t%s\n", + per.name, per.id, per.age, per.profession, per.un.course); + } + printf("============================================\n"); + + return 0; +} +``` + +## 选项 +### A +```c +#include +#include +#define STR_LEN 20 + +typedef struct +{ + char *name; + int id; + unsigned int age; + char profession[STR_LEN]; + union + { + float score; + char course[STR_LEN]; + }un; +}Person; + +int main() +{ + Person persons[] = { + {.name = "张三", .id = 1001, .age = 16, .profession = "学生", .un.score = 95.50}, + {.name = "李四", .id = 1002, .age = 15, .profession = "学生", .un.score = 90.00}, + {.name = "王五", .id = 2001, .age = 26, .profession = "教师", .un.course = "语文"} + }; + + size_t i, num_per = sizeof(persons) / sizeof(Person); + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号/工号\t年龄\t身份\t成绩/学科\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_per; ++i) { + Person per = persons[i]; + printf("%s\t%d\t\t%d\t%s\t%.2f\n", + per.name, per.id, per.age, per.profession, per.un.score); + } + printf("============================================\n"); + + return 0; +} +``` + +### B +```c +#include +#include +#define STR_LEN 20 + +typedef struct +{ + char *name; + int id; + unsigned int age; + char profession[STR_LEN]; + union + { + float score; + char course[STR_LEN]; + }un; +}Person; + +int main() +{ + Person persons[] = { + {"张三", 1001, 16, "学生", 95.50}, + {"李四", 1002, 15, "学生", 90.00}, + {"王五", 2001, 26, "教师", "语文"} + }; + + size_t i, num_per = sizeof(persons) / sizeof(Person); + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号/工号\t年龄\t身份\t成绩/学科\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_per; ++i) { + Person per = persons[i]; + if (strcmp(per.profession, "学生") == 0) + printf("%s\t%d\t\t%d\t%s\t%.2f\n", + per.name, per.id, per.age, per.profession, per.un.score); + else + printf("%s\t%d\t\t%d\t%s\t%s\n", + per.name, per.id, per.age, per.profession, per.un.course); + } + printf("============================================\n"); + + return 0; +} +``` + +### C +```c +#include +#include +#define STR_LEN 20 + +typedef struct +{ + char *name; + int id; + unsigned int age; + char profession[STR_LEN]; + union un + { + float score; + char course[STR_LEN]; + }; +}Person; + +int main() +{ + Person persons[] = { + {.name = "张三", .id = 1001, .age = 16, .profession = "学生", .un.score = 95.50}, + {.name = "李四", .id = 1002, .age = 15, .profession = "学生", .un.score = 90.00}, + {.name = "王五", .id = 2001, .age = 26, .profession = "教师", .un.course = "语文"} + }; + + size_t i, num_per = sizeof(persons) / sizeof(Person); + printf("=============== 学生基本信息 ===============\n"); + printf("姓名\t学号/工号\t年龄\t身份\t成绩/学科\n"); + printf("--------------------------------------------\n"); + for (i = 0; i < num_per; ++i) { + Person per = persons[i]; + if (strcmp(per.profession, "学生") == 0) + printf("%s\t%d\t\t%d\t%s\t%.2f\n", + per.name, per.id, per.age, per.profession, per.un.score); + else + printf("%s\t%d\t\t%d\t%s\t%s\n", + per.name, per.id, per.age, per.profession, per.un.course); + } + printf("============================================\n"); + + return 0; +} +``` diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/config.json" new file mode 100644 index 0000000..8589445 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/config.json" @@ -0,0 +1,11 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "枚举", + "枚举类型" + ], + "children": [], + "export": [ + "enum.json" + ] +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/enum.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/enum.c" new file mode 100644 index 0000000..1bd87cf --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/enum.c" @@ -0,0 +1,36 @@ +#include + +enum week {Mon, Tues, Wed, Thurs, Fri, Sat, Sun}; +enum week dw; + +int cal_day_of_week(int y, int m, int d) +{ + int dw; + if (m == 1 || m == 2) + { + m += 12; + y--; + } + dw = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7; + return dw; +} + +int main() +{ + int year, month, day; + printf("请输入日期(年-月-日):\n"); + scanf("%d-%d-%d", &year, &month, &day); + dw = cal_day_of_week(year, month, day); + switch (dw) + { + case week.Mon: printf("星期一\n"); break; + case week.Tues: printf("星期二\n"); break; + case week.Wed: printf("星期三\n"); break; + case week.Thurs: printf("星期四\n"); break; + case week.Fri: printf("星期五\n"); break; + case week.Sat: printf("星期六\n"); break; + case week.Sun: printf("星期日\n"); break; + } + + return 0; +} diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/enum.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/enum.json" new file mode 100644 index 0000000..b532802 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/enum.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "卢昕", + "source": "enum.md", + "exercise_id":"44888a39b3be432a854447cd603b348e" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/enum.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/enum.md" new file mode 100644 index 0000000..959d8c7 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/2.\346\236\232\344\270\276\347\261\273\345\236\213/enum.md" @@ -0,0 +1,166 @@ +# 枚举类型 + +输入一个日期(年月日),输出该日期是星期几。请选出错误答案。 + +## 答案 +```c +#include + +enum week {Mon, Tues, Wed, Thurs, Fri, Sat, Sun}; +enum week dw; + +int cal_day_of_week(int y, int m, int d) +{ + int dw; + if (m == 1 || m == 2) + { + m += 12; + y--; + } + dw = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7; + return dw; +} + +int main() +{ + int year, month, day; + printf("请输入日期(年-月-日):\n"); + scanf("%d-%d-%d", &year, &month, &day); + dw = cal_day_of_week(year, month, day); + switch (dw) + { + case week.Mon: printf("星期一\n"); break; + case week.Tues: printf("星期二\n"); break; + case week.Wed: printf("星期三\n"); break; + case week.Thurs: printf("星期四\n"); break; + case week.Fri: printf("星期五\n"); break; + case week.Sat: printf("星期六\n"); break; + case week.Sun: printf("星期日\n"); break; + } + + return 0; +} +``` + +## 选项 +### A +```c +#include + +enum week {Mon, Tues, Wed, Thurs, Fri, Sat, Sun}; +enum week dw; + +int cal_day_of_week(int y, int m, int d) +{ + int dw; + if (m == 1 || m == 2) + { + m += 12; + y--; + } + dw = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7; + return dw; +} + +int main() +{ + int year, month, day; + printf("请输入日期(年-月-日):\n"); + scanf("%d-%d-%d", &year, &month, &day); + dw = cal_day_of_week(year, month, day); + switch (dw) + { + case Mon: printf("星期一\n"); break; + case Tues: printf("星期二\n"); break; + case Wed: printf("星期三\n"); break; + case Thurs: printf("星期四\n"); break; + case Fri: printf("星期五\n"); break; + case Sat: printf("星期六\n"); break; + case Sun: printf("星期日\n"); break; + } + + return 0; +} +``` + +### B +```c +#include + +enum week {Mon=0, Tues=1, Wed=2, Thurs=3, Fri=4, Sat=5, Sun=6}; +enum week dw; + +int cal_day_of_week(int y, int m, int d) +{ + int dw; + if (m == 1 || m == 2) + { + m += 12; + y--; + } + dw = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7; + return dw; +} + +int main() +{ + int year, month, day; + printf("请输入日期(年-月-日):\n"); + scanf("%d-%d-%d", &year, &month, &day); + dw = cal_day_of_week(year, month, day); + switch (dw) + { + case Mon: printf("星期一\n"); break; + case Tues: printf("星期二\n"); break; + case Wed: printf("星期三\n"); break; + case Thurs: printf("星期四\n"); break; + case Fri: printf("星期五\n"); break; + case Sat: printf("星期六\n"); break; + case Sun: printf("星期日\n"); break; + } + + return 0; +} + +``` + +### C +```c +#include + +enum week {Mon=0, Tues, Wed, Thurs, Fri, Sat, Sun}; +enum week dw; + +int cal_day_of_week(int y, int m, int d) +{ + int dw; + if (m == 1 || m == 2) + { + m += 12; + y--; + } + dw = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7; + return dw; +} + +int main() +{ + int year, month, day; + printf("请输入日期(年-月-日):\n"); + scanf("%d-%d-%d", &year, &month, &day); + dw = cal_day_of_week(year, month, day); + switch (dw) + { + case Mon: printf("星期一\n"); break; + case Tues: printf("星期二\n"); break; + case Wed: printf("星期三\n"); break; + case Thurs: printf("星期四\n"); break; + case Fri: printf("星期五\n"); break; + case Sat: printf("星期六\n"); break; + case Sun: printf("星期日\n"); break; + } + + return 0; +} + +``` diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/config.json" new file mode 100644 index 0000000..6e3dabe --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/2.\350\201\224\345\220\210\344\275\223\344\270\216\346\236\232\344\270\276\347\261\273\345\236\213/config.json" @@ -0,0 +1,9 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "联合体", + "共用体", + "枚举类型", + "枚举" + ] +} \ No newline at end of file -- GitLab