# 函数(结构体)

110人阅读 评论(0)

//

//  main.m

//  C6_多文件,结构体

//

//  Created by dllo on 15/11/18.

//

#import <Foundation/Foundation.h>

#import "MyFunction.h"

int main(int argc, const char * argv[]) {

//定义三个学生变量

//    Student stu1={20,'w',"jj",90};

//    Student stu2={25,'m',"zhangsan",66};

//    Student stu3={22,'m',"fd",78};

// printStu(stu1);

//  getMaxScore(stu1,stu2,stu3);

////三个学生,找到最高成绩,并返回

//  printf("%.1f\n",maxScore(stu1,stu2,stu3));

//

//   Student maxStu=getMaxScore(stu1,stu2,stu3);

//    printStu(maxStu);

//

//    getMinAge(stu1,stu2,stu3);

//

//结构体数组

Student stu1={20,'w',"zhangsan",60};

Student stu2={22,'m',"lisi",53};

Student stu3={16,'m',"wangwu",70.5};

Student stu4={17,'w',"shenliu",90};

Student stu5={19,'w',"tianqi",50};

Student allStu[5]={stu1,stu2,stu3,stu4,stu5};

//

//    int childCount = children(allStu,5);

//    printf("%d\n",childCount);

//

//

//    int passCount=passChildren(allStu, 5);

//    printf("%d\n",passCount);

//

//    Student maxStu=getMaxScore(allStu, 5);

//    printStu(maxStu);

//

//    sortByScore(allStu,5);

//    printf("%ld\n",sizeof(Test));

Room room ={"yamfa4",stu1};

printf("%s\n",room.stu.stuName);

return 0;

}

///////////////////////////////////////////////////////////////////////

//

//  MyFunction.h

//  C6_多文件,结构体

//

//  Created by dllo on 15/11/18.

//

#import <Foundation/Foundation.h>

//结构体的声明

struct student{

int stuAge;

char stuSex;

char stuName[20];

float score;

};

typedef struct student Student;

void printStu(Student stu);

//打印学生的所有信息

//void printStu(Student stu);

//

////参数是三个学生,把成绩最高的那个学生返回给主函数

//

//Student getMaxScore(Student stu1,Student stu2,Student stu3);

//

////三个学生,找到最高成绩,并返回

//float maxScore(Student stu1,Student stu2,Student stu3);

//

//

////

//Student getMinAge(Student stu1,Student stu2,Student stu3);

//

//

////找到数组里未成年的个数,并且返回

//int children(Student allStu[],int count);

//

//

////及格以上的成绩个数

//int passChildren(Student allStu[],int count);

//找到成绩最高的人,并返回

Student getMaxScore (Student allStu[],int count);

//对五个人依据成绩进行排序

void sortByScore(Student allStu[],int count);

struct test{

int a;

int c;

char b;

char arr[19];

};

typedef struct test Test;

//结构体的嵌套

struct classRoom{

char roomName[20];

Student stu;

};

typedef struct classRoom Room;

//////////////////////////////////////

//

//  MyFunction.m

//  C6_多文件,结构体

//

//  Created by dllo on 15/11/18.

//

#import "MyFunction.h"

//void printStu(Student stu){

//

//    printf("姓名:%s, 性别:%c, 年龄:%d, 成绩:%g\n",stu.stuName,stu.stuSex,stu.stuAge,stu.score);

//    //打印中文会影响代码提示

//}

void printStu(Student stu){

printf("姓名:%s, 性别:%c, 年龄:%d, 成绩:%g\n",stu.stuName,stu.stuSex,stu.stuAge,stu.score);

}

//参数是三个学生,把成绩最高的那个学生返回给主函数

//Student getMaxScore(Student stu1,Student stu2,Student stu3){

//    Student maxScore=stu1.score>stu2.score?stu1:stu2;

//    maxScore.score>stu3.score?maxScore:stu3;

//    printf("%s\n",maxScore.stuName);

//    return maxScore;

//}

//三个学生,找到最高成绩,并返回

//float maxScore(Student stu1,Student stu2,Student stu3){

//    float maxScore=stu1.score>stu2.score?stu1.score:stu2.score;

//    return maxScore>stu3.score?maxScore:stu3.score;

//}

//找到年龄最小的人

//Student getMinAge(Student stu1,Student stu2,Student stu3){

//

//    Student minAge=stu1.stuAge<stu2.stuAge?stu1:stu2;

//    printf("%s\n",minAge.stuName);

//    return minAge.stuAge<stu3.stuAge?minAge:stu3;

//}

Student getMinAge(Student stu1,Student stu2,Student stu3){

Student minAge=stu1.stuAge<stu2.stuAge?stu1:stu2;

printf("%s\n",minAge.stuName);

return minAge.stuAge<stu3.stuAge?minAge:stu3;

}

//

int children(Student allStu[],int count){

int num=0;

for (int i=0; i<count ; i++) {

if (allStu[i].stuAge<18) {

num++;

}

}

return num;

}

//

int passChildren(Student allStu[],int count){

int num = 0;

for (int i = 0; i< count; i++) {

if (allStu[i].score>=60) {

num++;

}

}

return num;

}

//

//Student getMaxScore (Student allStu[],int count){

//    Student maxScore=allStu[0];

//    for (int i = 1; i<count; i++) {

//        maxScore=maxScore.score>allStu[i].score?maxScore:allStu[i];

//    }

//    printf("%s\n",maxScore.stuName);

//    return maxScore;

//

//}

Student getMaxScore (Student allStu[],int count){

Student maxStu={};

for (int i =0; i<count; i++) {

if (maxStu.score<allStu[i].score) {

maxStu= allStu[i];

}

}

return maxStu;

}

//

void sortByScore(Student allStu[],int count){

for (int i = 0; i<count-1; i++) {

for (int j = 0; j<count-1-i; j++) {

if (allStu[j].score<allStu[j+1].score) {

Student temp=allStu[j];

allStu[j]=allStu[j+1];

allStu[j+1]=temp;

}

}

}

for (int i = 0; i<count ; i++) {

printStu(allStu[i]);

}

}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：10325次
• 积分：499
• 等级：
• 排名：千里之外
• 原创：42篇
• 转载：0篇
• 译文：0篇
• 评论：1条
文章分类
文章存档