zoukankan      html  css  js  c++  java
  • 使用结构体数组

    (一)定义结构体数组

    定义结构体数组的一般形式
    (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("
    result:
    ");
    17     for(int i=0;i<3;i++)
    18        printf("%5s:%d
    ",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
    ",stu[i].num,stu[i].name,stu[i].score);
    23     printf("
    ");
    24 }
    务实,说实话!
  • 相关阅读:
    css gridlayout
    css position 属性
    简单的登陆界面
    introduce
    Event flow
    constructor和object的区别
    10th week (1)
    编程语言的历史和发展
    正则表达式的验证匹配
    js正则替换
  • 原文地址:https://www.cnblogs.com/xtuxiongda/p/8343210.html
Copyright © 2011-2022 走看看