實驗題目:學生信息管理系統
實驗要求:用戶可以選擇1-7可以分別進行學生信息的查看、添加、刪除,修改,計算平均成績,保存,退出系統操作。
提示:用一個結構體類型表示學生信息
1 typedef struct node /*定義結構體*/
2 { 3 int num; //學號
4 char name[15];//姓名
5 char sex[9]; //性別
6 int age; //年齡
7 int english; //英語成績
8 int math; //數學成績
9 int computer;//計算機成績
10 int average; //平均成績
11 struct node *next; //鏈表指針域
12 }student_info;
運行過程中采用一個結構體鏈表存儲學生信息。退出系統時可以采用文件存儲用戶輸入的信息,再次運行的時候從文件中讀取。
完整C語言程序分為三個文件,student.h、student.cpp和main.c文件。具體實現如下所示:
student.h文件中定義學生信息結構體,操作函數聲明。
1 #ifndef STUDENT_HEAD 2 #define STUDENT_HEAD 3 4 typedef struct node /*定義結構體*/ 5 { 6 int num; //學號 7 char name[15];//姓名 8 char sex[9]; //性別 9 int age; //年齡 10 int english; //英語成績 11 int math; //數學成績 12 int computer;//計算機成績 13 int average; //平均成績 14 struct node *next; //鏈表指針域 15 }student_info; 16 17 //學生信息鏈表 18 extern student_info* student_list; //全局變量聲明 19 20 21 //初始化函數聲明 22 //初始化學生信息鏈表 23 void init_student_info_list(); 24 //判斷學生信息鏈表是否為空 25 int student_list_empty(); 26 27 //操作函數聲明 28 //向學校信息表中添加學生信息記錄 29 int add_student_info(); 30 //根據學號刪除學生信息 31 int delete_student_info(int num); 32 //根據學號修改學生信息 33 int modify_student_info(int num); 34 //根據學號查找學生信息 35 student_info* search_student_info(int num); 36 //輸出每個學生的平均成績 37 void display_average(); 38 //顯示所有學生信息 39 void display_student_info(); 40 //將學生信息保存到文件 41 int save_file(); 42 //從文件中讀取學生信息 43 int read_file(); 44 #endif
student.c文件中是對student.h文件中聲明的函數進行定義,給出具體的實現代碼。
1 #include "student.h" 2 #include <stdio.h> 3 #include <string.h> 4 #include <malloc.h> 5 6 //初始化學生信息鏈表 7 void init_student_info_list() 8 { 9 //學生信息鏈表頭結點 10 student_list = (student_info*)malloc(sizeof(student_info)); 11 student_list->next = NULL; 12 } 13 //判斷學生信息鏈表是否為空 14 int student_list_empty() 15 { 16 return student_list->next == NULL; 17 } 18 //操作函數實現 19 //向學校信息表中添加學生信息記錄 20 int add_student_info() 21 { 22 student_info *pstu = (student_info*)malloc(sizeof(student_info)); 23 if(pstu == NULL) 24 { 25 printf("內存分配失敗.\n"); 26 return 0; 27 } 28 printf("請按要求一次輸入學生的信息.\n"); 29 printf("請輸入學號: "); 30 scanf("%d",&pstu->num); 31 //判斷該學號是否已經存在 32 if(search_student_info(pstu->num) != NULL) 33 { 34 printf("該學號已經存在學生信息表中.\n"); 35 return 0; 36 } 37 printf("請輸入姓名: "); 38 getchar(); 39 gets(pstu->name); 40 printf("請輸入性別: "); 41 scanf("%s",pstu->sex); 42 printf("請輸入年齡: "); 43 scanf("%d",&pstu->age); 44 printf("請輸入英語成績: "); 45 scanf("%d",&pstu->english); 46 printf("請輸入數學成績: "); 47 scanf("%d",&pstu->math); 48 printf("請輸入計算機成績: "); 49 scanf("%d",&pstu->computer); 50 pstu->average = (pstu->english + pstu->math + pstu->computer)/3; 51 //每次從學生信息鏈表的頭部插入; 52 pstu->next = student_list->next; 53 student_list->next = pstu; 54 return 1; 55 } 56 //根據學號刪除學生信息 57 int delete_student_info(int num) 58 { 59 student_info *pstu; 60 student_info *qstu; 61 if(search_student_info(num) == NULL) 62 { 63 printf("不存在該學好為%d的學生信息.\n",num); 64 return 0; 65 } 66 pstu = student_list->next; 67 qstu = student_list; 68 while(pstu->num != num) 69 { 70 qstu = pstu; 71 pstu = pstu->next; 72 } 73 qstu->next = pstu->next; 74 free(pstu); 75 return 1; 76 } 77 //根據學號修改學生信息 78 int modify_student_info(int num) 79 { 80 int choice; 81 student_info *pstu = search_student_info(num); 82 if(pstu == NULL) 83 { 84 printf("不存在該學好為%d的學生信息.\n",num); 85 return 0; 86 } 87 printf("1.姓名 2.性別 3.年齡 4.英語成績 5.數學成績 6.計算機成績.\n"); 88 printf("請選擇修改的信息: "); 89 scanf("%d",&choice); 90 switch(choice) 91 { 92 case 1: 93 printf("請輸入新的姓名: "); 94 getchar(); 95 gets(pstu->name); 96 break; 97 case 2: 98 printf("請輸入新的性別: "); 99 scanf("%s",pstu->sex); 100 break; 101 case 3: 102 printf("請輸入新的年齡: "); 103 scanf("%d",&pstu->age); 104 break; 105 case 4: 106 printf("請輸入新的英語成績: "); 107 scanf("%d",&pstu->english); 108 break; 109 case 5: 110 printf("請輸入新的數學成績: "); 111 scanf("%d",&pstu->math); 112 break; 113 case 6: 114 printf("請輸入新的計算機成績: "); 115 scanf("%d",&pstu->computer); 116 break; 117 default: 118 printf("請按提示要求操作.\n"); 119 } 120 return 1; 121 } 122 //根據學號查找學生信息 123 student_info* search_student_info(int num) 124 { 125 student_info *pstu; 126 pstu = student_list->next; 127 while(pstu && pstu->num != num) 128 { 129 130 pstu = pstu->next; 131 } 132 return pstu; 133 } 134 //輸出每個學生的平均成績 135 void display_average() 136 { 137 student_info *pstu; 138 pstu = student_list->next; 139 while(pstu) 140 { 141 printf("學號為%d,姓名為%s的學生平均成績為: %d\n",pstu->num,pstu->name,pstu->average); 142 pstu = pstu->next; 143 } 144 } 145 //顯示所有學生信息 146 void display_student_info() 147 { 148 student_info *pstu; 149 pstu = student_list->next; 150 printf("所有學生信息如下所示.\n"); 151 printf("學號\t姓名\t性別\t年齡\t英語\t數學\t計算機\t平均成績.\n"); 152 while(pstu) 153 { 154 printf("%d\t",pstu->num); 155 printf("%s\t",pstu->name); 156 printf("%s\t",pstu->sex); 157 printf("%d \t",pstu->age); 158 printf("%d \t",pstu->english); 159 printf("%d \t",pstu->math); 160 printf("%d \t",pstu->computer); 161 printf("%d\n",pstu->average); 162 pstu = pstu->next; 163 } 164 } 165 //將學生信息保存到文件 166 int save_file() 167 { 168 FILE *pfile; 169 student_info *pstu; 170 pfile = fopen("student.txt","w"); 171 if(pfile == NULL) 172 { 173 printf("打開文件失敗.\n"); 174 return 0; 175 } 176 pstu = student_list->next; 177 while(pstu) 178 { 179 fprintf(pfile,"%5d%15s%9s%3d%4d%4d%4d%4d",pstu->num,pstu->name,pstu->sex,pstu->age, 180 pstu->english,pstu->math,pstu->computer,pstu->average); 181 pstu = pstu->next; 182 } 183 fclose(pfile); 184 return 1; 185 } 186 187 //從文件中讀取學生信息 188 int read_file() 189 { 190 FILE *pfile; 191 student_info *pstu; 192 pfile = fopen("student.txt","r"); 193 if(pfile == NULL) 194 { 195 printf("打開文件失敗.\n"); 196 return 0; 197 } 198 while(!feof(pfile)) 199 { 200 pstu = (student_info*)malloc(sizeof(student_info)); 201 fscanf(pfile,"%5d%15s%9s%4d%4d%4d%4d%4d",&pstu->num,pstu->name,pstu->sex,&pstu->age, 202 &pstu->english,&pstu->math,&pstu->computer,&pstu->average); 203 pstu->average = (pstu->english + pstu->math + pstu->computer)/3; 204 //每次從學生信息鏈表的頭部插入; 205 pstu->next = student_list->next; 206 student_list->next = pstu; 207 } 208 fclose(pfile); 209 return 1; 210 }
主函數main.c文件進行測試調用,如下所示:
1 #include "student.h" 2 #include <stdlib.h> 3 #include <stdio.h> 4 5 void menu(); 6 7 //學生信息鏈表 8 student_info* student_list; 9 10 //用戶可以選擇1-7可以分別進行學生信息的查看、添加、刪除,修改,計算平均成績,保存,退出系統操作。 11 int main() 12 { 13 int choice; 14 int num; 15 printf("**************************\n"); 16 printf("歡迎使用學生信息管理系統\n"); 17 printf("**************************\n"); 18 printf("-----------------------------\n"); 19 init_student_info_list(); 20 if(read_file()) 21 printf("從文件中讀取學生信息成功.\n"); 22 else 23 printf("從文字中讀取學生信息失敗.\n"); 24 printf("-----------------------------\n"); 25 menu(); 26 while(1) 27 { 28 printf("請選擇操作: "); 29 scanf("%d",&choice); 30 switch(choice) 31 { 32 case 1: 33 if(student_list_empty()) 34 printf("學生信息表為空,請先添加學生信息.\n"); 35 else 36 display_student_info(); 37 break; 38 case 2: 39 if(add_student_info()) 40 printf("添加學生信息成功.\n"); 41 else 42 printf("添加學生信息失敗.\n"); 43 break; 44 case 3: 45 if(student_list_empty()) 46 printf("學生信息表為空,請先添加學生信息.\n"); 47 else 48 { 49 printf("請輸入要刪除學生信息的學號: "); 50 scanf("%d",&num); 51 if(delete_student_info(num)) 52 printf("成功刪除該學號對應的學生信息.\n"); 53 else 54 printf("刪除失敗.\n"); 55 } 56 break; 57 case 4: 58 if(student_list_empty()) 59 printf("學生信息表為空,請先添加學生信息.\n"); 60 else 61 { 62 printf("請輸入要修改學生信息的學號: "); 63 scanf("%d",&num); 64 if(modify_student_info(num)) 65 printf("成功修改該學號對應的學生信息.\n"); 66 else 67 printf("修改失敗.\n"); 68 } 69 break; 70 case 5: 71 if(student_list_empty()) 72 printf("學生信息表為空,請先添加學生信息.\n"); 73 else 74 display_average(); 75 break; 76 case 6: 77 if(student_list_empty()) 78 printf("學生信息表為空,請先添加學生信息.\n"); 79 else 80 if(save_file()) 81 printf("保存學生信息成功.\n"); 82 else 83 printf("保存學生信息失敗.\n"); 84 break; 85 case 0: 86 printf("歡迎下次使用,再見.\n"); 87 system("pause"); 88 exit(0); 89 break; 90 default: 91 printf("輸入錯誤,請重新選擇操作.\n"); 92 } 93 } 94 system("pause"); 95 return 0; 96 } 97 98 void menu() 99 { 100 printf("1.查看學生信息.\n"); 101 printf("2.添加學生信息.\n"); 102 printf("3.刪除學生信息.\n"); 103 printf("4.修改學生信息.\n"); 104 printf("5.輸出平均成績.\n"); 105 printf("6.保存學生信息.\n"); 106 printf("0.退出系統操作.\n"); 107 }
程序執行結果如下所示:
(1)第一次執行沒有學生信息,讀取文件失敗。
(2)以后執行先從文件中讀取學生信息,然后進行操作。