1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | #include <stdio.h> #include <string.h> #define SIZE 100 #define LEN 20 enum state {EXIT, RUN, WRONG_NAME, WRONG_CODE, Y, N}; int main( void ) { // 宣告預設的帳號及密碼 char userID[SIZE][LEN]; char userCODE[SIZE][LEN]; // 宣告暫存使用者輸入的帳號及密碼 char inputName[LEN]; char inputCode[LEN]; // 宣告暫存新註冊的帳號及密碼 char newName[LEN]; char newCode[LEN]; // 宣告暫存成功登入的帳號及密碼 char whoName[LEN]; char whoCode[LEN]; // administrator 為預設的管理者帳號, 0000 為其密碼 strcpy (userID[0], "administrator" ); strcpy (userCODE[0], "0000" ); // 宣告接收管理模式指令 char instruction[LEN]; // 宣告接收搜尋目標值 char searchname[LEN]; // 宣告排序時用的暫存變數 char tempName[LEN]; char tempCode[LEN]; // 宣告狀態變數、迴圈變數及陣列索引值變數 int state, i, j, k; int counter; for (i = 1; i < SIZE; i++) { strcpy (userID[i], "" ); strcpy (userCODE[i], "" ); } counter = 1; while (1) { state = WRONG_NAME; printf ( "login: " ); scanf ( "%s" , inputName); // "exit" 為內建指令,用來離開迴圈 if (! strcmp (inputName, "exit" )) { state = EXIT; printf ( "\n\n您將要離開本登入程式....\n\n" ); break ; } // "new" 為內建指令,用來新建帳號密碼 if (! strcmp (inputName, "new" )) { state = RUN; printf ( "\n請輸入新的註冊帳號: " ); scanf ( "%s" , newName); printf ( "請輸入新的登入密碼: " ); scanf ( "%s" , newCode); strcpy (userID[counter], newName); strcpy (userCODE[counter], newCode); counter++; printf ( "\n新的帳號、密碼已建立,請重新登入....\n" ); continue ; } // 從陣列中判斷是否有使用者輸入的帳號,若有才再繼續要求輸入密碼 for (i = 0; i < SIZE; i++) { if (! strcmp (inputName, userID[i])) { printf ( "password: " ); scanf ( "%s" , inputCode); if (i == 0 && ! strcmp (inputCode, userCODE[i])) { printf ( "\n\n您成功以管理員模式登入....\n" ); printf ( "將入帳號管理模式,請在提示符號 # 後輸入指令\n" ); while (1) { printf ( "\n# " ); scanf ( "%s" , instruction); // exit 為離開管理模式的指令 if (! strcmp (instruction, "exit" )) { break ; } // list 為列印使用者列表的指令 if (! strcmp (instruction, "list" )) { printf ( "\n以下為所有註冊使用者的帳號及密碼\n" ); printf ( "\n帳號 - 密碼\n" ); for (j = 0; j < SIZE; j++) { if (! strcmp (userID[j], "" )) { continue ; } printf ( "%s - %s\n" , userID[j], userCODE[j]); } continue ; } // search 為查詢帳號指令 if (! strcmp (instruction, "search" )) { state = N; printf ( "\n請輸入要查找的帳號: " ); scanf ( "%s" , searchname); for (j = 0;j < SIZE; j++) { if (! strcmp (userID[j], searchname)) { state = Y; break ; } } if (state == Y) { printf ( "\n帳號 %s 已經註冊,密碼是 %s\n" , userID[j], userCODE[j]); } else if (state == N) { printf ( "\n還沒有 %s 的帳號註冊唷!\n" , searchname); } continue ; } // delete 為刪除帳號指令 if (! strcmp (instruction, "delete" )) { state = N; printf ( "\n請輸入要刪除的帳號: " ); scanf ( "%s" , searchname); for (j = 0; j < SIZE; j++) { if (! strcmp (userID[j], searchname)) { state = Y; counter--; break ; } } if (state == Y) { for (j; j < SIZE; j++) { strcpy (userID[j], userID[j + 1]); strcpy (userCODE[j], userCODE[j + 1]); } printf ( "\n%s 的帳號資料已刪除\n" , searchname); } else if (state == N) { printf ( "\n%s 的帳號資料不存在,無法刪除\n" , searchname); } continue ; } // sort 為排序指令 if (! strcmp (instruction, "sort" )) { for (j = 1; j < SIZE - 1; j++) { for (k = 2; k < SIZE; k++) { if (userID[k - 1][0] > userID[k][0]) { strcpy (tempName, userID[k - 1]); strcpy (tempCode, userCODE[k - 1]); strcpy (userID[k - 1], userID[k]); strcpy (userCODE[k - 1], userCODE[k]); strcpy (userID[k], tempName); strcpy (userCODE[k], tempCode); } } } counter = 1; printf ( "\n資料已排序完成\n" ); continue ; } } printf ( "\n\n您即將離開管理模式,請重新登入....\n\n" ); state = RUN; break ; } if (! strcmp (inputCode, userCODE[i])) { strcpy (whoName, inputName); strcpy (whoCode, inputCode); printf ( "\n\n哈囉, %s , 歡迎使用本登入程式...\n\n" , whoName); state = RUN; break ; } else { state = WRONG_CODE; break ; } } } // 判斷目前狀態,顯示提示訊息 if (state == RUN) { continue ; } else if (state == WRONG_NAME) { printf ( "\n\n沒有這名使用者喔!請重新登入....\n\n" ); continue ; } else if (state == WRONG_CODE) { printf ( "\n\n密碼錯誤,請重新登入....\n\n" ); continue ; } } printf ( "\n\n程式即將關閉,所有建立的資料都會消失....\n\n" ); return 0; } /* 《程式語言教學誌》的範例程式 檔名:nc8.c 功能:利用 while 迴圈設計的登入系統 作者:張凱慶 時間:西元2010年7月 */ |
程式說明及編譯執行,請繼續參考
沒有留言:
張貼留言