提交 34cde60f 编写于 作者: L luxin

add 7 exercises

上级 2e6e4ce6
{ {
"node_id": "569d5e11c4fc5de7844053d9a733c5e8", "node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [ "keywords": [
"结构体", "结构体",
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": [
"structures.json"
]
} }
\ No newline at end of file
#include <stdio.h>
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
{
"type": "code_options",
"author": "卢昕",
"source": "structures.md",
"exercise_id":"51df13a16d96417bb5dbe6476c1ec05f"
}
\ No newline at end of file
# 结构体的定义与使用
定义一个学生结构体,并实例化一个学生对象,保存一个学生的基本信息,最后输出该学生信息。请选出错误答案。
## 答案
```c
#include <stdio.h>
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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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;
}
```
...@@ -5,5 +5,7 @@ ...@@ -5,5 +5,7 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": [
"structures_array.json"
]
} }
\ No newline at end of file
#include <stdio.h>
#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
{
"type": "code_options",
"author": "卢昕",
"source": "structures_array.md",
"exercise_id":"ce458884b396456d8b9993004272720e"
}
\ No newline at end of file
# 结构体数组
定义一个学生结构体,并实例化一个结构体数组,用于保存一个班级所有学生的基本信息,最后输出所有学生基本信息,以及班级的学生平均成绩。请选出错误答案。
## 答案
```c
#include <stdio.h>
#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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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;
}
```
{ {
"node_id": "569d5e11c4fc5de7844053d9a733c5e8", "node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [ "keywords": [
"结构体", "结构体",
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": [
"structures_pointers.json"
]
} }
\ No newline at end of file
#include <stdio.h>
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
{
"type": "code_options",
"author": "卢昕",
"source": "structures_pointers.md",
"exercise_id":"5fe2bafbe9974e62bf3544e50aefa3cc"
}
\ No newline at end of file
# 结构体指针
定义一个学生结构体,并实例化一个学生对象,保存一个学生的基本信息,最后使用结构体指针访问和输出该学生信息。请选出错误答案。
## 答案
```c
#include <stdio.h>
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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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;
}
```
...@@ -5,5 +5,5 @@ ...@@ -5,5 +5,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": ["structures_n_func.json"]
} }
\ No newline at end of file
#include <stdio.h>
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
{
"type": "code_options",
"author": "卢昕",
"source": "structures_n_func.md",
"exercise_id":"d05a0cfc3bd6483b9de9d0c97cc0ace8"
}
\ No newline at end of file
# 结构体与函数
定义一个学生结构体,并实例化一个结构体数组,用于保存一个班级所有学生的基本信息,最后输出所有学生基本信息,以及班级的学生平均成绩。请选出正确答案。
## 答案
```c
#include <stdio.h>
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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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;
}
```
{ {
"node_id": "569d5e11c4fc5de7844053d9a733c5e8", "node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [ "keywords": [
"结构体", "结构体",
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": [
"linked_list.json"
]
} }
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
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
{
"type": "code_options",
"author": "卢昕",
"source": "linked_list.md",
"exercise_id":"af79a964ebfa4116a79cd623357bee1d"
}
\ No newline at end of file
# 链式结构
定义一个学生结构体,并创建一个链表用于保存一个班级所有学生的基本信息,最后输出所有学生基本信息,以及班级的学生平均成绩。请选出正确答案。
## 答案
```c
#include <stdio.h>
#include <stdlib.h>
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 <stdio.h>
#include <stdlib.h>
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 <stdio.h>
#include <stdlib.h>
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 <stdio.h>
#include <stdlib.h>
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;
}
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"结构体",
"C语言"
],
"children": [],
"export": []
}
\ No newline at end of file
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"联合体",
"共用体"
],
"children": [],
"export": [
"unions.json"
]
}
\ No newline at end of file
#include <stdio.h>
#include <string.h>
#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
{
"type": "code_options",
"author": "卢昕",
"source": "unions.md",
"exercise_id":"73886fb5ce5c4b1e9db67223b260ee72"
}
\ No newline at end of file
# 联合体
定义一个师生信息结构体,并实例化一个结构体数组,用于保存师生的基本信息,其中 **un** 字段用于保存学生的成绩或者教师的教学科目,最后输出所有人员信息。请选出正确答案。
## 答案
```c
#include <stdio.h>
#include <string.h>
#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 <stdio.h>
#include <string.h>
#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 <stdio.h>
#include <string.h>
#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 <stdio.h>
#include <string.h>
#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;
}
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"枚举",
"枚举类型"
],
"children": [],
"export": [
"enum.json"
]
}
\ No newline at end of file
#include <stdio.h>
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;
}
{
"type": "code_options",
"author": "卢昕",
"source": "enum.md",
"exercise_id":"44888a39b3be432a854447cd603b348e"
}
\ No newline at end of file
# 枚举类型
输入一个日期(年月日),输出该日期是星期几。请选出错误答案。
## 答案
```c
#include <stdio.h>
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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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;
}
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"联合体",
"共用体",
"枚举类型",
"枚举"
]
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册