2015.12.07 ATM_Function
1 #include <stdio.h> 2 3 #include <stdlib.h> 4 5 #include <stdbool.h> 6 7 8 9 int password = 123456; 10 11 int balance = 10000; 12 13 bool isLogined = false; 14 15 16 17 void alert(char *content); 18 19 int getValidOperation(int maxNum); 20 21 void quit(int status); 22 23 void query(); 24 25 void isContinue(); 26 27 bool inputPassword(); 28 29 void takeMoney(); 30 31 void changePsw(); 32 33 34 35 36 37 int main(int argc, const char * argv[]) { 38 39 40 41 bool result; 42 43 44 45 while (1) { 46 47 alert("1.输入密码\n2.取款\n3.查询余额\n4.更改密码\n5.退出"); 48 49 int operation = INT32_MAX; 50 51 operation = getValidOperation(5); 52 53 switch (operation) { 54 55 //1.输入密码 56 57 case 1: 58 59 result = inputPassword(); 60 61 if (result == false) { 62 63 quit(EXIT_SUCCESS); 64 65 }else{ 66 67 isLogined = true; 68 69 } 70 71 break; 72 73 //2.取款 74 75 case 2: 76 77 takeMoney(); 78 79 isContinue(); 80 81 break; 82 83 84 85 //3.查询余额 86 87 case 3: 88 89 query(); 90 91 isContinue(); 92 93 getchar(); 94 95 break; 96 97 //4.更改密码 98 99 case 4: 100 101 changePsw(); 102 103 break; 104 105 //5.退出 106 107 case 5: 108 109 quit(EXIT_SUCCESS); 110 111 default: 112 113 break; 114 115 } 116 117 printf("\n"); 118 119 120 121 } 122 123 return 0; 124 125 } 126 127 128 129 void alert(char *content){ 130 131 printf("****************\n"); 132 133 printf("%s\n", content); 134 135 printf("****************\n"); 136 137 } 138 139 140 141 int getValidOperation(int maxNum){ 142 143 int operation; 144 145 146 147 printf("选一个吧:"); 148 149 scanf("%d", &operation); 150 151 152 153 while (operation < 1 || operation > maxNum) { 154 155 printf("别搞笑,好好输:"); 156 157 getchar(); 158 159 scanf("%d", &operation); 160 161 } 162 163 return operation; 164 165 } 166 167 168 169 void quit(int status){ 170 171 alert("拔卡!拔卡!拔卡!"); 172 173 exit(status); 174 175 } 176 177 178 179 180 181 void query(){ 182 183 if (isLogined == false) { 184 185 bool result = inputPassword(); 186 187 if (result == false) { 188 189 quit(EXIT_SUCCESS); 190 191 }else { 192 193 isLogined = true; 194 195 } 196 197 }else{ 198 199 printf("****************\n"); 200 201 printf("你还有¥%d\n", balance); 202 203 printf("****************\n"); 204 205 } 206 207 } 208 209 210 211 void isContinue(){ 212 213 char choose; 214 215 216 217 alert("继续不?(y/n):"); 218 219 getchar(); 220 221 scanf("%c", &choose); 222 223 224 225 while (choose != 'y' && choose != 'n') { 226 227 printf("好好输!(y/n)\n"); 228 229 getchar(); 230 231 scanf("%c", &choose); 232 233 } 234 235 if (choose == 'n') { 236 237 quit(EXIT_SUCCESS); 238 239 } 240 241 } 242 243 244 245 246 247 bool inputPassword(){ 248 249 int inputedpassword = INT32_MAX; 250 251 int totalWrongTime = 3; 252 253 254 255 do { 256 257 printf("%s", inputedpassword == INT32_MAX ? "密码是啥:": "别骗我!密码是啥:"); 258 259 getchar(); 260 261 scanf("%d", &inputedpassword); 262 263 264 265 totalWrongTime--; 266 267 } while (inputedpassword != password && totalWrongTime > 0); 268 269 270 271 if (inputedpassword != password) { 272 273 return false; 274 275 }else{ 276 277 return true; 278 279 } 280 281 } 282 283 284 285 286 287 void takeMoney(){ 288 289 if (isLogined == true) { 290 291 int totalMoney; 292 293 int num2_1; 294 295 alert("1.¥100\n2.¥300\n3.¥500\n4.其他金额"); 296 297 num2_1 = getValidOperation(4); 298 299 switch (num2_1) { 300 301 case 1: 302 303 totalMoney = 100; 304 305 break; 306 307 case 2: 308 309 totalMoney = 300; 310 311 break; 312 313 case 3: 314 315 totalMoney = 500; 316 317 break; 318 319 case 4: 320 321 printf("取多少:"); 322 323 scanf("%d", &totalMoney); 324 325 break; 326 327 default: 328 329 break; 330 331 } 332 333 if (totalMoney <= balance) { 334 335 printf("\n您的余额为:¥%d\n \n", balance -= totalMoney); 336 337 }else { 338 339 printf("\n穷B,钱不够!\n \n"); 340 341 } 342 343 344 345 }else{ 346 347 bool result = inputPassword(); 348 349 if (result == false) { 350 351 quit(EXIT_SUCCESS); 352 353 }else { 354 355 isLogined = true; 356 357 } 358 359 360 361 } 362 363 } 364 365 366 367 368 369 370 371 void changePsw(){ 372 373 if (isLogined == false) { 374 375 bool result = inputPassword(); 376 377 if (result == false) { 378 379 quit(EXIT_SUCCESS); 380 381 }else { 382 383 isLogined = true; 384 385 } 386 387 }else{ 388 389 printf("输入新密码:\n"); 390 391 scanf("%d", &password); 392 393 } 394 395 } 396 397 398 399