jackz007 发表于 2024-1-13 11:35:57

本帖最后由 jackz007 于 2024-1-13 16:34 编辑

#include <stdio.h>

typedef struct student {                // 【添加】
      intstudentID      ;         // 【添加】
      char studentName ;         // 【添加】
      char studentSex       ;         // 【添加】
      struct {                        // 【添加】
                int year      ;         // 【添加】
                int month   ;         // 【添加】
                int day       ;         // 【添加】
                } birthday    ;         // 【添加】
      int score          ;         // 【添加】
} STUDENT                     ;         // 【添加】

int main(void)
{
      int i , j , sum   ;
      STUDENT stu ={{100310121 ,"王刚" ,'M' , {1991,5,19} , {72,83,90,82}} ,
                            {100310122,"李小明",'M',{1992,8,20},{88,92,78,78}},
                            {100310123,"王丽红",'F',{1991,9,19},{98,72,89,66}},
                            {100310124,"陈莉莉",'F',{1992,3,22},{87,95,78,90}}} ;
      for (i = 0 ; i < 4 ; i ++) {
                sum = 0                                                      ;
                for(j = 0 ; j < 4 ; j ++) sum = sum + stu . score ;
                printf("|%10d | %6s | %c | %4d/%02d/%02d |%3d |%3d |%3d |%3d | %4.1f |\n" , //【修改】
                     stu.studentID,
                     stu.studentName,
                     stu.studentSex,
                     stu.birthday.year,
                     stu.birthday.month,
                     stu.birthday.day,
                     stu.score,
                     stu.score,
                     stu.score,
                     stu.score,
                     sum/4.0)                                              ;
      }
      return 0                                                                ;
}
      编译、运行实况:
D:\\C>g++ -static -o x x.c

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

D:\\C>
页: 1 [2]
查看完整版本: 结构体数组