函数(结构体)

//

//  main.m

//  C6_多文件,结构体

//

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

//  Copyright © 2015 dllo. All rights reserved.

//


#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.

//  Copyright © 2015 dllo. All rights reserved.

//


#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.

//  Copyright © 2015 dllo. All rights reserved.

//


#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值