zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    递归函数
    Java以缓冲字符流向文件写入内容(如果文件存在则删除,否则先创建后写入)
    Python将文本内容读取分词并绘制词云图
    查询数据库数据并传入servlet
    向数据库添加记录(若有则不添加)
    2月16日学习记录
    2月15日学习记录
    2月14日学习记录
    Echart学习
    JavaScript深入学习(六)Ajax技术
  • 原文地址:https://www.cnblogs.com/coding-time/p/4526667.html
Copyright © 2011-2022 走看看