我的學生資訊管理系統總結

期待一片自己的藍天發表於2014-06-14

我的學生管理系統總結


花了一天時間用C語言做了一個學生資訊管理系統,有一些想要總結的地方,在此記錄一下。 
雖然是一個簡單的系統,簡單到只有增刪改查功能,而且,儲存資料的地方不是資料庫,而是檔案,但是,我還是按照了做大型專案時採用的“三層架構”的思路來寫:大體上就是:底層,和資料庫打交道;中間層,處理使用者輸入的業務邏輯,並將錯誤資訊反饋給使用者;表面層,就是和將介面展現給使用者,接受使用者的輸入。 
下面是,總結的一些好的程式碼:

1、判斷鍵盤連續兩次輸入回車(也可以用來判斷連續兩次輸入某字元)。

  1. void back_button()/*在子選單下,使用者按兩下Enter鍵,返回主選單*/
  2. {
  3. printf("Please press \"Enter button\" two times to back previous menu.\n");
  4. char ch;
  5. do
  6. {
  7. while (fflush(stdin), ch = getch(), ch != '\r');
  8. if ((ch = getch()) == '\r'){
  9. break;
  10. }
  11. } while (1);
  12. system("cls");
  13. }

2、用fgets()接受螢幕輸入資訊,一方面可以限制輸入長度,一方面也能判斷使用者是直接按下Enter鍵,還是直接按下Ctrl+Z.

  1. void sub_choose_1()/*子選單1:根據學生姓名搜尋學生資訊*/
  2. {
  3. system("cls");
  4. char stu_name[STU_NAME_SIZE];
  5. memset(stu_name,0,STU_NAME_SIZE);/*將字串初始化為0*/
  6. loops_search_stu_name:printf("Please input a name for search: \n");
  7. fflush(stdin);
  8. fgets(stu_name, STU_NAME_SIZE, stdin);
  9. if (stu_name[0] == 0){
  10. return;
  11. }
  12. strProcess(stu_name);/*將fgets()新增的'\n'去掉*/
  13. if (strlen(stu_name) == 0){
  14. printf("Student's name cannot empty!Please input again.\n");
  15. goto loops_search_stu_name;
  16. }
  17. stu_pNode stu;
  18. stu = searchStuInfoByName(stuList,stu_name);
  19. showAllStuInfo(stu);
  20. back_button();
  21. }

3、上一個函式是對輸入字串時的錯誤資訊進行判斷,這個函式是對輸入數時的錯誤資訊進行判斷。

(總結一下,由於fgets()的返回值是輸入字串的指標或者NULL,所以沒法用返回值進行判斷,也就沒法用while迴圈。而scanf()函式的返回值是:正確接受時,返回接受的個數,沒正確接受時,返回0,遇到Ctrl+Z時,返回EOF(即-1),所以,可以用迴圈進行處理)

  1. void sub_choose_2()/*子選單2:根據學生學號搜尋學生資訊*/
  2. {
  3. system("cls");
  4. int stu_id, input_info;
  5. printf("Please input a sno for search: \n");
  6. printf("Sno: ");
  7. while (fflush(stdin), input_info = scanf("%d", &stu_id), input_info == EOF || input_info != 1){
  8. if (input_info == EOF){
  9. return;
  10. }
  11. printf("Sno is a number,please input again.\n");
  12. printf("Sno: ");
  13. }
  14. stu_pNode stu;
  15. stu = searchStuInfoById(stuList,stu_id);
  16. showOneStuInfo(stu);
  17. back_button();
  18. }

4、這段程式碼,對fgets()的返回值進行了非常細的劃分。

  1. loop_modify_name:printf("Name: ");
  2. if (fflush(stdin), fgets(new_stu.stu_name, STU_NAME_SIZE, stdin)){/*使用者敲回車,此條件成立*/
  3. strProcess(new_stu.stu_name);
  4. }
  5. else{/*使用者輸入Ctrl+Z,此條件成立*/
  6. strcpy(new_stu.stu_name, stu_info->stu_name);
  7. }
  8. if (strlen(new_stu.stu_name) == 0){
  9. printf("Name must be not empty.If don't want to modify,press Ctrl+Z.\n");
  10. goto loop_modify_name;
  11. }

5、這一大段程式碼,非常完美的模擬了使用者輸入使用者名稱密碼進行登入時的情景。其中包括了,允許使用者有三次輸錯的機會、模擬將使用者的輸入資訊轉換成‘*’在螢幕上顯示(包括按退格鍵時,‘*’跟著減少,密碼輸入過長,停止輸入,並且有提示音等細節功能)。

  1. void userNameprocess(char *str)
  2. {
  3. while (*str != 10){
  4. ++str;
  5. }
  6. *str = 0;
  7. }
  8. void backspace(int *length,char **ch)
  9. {
  10. --(*ch);
  11. --(*length);
  12. putchar(8);
  13. putchar(32);
  14. putchar(8);
  15. }
  16. void passwordGets(char *psw)
  17. {
  18. char ch;
  19. int psw_length = 0, sign = 0;
  20. while ((ch = getch()) != '\r'){/*使用者沒有輸入Enter鍵時,繼續監測鍵盤*/
  21. if (psw_length < USER_PASSWORD_LEN - 1){/*允許輸入的最大長度限制*/
  22. if (ch == 8){/*如果是退格鍵*/
  23. if (psw_length >0){
  24. sign = 1;
  25. backspace(&psw_length, &psw);
  26. }
  27. else{/*長度小於0,即退格到開頭了,響鈴警報*/
  28. putchar(7);
  29. }
  30. }
  31. else{/*不是退格鍵,繼續前進*/
  32. sign = 0;
  33. *(psw++) = ch;
  34. ++psw_length;
  35. }
  36. if (!sign){/*退格鍵標記,當使用者按下退格鍵時,不再列印'*'.*/
  37. putchar('*');
  38. }
  39. }
  40. else if (ch == 8){/*長度超過限制,為能重新輸入,即滿足輸入條件(lentgh<psw_length < USER_PASSWORD_LEN - 1),在按退格鍵時,退一格*/
  41. backspace(&psw_length, &psw);
  42. }
  43. else{/*長度已經超過最大限制了,如繼續輸入非回車非退格鍵,則響鈴警報*/
  44. putchar(7);
  45. }
  46. }
  47. *psw = '\0';
  48. }
  49. int login()
  50. {
  51. char user_name[USER_NAME_SIZE];
  52. char user_password[USER_PASSWORD_LEN];
  53. user_pNode user;
  54. int count = 3;
  55. do{
  56. --count;
  57. system("cls");
  58. printf("enter user_name: ");
  59. fgets(user_name, USER_NAME_SIZE + 1, stdin);/*獲取使用者名稱*/
  60. userNameprocess(user_name);
  61. system("cls");
  62. printf("enter usr_password: ");
  63. passwordGets(user_password);/*獲取使用者密碼*/
  64. user = searchUserAccountInfoByName(userList, user_name);
  65. if (user){
  66. if (strcmp(user->user_password, user_password) != 0){
  67. if (count == 0){
  68. system("cls");
  69. printf("You have already input Wrong 3 times,system will be exit.\n");
  70. }
  71. else{
  72. printf("\nPassword wrong,please try again!you have %d times.\n", count);
  73. system("pause");
  74. }
  75. }
  76. else{
  77. return user->role_type;
  78. }
  79. }
  80. else{
  81. if (count == 0){
  82. system("cls");
  83. printf("You have already input Wrong 3 times,system will be exit.\n");
  84. }
  85. else{
  86. printf("\nUser name is not exist,please try again!you have %d times.\n", count);
  87. system("pause");
  88. }
  89. }
  90. } while (count);
  91. exit(-1);
  92. }

相關文章