• c primer plus 习题答案(7)


    p421.5

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #define CSIZE 4
     4 #define SCORE 3
     5 
     6 void get_info(struct students *p);
     7 void get_average(struct students *p);
     8 void print_info(struct students *p);
     9 void print_class_average(struct students *p);
    10 
    11 struct names{
    12     char firstname[40];
    13     char lastname[40];
    14 };
    15 
    16 struct students{
    17     struct names name;
    18     float grade[SCORE];
    19     float average;
    20 };
    21 
    22 int main(void)
    23 {
    24     struct students sheet[CSIZE]={
    25         {{"absbs", "bdcdc"}, 0, 0, 0, 0},
    26         {{"cfvfv", "dfgfg"}, 0, 0, 0, 0},
    27         {{"enmnm", "fcvcv"}, 0, 0, 0 ,0},
    28         {{"ghbhb", "hiyiy"}, 0, 0, 0, 0}
    29     };
    30 
    31 
    32     get_info(sheet);
    33     get_average(sheet);
    34     print_info(sheet);
    35     print_info(sheet);
    36 
    37     system("pause");
    38     return 0;
    39 }
    40 
    41 void get_info(struct students *p)
    42 {
    43     int i,j;
    44     for(i=0; i<CSIZE; i++){
    45         puts("please input student name");
    46         scanf("%s%s",&((p+i)->name.firstname),&((p+i)->name.lastname));
    47         puts("input the score");
    48         for(j=0; j<SCORE; j++)
    49             scanf("%f",&((p+i)->grade[j]));
    50     }
    51 }
    52 
    53 void get_average(struct students *p)
    54 {
    55     float sum; 
    56     int i, j;
    57     for(i=0; i<CSIZE; i++){
    58         for(j=0, sum=0; j<SCORE; j++)
    59         sum+=((p+i)->grade[j]);
    60         (p+i)->average=sum/SCORE;
    61     }
    62 }
    63 
    64 void print_info(struct students *p)
    65 {
    66     int i, j;
    67     for(i=0; i<CSIZE; i++){
    68         printf("%s	%s
    ", (p+i)->name.firstname, (p+i)->name.lastname);
    69         printf("grade:
    ");
    70         for(j=0; j<SCORE; j++)
    71             printf("%f ",(p+i)->grade[j]);
    72         printf("
    ");
    73         printf("the average grade is %.2f", (p+i)->average);
    74     }
    75 }
    76 
    77 void print_class_average(struct students *p)
    78 {
    79     int i;
    80     float sum=0;
    81 
    82     for(i=0; i<CSIZE; i++)
    83         sum+=(p+i)->average;
    84 
    85     printf("the class average grade is %.2f", sum/CSIZE);
    86 }
  • 相关阅读:
    Python封装发送信息到钉钉群
    centos 7.6 安装php70
    小米5s plus刷机
    centos 7 安装webmin
    交易开拓者旗舰版(TB旗舰版)软件升级中如何迁移用户数据
    centos 7.6 修改vim配色方案
    centos 7.0 读写ntfs分区
    centos iptables 数据转发
    centos 7.6 配置VNC
    win下maridb 10.1.8下主从复制配置
  • 原文地址:https://www.cnblogs.com/coding-time/p/4526667.html
Copyright © 2020-2023  润新知