1. 程式人生 > 其它 >第8章實驗1:學生成績管理系統V1.0

第8章實驗1:學生成績管理系統V1.0

CQUPT
某班有最多不超過30人(具體人數由鍵盤輸入)參加某門課程的考試,用一維陣列作函式引數程式設計實現如下學生成績管理: (1)錄入每個學生的學號和考試成績; (2)計算課程的總分和平均分; (3)按成績由高到低排出名次表; (4)按學號由小到大排出成績表; (5)按學號查詢學生排名及其考試成績; (6)按優秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5個類別,統計每個類別的人數以及所佔的百分比; (7)輸出每個學生的學號、考試成績。 程式執行結果示例: Input student number(n<30): 6↙ Management for Students' scores 1.Input record 2.Caculate total and average score of course 3.Sort in descending order by score 4.Sort in ascending order by number 5.Search by number 6.Statistic analysis 7.List record 0.Exit Please Input your choice: 1↙ Input student's ID, name and score: 11003001 87↙ 11003005 98↙ 11003003 75↙ 11003002 48↙ 11003004 65↙ 11003006 100↙ Management for Students' scores 1.Input record 2.Caculate total and average score of course 3.Sort in descending order by score 4.Sort in ascending order by number 5.Search by number 6.Statistic analysis 7.List record 0.Exit Please Input your choice: 2↙ sum=473,aver=78.83 Management for Students' scores 1.Input record 2.Caculate total and average score of course 3.Sort in descending order by score 4.Sort in ascending order by number 5.Search by number 6.Statistic analysis 7.List record 0.Exit Please Input your choice: 3↙ Sort in descending order by score: 11003006 100 11003005 98 11003001 87 11003003 75 11003004 65 11003002 48 Management for Students' scores 1.Input record 2.Caculate total and average score of course 3.Sort in descending order by score 4.Sort in ascending order by number 5.Search by number 6.Statistic analysis 7.List record 0.Exit Please Input your choice: 4↙ Sort in ascending order by number: 11003001 87 11003002 48 11003003 75 11003004 65 11003005 98 11003006 100 Management for Students' scores 1.Input record 2.Caculate total and average score of course 3.Sort in descending order by score 4.Sort in ascending order by number 5.Search by number 6.Statistic analysis 7.List record 0.Exit Please Input your choice: 5↙ Input the number you want to search: 11003004 11003004 65 Management for Students' scores 1.Input record 2.Caculate total and average score of course 3.Sort in descending order by score 4.Sort in ascending order by number 5.Search by number 6.Statistic analysis 7.List record 0.Exit Please Input your choice: 6↙ <60 1 16.67% 60-69 1 16.67% 70-79 1 16.67% 80-89 1 16.67% 90-99 1 16.67% 100 1 16.67% Management for Students' scores 1.Input record 2.Caculate total and average score of course 3.Sort in descending order by score 4.Sort in ascending order by number 5.Search by number 6.Statistic analysis 7.List record 0.Exit Please Input your choice: 7↙ 11003001 87 11003002 48 11003003 75 11003004 65 11003005 98 11003006 100 Management for Students' scores 1.Input record 2.Caculate total and average score of course 3.Sort in descending order by score 4.Sort in ascending order by number 5.Search by number 6.Statistic analysis 7.List record 0.Exit Please Input your choice: 8↙ Input error! Management for Students' scores 1.Input record 2.Caculate total and average score of course 3.Sort in descending order by score 4.Sort in ascending order by number 5.Search by number 6.Statistic analysis 7.List record 0.Exit Please Input your choice: 0↙ End of program! 輸入格式: ( 1 )錄入學生的人數: **輸入資料格式:"%d" **提示資訊:"Input student number(n<30):\n" ( 2 )錄入每個學生的學號和考試成績: **輸入資料格式:"%ld%f" **提示資訊:"Input student's ID, name and score:\n" 輸出格式: 選單項的輸出顯示: Management for Students' scores 1.Input record 2.Caculate total and average score of course 3.Sort in descending order by score 4.Sort in ascending order by number 5.Search by number 6.Statistic analysis 7.List record 0.Exit Please Input your choice: 計算課程的總分和平均分: **輸出總分與平均分格式:"sum=%.0f,aver=%.2f\n" 按成績由高到低排出名次表: **輸出格式:"%ld\t%.0f\n" **提示資訊:"Sort in descending order by score:\n" 按學號由小到大排出成績表: **輸出格式:"%ld\t%.0f\n" **提示資訊:"Sort in ascending order by number:\n" 按學號查詢學生排名及其考試成績: **如果未查到此學號的學生,提示資訊:"Not found!\n" **如果查詢到該學生,輸出格式:"%ld\t%.0f\n" 按優秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5個類別,統計每個類別的人數以及所佔的百分比: **成績<60輸出格式:"<60\t%d\t%.2f%%\n" **成績=100輸出格式:"%d\t%d\t%.2f%%\n" **其他輸出百分比格式:"%d-%d\t%d\t%.2f%%\n" 輸出學生成績格式:"%ld\t%.0f\n"


思路:
1、採用動態連結串列的方式,對每個學生的成績進行儲存;
2、成績排序時,採用氣泡排序
  1 #include <stdlib.h>
  2 #include<stdio.h>
  3 
  4 typedef struct Student_Node
  5 {
  6     long ID;
  7     float score;
  8     struct Student_Node* next;
  9 } Student_Node, * STU_List;
 10 
 11 void Initialize(STU_List* S)
 12 {
 13     *S = (STU_List)malloc(sizeof(Student_Node));
14 (*S)->ID = 0; 15 (*S)->score = 0; 16 (*S)->next = NULL; 17 } 18 19 void Input_record(STU_List* S, int n) 20 { 21 STU_List p; 22 printf("Input student's ID, name and score:\n"); 23 for (int i = 0; i < n; i++) 24 { 25 p = (STU_List)malloc(sizeof
(Student_Node)); 26 scanf_s("%ld%f", &(p->ID), &(p->score)); 27 p->next = (*S)->next;//p成了末尾節點 28 (*S)->next = p;//把這個新節點接到前節點之後 29 } 30 } 31 32 void Print_stu(STU_List S) 33 { 34 STU_List p = S->next; 35 while (p) 36 { 37 printf("%ld\t%.0f\n", p->ID, p->score); 38 p = p->next; 39 } 40 } 41 42 void Total_average_score(STU_List* S, int n) 43 { 44 float total = 0; 45 STU_List p = (*S)->next; 46 while (p) 47 { 48 total = p->score + total; 49 p = p->next; 50 } 51 printf("sum=%.0f,aver=%.2f\n", total, total / n); 52 } 53 54 void Sort_score(STU_List* S, short int n) 55 { 56 STU_List p, q, temp; 57 short int times; 58 for (int i = 0; i < n - 1; i++) 59 { 60 p = (*S)->next; 61 q = (*S)->next->next; 62 temp = (*S); 63 times = n - 1 - i; 64 while (times) 65 { 66 if (p->score < q->score) 67 { 68 p->next = q->next; 69 q->next = p; 70 temp->next = q; 71 } 72 temp = temp->next; 73 p = temp->next; 74 q = temp->next->next; 75 times--; 76 } 77 } 78 printf("Sort in descending order by score:\n"); 79 Print_stu(*S); 80 } 81 82 void Sort_number(STU_List* S, short int n) 83 { 84 STU_List p, q, temp; 85 short int times; 86 for (int i = 0; i < n - 1; i++) 87 { 88 p = (*S)->next; 89 q = (*S)->next->next; 90 temp = (*S); 91 times = n - 1 - i; 92 while (times) 93 { 94 if (p->ID > q->ID) 95 { 96 p->next = q->next; 97 q->next = p; 98 temp->next = q; 99 } 100 temp = temp->next; 101 p = temp->next; 102 q = temp->next->next; 103 times--; 104 } 105 } 106 printf("Sort in ascending order by number:\n"); 107 Print_stu(*S); 108 } 109 110 void Search_score(STU_List S) 111 { 112 STU_List p = S->next; 113 long ID; 114 printf("Input the number you want to search:\n"); 115 scanf_s("%ld", &ID); 116 while (p) 117 { 118 if (p->ID == ID) 119 { 120 printf("%ld\t%.0f\n", ID, p->score); 121 break; 122 } 123 else 124 { 125 p = p->next; 126 } 127 } 128 if (!p) 129 { 130 printf("Not found!\n"); 131 } 132 } 133 134 void Analysis_score(STU_List S, short int n) 135 { 136 STU_List p = S->next; 137 float temp_score10 = 0, temp_score9 = 0, temp_score8 = 0, temp_score7 = 0, temp_score6 = 0, temp_score5 = 0; 138 float num = n; 139 while (p) 140 { 141 if (p->score == 100) 142 temp_score10++; 143 else if (90 <= p->score && p->score <= 99) 144 temp_score9++; 145 else if (80 <= p->score && p->score <= 89) 146 temp_score8++; 147 else if (70 <= p->score && p->score <= 79) 148 temp_score7++; 149 else if (60 <= p->score && p->score <= 69) 150 temp_score6++; 151 else if (p->score < 60) 152 temp_score5++; 153 p = p->next; 154 } 155 printf("<60\t%d\t%.2f%%\n", (int)temp_score5, (temp_score5 / num) * 100.0); 156 printf("%d-%d\t%d\t%.2f%%\n", 60, 69, (int)temp_score6, (temp_score6 / num) * 100.0); 157 printf("%d-%d\t%d\t%.2f%%\n", 70, 79, (int)temp_score7, (temp_score7 / num) * 100.0); 158 printf("%d-%d\t%d\t%.2f%%\n", 80, 89, (int)temp_score8, (temp_score8 / num) * 100.0); 159 printf("%d-%d\t%d\t%.2f%%\n", 90, 99, (int)temp_score9, (temp_score9 / num) * 100.0); 160 printf("%d\t%d\t%.2f%%\n", 100, (int)temp_score10, (temp_score10 / num) * 100.0); 161 } 162 163 void print_menu() 164 { 165 printf("Management for Students' scores\n"); 166 printf("1.Input record\n"); 167 printf("2.Caculate total and average score of course\n"); 168 printf("3.Sort in descending order by score\n"); 169 printf("4.Sort in ascending order by number\n"); 170 printf("5.Search by number\n"); 171 printf("6.Statistic analysis\n"); 172 printf("7.List record\n"); 173 printf("0.Exit\n"); 174 } 175 176 void choose(short int choice, STU_List* S, short int n) 177 { 178 switch (choice) 179 { 180 case 1: 181 Input_record(&S, n); 182 break; 183 case 2: 184 Total_average_score(&S, n); 185 break; 186 case 3: 187 Sort_score(&S, n); 188 break; 189 case 4: 190 Sort_number(&S, n); 191 break; 192 case 5: 193 Search_score(S); 194 break; 195 case 6: 196 Analysis_score(S, n); 197 break; 198 case 7: 199 Print_stu(S); 200 break; 201 default: 202 printf("Input error!\n"); 203 break; 204 } 205 206 } 207 208 int main() 209 { 210 short int n, choice; 211 STU_List S; 212 Initialize(&S); 213 printf("Input student number(n<30):\n"); 214 scanf_s("%hd", &n); 215 216 while (1) 217 { 218 print_menu(); 219 printf("Please Input your choice:\n"); 220 scanf_s("%hd", &choice); 221 if (choice == 0) 222 { 223 printf("End of program!\n"); 224 break; 225 } 226 else 227 { 228 choose(choice, S, n); 229 } 230 } 231 return 0; 232 }

#include <stdlib.h>#include<stdio.h>
typedef struct Student_Node{ long ID; float score; struct Student_Node* next;} Student_Node, * STU_List;
void Initialize(STU_List* S){ *S = (STU_List)malloc(sizeof(Student_Node)); (*S)->ID = 0; (*S)->score = 0; (*S)->next = NULL;}
void Input_record(STU_List* S, int n){ STU_List p; printf("Input student's ID, name and score:\n"); for (int i = 0; i < n; i++) { p = (STU_List)malloc(sizeof(Student_Node)); scanf_s("%ld%f", &(p->ID), &(p->score)); p->next = (*S)->next;//p成了末尾節點 (*S)->next = p;//把這個新節點接到前節點之後 }}
void Print_stu(STU_List S){ STU_List p = S->next; while (p) { printf("%ld\t%.0f\n", p->ID, p->score); p = p->next; }}
void Total_average_score(STU_List* S, int n){ float total = 0; STU_List p = (*S)->next; while (p) { total = p->score + total; p = p->next; } printf("sum=%.0f,aver=%.2f\n", total, total / n);}
void Sort_score(STU_List* S, short int n){ STU_List p, q, temp; short int times; for (int i = 0; i < n - 1; i++) { p = (*S)->next; q = (*S)->next->next; temp = (*S); times = n - 1 - i; while (times) { if (p->score < q->score) { p->next = q->next; q->next = p; temp->next = q; } temp = temp->next; p = temp->next; q = temp->next->next; times--; } } printf("Sort in descending order by score:\n"); Print_stu(*S);}
void Sort_number(STU_List* S, short int n){ STU_List p, q, temp; short int times; for (int i = 0; i < n - 1; i++) { p = (*S)->next; q = (*S)->next->next; temp = (*S); times = n - 1 - i; while (times) { if (p->ID > q->ID) { p->next = q->next; q->next = p; temp->next = q; } temp = temp->next; p = temp->next; q = temp->next->next; times--; } } printf("Sort in ascending order by number:\n"); Print_stu(*S);}
void Search_score(STU_List S){ STU_List p = S->next; long ID; printf("Input the number you want to search:\n"); scanf_s("%ld", &ID); while (p) { if (p->ID == ID) { printf("%ld\t%.0f\n", ID, p->score); break; } else { p = p->next; } } if (!p) { printf("Not found!\n"); }}
void Analysis_score(STU_List S, short int n){ STU_List p = S->next; float temp_score10 = 0, temp_score9 = 0, temp_score8 = 0, temp_score7 = 0, temp_score6 = 0, temp_score5 = 0; float num = n; while (p) { if (p->score == 100) temp_score10++; else if (90 <= p->score && p->score <= 99) temp_score9++; else if (80 <= p->score && p->score <= 89) temp_score8++; else if (70 <= p->score && p->score <= 79) temp_score7++; else if (60 <= p->score && p->score <= 69) temp_score6++; else if (p->score < 60) temp_score5++; p = p->next; } printf("<60\t%d\t%.2f%%\n", (int)temp_score5, (temp_score5 / num) * 100.0); printf("%d-%d\t%d\t%.2f%%\n", 60, 69, (int)temp_score6, (temp_score6 / num) * 100.0); printf("%d-%d\t%d\t%.2f%%\n", 70, 79, (int)temp_score7, (temp_score7 / num) * 100.0); printf("%d-%d\t%d\t%.2f%%\n", 80, 89, (int)temp_score8, (temp_score8 / num) * 100.0); printf("%d-%d\t%d\t%.2f%%\n", 90, 99, (int)temp_score9, (temp_score9 / num) * 100.0); printf("%d\t%d\t%.2f%%\n", 100, (int)temp_score10, (temp_score10 / num) * 100.0);}
void print_menu(){ printf("Management for Students' scores\n"); printf("1.Input record\n"); printf("2.Caculate total and average score of course\n"); printf("3.Sort in descending order by score\n"); printf("4.Sort in ascending order by number\n"); printf("5.Search by number\n"); printf("6.Statistic analysis\n"); printf("7.List record\n"); printf("0.Exit\n");}
void choose(short int choice, STU_List* S, short int n){ switch (choice) { case 1: Input_record(&S, n); break; case 2: Total_average_score(&S, n); break; case 3: Sort_score(&S, n); break; case 4: Sort_number(&S, n); break; case 5: Search_score(S); break; case 6: Analysis_score(S, n); break; case 7: Print_stu(S); break; default: printf("Input error!\n"); break; }
}
int main(){ short int n, choice; STU_List S; Initialize(&S); printf("Input student number(n<30):\n"); scanf_s("%hd", &n);
while (1) { print_menu(); printf("Please Input your choice:\n"); scanf_s("%hd", &choice); if (choice == 0) { printf("End of program!\n"); break; } else { choose(choice, S, n); } } return 0;}