day07 结构体 匿名结构体 结构体的初始化 访问 结构体嵌套 结构体的存储空间 结构体数组

// 结构体的定义
/*
 struct
结构体的定义 {
 
数据类型 成员变量 1;
 
数据类型 成员变量 2;
 .....
 
数据类型 成员变量 n;
 };
注意 “;”
 */

//typedef( 第一种方式 )
// 先定义结构体,在说明结构体变量
//struct myPoint{
//    float x;// 行坐标
//    float y;// 纵坐标
//};
//typedef struct myPoint point;

// 2 中方式
// 在定义结构体的同时,说明结构体变量
/*
typedef struct myPoint{
    float x;//
行坐标
    float y;//
纵坐标
}point;
 */


// 定义第三种方式
// 匿名结构体
//struct{
//    char name[20];
//    int age;
//    char sex;
//}stu1,stu2,stu3;
//
//typedef struct student
//{
//    char name[20];
//    int age;
//    int number;
//}Student;

typedef struct student1{
   
char name[ 20 ];
   
char sex;
   
int num;
   
int age;
   
float score;
}Student;


void sortStudentByScore( Student stu[], int count)
{
   
for ( int i= 0 ; i<count- 1 ; i++) {
       
for ( int j= 0 ; j<count- 1 -i; j++) {
           
if (stu[j]. score >stu[j+ 1 ]. score ) {
               
Student tmp=stu[j];
                stu[j]=stu[j+
1 ];
                stu[j+
1 ]=tmp;
            }
        }
    }
}

// 获取学生数组成绩的最高者
Student getStudentMaxByScore( Student stu[], int count)
{
   
Student maxScore={ 0 };
   
for ( int i= 0 ; i<count; i++)
    {
       
// 通过成绩比较最大值
        maxScore=stu[i].
score > maxScore. score ? stu[i] : maxScore;
    }
   
return maxScore;
}

Student getStudentMaxByAge( Student stu[], int count)
{
   
Student maxAge={ 0 };
   
for ( int i= 0 ; i<count; i++)
    {
       
// 通过成绩比较最大值
        maxAge=stu[i].
age > maxAge. age ? stu[i] : maxAge;
    }
   
return maxAge;
}

void printStudent( Student stu[], int count)
{
   
for ( int i= 0 ; i<count; i++) {
       
printf ( "name:%s,sex:%c,num:%d,age:%d,score:%.2f\n" ,stu[i]. name ,stu[i]. sex ,stu[i]. num ,stu[i]. age ,stu[i]. score );
    }
}

int main( int argc, const char * argv[]) {
#pragma mark- 结构体
    //struct myPoint point={2.0,5.3};// 定义一个数据类型为
   
//struct myPoint, 变量名为 point ,初值为( 2.0 5.3 )的一个变量。
   
//printf("%lu %lu",sizeof(struct myPoint),sizeof(point));
   
// 前面加 typedef ,后面加上 point ,以后可以用 point 代替结构体变量
   
//    Student stu1={"laowang",18,001};
//    Student stu2={"laozhang",80,002};
//    // 访问结构体 .
//    stu1.age=19;
//    stu2.number=003;
//    strcpy(stu1.name,"wangbadan");
//    printf("name=%s,age=%d,number=%d",stu1.name,stu1.age,stu1.number);
   
  
// Stu stu1={" 张三 ",'n',18,001,59.99};
  
// printf("name=%s,sex=%c,age=%d,number=%d,score=%.2f\n",stu1.name,stu1.sex,stu1.age,stu1.num,stu1.score);
   
// 给成员变量赋值
  
// stu1.age=28;
   
//Stu stu2={" 王五 ",'f',19,002,60.0};
  
// printf("name=%s,sex=%c,age=%d,number=%d,score=%.2f\n",stu2.name,stu2.sex,stu2.age,stu2.num,stu2.score);
   
//Tea tea1={" 张全福 ",'n',25,150,1200.00};
//    printf("name=%s,sex=%c,age=%d,height=%d,money=%.2f\n",tea1.name,tea1.sex,tea1.age,tea1.height,tea1.money);
#pragma mark- 匿名结构体
//    struct
//    {
//        char name[20];
//        short num;
//        char sex;
//        int age;
//    }stu={0};
//    struct {
//        float x;
//        float y;
//    }point1={0};
//    struct myPoint point2={0};
//    struct myPoint point3={0};
//    struct student stu4={" 张三丰 ",19,'m'};
//    printf("%s %d %s",stu4.name,stu4.age,stu4.sex);
#pragma mark- 结构体的初始化
    // 第一种初始化方法
   
// 先定义结构体,在初始化
//    struct student{
//        char name[20];
//        int age;
//        char sex;
//    };
//    struct student boy1={" 张三 ",29,‘f’};
   
// 误点
   
//struct student boy1.name="zhangsanfeng";// 错误
   
//strcpy(boy1.name,"zhangsanfeng");
   
// 误点
   
//boy1={"laowang",89,'m'};
   
// 正确 boy1= struct student {"laowang",89,'m'};
   
// 2 中初始化的方法
   
//struct student{
//        char name[20];
//        int age;
//        char sex;
//    }boy2={"wangnima",78,'m'};
#pragma mark- 访问
    // 更改姓名:
   
//strcpy(boy2.name," 你是谁 ");
  
// printf(" 姓名是: %s 年龄是: %d 性别是: %c",boy2.name,boy2.age,boy2.sex);
#pragma mark- 结构体嵌套
//    struct birthday{
//        int year;
//        int month;
//        int day;
//    };
//    struct teacher
//    {
//        char name[20];
//        int age;
//        char sex;
//        struct birthday bir;
//    };
//   
//    struct teacher tea={"laogao",49,'m',{1970,11,11}};
//    printf(" 姓名 :%s, 年龄 :%d, 性别 :%c, 生日是 :(%d %d %d)\n",tea.name,tea.age,tea.sex,tea.bir.year,tea.bir.month,tea.bir.day);
   
// 第一种方式   已经有了参数列表
  
// typedef struct struct birthday Birthday;
   
// 2 中方式
//   typedef struct birthday{
//        int year;
//        int month;
//        int day;
//    }Birthday;
#pragma mark- 结构体的存储空间
//    struct teacher{
//        char a;  //1
//        int c;   //4
//        long d;  //8
//        short e;  //2
//        double b;  //8
//        float f;//4
//    };
//    printf("%lu",sizeof(struct teacher));
#pragma mark- 结构体数组
   
   
Student xiaoMing={ "xiaoMing" , 'm' , 01 , 13 , 59.99 };
   
Student xiaoHong={ "xiaoHong" , 'f' , 02 , 12 , 89.9 };
   
Student xiaoTang={ "xiaotang" , 'f' , 03 , 14 , 100.00 };
   
Student xiaoWangBa={ "xiaoWangBa" , 'm' , 04 , 11 , 92.9 };
   
Student xiaoDong={ "xiaoDong" , 'm' , 05 , 15 , 15 };
   
   
Student stu[ 5 ]={xiaoMing,xiaoHong,xiaoTang,xiaoWangBa,xiaoDong};
   
   
   
// 调用
  
// sortStudentByScore(stu, 5);
   
//printStudent(stu, 5);
   
   
Student maxScore= getStudentMaxByScore (stu, 5 );
   
printf ( "name:%s,sex:%c,num:%d,age:%d,score:%.2f\n" ,maxScore. name ,maxScore. sex ,maxScore. num ,maxScore. age ,maxScore. score );
   
   
Student maxAge= getStudentMaxByAge (stu, 5 );
   
printf ( "name:%s,sex:%c,num:%d,age:%d,score:%.2f\n" ,maxAge. name ,maxAge. sex ,maxAge. num ,maxAge. age ,maxAge. score );
   
return 0 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值