C語言結構學習-學產生績簡單統計

來源:互聯網
上載者:User

標籤:c語言   統計   結構   

/*本次作業對於我還是很有難度的,經過不斷失敗、嘗試,總算通過了。由於自己的不算規範,以下標出自己的環境:Linux 3.13.0-29-generic #53-Ubuntu SMP Wed Jun 4 21:00:20 UTC 2014 x86_64 x86_64 x86_64 GNU/Linuxgcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2Copyright (C) 2013 Free Software Foundation, Inc.stustruct.c: In function ‘student_input’:stustruct.c:57:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat=]  scanf("%s", &pStudent->name);  ^stustruct.c: In function ‘student_get_score’:stustruct.c:82:2: warning: writing into constant object (argument 2) [-Wformat=]  scanf("%d", &pStudent->score[index]);  ^ K.Weng 5 5 5T.Dixon 4 3 2V.Chu 3 4 5L.Tson 4 3 4 L.Lee 3 4 3I.Young 4 2 5K.Hiro 4 2 1G.Ping 4 4 4H.Gu 2 3 4J.Jon 5 4 3資料儲存為studata檔案一次輸入。*/#include <stdio.h>typedef struct {    char name[20];    int score[3];    } Student;   //    輸入一個學生的資料Student* student_input(Student *pStudent);//    輸出一個學生的資料,包括平均成績void student_print(const Student *pStudent);//    計算一個學生的平均成績double student_average(const Student *pStudent);    //    獲得學生的一個成績int student_get_score(const Student *pStudent, int index);int main(int argc, char const *argv[]){    double avgs[3],sum[3];    int min[3], max[3];    int i,j;    Student St[10];    for(i=0;i<10;i++){    //printf("Input a Student data:");            student_input(&St[i]);        for(j=0;j<3;j++){            St[i].score[j]=student_get_score(&St[i], j);        }    }    printf("no\tname\t\tscore1\tscore2\tscore3\taverage\n");    for(i=0;i<10;i++){        printf("%d\t", i+1);        student_print(&St[i]);    }        for(j=0;j<3;j++){        sum[j]=min[j]=max[j]=St[0].score[j];        for(i=1;i<10;i++){            if(St[i].score[j]<min[j]){                min[j]=St[i].score[j];            } else if(St[i].score[j]>max[j]){                max[j]=St[i].score[j];            }                sum[j] +=St[i].score[j];            }        avgs[j]=sum[j]/10;        }    printf("  \taverage\t\t%.1f\t%.1f\t%.1f\n", avgs[0], avgs[1], avgs[2]);    printf("  \tmin\t\t%d\t%d\t%d\n", min[0], min[1], min[2]);    printf("  \tmax\t\t%d\t%d\t%d\n", max[0], max[1], max[2]);    return 0;    }Student* student_input(Student *pStudent) {    scanf("%s", &pStudent->name);    return pStudent;}void student_print(const Student *pStudent) {    int j;    double avg;    printf("%s\t", pStudent->name);    for(j=0;j<3;j++){        printf("\t%d", pStudent->score[j]);            avg=student_average(pStudent);    }    printf("\t%.5lg\n", avg);}double student_average(const Student *pStudent) {    double sum=0;    int i;    for(i=0;i<3;i++){        sum += pStudent->score[i];    }    return sum/3;}   int student_get_score(const Student *pStudent, int index) {    scanf("%d", &pStudent->score[index]);    return pStudent->score[index];}/*./stustruct <studata no    name        score1    score2    score3    average1    K.Weng        5    5    5    52    T.Dixon        4    3    2    33    V.Chu        3    4    5    44    L.Tson        4    3    4    3.66675    L.Lee        3    4    3    3.33336    I.Young        4    2    5    3.66677    K.Hiro        4    2    1    2.33338    G.Ping        4    4    4    49    H.Gu        2    3    4    310    J.Jon        5    4    3    4      average        3.8    3.4    3.6      min        2    2    1      max        5    5    5*/


本文出自 “StudyPark” 部落格,請務必保留此出處http://swordautumn.blog.51cto.com/1485402/1622745

C語言結構學習-學產生績簡單統計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.