1 #include"stdio.h" 2 #include"stdlib.h" 3 #include"string.h" 4 #include"conio.h" 5 typedef struct node 6 { 7 char xh[11]; //學號 8 char xm[10]; //姓名 9 char xb[3]; //性別 10 int nl; //年齡 11 char dh[12]; //電話 12 char jg[30]; //籍貫 13 float rxcj; //成績 14 }student; 15 int menu(); 16 void add(); 17 void display(); 18 void dele(); 19 void sort(); 20 void update(); 21 void search(); 22 23 int menu() 24 { 25 system("cls"); 26 char ch[2]; int num1; 27 printf("學生信息管理系統\n"); 28 printf("---------------------\n"); 29 printf("1.學生信息錄入\n"); 30 printf("2.學生信息顯示\n"); 31 printf("3.學生信息查詢\n"); 32 printf("4.學生信息排序\n"); 33 printf("5.學生信息刪除\n"); 34 printf("6.學生信息修改\n"); 35 printf("0.退出管理系統\n"); 36 printf("---------------------\n"); 37 printf("請輸入你的選擇: "); 38 //fflush(stdin); 39 gets_s(ch); 40 num1 = atoi(ch); //將字符串轉化爲int型的數字 41 return num1; 42 } 43 void add() 44 { 45 student stu; 46 FILE *fp; 47 if ((fp = fopen("學生基本信息.txt", "a")) == NULL) 48 { 49 printf("打開文件失敗!"); 50 } 51 52 printf("請輸入學生的學號:"); 53 scanf("%s", stu.xh); 54 printf("請輸入學生的姓名:"); 55 scanf("%s", stu.xm); 56 printf("請輸入學生的性別:"); 57 scanf("%s", stu.xb); 58 printf("請輸入學生的年齡:"); 59 scanf("%d", &stu.nl); 60 printf("請輸入學生的電話:"); 61 scanf("%s", stu.dh); 62 printf("請輸入學生的籍貫:"); 63 scanf("%s", stu.jg); 64 printf("請輸入學生的成績:"); 65 scanf("%f", &stu.rxcj); 66 fwrite(&stu, sizeof(student), 1, fp); 67 fclose(fp); 68 //fflush(stdin); 69 getchar(); 70 } 71 void display() 72 { 73 student stu[20]; 74 FILE *fp; 75 if ((fp = fopen("學生基本信息.txt", "r")) == NULL) 76 { 77 printf("打開文件失敗!"); 78 } 79 printf("輸入學生的學號 姓名 性別 年齡 電話 籍貫 入學成績:\n"); 80 int i, n = 0; 81 for (i = 0; fread(&stu[i], sizeof(student), 1, fp); i++) 82 { 83 n++; 84 } 85 for (i = 0; i<n; i++) 86 { 87 printf("%s %s %s %d %s %s %f\n", stu[i].xh, stu[i].xm, stu[i].xb, stu[i].nl, stu[i].dh, stu[i].jg, stu[i].rxcj); 88 } 89 fclose(fp); 90 } 91 void main(void) 92 { 93 int num; 94 system("color F9"); 95 system("cls"); 96 system("mode con cols=100 lines=30"); //調整系統Console控制檯顯示的寬度和高度,高度爲30個字符,寬度爲100個字符 97 system("title 學生信息管理系統"); //起標題 98 fflush(stdin); //清空輸入緩衝區 99 do 100 { 101 num = menu(); 102 switch (num) 103 { 104 case 1:add(); system("pause"); break; 105 case 2:display(); system("pause"); break; 106 case 3:search(); system("pause"); break; 107 case 4:sort(); system("pause"); break; 108 case 5:dele(); system("pause"); break; 109 case 6:update(); system("pause"); break; 110 case 0:printf("退出\n"); exit(1); system("pause"); break; 111 exit(1); break; 112 } 113 } while (1); 114 } 115 void search() 116 { 117 system("cls"); 118 FILE *fp; 119 char delxh[11]; 120 student stu[45]; 121 char ch[2]; int num1; 122 printf("\t學生信息查詢\n"); 123 printf("\t------------------\n"); 124 printf("\t1.按學號查詢\n"); 125 printf("\t2.按姓名查詢\n"); 126 printf("\t------------------\n"); 127 printf("請輸入你的選擇:"); 128 gets_s(ch); 129 num1 = atoi(ch); 130 if (num1 == 1) 131 { 132 if ((fp = fopen("學生基本信息.txt", "r")) == NULL) 133 { 134 printf("打開文件失敗!"); 135 } 136 int i, n = 0; 137 for (i = 0; fread(&stu[i], sizeof(student), 1, fp); i++) 138 { 139 n++; 140 } 141 fclose(fp); 142 printf("請輸入要查詢的學號:"); 143 gets_s(delxh); 144 for (i = 0; i<n; i++) 145 { 146 if (!strcmp(stu[i].xh, delxh)) 147 { 148 printf("你要查詢的學生爲:\n"); 149 printf("%s %s %s %d %s %s %6.2f\n", stu[i].xh, stu[i].xm, stu[i].xb, stu[i].nl, stu[i].dh, stu[i].jg, stu[i].rxcj); 150 break; 151 } 152 } 153 } 154 else if (num1 == 2) 155 { 156 if ((fp = fopen("學生基本信息.txt", "r")) == NULL) 157 { 158 printf("打開文件失敗!"); 159 } 160 int i, n = 0; 161 for (i = 0; fread(&stu[i], sizeof(student), 1, fp); i++) 162 { 163 n++; 164 } 165 fclose(fp); 166 printf("請輸入要查詢的姓名:"); 167 gets_s(delxh); 168 for (i = 0; i<n; i++) 169 { 170 if (!strcmp(stu[i].xm, delxh)) 171 { 172 printf("你要查詢的學生爲:\n"); 173 printf("%s %s %s %d %s %s %6.2f\n", stu[i].xh, stu[i].xm, stu[i].xb, stu[i].nl, stu[i].dh, stu[i].jg, stu[i].rxcj); 174 break; 175 } 176 } 177 } 178 } 179 void update() 180 { 181 system("cls"); 182 FILE *fp; 183 int i, j, m = 0, snum; 184 student stu[45]; 185 char ch[2]; int num1; 186 char updatexh[11]; 187 printf("\t學生信息修改\n"); 188 printf("\t------------------\n"); 189 printf("\t1.按學號修改\n"); 190 printf("\t2.按姓名修改\n"); 191 printf("\t------------------\n"); 192 printf("請輸入你的選擇:"); 193 gets_s(ch); 194 num1 = atoi(ch); 195 if (num1 == 1) 196 { 197 if ((fp = fopen("學生基本信息.txt", "ab+")) == NULL) 198 { 199 printf("can not open\n"); 200 return; 201 } 202 while (!feof(fp)) //檢測文件是否達到哦末尾,出錯或者文件指針到了文件末尾(EOF)則返回 TRUE,不然返回 FALSE。 203 if (fread(&stu[m], sizeof(student), 1, fp) == 1) 204 m++; 205 if (m == 0) 206 { 207 printf("no record!\n"); 208 fclose(fp); 209 return; 210 } 211 printf("請輸入要修改的學號:\n"); 212 gets_s(updatexh); 213 for (i = 0; i<m; i++) // strcmp(const char *s1,const char *s2) 214 if (!strcmp(stu[i].xh, updatexh)) // 當s1<s2時,返回爲負數;當s1=s2時,返回值= 0;當s1>s2時,返回正數。 215 { 216 printf("你要修改的學生爲:\n"); 217 printf("%s %s %s %d %s %s %6.2f\n", stu[i].xh, stu[i].xm, stu[i].xb, stu[i].nl, stu[i].dh, stu[i].jg, stu[i].rxcj); 218 break; 219 } 220 221 if (i<m) 222 { 223 printf("請輸入學生的姓名:"); 224 scanf("%s", &stu[i].xm); 225 printf("請輸入學生的性別:"); 226 scanf("%s", &stu[i].xb); 227 printf("請輸入學生的年齡:"); 228 scanf("%d", &stu[i].nl); 229 printf("請輸入學生的電話:"); 230 scanf("%s", &stu[i].dh); 231 printf("請輸入學生的籍貫:"); 232 scanf("%s", &stu[i].jg); 233 printf("請輸入學生的成績:"); 234 scanf("%f", &stu[i].rxcj); 235 } 236 else 237 { 238 printf("can not find!"); 239 getchar(); 240 return; 241 } 242 if ((fp = fopen("學生基本信息.txt", "wb")) == NULL) 243 { 244 printf("can not open\n"); 245 return; 246 } 247 for (j = 0; j<m; j++)//將新修改的信息寫入指定的磁盤文件中 248 if (fwrite(&stu[j], sizeof(student), 1, fp) != 1) 249 //若是成功,該函數返回一個 size_t 對象,表示元素的總數, 250 //該對象是一個整型數據類型。若是該數字與 nmemb 參數不一樣,則會顯示一個錯誤 251 { 252 printf("can not save!"); 253 _getch(); //一個不回顯函數,從控制檯讀取一個字符,但不顯示在屏幕上 254 } 255 fclose(fp); 256 getchar(); //接收回車符,防止直接跳出 257 //fflush(stdin); //沒用 258 //char c = getchar(); //此處疑問,爲何用fflush(stdin) 不行,仍是會直接退出程序,而用getchar(); 則不會 259 //printf("---%c---", c); //測試發現是一個回車 260 261 262 } 263 else if (num1 == 2) 264 { 265 if ((fp = fopen("學生基本信息.txt", "ab+")) == NULL) 266 { 267 printf("can not open\n"); 268 return; 269 } 270 while (!feof(fp)) 271 if (fread(&stu[m], sizeof(student), 1, fp) == 1) 272 m++; 273 if (m == 0) 274 { 275 printf("no record!\n"); 276 fclose(fp); 277 return; 278 } 279 printf("請輸入要修改的姓名:\n"); 280 gets_s(updatexh); 281 for (i = 0; i<m; i++) 282 if (!strcmp(stu[i].xm, updatexh)) 283 { 284 printf("你要修改的學生爲:\n"); 285 printf("%s %s %s %d %s %s %6.2f\n", stu[i].xh, stu[i].xm, stu[i].xb, stu[i].nl, stu[i].dh, stu[i].jg, stu[i].rxcj); 286 break; 287 } 288 289 if (i<m) 290 { 291 printf("請輸入學生的學號:"); 292 scanf("%s", &stu[i].xh); 293 printf("請輸入學生的性別:"); 294 scanf("%s", &stu[i].xb); 295 printf("請輸入學生的年齡:"); 296 scanf("%d", &stu[i].nl); 297 printf("請輸入學生的電話:"); 298 scanf("%s", &stu[i].dh); 299 printf("請輸入學生的籍貫:"); 300 scanf("%s", &stu[i].jg); 301 printf("請輸入學生的成績:"); 302 scanf("%f", &stu[i].rxcj); 303 } 304 else 305 { 306 printf("can not find!"); 307 getchar(); 308 return; 309 } 310 if ((fp = fopen("學生基本信息.txt", "wb")) == NULL) 311 { 312 printf("can not open\n"); 313 return; 314 } 315 for (j = 0; j<m; j++)//將新修改的信息寫入指定的磁盤文件中 316 if (fwrite(&stu[j], sizeof(student), 1, fp) != 1) 317 { 318 printf("can not save!"); 319 _getch(); 320 } 321 fclose(fp); 322 } 323 324 } 325 326 void dele() 327 { 328 system("cls"); 329 FILE *fp; 330 char delxh[11]; 331 student stu[45]; 332 char ch[2]; int num1; 333 printf("\t學生信息刪除\n"); 334 printf("\t------------------\n"); 335 printf("\t1.按學號刪除\n"); 336 printf("\t2.按姓名刪除\n"); 337 printf("\t------------------\n"); 338 printf("請輸入你的選擇:"); 339 gets_s(ch); 340 num1 = atoi(ch); 341 if (num1 == 1) 342 { 343 if ((fp = fopen("學生基本信息.txt", "r")) == NULL) 344 { 345 printf("打開文件失敗!"); 346 } 347 int i, j, n = 0; 348 for (i = 0; fread(&stu[i], sizeof(student), 1, fp); i++) 349 { 350 n++; 351 } 352 fclose(fp); 353 printf("請輸入要刪除的學號:"); 354 gets_s(delxh); 355 for (i = 0; i<n; i++) 356 { 357 if (!strcmp(stu[i].xh, delxh)) 358 { 359 printf("你要刪除的學生爲:\n"); 360 printf("%s %s %s %d %s %s %6.2f\n", stu[i].xh, stu[i].xm, stu[i].xb, stu[i].nl, stu[i].dh, stu[i].jg, stu[i].rxcj); 361 break; 362 } 363 } 364 if (i == n) 365 { 366 printf("你要刪除的學生沒有找到!\n"); 367 } 368 else 369 { 370 char ch; 371 printf("你是否刪除(y/n)?\n"); 372 ch = getchar(); 373 getchar(); 374 if (ch == 'y' || ch == 'Y') 375 { 376 for (j = i; j<n; j++) 377 stu[j] = stu[j + 1]; 378 n--; 379 if ((fp = fopen("學生基本信息.txt", "w")) == NULL) 380 { 381 printf("打開文件失敗!"); 382 } 383 else 384 { 385 for (i = 0; i<n; i++) 386 { 387 fwrite(stu, sizeof(student), 1, fp); 388 } 389 fclose(fp); 390 printf("刪除成功!\n"); 391 } 392 } 393 } 394 395 } 396 if (num1 == 2) 397 { 398 if ((fp = fopen("學生基本信息.txt", "r")) == NULL) 399 { 400 printf("打開文件失敗!"); 401 } 402 int i, j, n = 0; 403 for (i = 0; fread(&stu[i], sizeof(student), 1, fp); i++) 404 { 405 n++; 406 } 407 fclose(fp); 408 printf("請輸入要刪除的姓名:"); 409 gets_s(delxh); 410 for (i = 0; i<n; i++) 411 { 412 if (!strcmp(stu[i].xm, delxh)) 413 { 414 printf("你要刪除的學生爲:\n"); 415 printf("%s %s %s %d %s %s %6.2f\n", stu[i].xh, stu[i].xm, stu[i].xb, stu[i].nl, stu[i].dh, stu[i].jg, stu[i].rxcj); 416 break; 417 } 418 } 419 if (i == n) 420 { 421 printf("你要刪除的學生沒有找到!\n"); 422 } 423 else 424 { 425 char ch; 426 printf("你是否刪除(y/Y)?\n"); 427 ch = getchar(); 428 getchar(); 429 if (ch == 'y' || ch == 'Y') 430 { 431 for (j = i; j<n; j++) 432 stu[j] = stu[j + 1]; 433 n--; 434 if ((fp = fopen("學生基本信息.txt", "w")) == NULL) 435 { 436 printf("打開文件失敗!"); 437 } 438 else 439 { 440 for (i = 0; i<n; i++) 441 { 442 fwrite(&stu, sizeof(student), 1, fp); 443 } 444 fclose(fp); 445 printf("刪除成功!\n"); 446 } 447 } 448 } 449 450 } 451 } 452 453 454 455 456 457 458 void sort() 459 { 460 student stu[45]; 461 FILE *fp; 462 if ((fp = fopen("學生基本信息.txt", "r")) == NULL) 463 { 464 printf("排序前的數據!\n"); 465 } 466 printf("輸入學生的學號 姓名 性別 年齡 電話 籍貫 入學成績:\n"); 467 int i, n = 0; 468 for (i = 0; fread(&stu[i], sizeof(student), 1, fp); i++) 469 { 470 n++; 471 } 472 for (i = 0; i<n; i++) 473 { 474 printf("%s %s %s %d %s %s %f\n", stu[i].xh, stu[i].xm, stu[i].xb, stu[i].nl, stu[i].dh, stu[i].jg, stu[i].rxcj); 475 } 476 fclose(fp); 477 int j; 478 student temp; 479 for (i = 0; i<n - 1; i++) 480 for (j = 0; j<n - 1 - i; j++) 481 if (stu[j].rxcj<stu[j + 1].rxcj) 482 { 483 temp = stu[j]; 484 stu[j] = stu[j + 1]; 485 stu[j + 1] = temp; 486 } 487 printf("\n\n排序後的數據!\n"); 488 printf("輸入學生的學號 姓名 性別 年齡 電話 籍貫 入學成績:\n"); 489 for (i = 0; i<n; i++) 490 { 491 printf("%s %s %s %d %s %s %f\n", stu[i].xh, stu[i].xm, stu[i].xb, stu[i].nl, stu[i].dh, stu[i].jg, stu[i].rxcj); 492 } 493 FILE *fp1; 494 if ((fp1 = fopen("學生基本信息.txt", "w")) == NULL) 495 { 496 printf("打開文件失敗!"); 497 } 498 }