header.m#import "header.h" void printStudent(STUDENT boy){ printf("%-11s %d %d-%d-%d %c %.2f\n",boy.name,boy.age,boy.birthday.year,boy.birthday.month,boy.birthday.day, boy.sex,boy.score); } void printAllStudents(STUDENT stus[],int count){ for (int i = 0; i < count; i++) { printStudent(stus[i]); } } STUDENT findMaxScoreStudent(STUDENT stus[],int count){ STUDENT tem = {0}; for (int i = 0; i < count; i++) { if (tem.score < stus[i].score ) { tem = stus[i]; } } return tem; } void sortByScoreWithBubble(STUDENT stus[],int count){ for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - 1 - i; j++) { if (stus[j].score > stus[j + 1].score) { STUDENT tem = stus[j]; stus[j] = stus[j + 1]; stus[j + 1] = tem; } } } printAllStudents(stus,count); } void sortByScoreWithChoose(STUDENT stus[],int count){ for (int i = 0; i < count - 1; i++) { int min = i; for (int j = i + 1; j < count; j++) { if (stus[j].birthday.year < stus[min].birthday.year) { min = j; } } if (min != i ) { STUDENT tem = stus[min]; stus[min] = stus[i]; stus[i] = tem; } } printAllStudents(stus, 5); } |
header.h#import <Foundation/Foundation.h> typedef struct Date{ int year; int month; int day; }MYDath; typedef struct Students{ char name[20]; int age; MYDath birthday; char sex; float score; }STUDENT; void printStudent(STUDENT boy); void printAllStudents(STUDENT stus[],int count); STUDENT findMaxScoreStudent(STUDENT stus[],int count); void sortByScoreWithBubble(STUDENT stus[],int count); void sortByScoreWithChoose(STUDENT stus[],int count); |
main.m#import <Foundation/Foundation.h> #import "header.h" //void test(){ // for (int i = 0; i < 5; i++){ // printf("从前有座山,山上有个老和尚,老和尚给小和尚讲:"); // test(); // if (i == 5) { // break; // } // } //} //long fact(int n){ // if (n == 1) { // return n; // } // return n * fact(n - 1); // //} //struct student{ // int number;//结构体类型的成员变量列表 // char name[20]; // int age; // char sex; // float score; //}; // //typedef struct Date{ // int year; // int month; // int day; //}MYDath; // // ////结构体类型所占用的内存空降是最大数据类型的整数倍 ////因为结构体类型的变量在分配内存是有"内存对齐" // ////第二种,定义结构体同事tppedef //typedef struct Students{ // char name[20]; // int age; // MYDath birthday; // char sex; // float score; //}STUDENT;//新类型名 // ////类型重定义,给现有的类型重新起别名 //// 原类型 新类型 //typedef int Integer; //typedef struct Students Stu; int main(int argc, const char * argv[]) { // //函数的递归调用 //// test(); // // //函数的递归调用(允许函数体里面再次调用函数本身) // //使用递归一定要有出口; // long s = fact(6); // printf("s = %ld",s); //结构体.构造类型 // struct student{ // int number; // char name[20]; // int age; // char sex // float score; // }; // struct student boy1 ={1,"zhangshan",19,'M',89.05}; // struct student girl2 = boy1; // girl2.sex = 'F'; // // printf("%d, %s, %d, %c, %f\n",boy1.number,boy1.name,boy1.age,boy1.sex,boy1.score); // printf("%d, %s, %d, %c, %f",girl2.number,girl2.name,girl2.age,girl2.sex,girl2.score); // printf("\n%lu",sizeof(struct student)); // STUDENT boy1 = {"zhangyishan", 19, {1992,5,6}, 'M', 59.99}; // struct Students girl1 = {"lisi", 20, {1996,4,6},'w',89.98}; // STUDENT girl2 = {"lose", 18, {1995,1,5},'W',76.59}; // struct Students girl3 = {0}; // // boy1.score = 69.89; // girl1.score = 99.21; // girl3.score = 89.99; // strcpy(girl3.name, "annt"); // girl2.birthday.year = 1969; // printf("%-11s, %d, %c, %.2f\n",boy1.name,boy1.age,boy1.sex,boy1.score); // printf("%-11s, %d, %c, %.2f\n",girl1.name,girl1.age,girl1.sex,girl1.score); // printf("%-11s, %d, %c, %.2f\n",girl2.name,girl2.age,girl2.sex,girl2.score); // struct Students stu = {0}; // stu = boy1.score > girl1.score ? boy1 : girl1; // stu = stu.score >girl2.score ? stu :girl2; //数组不可以直接赋值,但是结构体类型的变量可以 //boy1 = girl2; // stu = (boy1.score > girl1.score ? boy1.score : girl1.score) > girl2.score ? // (boy1.score > girl1.score ? boy1 : girl1) : girl2; // // printf("%s %d %c %.2f",stu.name,stu.age,stu.sex,stu.score); // struct Students minAgeStu = {0}; // // minAgeStu = (boy1.age < girl2.age ? boy1.age : girl2.age) < girl1.age ? (boy1.age < girl2.age ? boy1 : girl2) : girl1; // printf("%-11s %d %d-%d-%d %c %.2f",minAgeStu.name,minAgeStu.age,minAgeStu.birthday.year,minAgeStu.birthday.month,minAgeStu.birthday.day, minAgeStu.sex,minAgeStu.score); // printStudent(girl2); // printStudent(boy1); // printStudent(girl1); STUDENT stus[5] = { {"zhangyishan", 19, {1992,5,6}, 'M', 59.99}, {"lisi", 20, {1996,4,6},'w',89.98}, {"lose", 18, {1995,1,5},'W',76.59}, {"shanyang", 19, {1993,8,5},'M',96.55}, {"yanzi", 16, {1994,9,4},'W',78.96}, }; // // for (int i = 0; i < 5; i++) { // printStudent(stus[i]); // } // printAllStudents(stus,5); //打印最大成绩的那个学生 // STUDENT tem = findMaxScoreStudent(stus, 5); // printStudent(tem); // printStudent(findMaxScoreStudent(stus,5)); // sortByScoreWithBubble(stus,5); sortByScoreWithChoose(stus,5); // printf("%s %d",stus[2].name,stus[2].age); // printStudent(stus[3]); // printf("%lu",sizeof(struct Students)); return 0; } |