C7_结构体多文件

来源:互联网 发布:分级基金套利软件 编辑:程序博客网 时间:2024/06/06 02:33

//

//  MyFunction.h

//  C7_结构体多文件

//

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

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

//


#import <Foundation/Foundation.h>

// .h文件写函数的声明


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

struct student{

   int stuAge;

   float stuScore;

   char stuSex;

   char stuName[20];

};

typedefstruct student Student;

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

void printStu(Student stu);// stu是形参名字可以随意


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

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

// 找到桑学生的平均成绩

float averageScore(Student stu1, Student stu2, Student stu3);

// 找这三个学生的平均年龄

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




////**************************************************************

struct test{

   long g;

   short a ;

   char b;

    char str[19];// 此处所分配的字节数应为int所占字节即4的倍数,每一个成员变量分配的字节数为其中最长的变量占的字节数

   int c;

};

typedef struct test Test;





////**************************************************************

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

struct CPoint{

   int x;

   int y;

   // int z;

};

typedefstruct CPoint CPoint;


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

BOOL horizontalline(CPoint spot1, CPoint spot2);


// 2. 判断两个点是否在垂直线上

BOOL perpendicular(CPoint p1, CPoint p2);

BOOL verticaline(CPoint p1, CPoint p2);

// 3. 判断两个点是否相等


BOOL equal(CPoint p1, CPoint p2);




////************************************************************

// 定义一个关于日期的结构体

struct date{

   int year;

   int month;

   int day;

};

typedef struct date date;



// 根据年月日,计算这一天是一年中的第几天


int dayOfYear(date date);




////************************************************************

// 结构体数组

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

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



////************************************************************

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

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


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

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


////************************************************************

// 结构体的嵌套使用


struct classRoom{

   char roomName[20];// 房间名

    Student stu;

};


struct school{

   char schoolName[20];

    // struct classRoom room;

   struct classRoom room;

    

};





//

//  MyFunction.m

//  C7_结构体多文件

//

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

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

//


#import "MyFunction.h"

// .m写函数的定义


void printStu(Student stu){

    printf("age = %d, sex = %c, score = %g, name = %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;

}


float averageScore(Student stu1, Student stu2, Student stu3){

   float aver = (stu1.stuScore + stu2.stuScore + stu3.stuScore) /3;

   return aver;

}

int averageAge(Student stu1, Student stu2, Student stu3){

   int averageage = (stu1.stuAge + stu2.stuAge + stu3.stuAge) /3;

   return averageage;

}




////**************************************************************

BOOL horizontalline(CPoint spot1, CPoint spot2){

   if (spot1.y == spot2.y) {

       return YES;

    }else{

       return NO;

    }

}

BOOL verticaline(CPoint p1, CPoint p2){

   return  p1.x == p2.x;

}

BOOL perpendicular(CPoint p1, CPoint p2){

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

       return YES;

    }else {

       return NO;

    }

}


BOOL equal(CPoint p1, CPoint p2){

    // 或者直接用 return (p1.x == p2.x) && (p1.y ==p2.y);

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

       return YES;

    }else {

       return  NO;

    }

}




////**************************************************************

int dayOfYear(date date){

   switch (date.month - 1) {

       case 11:

            date.day +=30;

       case 10:

            date.day +=31;

       case 9:

            date.day +=30;

       case 8:

            date.day +=31;

       case 7:

            date.day +=31;

       case 6:

            date.day +=30;

       case 5:

            date.day +=31;

       case 4:

            date.day +=30;

       case 3:

            date.day +=31;

       case 2:

            date.day +=30;

       case 1:

            date.day +=31;

           if (date.year % 400 ==0|| (date.year % 4 ==0 && date.year % 10 !=0)) {

                date.day = date.day;

            }else {

                date.day +=1;

               break;

            }

          

    }

   return  date.day;

}



////***********************************************************

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

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

   for (int i =0; i < count ; 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 passExamStu(Student stu[],int count){

   int num = 0;

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

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

            num++;

        }

    }

   return num;

}


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

int findinitial(Student stu[],int count){

   int num = 0;

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

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

            num++;

        }

    }

   return num;

}




//

//  main.m

//  C7_结构体多文件

//

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

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

//


#import <Foundation/Foundation.h>

// 多文件,一定要先引头文件

#import "MyFunction.h"

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

//   // 这里加入函数的调用

//    

//    // 使用类型,定义变量

//    Student stu = {20, 90.5, 'w', "wangwu"};

//    printStu(stu);

//    

//    Student temp = stu;

//    printStu(temp);

//    // 结构体可以进行直接赋值,数组不能

//

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

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

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

//    // 条件运算符

//    

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

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

//    

//    // 找到成绩最好的学生信息

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

//    printStu(stu);

    

//  // 比较三个人谁的成绩最好

//    // 定义三个学生

//    // 调用比较的函数

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

//    Student stu2 = { 90, 60, 'w', "lisi"};

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

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

//    printStu(temp);

//    

    

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

//    

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

//    

//    

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

//    Student stu2 = { 90, 60, 'w', "lisi"};

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

//    float  temp = averageScore(stu1, stu2, stu3);

//    printf("%g\n", temp);

//    printf("%d\n",averageAge(stu1, stu2, stu3));

    

    

////***********************************************************

//    // 两个点

//    CPoint p1 = {5, 6};

//    CPoint p2 = {8, 6};

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

//        printf("\n");

//    } else {

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

//    }

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

//        printf("\n");

//    } else {

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

//    }

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

//        printf("\n");

//    } else {

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

//    }


////*************************************************************

//    date day = {};

//    scanf("%d %d %d", &day.year, &day.month, &day.day);

//    int dayNum = dayOfYear(day);

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

//    printf("\n");

    

////*************************************************************

//    // 结构体数组

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

//    Student stu2 = {30, 90.1, 'm', "lisi"};

//    Student stu3 = {82, 55, 'w', "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; i++) {

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

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

//                // 交换的是人,不是成绩

//                Student temp = stu[j];

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

//                stu[j + 1] = temp;

//            }

//        }

//    }

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

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

//    }

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

//        printStu(stu[k]);

//    }

//

    

//    // 以函数的形式进行冒泡排序

//    // 调用排序的函数

//    bubbleSortStu(stu, 3);

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

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

//    }

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

//        printStu(stu[k]);

//    }

//

    

////************************************************************

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

//    Student stu2 = {30, 90.1, 'm', "lisi"};

//    Student stu3 = {82, 55, 'w', "liushanshan"};

//    Student stu4 = {19, 61, 'm', "wangwu"};

//    Student stu5 = {21, 70, 'w', "yangyang"};

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

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

//    printf("及格人数:%d\n",passExamStu(stu, 5));

//    

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

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

//    printf("%d\n",findinitial(stu, 5));

    

////***********************************************************

    //结构体的嵌套使用

    Student stu1 = {20,70.5, 'w', "zhangsan"}; // stu1函数名

   struct classRoom room = {"yanfaqi", stu1};

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

    printf("%s\n", room.stu.stuName);// stu成员变量

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

    printf("%s\n", sch.room.roomName);

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

    

    printf("\n");

   return 0;

}










0 0
原创粉丝点击