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

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

1///////////////Lesson 07 结构体 上课笔记////////////////2//结构体作用:1.是一种自定义的数据类型,可以用来定义变量3//2.是一个大容器,比数组灵活,可以存储不同数据类型的变量4//结构体定义 :定义就是一种格式,一种模板, 只要按照该格式就能定义出结构体5//定义一个学生结构体6//typedef, 类型重定义,给类型重新取一个名字,新的名字和原有的名字的作用是一样的7//typedef两种定义方式:1.先定义结构体, 后typedef8//2.定义结构体的同时,定义typedef9typedefstructstudent{10intnum;11charname[20];12intage;13chargender;14floatscore;15}Student;16//点结构体17//struct point{18//int x;19//int y;20//};21//typedef struct point point;//point代替struct point22////矩形结构体23//struct rectangle{24//float width;25//float length;26//};27////立方体结构体28//struct cube{29//float width;30//float height;31//float length;32//};33intmain(intargc,constchar*argv[]) {34//@autoreleasepool {35////insert code here...36//NSLog(@"Hello, World!");37//}3839/////使用结构体类型(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);515253///////结构体变量的赋值操作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);6566////////////练习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);9192//////////////结构体数组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//}136137/////////////////结构体的内存对齐方式 ,以占字节数最大的类型的字节的陪数分配138//typedef struct person{139//char name[12];140//char gender;141//int age;142//}Person;143//printf("%lu\n", sizeof(Person));144////、、、、、结构体嵌套(在一个结构体中, 结构体的成员又是另外一个结构体的变量)145typedefstructbirth{146intyear;//出生年份147intmonth;//出生月份148intday;//出生日149}Birth;150151typedefstructteacher{152charname[12];//姓名153intage;//年龄154Birth birthday;155}Teacher;156157Teacher tea1 = {"Jack",24, {1991,5,23}};158printf("%s, %d, %d, %d, %d", tea1.name, tea1.age, tea1.birthday.year, tea1.birthday.month, tea1.birthday.day);159160161162return0;

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航