C語言及程式設計 實踐參考——學生成績統計
阿新 • • 發佈:2019-01-30
【專案1-學生成績統計】
每位同學的資訊學號、姓名、C、高數、英語成績。定義一個學生成績的結構體陣列,其中的資料成員包括學號(char num[13])、姓名(name)、三門課的成績(grade)、總分(score)、均分(average))。
(1)從鍵盤上輸入N名學生的資訊(N定義為常變數);
(2)求出每名同學的總分和均分,並存儲在結構體陣列中(可以讀入過程中“順便”計算);
(3)輸出每位同學的資訊學號、姓名、總分和均分。
請參考下面的程式碼,將需要的其他程式碼寫出來。
#include <stdio.h>struct Student{ char num[13]; char name[10]; int c; int math; int english; int grade; double average;};const int N=3;int main( ){ int i, j, k; //定義結構體陣列 struct Student stu[N]; //寫出程式碼,實現要求的功能 return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
參考執行結果如圖:
[參考解答]
#include <stdio.h>struct Student{ char num[13]; //儘管都是由數字構成,但看作為字元更合適。學號12位,定義為13個字元,便於處理成字串 char name[10]; //每個漢字佔兩個位元組,中國人的名字,5個漢字夠用 int c; int math; int english; int grade; double average;};const int N=3;int main( ){ int i; //定義結構體陣列 struct Student stu[N]; double total=0; printf("請輸入學號、姓名、C、高數、英語成績:\n"); for(i=0; i<N; i++) { scanf("%s %s %d %d %d" , stu[i].num, stu[i].name, &stu[i].c, &stu[i].math, &stu[i].english); stu[i].grade=stu[i].c+stu[i].math+stu[i].english; stu[i].average=stu[i].grade/3.0; total+=stu[i].average; } //輸出成績單 printf("學號\t姓名\t總分\t均分\n"); for(i=0; i<N; i++) printf("%s\t%s\t%d\t%.1f\n", stu[i].num,stu[i].name,stu[i].grade,stu[i].average); printf("所有同學均分的均值是:%.1f\n", total/N); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
(4)使用相同的結構體型別,在main函式中已經對結構體陣列進行了初始化,請完成相關函式的定義,實現main函式中要求的功能。
#include <stdio.h>struct Student //同前{ ……};void calculate(struct Student s[],int n); //計算總分和均分,資料在s陣列中,共有n個元素void sort1(struct Student [],int); //按總分降序排序void sort2(struct Student [],int); //按學號升序排序void output(struct Student [],int); //輸出成績單void outputExcellent(struct Student [],int); //輸出優秀者:總分超285,單科不低於90int main(){ struct Student stu[]= {{"201152501104","Tom",65,69 ,68 }, {"201152501114","Jeery",94 ,89 ,63 }, {"201152501138","Speike",67 ,62 ,84 }, {"201152501204","Tyke",100 ,95 ,91 }, {"201152501202","Tuffy",59 ,80 ,55 }, {"201152501115","Butch",100 ,99 ,89 }, {"201152501201","Lightning",95 ,92 ,99 }, {"201152501145","Topsy",88 ,56 ,67 }, {"201152501203","Droopy",62 ,62 ,95 }, {"201152501140","Drupa",80 ,60 ,86 }, {"201152501205","Buzz",73 ,90 ,94} }; int stuNum=sizeof(stu)/sizeof(stu[0]); //用sizeof運算確定陣列中元素個數 //計算總分和均分後輸出 calculate(stu,stuNum); printf("下面是成績單:\n"); output(stu,stuNum); printf("優秀者名單:\n"); outputExcellent(stu,stuNum); //按總分降序排序後輸出 sort1(stu,stuNum); printf("按總分降序排序後:\n"); output(stu,stuNum); //按學號升序排序後輸出 sort2(stu,stuNum); printf("按學號升序排序後:\n"); output(stu,stuNum); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
[參考解答]
#include <stdio.h>#include <string.h>struct Student //同前{ char num[13]; char name[10]; int c; int math; int english; int grade; double average;};void calculate(struct Student s[],int n); //計算總分和均分,資料在s陣列中,共有n個元素void sort1(struct Student [],int); //按總分降序排序void sort2(struct Student [],int); //按學號升序排序void output(struct Student [],int); //輸出成績單void outputExcellent(struct Student [],int); //輸出優秀者:總分超285,單科不低於90int main(){ struct Student stu[]= {{"201152501104","Tom",65,69 ,68 }, {"201152501114","Jeery",94 ,89 ,63 }, {"201152501138","Speike",67 ,62 ,84 }, {"201152501204","Tyke",100 ,95 ,91 }, {"201152501202","Tuffy",59 ,80 ,55 }, {"201152501115","Butch",100 ,99 ,89 }, {"201152501201","Lightning",95 ,92 ,99 }, {"201152501145","Topsy",88 ,56 ,67 }, {"201152501203","Droopy",62 ,62 ,95 }, {"201152501140","Drupa",80 ,60 ,86 }, {"201152501205","Buzz",73 ,90 ,94} }; int stuNum=sizeof(stu)/sizeof(stu[0]); //用sizeof運算確定陣列中元素個數 //計算總分和均分後輸出 calculate(stu,stuNum); printf("下面是成績單:\n"); output(stu,stuNum); printf("優秀者名單:\n"); outputExcellent(stu,stuNum); //按總分降序排序後輸出 sort1(stu,stuNum); printf("按總分降序排序後:\n"); output(stu,stuNum); //按學號升序排序後輸出 sort2(stu,stuNum); printf("按學號升序排序後:\n"); output(stu,stuNum); return 0;}void calculate(struct Student s[],int n) //計算總分和均分{ int i; for(i=0; i<n; i++) { s[i].grade=s[i].c+s[i].math+s[i].english; s[i].average=s[i].grade/3.0; } return;}void sort1(struct Student s[],int n) //按總分降序排序{ int i,j; struct Student t; for(j=0; j<n-2; j++) { for(i=0; i<n-j-1; i++) if (s[i].grade<s[i+1].grade) { t=s[i]; //交換結構體 s[i]=s[i+1]; s[i+1]=t; } } return;}void sort2(struct Student s[],int n) //按學號升序排序{ int i,j; struct Student t; for(j=0; j<n-2; j++) { for(i=0; i<n-j-1; i++) if (strcmp(s[i].num,s[i+1].num)>0) { t=s[i]; s[i]=s[i+1]; s[i+1]=t; } } return;}void output(struct Student s[],int n) //輸出成績單{ int i; for(i=0; i<n; i++) { printf("%s, %s, ", s[i].num, s[i].name); printf("%d, %d, %d, ", s[i].c, s[i].math, s[i].english); printf("%d %.1f\n", s[i].grade, s[i].average); } printf("\n"); return;}void outputExcellent(struct Student s[],int n) //輸出優秀者{ int i; for(i=0; i<n; i++) if(s[i].grade>=285&&s[i].c>=90&&s[i].math>=90&&s[i].english>=90) { printf("%s, %s, ", s[i].num, s[i].name); printf("%d, %d, %d, ", s[i].c, s[i].math, s[i].english); printf("%d %.1f\n", s[i].grade, s[i].average); } printf("\n"); return;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
(5)score.txt中是一些同學的學號、姓名、C++、高數、英語成績,利用前面定義的結構體陣列,讀取檔案score.txt中的資料,完成下面的應用:
- 從檔案中讀出資料,存放到結構體陣列中;
- 求出每名同學的總分(可以在讀入過程中“順便”計算);
- 輸出結構體陣列中所有同學的記錄;
- 按總分排序(降序);
- 輸出排序後的成績單;
- 有30名同學可以獲得獎學金,規則是總分高者優先,有掛科不能得獎學金。請輸出可以得獎學金同學的名單。若符合得將的最後一名同學有重複(例如總分全為S),則總分為S的同學全得獎。
所有功能可以在main()函式中實現,推薦設計自定義函式實現任務,更推薦用多檔案組織完成。
[參考解答]
#include <stdio.h>#include <stdlib.h> //在codeblocks下,exit(1)需要這個標頭檔案struct Student{ char num[13]; char name[10]; int c; int math; int english; int grade;};const int N=200;int main( ){ int i, j, k, stuNum=0; //定義結構體陣列 struct Student stu[N], t_stu; //(1)從檔案中讀出資料,存放到你定義的結構體陣列中; FILE *infile=fopen("score.txt","r"); //以輸入的方式開啟檔案 if(!infile) //測試是否成功開啟 { printf("open error!\n"); exit(1); } i=0; fscanf(infile, "%s %s %d %d %d", stu[i].num, stu[i].name, &stu[i].c, &stu[i].math, &stu[i].english); while(!feof(infile)) { stu[i].grade=stu[i].c+stu[i].math+stu[i].english; ++stuNum; ++i; fscanf(infile, "%s %s %d %d %d", stu[i].num, stu[i].name, &stu[i].c, &stu[i].math, &stu[i].english); } fclose(infile); //(2)求出每名同學的總分(可以在讀入過程中“順便”計算)——上面已經順便完成 了; //(3)輸出結構體陣列中所有同學的記錄。 printf("成績單\n"); for(i=0; i<stuNum; ++i) { printf("%s\t%s\t%d\t%d\t%d\t%d\n", stu[i].num,stu[i].name,stu[i].c,stu[i].math,stu[i].english,stu[i].grade); } printf("\n"); system("PAUSE"); //(4)按總分排序(降序),用了選擇排序 for(i=0; i<stuNum-1; i++) { k=i; for(j=i+1; j<stuNum; j++) if(stu[j].grade>stu[k].grade) k=j; t_stu=stu[k]; //交換結構體陣列元素 stu[k]=stu[i]; stu[i]=t_stu; } //(5)輸出排序後的成績單; printf("排序後成績單\n"); for(i=0; i<stuNum; ++i) { printf("%s\t%s\t%d\t%d\t%d\t%d\n", stu[i].num,stu[i].name,stu[i].c,stu[i].math,stu[i].english,stu[i].grade); } printf("\n"); system("PAUSE"); //(6)有30名同學可以獲得獎學金,規則是總分高者優先,有掛科不能得獎學金。請輸出可以得獎學金同學的名單。 printf("恭喜以下同學獲得獎學金(獎學金用於改善學習條件,杜絕請客吃飯!)\n"); i=0,j=1; int g; while (j<=30) { if (stu[i].c>=60&&stu[i].math>=60&&stu[i].english>=60) { printf("%d\t%s\t%d\n", j, stu[i].name, stu[i].grade); g=stu[i].grade; j++; } i++; } while(g==stu[i].grade)//和剛才輸出的最後一個總分相同的同學都有機會獲得獎學金 { if (stu[i].c>=60&&stu[i].math>=60&&stu[i].english>=60) { printf("%d\t%s\t%d\n", j, stu[i].name, stu[i].grade); j++; } i++; } return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
(6)在前面工作的基礎擬出新的需求,將程式用一個“選單”組織起來(如圖所示),做成一個“學生成績管理系統”之類的應用程式。
[參考解答]
#include <stdio.h>#include <stdlib.h> struct Student{ char num[13]; char name[10]; int c; int math; int english; int grade;};//宣告函式int getData(struct Student s[]);void outputData(struct Student s[],int n);void sort(struct Student s[],int n);void listScholars1(struct Student s[],int n);void listScholars2(struct Student s[],int n);const int N=200;int main( ){ int stuNum; //定義結構體陣列 struct Student stu[N]; //(1)從檔案中讀出資料,存放到結構體陣列中; //(2)求出每名同學的總分(可以在讀入過程中“順便”計算); stuNum=getData(stu); printf("資料讀取完畢\n"); int go=1; int ch; while(go) { printf("請選擇下面的功能:\n"); printf("1. 輸出成績單\n"); printf("2. 按總分排序並輸出成績單\n"); printf("3. 輸出獲得獎學金的同學的名單\n"); printf("4. 輸出C語言不及格同學姓名和C語言成績\n"); printf("5. 你可以想出的成績管理系統中的其他功能\n"); printf("0. 結束\n"); printf("請選擇(0-5):"); scanf("%d", &ch); switch(ch) { case 1: outputData(stu,stuNum); break; case 2: sort(stu,stuNum); outputData(stu,stuNum); break; case 3: listScholars1(stu,stuNum); break; case 4: printf("請自行設計函式實現.\n"); printf("相信自己能夠!\n"); break; case 5: printf("你可以想出很多這方面的需求,增加、刪除資料,各種查詢…… \n"); printf("想到就能做到! \n"); break; case 0: go=0; break; } printf("\n"); } printf("下次再見!\n"); return 0;}//從檔案中讀取資料int getData(struct Student s[]){ FILE *infile=fopen("score.txt","r"); //以輸入的方式開啟檔案 if(!infile) //測試是否成功開啟 { printf("open error!\n"); exit(1); } int i=0; fscanf(infile, "%s %s %d %d %d", s[i].num, s[i].name, &s[i].c, &s[i].math, &s[i].english); while(!feof(infile)) { s[i].grade=s[i].c+s[i].math+s[i].english; ++i; fscanf(infile, "%s %s %d %d %d", s[i].num, s[i].name, &s[i].c, &s[i].math, &s[i].english); } fclose(infile); return i;}//按總分排序(降序),用了選擇排序void sort(struct Student s[],int n){ int i,j,k; struct Student t_stu; for(i=0; i<n-1; i++) { k=i; for(j=i+1; j<n; j++) if(s[j].grade>s[k].grade) k=j; t_stu=s[k]; s[k]=s[i]; s[i]=t_stu; } return;}//輸出成績單;void outputData(struct Student s[],int n){ int i; for(i=0; i<n; ++i) { printf("%s\t%s\t%d\t%d\t%d\t%d\n", s[i].num,s[i].name,s[i].c,s[i].math,s[i].english,s[i].grade); } printf("\n"); return;}//輸出得獎學金同學的名單//有30名同學可以獲得獎學金,規則是總分高者優先void listScholars1(struct Student s[],int n){ sort(s,n); //在應用中,並不能保證按總分有序,故需要先排序 printf("恭喜以下同學獲得獎學金: \n"); int i=0,j=1; while (j<=30&&j<=n) { if (s[i].c>=60&&s[i].math>=60&&s[i].english>=60) { printf("%d\t%s\t%d\n", j, s[i].name, s[i].grade); j++; } i++; }}//另一種獎學金規則void listScholars2(struct Student s[],int n){ sort(s,n); //在應用中,並不能保證按總分有序,故需要先排序 printf("恭喜以下同學獲得獎學金: \n"); int i=0,j=1; int g; while (j<=30&&j<=n) { if (s[i].c>=60&&s[i].math>=60&&s[i].english>=60) { printf("%d\t%s\t%d\n", j, s[i].name, s[i].grade); g=s[i].grade; j++; } i++; } while(g==s[i].grade)//和剛才輸出的最後一個總分相同的同學都有機會獲得獎學金 { if (s[i].c>=60&&s[i].math>=60&&s[i].english>=60) { printf("%d\t%s\t%d\n", j, s[i].name, s[i].grade); j++; } i++; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169