C language basics of iOS development Lesson-07 structure class notes and exam exercises, ioslesson-07
1 /////////// Lesson 07 structure class notes ///////// // 2 // struct function: 1. is a custom data type, which can be used to define variable 3 // 2. it is a large container that is more flexible than arrays and can store variables of different data types. 4 // struct definition: definition is a format, a template, you can define struct 5 // define a student struct 6 // typedef according to the format, type redefinition, and type a new name, the new name and the original name are used in the same 7 // typedef two definition methods: 1. first define the struct, then typedef 8 // 2. when defining struct, define typedef 9 typedef struct student {10 int num; 11 char name [20]; 12 int age; 13 char gender; 14 float score; 15} Student; 1 6 // point struct 17 // struct point {18 // int x; 19 // int y; 20 //}; 21 // typedef struct point; // point instead of struct point 22 // rectangular structure 23 // struct rectangle {24 // float width; 25 // float length; 26 //}; 27 /// cube struct 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 // use the struct type (struct + struct name) and define the struct variable 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 /////// access the struct variable of the struct. struct member 48 // printf ("% s", stu1.name); 49 // strcpy (stu1.name, "Ashen-Zhao"); 50 // printf ("% s", stu1.name ); 51 52 53 // assign values to struct variables 54 // Student stu1 = {"Ashen", 33, 'M '}; 55 // Student stu2 = {0}; 56 // Student stu3 = {0}; 57 // 1. A single member is assigned 58 // strcpy (stu2.name, stu1.name); 59 // stu2.age = stu1.age; 60 // stu2.gender = stu1.gender; 61 // 2. structure Variable overall copy 62 // if you want to copy the elements in an array to another array, the array cannot be directly assigned by the assign value (=, in this case, you can place the array in the struct and use the struct variable to complete the copy operation. 63 // stu3 = stu1; 64 // printf ("% s", stu3.name ); 65 66 // exercise 67 // Student stu1 = {"Ashen1", 21, 'M', 90 }; 68 // Student stu2 = {"ying", 22, 'F', 80}; 69 // Student stu3 = {"zhao", 23, 'M', 100 }; 70 // 71 // 72 /// identify the persons whose scores are the highest. 73 // Student maxScoreStu = {0 }; // 74 people with the highest storage score // 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 // structure array 93 // define the structure array, store 5 students 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 Output all Student Information 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. The highest score is 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, minimum age 114 // Student minScoreStu = students [0]; // stores the information of the first Student in the array, assume that the first student has the minimum age and is compared with other students. 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 sort by age in ascending order 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]; // exchange two struct variables instead of only age 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 // The Memory alignment of the struct, 138 // typedef struct person {139 // char name [12]; 140 // char gender; 141 // int age; 142 //} Person; 143 // printf ("% lu \ n", sizeof (Person )); 144 ///, and struct nesting (in a struct, the struct member is another struct variable) 145 typedef struct birth {146 int year; // Year of Birth 147 int month; // month of Birth 148 int day; // Date of Birth 149} Birth; 150 151 typedef struct teacher {152 char name [12]; // name 153 int age; // age 154 Birth birthday; 155} Teacher; 156 157 Teacher tea1 = {"Jack", 24, {1991, 5, 23 }}; 158 printf ("% s, % d", tea1.name, tea1.age, tea1.birthday. year, tea1.birthday. month, tea1.birthday. day); 159 160 161 162 return 0;