小小君的C语言第七课

Function.h 文件:

#import <Foundation/Foundation.h>


// 取消结构体自动补齐

// 结构体在内存中计算占多少字节

// 计算的顺序由上至下依次计算

// 一个成员变量一个成员变量的计算

// 并且自动补齐为该结构体中所占最大字节数的变量的字节数来进行补齐的

// 如果结构体中最大的是 float ,就按4 的倍数补齐。 最大的是double 就按8的倍数补齐

#pragma pack(1);


/*

 声明一个结构体

 关键字 struct

 

 struct 结构体名字{

 类型名1 变量名1;

 类型名2 变量名2;

 ......

 

 */


// 声明一个描述学生的结构体

// 结构体中声明的变量 一般叫做 成员变量

// 20 + 1 + 8 + 4 + 4 自动按8的倍数补齐,sizeof40

typedef struct Student{

    char name[20];        // 注意 中间用分号分离

    char sex;

    double number;

    float score;

    int age;

}Student;          // 声明结束 也用分号结束




// 声明描述一个点的结构体


struct MyPoint{

    int x;

    int y;

};

typedef struct MyPoint MyPoint;


// 声明描述一个日期的结构体


struct Date{

    int year;

    int month;

    int day;

};

typedef struct Date Date;


// 变量的数据类型 变量名以前 全是类型

void printfStudent(struct Student stu1);


struct HomePerson{

    char mother[20];

    char father[20];

};

typedef struct HomePerson HomePerson;



// 需求:描述一个人的出生年月日和家庭成员

// 结构体中可以有其他结构体当作成员变量

// 结构体中可以嵌套结构体

struct Person{

    char name[20];

    Date birthday;

    HomePerson home;

};

typedef struct Person Person;


// 声明一个函数 冒泡排序

void SortStudent(struct Student stu[], int count);


Function.m 文件:

#import "Function.h"




void printfStudent(struct Student stu1){

    printf("name = %s  sex = %c  age = %d  number = %.0f  score = %.2f \n", stu1.name, stu1.sex, stu1.age, stu1.number, stu1.score);

}





void SortStudent(struct Student stuArr[], int count){

    // 注意1 比的是数组中 结构体变量的分数

    // 注意2 交换的是数组中 结构体变量

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

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

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

                Student temp = stuArr[j];

                stuArr[j] = stuArr[j + 1];

                stuArr[j + 1] = temp;

            }

        }

    }

    

    printfStudent(stuArr[0]);

    

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

        // 打印的是数组中 每个结构体变量

        printfStudent(stuArr[i]);

    }

}


main.m 文件:

#import <Foundation/Foundation.h>

#import "Function.h"


// 声明一个匿名结构体  没有名字


struct {

    char name[20];

    char sex;

    int age;

}stu1 = {"dajun", 'x', 23},

stu2= {"mtt", 'x', 23};



// 起别名 给类型起别名

// 使用关键字 typedef 老名字 新名字


typedef int DaJun;


// float起个别名

typedef float Ff;


// int array[5]起个别名

typedef int myArray[5];


// 给结构体起名

// 描述一个男的

struct Man{

    char name[20];

    float height;

};

typedef struct Man SupperMan;


// 声明结构体和起别名 连一起写

typedef struct Wenman{

    char name[20];

    char sex;

}SupperWenmen;


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

    

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

       

    // 定义一个结构体变量

    // struct 结构体名 变量名={初值};

    // 初值的顺序 需要跟声明时相同

    // 取出结构体中的成员变量的值

    // 结构体变量 . 成员变量

    

    // dajun

    struct Student stu1 = {"dajun", 'x', 12, 90, 23};

    struct Student stu2 = {"wangfeng", 'n', 25, 88, 27};

    struct Student stu3 = {"mtt", 'x', 14, 95, 23};

    struct Student stu4 = {"zp", 'n', 23, 60, 23};

    

    printf("%s\n", stu1.name);

    printf("%d\n", stu1.age);

    // 修改结构体变量的成员变量的值

    stu1.age = 22;

    printf("%d\n", stu1.age);

    

    // 修改名字

    strcpy(stu1.name, "dj");

     printf("%s\n", stu1.name);

    

    

    // 编写个函数打印学生结构提的所有信息

    

    printfStudent(stu1);

    

    

    

    // 起别名(外号)

    

    int a = 6;

    DaJun b = 9;

    

    myArray aa = {1, 3, 5, 7, 9};

    

    struct Man m1 = {"aa",180.2};

    SupperMan m2 = {"bb", 183.1};

    

    Date d1 = {2015, 10, 16};

    

    

    // 声明三个学生

    Student s1 = {"dj", 'x', 14, 88, 23};

    Student s2 = {"mtt", 'x', 15, 78, 24};

    Student s3 = {"cqq", 'x', 16, 98, 25};

    

    // 找出分数最高者

    float score = 0;

    score = s1.score > s2.score ? s1.score : s2.score;

    score = score > s3.score ? score : s3.score;

    printf("max = %.2f\n", score);

    if (score == s1.score) {

        printfStudent(s1);

    }else if(score == s2.score){

        printfStudent(s2);

    }else{

        printfStudent(s3);

    }

    

    // 找出年龄最小者

    int age = 0;

    age = s1.age < s2.age ? s1.age : s2.age;

    age = age < s3.age ? age : s3.age;

    printf("min = %d\n", age);

    if (age == s1.age) {

        printfStudent(s1);

    }else if(age == s2.age){

        printfStudent(s2);

    }else{

        printfStudent(s3);

    }

    

    

    // 结构体变量可以直接进行赋值 赋值过程是一个拷贝的过程

//    Student s4 = s1;

//    printfStudent(s4);

    

    // 数组不能直接进行赋值 数组名字是首元素的地址 是常量 是程序运行期间不能改的

    

    

    // 数据类型 数组名[元素个数] = {初值};

 //   Student studentArray[] = {s1, s2, s3, s4};

    

    

    

    // 声明一个person的结构体变量

    // 声明一个date结构体变量

    Date date1 = {1993,2,15};

    HomePerson h1 = {"mama", "die"};

    Person p1 = {"dajun", date1 , h1};

    printf("%s  %s\n", p1.name, p1.home.father);

   

    

   //  5名学⽣保存在结构体数组中,编程查找 成绩最高者,输出该学生全部信息

    Student s4={"zp", 'x', 17, 88, 23};

    Student s5={"zz", 'x', 18, 96, 25};

    

    // 5个学生 放进一个数组中

    Student stuArr[5] = {s1, s2, s3, s4, s5};


    

//    Student tempStu =  stuArr[2];

//    tempStu.name;

    


    SortStudent(stuArr, 5);

    

    

    

    

    return 0;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值