C_Language_08

C_Language_08

声明结构体 关键字 struct
声明一个用于描述学生对象的结构体类型
声明成员变量时不能赋值,因为这是在声明结构体类型
示例:
struct student{
    int num;//成员变量之间用;隔开
    char name[30];//必须指定字符数组的大小
    char sex;
    float score;
};//不要忘了;

使用typedef
示例:
typedef struct student{
    int num;
    char name[30];
    char sex;
    float score;
}Student;//形式一,常用
typedef struct student Student;//方式二,不常用

int main(int argc, const char * argv[]) {
    结构体属于构造类型
    定义结构体变量
    struct关键字
    赋值1.设置初始值使用{} 2.按照成员变量的顺序赋值 3.如果设置初始值为默认值0时使用{0}
    struct student stu1 = {1,"zhangsna",'m',88};
    struct student stu2 = {2,"lisi",'m',67};
    struct student stu3 = {3,"angna",'n',96};
    结构体可以变量互相赋值(数组名不可以)
    结构体变量定义之后,不能再整体赋值。只能给成员变量赋值

访问结构体成员变量:
不能使用结构体变量名输出所有的成员变量
访问成员变量方式:结构体变量.成员变量
成员变量和基本类型变量一样使用:参与运算
访问示例:
    printf("%d\n",stu1.num);
    printf("%s\n",stu1.name);
    printf("%c\n",stu1.sex);
    printf("%0.2f\n",stu1.score);

修改成员变量 成员变量可以直接赋值
    stu2.score = 66;
    printf("%0.2f\n",stu2.score);
    strcpy(stu3.name, "laowu");
    printf("%s\n",stu3.name);
将控制台输入的内容,存储到成员变量中
    struct student stu4 = {0};
    printf("请输入学生学号:");
    scanf("%d",&stu4.num);//优先级 点.高于取地址&
    printf("%d\n",stu4.num);

typedef:
    格式:typedef 要改的 改成什么     //重命名
    与auto、extern、mutable、static、register等关键字不能出现在同一个表达式中
示例:   
    int a = 3;
    printf("%d\n",a);
    typedef int low;
    low c = 5;//即int c = 5;
    printf("%d\n",c);

    结构体的声明方式
    1.匿名结构体,结构体声明和变量定义 组合在一起
    基本不使用;
    2.使用typedef声明结构体
    使用typedef为结构体类型起别名
    结构体内存占用
    1.以最⼤成员变量类型所占空间为分配单位, 按结构体成员声明顺序自上⽽下分配。
      注:分配空间不足以存储成员变量时,分配新的空间单位。

    Student stu5 = {0};
    strcpy(stu5.name, "strcpy");
    printf("%s\n",stu5.name);

    return 0;
}

示例:有三个学生,编程找出分数最高者以及年龄最小者。
typedef struct student{//定义结构体
    char name[30];
    float score;
    int age;
}student;
int main(int argc, const char * argv[]) {
    student stu1 = {"lier",94,21};
    student stu2 = {"xiaoming",45,20};
    student stu3 = {"xiaohua",67,23};
    student maxScoreStu = {0};
    maxScoreStu = stu1.score > stu2.score ? stu1 : stu2;
    maxScoreStu = stu3.score > maxScoreStu.score ? stu3 : maxScoreStu;
    printf("%.2f\n",maxScoreStu.score);
    student minAgeStu = {0};
    minAgeStu = stu1.age < stu2.age ? stu1 : stu2;
    minAgeStu = stu3.age < minAgeStu.age ? stu3 : minAgeStu;
    printf("%d\n",minAgeStu.age);
    return 0;
}

结构体嵌套:
结构体的成员变量可以是其他结构体类型
结构体的成员变量也是结构体,使用{}赋值
示例:
typedef struct birthdate{
    int year;
    int month;
    int day;
}BirthDate;
typedef struct student{
    char name[30];
    float score;
    int age;
    BirthDate birthday;
}student;
int main(int argc, const char * argv[]) {
    student stu1 = {"lier",94,21,{1993,1,11}};
    student stu2 = {"xiaoming",45,20,{1995,4,3}};
    student stu3 = {"xiaohua",67,23,{1991,5,1}};
    student maxScoreStu = {0};
    maxScoreStu = stu1.score > stu2.score ? stu1 : stu2;
    maxScoreStu = stu3.score > maxScoreStu.score ? stu3 : maxScoreStu;
    maxScoreStu.birthday.year = 1990;
    printf("分数最高者出生于%d年\n",maxScoreStu.birthday.year);
    student minAgeStu = {0};
    minAgeStu = stu1.age < stu2.age ? stu1 : stu2;
    minAgeStu = stu3.age < minAgeStu.age ? stu3 : minAgeStu;
    printf("年龄最小者出生于%d年\n",minAgeStu.birthday.year);
    return 0;
}

结构体数组:
示例:
typedef struct student{
    int num;
    char name[20];
    char sex;
    float score;
}Student;
int main(int argc, const char * argv[]) {
//    整形数组    字符数组
//    结构体数组:数组元素都是结构体
    Student stus[5] = {{001,"lier",'m',71},{002,"xiaoming",'m',80},{003,"xiaohua",'m',93},{004,"lidan",'m',91},{005,"lusey",'n',97}};
//    访问素组元素
//    优先级[]高于.
//    printf("%0.1f\n",stus[0].score);
    for (int i = 0; i< 5; i++) {
        printf("学号%d 名字%-8s 性别%c 分数%0.2f\n",stus[i].num,stus[i].name,stus[i].sex,stus[i].score);
    }
    return 0;
}

示例:有5名学生保存在结构体数组中,编程查找成绩最⾼者,输出该学生全部信息
typedef struct student{
    int num;
    char name[30];
    float score;
    int age;
}student;
int main(int argc, const char * argv[]) {
    student stu[5] = {{001,"lier",94,21},
                      {002,"xiaoming",45,20},
                      {003,"xiaohua",67,23},
                      {004,"lidan",75,21},
                      {005,"lusey",97,20}};
    student maxScoreStu = {0};
    for (int i = 0; i < 5; i++) {
        maxScoreStu = stu[i].score > maxScoreStu.score ? stu[i] : maxScoreStu;
    }
    printf("%d %s %0.2f %d\n",maxScoreStu.num,maxScoreStu.name,maxScoreStu.score,maxScoreStu.age);
    return 0;
}

示例:有5名学生保存在结构体数组中,按名字从大到小排序, 并输出
typedef struct student{
    int num;
    char name[30];
    float score;
    int age;
}student;
int main(int argc, const char * argv[]) {
    student stu[5] = {{001,"lier",94,21},
        {002,"xiaoming",45,20},
        {003,"xiaohua",67,23},
        {004,"lidan",75,21},
        {005,"lusey",97,20}};
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4 - i; j++) {
            if (strcmp(stu[j].name, stu[j+1].name) < 0) {
                student temp = stu[j];
                stu[j] = stu[j+1];
                stu[j+1] = temp;
            }
        }
    }
    for (int i = 0; i < 5; i++) {
    printf("%d %-8s %-1.2f %d\n",stu[i].num,stu[i].name,stu[i].score,stu[i].age);
    }
    return 0;
}

创建文件管理结构体:
创建.h和.m文件,负责管理结构体及相关函数
文件名与结构体名相同,便于查找和管理

posted @ 2015-11-28 14:32  DH_Fantasy  阅读(91)  评论(0)    收藏  举报