首页 > 代码库 > 长路漫漫,唯剑作伴--基础

长路漫漫,唯剑作伴--基础

一、结构体

结构体定义

    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在学习小组
        float score;  //成绩
    }

结构体变量

struct stu stu1, stu2;
    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在学习小组
        float score;  //成绩
    } stu1, stu2;
    struct{  //没有写 stu
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在学习小组
        float score;  //成绩
    } stu1, stu2;

结构体访问

    #include <stdio.h>
    int main(){
        struct{
            char *name;  //姓名
            int num;  //学号
            int age;  //年龄
            char group;  //所在小组
            float score;  //成绩
        } stu1;
        //给结构体成员赋值
        stu1.name = "Tom";
        stu1.num = 12;
        stu1.age = 18;
        stu1.group = A;
        stu1.score = 136.5;
        //读取结构体成员的值
        printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!\n", stu1.name, stu1.num, stu1.age, stu1.group, stu1.score);
        return 0;
    }

结构体赋值

    struct{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组
        float score;  //成绩
    } stu1, stu2 = { "Tom", 12, 18, A, 136.5 };

二、结构体数组

    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组 
        float score;  //成绩
    }class[5];
    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组 
        float score;  //成绩
    }class[5] = {
        {"Li ping", 5, 18, C, 145.0},
        {"Zhang ping", 4, 19, A, 130.5},
        {"He fang", 1, 18, A, 148.5},
        {"Cheng ling", 2, 17, F, 139.0},
        {"Wang ming", 3, 17, B, 144.5}
    };
    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组 
        float score;  //成绩
    }class[] = {
        {"Li ping", 5, 18, C, 145.0},
        {"Zhang ping", 4, 19, A, 130.5},
        {"He fang", 1, 18, A, 148.5},
        {"Cheng ling", 2, 17, F, 139.0},
        {"Wang ming", 3, 17, B, 144.5}
    };

三、结构体和指针

    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组
        float score;  //成绩
    } stu1 = { "Tom", 12, 18, A, 136.5 };
    //结构体指针
    struct stu *pstu = &stu1;
    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组
        float score;  //成绩
    } stu1 = { "Tom", 12, 18, A, 136.5 }, *pstu = &stu1;
    #include <stdio.h>
    int main(){
        struct{
            char *name;  //姓名
            int num;  //学号
            int age;  //年龄
            char group;  //所在小组
            float score;  //成绩
        } stu1 = { "Tom", 12, 18, A, 136.5 }, *pstu = &stu1;
        //读取结构体成员的值
        printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!\n", (*pstu).name, (*pstu).num, (*pstu).age, (*pstu).group, (*pstu).score);
        printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!\n", pstu->name, pstu->num, pstu->age, pstu->group, pstu->score);
        return 0;
    }

四、枚举

    enum week{ Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun };
    enum week a = Mon, b = Wed, c = Sat;

 五、C++结构体

     //定义一个结构体,类型为struct Student
2      struct  Student     
3      {
4          string name;
5          double eng;
6          double ch;
7      }; 
8       
9      //定义了一个结构体,类型为struct Student;且定义了一个结构体实例,名叫Stu
10      struct  Student    
11      {
12          string name;
13          double eng;
14          double ch;
15      }Stu; 
16       
17      //定义了无名的结构体,且定义了一个结构体实例,名叫Stu
18      struct 
19      {
20          string name;
21          double eng;
22          double ch;
23      }Stu; 
24       
25      //重定义结构体,类型为struct Student 或者是Stu
26      typedef struct  Student  
27      {
28          string name;
29          double eng;
30          double ch;
31      }Stu; 
32       
33      //重定义结构体,类型为Stu
34      typedef struct  
35      {
36          string name;
37          double eng;
38          double ch;
39      }Stu;

 

长路漫漫,唯剑作伴--基础