主要用到了指針、結構體、數組、鏈表、文件讀取
由於平時用java,所以感覺自己的代碼還是可以看看的,盡量用面向對象的思想去寫,這也使得原本近千行代碼的程序只用了不到一半的代碼完成。
User.h //用戶對象
struct User { char UserAccount[100]; char UserPassword[100]; int Money; struct User *next; };
UI.h //就是一個簡單的菜單顯示
int showMenu(char MenuItem[][20], int itemCount){ system("cls"); for (int i = 0; i < 10;i++){ printf("\n"); } printf("***************************************************\n"); for (int i = 0; i < itemCount; i++){ printf("* "); printf("%d.", i+1); int Str_size = 20; for (int j = 0; j < 10; j++){ if (MenuItem[i][j] != '\0'){ putchar(MenuItem[i][j]); } else{ putchar(' '); } } printf(" *\n"); } printf("***************************************************"); return 0; }
void writeIntoFIle(FILE *fp, char path[]){ fopen_s(&fp, path, "w+"); struct User *temp = (struct User *)malloc(sizeof(struct User)); temp = head; for (int i = 0; i < 5; i++){ // printf("%s %s %d\n", temp->UserAccount, temp->UserPassword, temp->Money); fprintf(fp, "%s %s %d\n", temp->UserAccount, temp->UserPassword, temp->Money); if (temp->next != NULL) temp = temp->next; } } void initFile(FILE *fp, char path[]){ //fopen_s(&fp, path, "w+"); if (feof(fp)){ fprintf(fp, "%s %s %d\n", "6210300022352323", "123123", 10000); fprintf(fp, "%s %s %d\n", "6210300022352527", "132132", 1000000); fprintf(fp, "%s %s %d\n", "6210303002352323", "123123", 10000); fprintf(fp, "%s %s %d\n", "6210300202235233", "123123", 10000); fprintf(fp, "%s %s %d\n", "6213002323523233", "123123", 10000); } // fclose(fp); } void initdata(FILE *fp, char path[]){ /*printf("%d", fopen_s(&fp, path, "r+")); printf("%d", fopen_s(&fp, path, "r+")); printf("%d",fgetc(fp)==EOF);*/ if (fopen_s(&fp, path, "r+") == 0 && fgetc(fp) == EOF){ initFile(fp, path); } rewind(fp); struct User *user; Current_user = user = (struct User *)malloc(sizeof(struct User)); char temp[100]; //_getch(); while (fscanf_s(fp, "%s", &user->UserAccount, 100) != EOF){ fscanf_s(fp, "%s", &user->UserPassword, 100); fscanf_s(fp, "%d\n", &user->Money, 100); struct User *nextuser; nextuser = (struct User *)malloc(sizeof(struct User)); printf("%s %s %d\n", user->UserAccount, user->UserPassword, user->Money); if (head == NULL) head = user; user->next = nextuser; user = nextuser; } fclose(fp); }
bool checkAccount(char account[],struct User *u){ struct User *temp = (struct User *)malloc(sizeof(struct User)); struct User *headCopy = (struct User *)malloc(sizeof(struct User)); temp = head; *headCopy =* head; while (temp != NULL){ bool isExist = true; /*printf("first%s %d\n", head->UserAccount, head->Money); printf("temp%s %d\n", temp->UserAccount, temp->Money);*/ for (int i = 0; i < 16; i++) { if (account[i]!=temp->UserAccount[i]) { isExist = false; break; } } if (isExist) { u=temp; Current_user = u; //*head =* headCopy; /* printf("findout%s\n",u->UserAccount); printf("afterchange%s %s\n", Current_user->UserAccount, head->UserAccount); printf("headcopy%s %s\n", headCopy->UserAccount, headCopy->UserPassword); _getch();*/ return true; } temp = (temp->next); } return false; } bool checkPassword(char Userpwd[], char Inputpwd[]){ for (int i = 0; i<6; i++){ if (Userpwd[i] != Inputpwd[i]){ return false; } } return true; } void showAccount(char account[]){ int length = strlen(account); if (length>16) length=16; for (int i = 0; i < length; i++){ putchar(account[i]); if ((i+1) % 4 == 0&&i!=0){ putchar(' '); } } } void showPassword(int length){ if (length>6) length = 6; for (int i = 0; i < length; i++){ _putch('*'); } } void InputAccount(char UserAccount[]){ int i = 0; system("cls"); printf("請輸入用戶名:"); while ((UserAccount[i] = _getch()) != '\r'&&i<16){ if (UserAccount[i] == 8&&i>0){ UserAccount[i] = '\0'; UserAccount[i-1] = '\0'; i--; } else if (UserAccount[i] >= '0' && UserAccount[i] <= '9'){ if (i <= 15) i++; } system("cls"); printf("請輸入用戶名:"); showAccount(UserAccount); } } void InputPassword(char account[],char pwd[]){ int i = 0; system("cls"); printf("用戶名:"); showAccount(account); printf("\n請輸入密碼:"); while ((pwd[i] = _getch()) != '\r'){ if (pwd[i] == 8){ pwd[i] = '\0'; pwd[i - 1] = '\0'; i--; } else if (pwd[i] >= '0' && pwd[i] <= '9'){ if (i <= 5) i++; } system("cls"); printf("用戶名:"); showAccount(account); printf("\n請輸入密碼:"); showPassword(strlen(pwd)); } } void WithDraw(int Count){ if (Count > Current_user->Money){ printf("取款金額超出存款"); return; } else { system("cls"); printf("取款金額%d\n按任意鍵返回上一菜單",Count); Current_user->Money -= Count; } } void Deposit(int Count){ system("cls"); printf("存款金額%d\n按任意鍵返回上一菜單", Count); Current_user->Money += Count; }
Main.cpp
#include#include #include #include struct User *head, *Current_user; #include "User.h" #include "UI.h" #include "AccountController.h" #include "FileController.h" int InputMoney(){ int result = 100; char inputItem = '\0'; int i = 0; system("cls"); printf("輸入金額:"); while ((inputItem = _getch()) != '\r'){ system("cls"); printf("輸入金額:"); if (inputItem >= '0'&&inputItem <= '9'){ if (result != 100){ result *= 10; result += (inputItem - 48) * 100; } else { result = (inputItem - 48) * 100; } } if (result > 5000){ printf("取款限額為5000"); result = 0; } printf("\n%d", result); } return result; } int SelectMoney(){ int result = 100; char inputItem = '\0'; char MoneyItem[][20] = { "100", "200", "500", "1000", "輸入金額" }; showMenu(MoneyItem, 5); while ((inputItem = _getch()) >='1'&&inputItem<='5') { switch (inputItem) { case '1': return 100; break; case '2': return 200; break; case '3': return 500; break; case '4': return 1000; break; case '5': return InputMoney(); break; default: printf("請輸入正確選項\n"); } } } bool OperationMenu(){ char input_seletor = '\0'; int Money = 0; struct User *temp = (struct User *)malloc(sizeof(struct User)); char newPassword[7] = { '\0' }; char confirmpassword[7] = { '\0' }; char LoginMenu[][20] = { "取款", "存款", "查詢余額", "修改密碼" ,"退卡"}; showMenu(LoginMenu, 5); while ((input_seletor = _getch()) >= '1'&&input_seletor <= '5') { switch (input_seletor) { case '1': Money = SelectMoney(); WithDraw(Money); break; case '2': Money = SelectMoney(); Deposit(Money); break; case '3': system("cls"); printf("當前余額:%d\n按任意鍵返回", Current_user->Money); break; case '4': system("cls"); InputPassword(Current_user->UserAccount, newPassword); system("cls"); printf("再次輸入以確認密碼\n,按任意鍵繼續輸入"); _getch(); InputPassword(Current_user->UserAccount, confirmpassword); if (checkPassword(newPassword, confirmpassword)){ for (int i = 0; Current_user->UserPassword[i] != '\0';i++){ Current_user->UserPassword[i] = newPassword[i]; } printf("%s %s ",newPassword,confirmpassword); printf("\n密碼修改成功!"); } else{ system("cls"); printf("兩次密碼輸入不一致"); } break; case '5': return true; break; default: break; } _getch(); break; } OperationMenu(); } void startATM(){ FILE *fp = NULL; char path[] = "C:\\Users\\user\\Desktop\\Account.txt"; initdata(fp, path);//初始化數據 int Islogin = 0; char input_seletor = '\0'; char UserAccount[100] = { '\0' }; char UserPwd[100] = { '\0' }; char InputPwd[100] = { '\0' }; InputAccount(UserAccount); //strcpy_s(UserAccount, "6210300022352527"); if (checkAccount(UserAccount, Current_user)){ while (Islogin<=2){ InputPassword(UserAccount, InputPwd); //strcpy_s(InputPwd, "132132"); if (checkPassword(Current_user->UserPassword, InputPwd)){ printf("login successful\n"); if (OperationMenu()){ break; } Islogin = 0; } else{ Islogin++; printf("password error"); } } if (Islogin >= 2){ system("cls"); printf("密碼三次輸入錯誤,吞卡\n"); } else{ writeIntoFIle(fp,path); system("cls"); printf("數據保存成功"); } } _getch(); } int main(){ startATM(); return 0; }