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 // 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;
新聞熱點
疑難解答