c语言用结构体创建一个包含姓名,四个专业的志愿编码填写,成绩的学生志愿表有关代码
时间: 2024-01-22 09:19:57 浏览: 55
下面是一个简单的示例代码,其中使用了结构体来表示学生志愿表的每个条目:
```c
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LENGTH 50
#define NUM_DEPARTMENTS 4
// 定义结构体
typedef struct {
char name[MAX_NAME_LENGTH];
int department_choices[NUM_DEPARTMENTS];
float score;
} Student;
int main() {
// 初始化一些学生志愿表的数据
Student students[3];
strcpy(students[0].name, "张三");
students[0].department_choices[0] = 1;
students[0].department_choices[1] = 2;
students[0].department_choices[2] = 4;
students[0].department_choices[3] = 3;
students[0].score = 87.5;
strcpy(students[1].name, "李四");
students[1].department_choices[0] = 3;
students[1].department_choices[1] = 2;
students[1].department_choices[2] = 1;
students[1].department_choices[3] = 4;
students[1].score = 92.0;
strcpy(students[2].name, "王五");
students[2].department_choices[0] = 2;
students[2].department_choices[1] = 1;
students[2].department_choices[2] = 2;
students[2].department_choices[3] = 3;
students[2].score = 81.5;
// 输出所有学生的志愿表
for (int i = 0; i < 3; i++) {
printf("姓名:%s\n", students[i].name);
printf("专业志愿:");
for (int j = 0; j < NUM_DEPARTMENTS; j++) {
printf("%d ", students[i].department_choices[j]);
}
printf("\n");
printf("成绩:%f\n\n", students[i].score);
}
return 0;
}
```
在这个示例中,我们定义了一个 `Student` 结构体,其中包含了学生的姓名、四个专业的志愿编码填写、和成绩。在 `main()` 函数中,我们创建了三个 `Student` 对象,并初始化了它们的数据。最后,我们使用循环遍历所有的学生对象,并输出它们的姓名、专业志愿和成绩。
阅读全文