鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 朕也来学学

结构体数组

[复制链接]
发表于 2024-1-13 11:35:57 | 显示全部楼层
本帖最后由 jackz007 于 2024-1-13 16:34 编辑
  1. #include <stdio.h>

  2. typedef struct student {                // 【添加】
  3.         int  studentID        ;         // 【添加】
  4.         char studentName[256] ;         // 【添加】
  5.         char studentSex       ;         // 【添加】
  6.         struct {                        // 【添加】
  7.                 int year      ;         // 【添加】
  8.                 int month     ;         // 【添加】
  9.                 int day       ;         // 【添加】
  10.                 } birthday    ;         // 【添加】
  11.         int score[4]          ;         // 【添加】
  12. } STUDENT                     ;         // 【添加】

  13. int main(void)
  14. {
  15.         int i , j , sum[30]   ;
  16.         STUDENT stu[30] =  {{100310121 ,"王刚" ,'M' , {1991,5,19} , {72,83,90,82}} ,
  17.                             {100310122,"李小明",'M',{1992,8,20},{88,92,78,78}},
  18.                             {100310123,"王丽红",'F',{1991,9,19},{98,72,89,66}},
  19.                             {100310124,"陈莉莉",'F',{1992,3,22},{87,95,78,90}}} ;
  20.         for (i = 0 ; i < 4 ; i ++) {
  21.                 sum[i] = 0                                                      ;
  22.                 for(j = 0 ; j < 4 ; j ++) sum[i] = sum[i] + stu[i] . score [j]  ;
  23.                 printf("|%10d | %6s | %c | %4d/%02d/%02d |%3d |%3d |%3d |%3d | %4.1f |\n" , //【修改】
  24.                        stu[i].studentID,
  25.                        stu[i].studentName,
  26.                        stu[i].studentSex,
  27.                        stu[i].birthday.year,
  28.                        stu[i].birthday.month,
  29.                        stu[i].birthday.day,
  30.                        stu[i].score[0],
  31.                        stu[i].score[1],
  32.                        stu[i].score[2],
  33.                        stu[i].score[3],
  34.                        sum[i]/4.0)                                              ;
  35.         }
  36.         return 0                                                                ;
  37. }
复制代码

        编译、运行实况:
  1. D:\[exercise]\C>g++ -static -o x x.c

  2. D:\[exercise]\C>x
  3. | 100310121 |   王刚 | M | 1991/05/19 | 72 | 83 | 90 | 82 | 81.8 |
  4. | 100310122 | 李小明 | M | 1992/08/20 | 88 | 92 | 78 | 78 | 84.0 |
  5. | 100310123 | 王丽红 | F | 1991/09/19 | 98 | 72 | 89 | 66 | 81.2 |
  6. | 100310124 | 陈莉莉 | F | 1992/03/22 | 87 | 95 | 78 | 90 | 87.5 |

  7. D:\[exercise]\C>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-7 10:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表