iOS C语言7.2_结构体多文件

来源:互联网 发布:360压缩软件mac版 编辑:程序博客网 时间:2024/05/22 02:28

.h 声明文件

//

//  MyFunction.h

//  C7_结构体多文件

//

//  Created by dllo on 15/7/8.

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


#import <Foundation/Foundation.h>

// .h文件, 我们写函数的声明

// .m文件,写函数的定义

// 声明一个学生类型的结构体,typedef设置

struct student{

    

   int stuAge;

   float stuScore;

   char stuSex;

   char stuName[20];

};

typedef struct student Student;


struct test{

   int a;

    

   int b;

   char str[17];

   int c;

};

typedef struct test Test;










// 打印学生类型变量的所有内容

void printStu(Student stu);

//找到三个学生成绩最好的人,并且把这个人返回

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

// 找学生平均年龄

int avgStuAge(Student stu1,Student stu2,Student stu3);

// 定义一个关于坐标的结构体

struct Cpoint{

   int x;

   int y;

};

typedef struct Cpoint CPoint;

// 1.判断两个点是否在一个水平线上,返回BOOL

BOOL horizontalRule(CPoint p1,CPoint p2);

// 2.判断垂直

BOOL vertical(CPoint p1,CPoint p2);

//3.判断是否相等

BOOL samePoint(CPoint p1,CPoint p2);

// 判断一年的第几天

struct days{

   int year;

   int month;

   int day;

};

typedef struct days Days;

// 根据年月日,计算第几天

int dayOfYear(Days day);



// 通过函数,对学生类型的结构体数组,进行冒泡排序

void bubbleSortStu(Student stu[],int count);

// 找到及格的学生的个数

int passTestStu(Student stu[],int count);

// 找到姓名的首字母是l的人数

int initialName(Student stu[],int count);

// 结构体的嵌套使用

struct clssRoom{

   char roomName[20];    // 房间名

   Student stu;

};


struct school{

   char schoolName[20];

   struct clssRoom room;

};














.m实现文件

//

//  MyFunction.m

//  C7_结构体多文件

//

//  Created by dllo on 15/7/8.

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


#import "MyFunction.h"

// 多文件的话,一定要引头文件



//void printStu(Student stu){

//    printf("age=%d,sex=%c,score=%g,mame=%s\n",stu.stuAge,stu.stuSex,stu.stuScore,stu.stuName);

//}

//

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

//    Student max = stu1.stuScore > stu2.stuScore ? stu1 :stu2;

//    max = max.stuScore > stu3.stuScore ? max:stu3;

//    return max;

//

//}




//BOOL horizontalRule(CPoint p1,CPoint p2){

//    if (p1.y == p2.y) {

//        return YES;

//    }return NO;

//}

//

//BOOL vertical(CPoint p1,CPoint p2){

//    if (p1.x == p2.x) {

//        return YES;

//    }return NO;

//}

//

//

//BOOL samePoint(CPoint p1,CPoint p2){

//    if (p1.x == p2.x && p1.y == p2.y) {

//        return YES;

//    }return NO;

//}

//

//

//int dayOfYear(Days day){

//    

//    switch (day.month - 1) {

//            

//        case 11:

//            day.day += 30;

//        case 10:

//            day.day += 31;

//        case 9:

//            day.day += 30;

//        case 8:

//            day.day += 31;

//        case 7:

//            day.day += 31;

//        case 6:

//            day.day += 30;

//        case 5:

//            day.day += 31;

//        case 4:

//            day.day += 30;

//        case 3:

//            day.day += 31;

//        case 2:

//            if ((day.year % 4 == 0 && day.year % 100 != 0) || day.year % 400 == 0 ) {

//                day.day += 29;

//            }day.day += 28;

//        case 1:

//            day.day += 31;

//            break;

//            

//        default:

//            break;

//    }return day.day;

//    

//    

//    

//    

//    

//}



//void bubbleSortStu(Student stu[],int count){

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

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

//            if (stu[j].stuScore > stu[j+1].stuScore) {

//                Student temp = stu[j];

//                stu[j]=stu[j+1];

//                stu[j+1]=temp;

//            }

//        }

//    }

//    

//    

//}

//int personCount = 0;

//int passTestStu(Student stu[],int count){

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

//        if (stu[i].stuScore >= 60) {

//        personCount = personCount+1;

//        }

//    }

//    return personCount;

//    

//    

//    

//}

//

//int num=0;

//int initialName(Student stu[],int count){

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

//        if (stu[i].stuName[0] == 'l' ) {

//            num++;

//        }

//    }

//    return num;

//}


























main主文件

//

//  main.m

//  C7_结构体多文件

//

//  Created by dllo on 15/7/8.

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "MyFunction.h"

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

    

    

 //使用类型,定义变量

//Student stu ={20,99.5,'w',"zhangsan"};

//    

//    printStu(stu);

//    Student temp =stu;

//    printStu(temp);

//    // 结构体可以进行直接赋值但是数组不可以

    

//    Student stu1={21,90,'w',"zhangsan"};

//    Student stu2={20,90.5,'w',"lisi"};

    //找到两个人中较大的成绩

    // 条件运算符

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

//    printf("%g",score);

    

    //找到成绩最好的人

//    if (stu1.stuScore > stu2.stuScore) {

//    printStu(stu1);

//    }printStu(stu2);

//    Student temp = stu1.stuScore > stu2.stuScore?stu1:stu2;

//    printStu(temp);

    

    // 定义三个学生

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

//    Student stu2 = {21,90.5,'w',"lisi"};

//    Student stu3 = {15, 97,'m',"wangwu"};

//    Student temp=maxScore(stu1, stu2, stu3);

//    printStu(temp);

//

    

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

    

    

//    // 写两个点

//    CPoint p1={19,7};

//    CPoint p2={25,7};

//    if (horizontalRule(p1,p2)) {

//        printf("\n");

//    }else{

//        printf("不是\n");

//    }

    

    

    // 结构体数组

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

//    Student stu2 = {21,90.5,'w',"lisi"};

//    Student stu3 = {15, 40,'m',"wangwu"};

//    Student stu[3]={stu1,stu2,stu3};

    

//    // 打印第二个人的姓名

//    printf("%s\n",stu[1].stuName);

//    

//    for (int i=0; i<3; i++) {

//        printf("%s\n",stu[i].stuName);

//    }

    

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

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

//            if (stu[j].stuScore > stu[j+1].stuScore) {

//                Student temp =stu[j];

//                stu[j]=stu[j+1];

//                stu[j+1]=temp;

//            }

//                

//            }

    

    

    

    

    

    

    

    

//    bubbleSortStu(stu, 3);

//

//    for (int i=0; i<3; i++) {

//            printf("%s ",stu[i].stuName);

//        }


    

    

    

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

//    Student stu2 = {21,90.5,'w',"lisi"};

//    Student stu3 = {15, 40,'m',"wangwu"};

//    Student stu4={18,46,'m',"liushanshan"};

//    Student stu5={19,47,'m',"yanglin"};

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

//    

////    printf("%c\n",stu[0].stuName[0]);

//   

//    int result=initialName(stu,5);

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

//    

    

//    // 定义一个学生类型的变量

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

//    

//    struct clssRoom room = {"yanfaqi",stu1};

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

//    

//    

//    struct school sch={"lanou",room};

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

    

    

    

    

    

  

    

    

    

    

    

   return 0;

}











0 0
原创粉丝点击