• iOS开发之c语言基础Lesson-07 结构体 上课笔记 与 试题练习


      1 ///////////////Lesson 07 结构体  上课笔记 ////////////////
      2 //结构体作用:1.是一种自定义的数据类型,可以用来定义变量
      3 //            2.是一个大容器,比数组灵活,可以存储不同数据类型的变量
      4 //结构体定义 :定义就是一种格式,一种模板, 只要按照该格式就能定义出结构体
      5 //定义一个学生结构体
      6 //typedef, 类型重定义,给类型重新取一个名字,新的名字和原有的名字的作用是一样的
      7 //typedef两种定义方式:1.先定义结构体, 后typedef
      8 //                       2.定义结构体的同时,定义typedef
      9 typedef struct student{
     10     int num;
     11     char name[20];
     12     int age;
     13     char gender;
     14     float score;
     15 }Student;
     16 //点结构体
     17 //struct point{
     18 //    int x;
     19 //    int y;
     20 //};
     21 //typedef  struct point  point; //point代替struct point
     22 ////矩形结构体
     23 //struct rectangle{
     24 //    float width;
     25 //    float length;
     26 //};
     27 ////立方体结构体
     28 //struct cube{
     29 //    float width;
     30 //    float height;
     31 //    float length;
     32 //};
     33 int main(int argc, const char * argv[]) {
     34 //    @autoreleasepool {
     35 //        // insert code here...
     36 //        NSLog(@"Hello, World!");
     37 //    }
     38     
     39 /////使用结构体类型(struct + 结构体名字), 定义结构体变量
     40 //    struct student stu1 = {"Ashen", 24, 'm'};
     41 //    Student stu2 = {"Ying", 23, 'f'};
     42 //    
     43 //    struct point p1 = {12, 14};
     44 //    struct rectangle r1 = {3.0, 4.0};
     45 //    struct cube c1 = {3.0, 4.0, 5.0};
     46 //    
     47 ////////访问结构体成员  结构体变量.结构体成员
     48 //    printf("%s ", stu1.name);
     49 //    strcpy(stu1.name, "Ashen-Zhao");
     50 //     printf("%s ", stu1.name);
     51     
     52     
     53 ///////结构体变量的赋值操作
     54 //    Student stu1 = {"Ashen", 33, 'm'};
     55 //    Student stu2 = {0};
     56 //    Student stu3 = {0};
     57 //    //1.单一成员赋值
     58 //    strcpy(stu2.name, stu1.name);
     59 //    stu2.age = stu1.age;
     60 //    stu2.gender = stu1.gender;
     61 //    //2.结构体变量整体拷贝
     62 //    //如果想把一个数组中的元素,拷贝到另外一个数组中,数组不能直接通过赋值号(=)赋值, 此时可以把数组放到结构体中,通过结构体变量来完成拷贝操作。
     63 //    stu3 = stu1;
     64 //    printf("%s ", stu3.name);
     65     
     66 ////////////练习
     67     //    Student stu1 = {"Ashen1", 21, 'm', 90};
     68     //    Student stu2 = {"ying", 22, 'f', 80};
     69     //    Student stu3 = {"zhao", 23, 'm', 100};
     70     //
     71     //
     72     //    //找出分数   最高   者者者者者者者者者者者者
     73     //    Student maxScoreStu = {0}; //存储分数最高的人
     74     //    //    if (stu1.score > stu2.score) {
     75     //    //        if (stu1.score > stu3.score) {
     76     //    //            maxScoreStu = stu1;
     77     //    //        }else{
     78     //    //            maxScoreStu = stu3;
     79     //    //        }
     80     //    //    }else if(stu2.score > stu3.score){
     81     //    //        maxScoreStu = stu2;
     82     //    //    }else{
     83     //    //        maxScoreStu = stu3;
     84     //    //    }
     85     //
     86     //
     87     //    maxScoreStu = stu1.score > stu2.score ? stu1.score > stu3.score ? stu1 : stu3 : stu2.score > stu3.score ?stu2 : stu3;
     88     //
     89     //
     90     //    printf("name:%s, age:%d, score:%.2f", maxScoreStu.name, maxScoreStu.age, maxScoreStu.score);
     91     
     92     //////////////结构体数组
     93     ////定义结构体数组,存储5个学生
     94     //    Student students[5]={
     95     //        {1001, "Ashen", 24, 'f', 100},
     96     //        {1003, "Ying", 23, 'm', 76},
     97     //        {1004, "BenBen", 22, 'f', 90},
     98     //        {1005, "Dandan", 18, 'm', 89},
     99     //        {1002, "Love", 31, 'f', 56}
    100     //    };
    101     ////1输出所有学生的信息
    102     ////    for (int i = 0; i < 5; i++) {
    103     ////        printf("num:%d,name:%s,age:%d,gender:%c,score:%.2f 
    ", students[i].num, students[i].name, students[i].age, students[i].gender, students[i].score);
    104     ////    }
    105     ////2找出成绩最高者
    106     //    Student maxScoreStu = {0};
    107     //    for (int i = 0; i < 5; i++) {
    108     //        if (students[i].score > maxScoreStu.score) {
    109     //            maxScoreStu = students[i];
    110     //        }
    111     //    }
    112     //    printf("%.2f 
    ", maxScoreStu.score);
    113     ////3年龄最小的
    114     //    Student minScoreStu = students[0];  //存储数组中第一个学生的信息,假设第一个学生的年龄最小, 与其他的学生进行比较。
    115     //    for (int i = 1; i < 5; i++) {
    116     //        if (students[i].age < minScoreStu.age) {
    117     //            minScoreStu = students[i];
    118     //        }
    119     //    }
    120     //    printf("%d
    ", minScoreStu.age);
    121     ////4按年龄升序排序
    122     //
    123     //
    124     //    for (int i = 0; i < 5 - 1; i++) {
    125     //        for (int j = 0; j < 5 - 1 - i; j++) {
    126     //            if(students[j].age > students[j + 1].age){
    127     //                Student temp = students[j];    //交换两个结构体变量, 而不是只交换年龄
    128     //                students[j] = students[j + 1];
    129     //                students[j + 1] = temp;
    130     //            }
    131     //        }
    132     //    }
    133     //       for (int i = 0; i < 5; i++) {
    134     //        printf("%d , %s 
    ", students[i].age, students[i].name);
    135     //    }
    136     
    137 /////////////////结构体的内存对齐方式  ,以占字节数最大的类型的字节的陪数分配
    138 //    typedef struct person{
    139 //        char name[12];
    140 //        char gender;
    141 //        int age;
    142 //    }Person;
    143 //    printf("%lu
    ", sizeof(Person));
    144 ////、、、、、结构体嵌套(在一个结构体中, 结构体的成员又是另外一个结构体的变量)
    145     typedef struct birth{
    146         int year;  //出生年份
    147         int month; //出生月份
    148         int day;   //出生日
    149     }Birth;
    150     
    151     typedef struct teacher{
    152         char name[12];//姓名
    153         int age;//年龄
    154         Birth birthday;
    155     }Teacher;
    156     
    157     Teacher tea1 = {"Jack", 24, {1991, 5, 23}};
    158     printf("%s, %d, %d, %d, %d", tea1.name, tea1.age, tea1.birthday.year, tea1.birthday.month, tea1.birthday.day);
    159     
    160     
    161     
    162     return 0;

     

  • 相关阅读:
    Git版本控制使用方法入门教程
    github 与git 使用 及配置
    IOS ASI http 框架详解
    block 理解及 简单回调
    IOS在Xcode 4.x以上如何 创建 和 添加 静态库
    dispatch queues GCD
    理解UIApplication(转)
    android 标签对应的代码怎么写alignParentRight、marginRight
    Android 资源文件的命名规范问题
    输入法弹出,屏幕自动适应
  • 原文地址:https://www.cnblogs.com/zhaoashen/p/4371039.html
Copyright © 2020-2023  润新知