课程用数组不是更好么, 用结构体倒也可以实现, 不过遍历还是用了数组了.
- #include <stdio.h>
- typedef struct {
- //数据结构,高数,思修,英语,绘画基础
- int data;
- int math;
- int english;
- int paint;
- } Course;
- typedef struct {
- char * number;
- char * sex;
- Course course;
- } Student;
- Course makeCourse(int data, int math, int english, int paint)
- {
- Course c;
- c.data = data;
- c.math = math;
- c.english = english;
- c.paint = paint;
- return c;
- }
- Student makeStudent(char * number, char * sex, Course course)
- {
- Student s;
- s.number = number;
- s.sex = sex;
- s.course = course;
- return s;
- }
- int main()
- {
- char * coures[4] = { "data", "math", "english", "paint" };
- Course c1 = makeCourse(0, 1, 0, 1);
- int * p = (int *)&c1;
- Student stu1 = makeStudent("01052001", "man", c1);
- printf("学号:%s 性别:%s 所选课程有: ", stu1.number, stu1.sex);
- for (int i = 0; i < 4; i++) {
- if (*(p + i))
- printf("%s ", coures[i]);
- }
- printf("\n");
- Course c2 = makeCourse(0, 1, 1, 1);
- p = (int *)&c2;
- Student stu2 = makeStudent("01052002", "woman", c2);
- printf("学号:%s 性别:%s 所选课程有: ", stu2.number, stu2.sex);
- for (int i = 0; i < 4; i++) {
- if (*(p + i))
- printf("%s ", coures[i]);
- }
-
- return 0;
- }
复制代码 |