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/Linux
gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
 K.Weng 5 5 5
T.Dixon 4 3 2
V.Chu 3 4 5
L.Tson 4 3 4 
L.Lee 3 4 3
I.Young 4 2 5
K.Hiro 4 2 1
G.Ping 4 4 4
H.Gu 2 3 4
J.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) {
    Student Stu=*pStudent;
    scanf("%d", &Stu.score[index]);
    return Stu.score[index];
}
/*
./stustruct <studata 
no    name        score1    score2    score3    average
1    K.Weng        5    5    5    5
2    T.Dixon        4    3    2    3
3    V.Chu        3    4    5    4
4    L.Tson        4    3    4    3.6667
5    L.Lee        3    4    3    3.3333
6    I.Young        4    2    5    3.6667
7    K.Hiro        4    2    1    2.3333
8    G.Ping        4    4    4    4
9    H.Gu        2    3    4    3
10    J.Jon        5    4    3    4
      average        3.8    3.4    3.6
      min        2    2    1
      max        5    5    5
*/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章