基於Linux系統下的學習
簡單的學生信息管理系統:支持管理員和學生本人登錄
學生登錄:賬號是學生的ID,密碼是學生的名字;登錄成功后會看到自己的信息
管理員登錄:賬號和密碼都是admin,允許3次登錄;登錄成功后可對學生信息進行增、刪、改、查等操作;學生信息保存在文件
1 #include <stdio.h> 2 #include <string.h> 3 #include <unistd.h> 4 #include <stdlib.h> 5 typedef struct stu 6 { 7 char name[30]; 8 char class[30]; 9 int nId; 10 double EnScore; 11 double ChScore; 12 double MathScore; 13 }Stu; 14 15 void Welcome();//歡迎界面 16 void AdminMenu();//管理員登錄界面 17 int Login();//3次登錄 18 void Select();//各種功能選擇 19 void Insert(); //添加學生信息 20 void ShowStu(); //顯示學生信息 21 void Delete();//刪除學生信息 22 void Modify();//修改學生信息 23 void Query();//同過學號查詢學生信息 24 void Sort();//總分排序 25 int FindBiaohao(int nID, int size);//通過學號返回標號 26 void SmallMenu(); 27 void StuLogin(); 28 void ShowClass(); 29 30 Stu stu[100]; 31 32 int FindBiaohao(int n, int size) 33 { 34 int ret = -1; 35 int i = 0; 36 for(i = 0; i < size; i++) 37 { 38 if(stu[i].nId == n) 39 { 40 return i; 41 //break; 42 } 43 } 44 return ret; 45 } 46 47 void ShowClass() 48 { 49 int UnPassNum = 0; 50 int ClassNum = 0; 51 int GoodNum = 0; 52 int PassNum = 0; 53 double Sum = 0; 54 double dblPass = 0; 55 int i = 0; 56 double dbl[100]; 57 char Cla[30]; 58 int nCount = 0; 59 int nPtr = 0; 60 //double dbl[100]; 61 FILE* fp = NULL; 62 fp = fopen("stu.txt", "r"); 63 if(fp == NULL) 64 { 65 puts("文件打開失敗!!!"); 66 return ; 67 } 68 fseek(fp, 0, SEEK_END); 69 nPtr = ftell(fp); 70 nCount = nPtr / sizeof(stu[0]); 71 fseek(fp, 0, SEEK_SET); 72 for(i = 0; i < nCount; i++) 73 { 74 fread(&stu[i], sizeof(stu[i]), 1, fp); 75 } 76 if(fp != NULL) 77 { 78 fclose(fp); 79 fp = NULL; 80 } 81 printf("請輸入你要查詢的班級名:"); 82 scanf("%29s", Cla); 83 int flag=0; 84 for(i = 0; i < nCount; i++) 85 { 86 if(strcmp(Cla, stu[i].class) == 0) 87 { 88 flag=1; 89 int j = 0; 90 for(j = 0; j < nCount; j++) 91 { 92 dbl[j] = stu[i].EnScore + stu[i].ChScore + stu[i].MathScore; 93 //printf("%lf ", dbl[i]); 94 } 95 ClassNum++; 96 } 97 } 98 if(flag==0) 99 { 100 puts("查無此班!!!"); 101 return; 102 } 103 for(i = 0; i < ClassNum; i++) 104 { 105 if(dbl[i] > 240) 106 { 107 GoodNum++; 108 } 109 if(dbl[i] > 180) 110 { 111 PassNum++; 112 } 113 else 114 { 115 UnPassNum++; 116 } 117 Sum += dbl[i]; 118 } 119 dblPass = PassNum / ClassNum; 120 printf("班級報表信息如下:\r\n"); 121 printf("班級人數:%d\n", ClassNum); 122 printf("班級總成績:%6.2lf\n", Sum); 123 printf("優秀人數(總分240以上):%d\n", GoodNum); 124 printf("及格人數(總分180以上):%d\n", PassNum); 125 printf("不及格人數(總分180以下): %d\n", UnPassNum); 126 printf("班級及格率:%6.1lf\%\n", dblPass*100); 127 } 128 129 //總分排序 130 void Sort() 131 { 132 int i = 0; 133 int nCount = 0; 134 int nPtr = 0; 135 double dbl[100]; 136 FILE* fp = NULL; 137 fp = fopen("stu.txt", "r"); 138 if(fp == NULL) 139 { 140 puts("文件打開失敗!!!"); 141 return ; 142 } 143 fseek(fp, 0, SEEK_END); 144 nPtr = ftell(fp); 145 nCount = nPtr / sizeof(stu[0]); 146 fseek(fp, 0, SEEK_SET); 147 for(i = 0; i < nCount; i++) 148 { 149 fread(&stu[i], sizeof(stu[i]), 1, fp); 150 } 151 if(fp != NULL) 152 { 153 fclose(fp); 154 fp = NULL; 155 } 156 for(i = 0; i < nCount; i++) 157 { 158 dbl[i] = stu[i].EnScore + stu[i].ChScore + stu[i].MathScore; 159 } 160 int j = 0; 161 Stu temp = {0}; 162 double tmp = 0; 163 for(i = 0; i < nCount-1; i++) 164 { 165 for(j = 0;j < nCount - i - 1; j++) 166 { 167 if(dbl[j] < dbl[j+1]) 168 { 169 tmp = dbl[j]; 170 dbl[j] = dbl[j + 1]; 171 dbl[j+1] = tmp; 172 temp = stu[j]; 173 stu[j] = stu[j+1]; 174 stu[j+1] = temp; 175 } 176 } 177 } 178 179 for(i = 0; i < nCount; i++) 180 { 181 //fread(&stu[i], sizeof(stu[i]), 1, fp); 182 printf("姓名:%s 班級:%s 學號:%d 英語:%6.2lf 語文:%6.2lf 數學:%6.2lf 總分:%6.2lf 第%d名\r\n", 183 stu[i].name, stu[i].class, stu[i].nId, 184 stu[i].EnScore, stu[i].ChScore, stu[i].MathScore, 185 stu[i].EnScore+stu[i].ChScore+stu[i].MathScore, i+1); 186 } 187 188 } 189 190 //歡迎界面 191 void Welcome() 192 { 193 printf("*************************************\r\n"); 194 printf("* *\r\n"); 195 printf("* 歡迎使用 *\r\n"); 196 printf("* *\r\n"); 197 printf("* 學 生 信 息 管 理 系 統 *\r\n"); 198 printf("* *\r\n"); 199 printf("*************************************\r\n"); 200 } 201 202 //管理員登錄界面 203 void AdminMenu() 204 { 205 //printf("歡迎管理員admin登錄系統! \r\n"); 206 printf("**************************************\r\n"); 207 printf("* ID編號 | 功能項 *\r\n"); 208 printf("* 1 | 添加學員信息 *\r\n"); 209 printf("* 2 | 刪除學員信息 *\r\n"); 210 printf("* 3 | 修改學員信息 *\r\n"); 211 printf("* 4 | 查詢學員信息 *\r\n"); 212 printf("* 5 | 學員成績排名 *\r\n"); 213 printf("* 6 | 顯示學員信息 *\r\n"); 214 printf("* 7 | 退出學員系統 *\r\n"); 215 printf("* 8 | 打印班級報表 *\r\n"); 216 printf("**************************************\r\n"); 217 } 218 219 void StuLogin() 220 { 221 int i = 0; 222 int nCount = 0; 223 int nPtr = 0; 224 FILE* fp = NULL; 225 fp = fopen("stu.txt", "r"); 226 if(fp == NULL) 227 { 228 puts("文件打開失敗!!!"); 229 return ; 230 } 231 fseek(fp, 0, SEEK_END); 232 nPtr = ftell(fp); 233 nCount = nPtr / sizeof(stu[0]); 234 fseek(fp, 0, SEEK_SET); 235 for(i = 0; i < nCount; i++) 236 { 237 fread(&stu[i], sizeof(stu[i]), 1, fp); 238 } 239 if(fp != NULL) 240 { 241 fclose(fp); 242 fp = NULL; 243 } 244 245 int id = 0; 246 char na[30]; 247 printf("請輸入學生帳號和密碼登錄\r\n"); 248 printf("請輸入帳號:"); 249 scanf("%d", &id); 250 printf("請輸入密碼:"); 251 scanf("%29s", na); 252 253 int nRet = FindBiaohao(id, nCount); 254 if(nRet == -1) 255 { 256 puts("登錄失敗!!!"); 257 } 258 else 259 { 260 if(strcmp(na, stu[nRet].name) == 0) 261 { 262 printf("該學生信息:\n"); 263 printf("姓名:%s 班級:%s 學號:%d 英語:%6.2lf 語文:%6.2lf 數學:%6.2lf\n", 264 stu[nRet].name, stu[nRet].class, stu[nRet].nId, 265 stu[nRet].EnScore, stu[nRet].ChScore, stu[nRet].MathScore); 266 } 267 else 268 { 269 puts("登錄失敗!!!"); 270 } 271 } 272 } 273 274 //登錄 275 int Login() 276 { 277 char strName[30]; 278 char strPwd[30]; 279 int i = 0; 280 puts("請輸入帳號名和密碼登錄系統"); 281 while(1) 282 { 283 if(i == 3) 284 { 285 return 0; 286 break; 287 } 288 printf("請輸入帳號名稱:"); 289 scanf("%29s", strName); 290 printf("請輸入帳號密碼:"); 291 scanf("%29s", strPwd); 292 if(strcmp(strName, "admin") == 0 && strcmp(strPwd, "admin") == 0) 293 { 294 return 1; 295 } 296 else 297 { 298 i++; 299 sleep(1); 300 } 301 } 302 303 } 304 305 //各種功能操作 306 void Select() 307 { 308 int n = 0; 309 while(1) 310 { 311 AdminMenu(); 312 puts("請選擇你要執行操作對應的菜單編號:"); 313 scanf("%d", &n); 314 switch(n) 315 { 316 case 1: 317 printf("添加學員信息\r\n"); 318 Insert(); 319 //ShowStu(); 320 break; 321 case 2: 322 printf("刪除學員信息\r\n"); 323 Delete(); 324 //ShowStu(); 325 break; 326 case 3: 327 printf("修改學員信息\r\n"); 328 Modify(); 329 //ShowStu(); 330 break; 331 case 4: 332 printf("查詢學員信息\r\n"); 333 Query(); 334 break; 335 case 5: 336 printf("學員成績排名\r\n"); 337 Sort(); 338 break; 339 case 6: 340 printf("顯示學員信息\r\n"); 341 ShowStu(); 342 break; 343 case 7: 344 printf("保存信息中...\r\n"); 345 sleep(3); 346 printf("退出系統\r\n"); 347 //getchar(); 348 exit(0); 349 break; 350 case 8: 351 printf("打印班級報表\n"); 352 ShowClass(); 353 break; 354 } 355 if( n < 1 && n > 8) 356 { 357 continue; 358 } 359 } 360 } 361 //添加學生信息 362 void Insert() 363 { 364 int nCount = 0; 365 int nPtr = 0; 366 FILE *fp = NULL; 367 fp = fopen("stu.txt", "a+"); 368 if(fp == NULL) 369 { 370 puts("文件打開失敗!!!"); 371 return ; 372 } 373 fseek(fp, 0, SEEK_END); 374 nPtr = ftell(fp); 375 nCount = nPtr / sizeof(stu[0]); 376 377 if(nCount == 0) 378 { 379 printf("輸入學生姓名:"); 380 scanf("%29s", stu[nCount].name); 381 scanf("%*[^\n]"); 382 scanf("%*c"); 383 printf("輸入學生的班級:"); 384 scanf("%29s", stu[nCount].class); 385 scanf("%*[^\n]"); 386 scanf("%*c"); 387 printf("輸入學生的ID:"); 388 scanf("%d", &stu[nCount].nId); 389 printf("請輸入英語成績:"); 390 scanf("%lf", &stu[nCount].EnScore); 391 printf("請輸入語文成績:"); 392 scanf("%lf", &stu[nCount].ChScore); 393 printf("請輸入數學成績:"); 394 scanf("%lf", &stu[nCount].MathScore); 395 396 397 } 398 else 399 { 400 int i = 0; 401 for(i = 0; i < nCount; i++) 402 { 403 fread(&stu[i], sizeof(stu[i]), 1, fp); 404 } 405 printf("輸入學生姓名:"); 406 scanf("%29s", stu[nCount].name); 407 scanf("%*[^\n]"); 408 scanf("%*c"); 409 printf("輸入學生的班級:"); 410 scanf("%29s", stu[nCount].class); 411 scanf("%*[^\n]"); 412 scanf("%*c"); 413 while(1) 414 { 415 int nID = 0; 416 int flag = 0; 417 printf("輸入學生的ID:"); 418 scanf("%d", &nID); 419 for(i = 0; i < nCount; i++) 420 { 421 if(stu[i].nId == nID) 422 { 423 break; 424 } 425 } 426 if(i == nCount) 427 { 428 stu[nCount].nId = nID; 429 break; 430 } 431 printf("當前學號已存在,"); 432 continue; 433 434 } 435 printf("請輸入英語成績:"); 436 scanf("%lf", &stu[nCount].EnScore); 437 printf("請輸入語文成績:"); 438 scanf("%lf", &stu[nCount].ChScore); 439 printf("請輸入數學成績:"); 440 scanf("%lf", &stu[nCount].MathScore); 441 } 442 443 fwrite(&stu[nCount], sizeof(stu[nCount]), 1, fp); 444 445 if(fp != NULL) 446 { 447 fclose(fp); 448 fp = NULL; 449 } 450 } 451 452 //刪除學生信息 453 void Delete() 454 { 455 456 int i = 0; 457 int nCount = 0; 458 int nPtr = 0; 459 FILE* fp = NULL; 460 fp = fopen("stu.txt", "r"); 461 if(fp == NULL) 462 { 463 puts("文件打開失敗!!!"); 464 return ; 465 } 466 fseek(fp, 0, SEEK_END); 467 nPtr = ftell(fp); 468 nCount = nPtr / sizeof(stu[0]); 469 fseek(fp, 0, SEEK_SET); 470 for(i = 0; i < nCount; i++) 471 { 472 fread(&stu[i], sizeof(stu[i]), 1, fp); 473 } 474 if(fp != NULL) 475 { 476 fclose(fp); 477 fp = NULL; 478 } 479 480 int nID = 0; 481 printf("請輸入你要刪除學生的學號:"); 482 scanf("%d", &nID); 483 int nRet = FindBiaohao(nID, nCount); 484 if(nRet == -1) 485 { 486 puts("查無此人!!!"); 487 return; 488 } 489 else 490 { 491 char c = 0; 492 printf("姓名:%s 班級:%s 學號:%d 英語:%6.2lf 語文:%6.2lf 數學:%6.2lf\n", 493 stu[nRet].name, stu[nRet].class, stu[nRet].nId, 494 stu[nRet].EnScore, stu[nRet].ChScore, stu[nRet].MathScore); 495 puts("確認刪除嗎?<y/n>"); 496 getchar(); 497 scanf("%c", &c); 498 getchar(); 499 if(c != 'n') 500 { 501 int i = 0; 502 for(i = nRet; i < nCount-1; i++) 503 { 504 stu[i] = stu[i+1]; 505 } 506 nCount--; 507 } 508 else 509 { 510 return; 511 } 512 } 513 514 515 //FILE* fp = NULL; 516 fp = fopen("stu.txt", "w"); 517 if(fp == NULL) 518 { 519 puts("打開文件失敗!!!"); 520 return ; 521 } 522 523 for(i = 0; i < nCount; i++) 524 { 525 fwrite(&stu[i], sizeof(stu[i]), 1, fp); 526 } 527 528 if(fp != NULL) 529 { 530 fclose(fp); 531 fp = NULL; 532 } 533 } 534 //修改學生信息 535 void Modify() 536 { 537 int i = 0; 538 int nCount = 0; 539 int nPtr = 0; 540 FILE* fp = NULL; 541 fp = fopen("stu.txt", "r"); 542 if(fp == NULL) 543 { 544 puts("文件打開失敗!!!"); 545 return ; 546 } 547 fseek(fp, 0, SEEK_END); 548 nPtr = ftell(fp); 549 nCount = nPtr / sizeof(stu[0]); 550 fseek(fp, 0, SEEK_SET); 551 for(i = 0; i < nCount; i++) 552 { 553 fread(&stu[i], sizeof(stu[i]), 1, fp); 554 } 555 if(fp != NULL) 556 { 557 fclose(fp); 558 fp = NULL; 559 } 560 561 int nID = 0; 562 printf("請輸入你要修改學生的學號:"); 563 scanf("%d", &nID); 564 int nRet = FindBiaohao(nID, nCount); 565 if(nRet == -1) 566 { 567 puts("查無此人!!!"); 568 return; 569 } 570 else 571 { 572 char c = 0; 573 printf("姓名:%s 班級:%s 學號:%d 英語:%6.2lf 語文:%6.2lf 數學:%6.2lf\n", 574 stu[nRet].name, stu[nRet].class, stu[nRet].nId, 575 stu[nRet].EnScore, stu[nRet].ChScore, stu[nRet].MathScore); 576 puts("確認修改嗎?<y/n>"); 577 getchar(); 578 scanf("%c", &c); 579 getchar(); 580 if(c != 'n') 581 { 582 int i = 0; 583 for(i = nRet; i < nCount-1; i++) 584 { 585 stu[i] = stu[i+1]; 586 } 587 nCount--; 588 } 589 else 590 { 591 return; 592 } 593 } 594 595 596 //FILE* fp = NULL; 597 fp = fopen("stu.txt", "w"); 598 if(fp == NULL) 599 { 600 puts("打開文件失敗!!!"); 601 return ; 602 } 603 604 for(i = 0; i < nCount; i++) 605 { 606 fwrite(&stu[i], sizeof(stu[i]), 1, fp); 607 } 608 609 if(fp != NULL) 610 { 611 fclose(fp); 612 fp = NULL; 613 } 614 615 Insert(); 616 } 617 618 //查詢學生信息 619 void Query() 620 { 621 int i = 0; 622 int nCount = 0; 623 int nPtr = 0; 624 FILE* fp = NULL; 625 fp = fopen("stu.txt", "r"); 626 if(fp == NULL) 627 { 628 puts("文件打開失敗!!!"); 629 return ; 630 } 631 fseek(fp, 0, SEEK_END); 632 nPtr = ftell(fp); 633 nCount = nPtr / sizeof(stu[0]); 634 fseek(fp, 0, SEEK_SET); 635 for(i = 0; i < nCount; i++) 636 { 637 fread(&stu[i], sizeof(stu[i]), 1, fp); 638 } 639 if(fp != NULL) 640 { 641 fclose(fp); 642 fp = NULL; 643 } 644 645 int nID = 0; 646 printf("請輸入你要查詢學生的學號:"); 647 scanf("%d", &nID); 648 int nRet = FindBiaohao(nID, nCount); 649 if(nRet == -1) 650 { 651 puts("查無此人!!!"); 652 } 653 else 654 { 655 char c = 0; 656 printf("姓名:%s 班級:%s 學號:%d 英語:%6.2lf 語文:%6.2lf 數學:%6.2lf\n", 657 stu[nRet].name, stu[nRet].class, stu[nRet].nId, 658 stu[nRet].EnScore, stu[nRet].ChScore, stu[nRet].MathScore); 659 } 660 } 661 662 663 //顯示學生信息 664 void ShowStu() 665 { 666 int i = 0; 667 int nCount = 0; 668 int nPtr = 0; 669 FILE* fp = NULL; 670 fp = fopen("stu.txt", "r"); 671 if(fp == NULL) 672 { 673 puts("文件打開失敗!!!"); 674 return ; 675 } 676 fseek(fp, 0, SEEK_END); 677 nPtr = ftell(fp); 678 nCount = nPtr / sizeof(stu[0]); 679 fseek(fp, 0, SEEK_SET); 680 for(i = 0; i < nCount; i++) 681 { 682 fread(&stu[i], sizeof(stu[i]), 1, fp); 683 printf("姓名:%s 班級:%s 學號:%d 英語:%6.2lf 語文:%6.2lf 數學:%6.2lf\n", 684 stu[i].name, stu[i].class, stu[i].nId, 685 stu[i].EnScore, stu[i].ChScore, stu[i].MathScore); 686 } 687 //puts(""); 688 689 if(fp != NULL) 690 { 691 fclose(fp); 692 fp = NULL; 693 } 694 } 695 696 void SmallMenu() 697 { 698 int n = 0; 699 while(1) 700 { 701 //AdminMenu(); 702 puts(""); 703 printf("*******************************\r\n"); 704 puts(""); 705 printf("****** 1.管理員登錄 *********\r\n"); 706 puts(""); 707 printf("****** 2.學生登錄 *********\r\n"); 708 puts(""); 709 printf("****** 3.退出系統 *********\r\n"); 710 puts(""); 711 printf("*******************************\r\n"); 712 713 puts("請選擇你要執行操作對應的菜單編號:"); 714 scanf("%d", &n); 715 switch(n) 716 { 717 case 1: 718 printf("管理員登錄\r\n"); 719 int nRet = Login(); 720 if(nRet) 721 { 722 while(1) 723 { 724 //AdminMenu(); 725 726 //puts("請選擇你要執行操作對應的菜單編號:"); 727 printf("歡迎管理員admin登錄系統!\r\n"); 728 Select(); 729 } 730 } 731 else 732 { 733 printf("Fuck You!\r\n"); 734 puts("你的帳號和密碼錯誤,請聯系管理員"); 735 } 736 break; 737 738 case 2: 739 printf("學生登錄:\r\n"); 740 StuLogin(); 741 break; 742 743 case 3: 744 puts("退出系統"); 745 exit(0); 746 break; 747 748 } 749 if( n < 1 && n > 3) 750 { 751 continue; 752 } 753 } 754 } 755 756 int main() 757 { 758 Welcome(); 759 sleep(2); 760 SmallMenu(); 761 return 0; 762 }
