iOS開發之c語言基礎Lesson-07 結構體 上課筆記 與 試題練習,ioslesson-07

來源:互聯網
上載者:User

iOS開發之c語言基礎Lesson-07 結構體 上課筆記 與 試題練習,ioslesson-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 \n", 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 \n", 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\n", 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 \n", 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\n", 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;

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.