雪*C语言结构体总结七

1.结构体 :是一种自定义的数据类型

声明   struct  结构体名称{

              类型说明符  成员名;

                 。。。。。。。

             类型说明符 成员名;

};

struct student 1{

  int num;

  char name[20];

  float score;

};

对结构体变量的定义:

由结构体修饰的变量叫做结构体变量;

struct

struct  结构体名student1  变量名stu1 = {12,"zhangsan",90};

struct  结构体名student1  变量名stu2 = {13,"lisi",80};

输出:变量名.成员名

printf(“学号 %d  姓名 %s 分数%.1f\n”,stu1.num,stu1.name,stu1.score);

printf("学号 %d姓名 %s分数 %.1f\n",stu2.num,stu2.name,stu2.score);

2.匿名结构体

结构体的声明和变量定义结合在一起就叫做匿名结构体。

struct {

int num;

char name[30];

float score;

}

st1 = {2,"guofei",99},

st2 = {3,"dabao",90},

st3 = {4,"huaer",98};

3.typedef 是为原有类型创建一个类型别名

格式 typedef  原类型名  新类型名;

例如

(1) typedef  float  fudian;

     fudian   a = 3.14;

     printf("a = %.1f\n",a);



(2)结构体

typedef struct car{

int tyer;

char color;

int speed;

}Car;

调用

别名Car 变量名car1 = {4,"白色",450};

输出:printf("car1为 %d %s %d\n",car1.tyer,car1.color,car1.speed);

4.结构体成员依然还可以是结构体

例如

typedef  struct  date{

  int  year;

  int month;

  int day;

}MyDate;


typedef struct student3{

   char name;

   MyDate birthday;

}Stu;

调用

Stu s1 = {"YangYang",{1990,2,4}};

printf("姓名 %s  生日 %d %d %d\n",s1.name,s1.year,s1.month,s1.day);

5.结构体数组

将多个结构体变量存放在数组中,构成结构体变量。

练习2. 有5名学生保存在 结构体数组中,编程查找成绩最高者,输出该学生全部信息;对上述5名学生数组,按成绩从高到低排序,并输出。

typedef  struct student {

  int num;

  char name[25];

  float score;

}Student;

Student  studen[4] = {{2,"李白",67},{1,"杜甫", 90},{4,"李清照",78},{8,"琼瑶",98}};

int  temp = 0;

//判断成绩最高者;

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

         if(studen[i] > temp){

           temp = studen[i];

}

}

//输出成绩最高者的信息

printf("分数最高者为 %.1f\b",temp);

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

    if(temp == studen[i].score){

   printf("学号 %d 姓名 %s 分数 %.1f\n ",studen[i].num,studen[i].name,studen[i].score);

}

}

//对上述5名学生数组,按成绩从高到低排序,并输出。

这个可以用冒泡排序。

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

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

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

     temp = stude1[j].score;

    stude1[j].score = stude1[j + 1].score;

    stude1[j].score = temp;

     }

   }

}

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


printf("学号 %d姓名 %s性别 %c 分数 %.1f\n",stude1[i].num,stude1[i].name,stude1[i].sex,stude1[i].score);

}








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值