使用结构体数组

(一)定义结构体数组

定义结构体数组的一般形式
(1)struct 结构体名
{
成员
列表
}数组名[数组长度];
(2)结构体类型 数组名[数组长度]
struct person leader[3];

定义结构体数组例子:

 1 #include<stdio.h>
 2 #include<string.h>
 3     struct person{
 4         char name[20];
 5         int count;
 6     }leader[3]={"Li",0,"Zhang",0,"Sun",0};
 7 int main(){
 8     int i,j;
 9     char lname[20];
10     for(int i=1;i<=10;i++){
11         scanf("%s",lname);
12         for(int j=0;j<3;j++){
13             if(strcmp(lname,leader[j].name)==0) leader[j].count++;
14         }
15     }
16     printf("\nresult:\n");
17     for(int i=0;i<3;i++)
18        printf("%5s:%d\n",leader[i].name,leader[i].count);
19 } 

(二)使用结构体数组小例子

 1 #include<stdio.h>
 2 struct student{
 3     int num;
 4     char name[20];
 5     float score;
 6 };
 7 int main(){
 8     struct student stu[5]{
 9         {10101,"Zhang",78},{10103,"Wang",98.5},{10106,"Li",86},{10108,"Ling",73.5},{10110,"Sun,",78}
10     };
11     struct student temp;
12     const int n=5;
13     int i,j,k;
14     for(i=0;i<n-1;i++){
15         k=i;
16         for(j=i+1;j<n;j++){
17             if(stu[j].score>stu[k].score) k=j;
18         }
19         temp=stu[k];stu[k]=stu[i];stu[i]=temp;
20     }
21     for(i=0;i<n;i++)
22         printf("%d %s %.2f\n",stu[i].num,stu[i].name,stu[i].score);
23     printf("\n");
24 }

 

转载于:https://www.cnblogs.com/xtuxiongda/p/8343210.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值