Implement ATM system in C Language
Mainly used for reading pointers, struct, arrays, linked lists, and files
Since java is used at ordinary times, I still feel that my code can be viewed. I try to write it with the object-oriented idea, this also makes the program with nearly lines of code only use less than half of the Code.
User. h // User object
struct User{char UserAccount[100];char UserPassword[100];int Money;struct User *next;};
UI. h // is a simple menu display
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;}
FileContrller. h is used for file-related operations and initialization.
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);}
AccountController. h // account-related operations, account search, password verification or something
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 \ n", Current_user-> UserAccount, head-> UserAccount); printf ("headcopy % 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 ("Enter the User name:"); 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 ("Enter the User name:"); showAccount (UserAccount );}} void InputPassword (char account [], char pwd []) {int I = 0; system ("cls"); printf ("username:"); showAccount (account ); printf ("\ n enter the password:"); 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 ("username:"); showAccount (account); printf ("\ n, enter the password :"); showPassword (strlen (pwd) ;}} void WithDraw (int Count) {if (Count> Current_user-> Money) {printf ("withdrawal amount exceeds deposit"); return ;} else {system ("cls"); printf ("withdrawal amount % d \ n press any key to return to the previous menu", Count); Current_user-> Money-= Count ;}} void Deposit (int Count) {system ("cls"); printf ("Deposit amount % d \ n press any key to return to the previous menu", Count ); current_user-> Money + = Count ;}
Main program entry
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 (" input amount: "); while (inputItem = _ getch ())! = '\ R') {system ("cls"); printf ("input amount:"); if (inputItem> = '0' & inputItem <= '9 ') {if (result! = 100) {result * = 10; result + = (inputItem-48) * 100;} else {result = (inputItem-48) * 100 ;}} if (result> 5000) {printf ("withdrawal limit: 5000"); result = 0;} printf ("\ n % d", result);} return result ;} int SelectMoney () {int result = 100; char inputItem = '\ 0'; char MoneyItem [] [20] = {"100", "200", "500 ", "1000", "input amount"}; showMenu (MoneyItem, 5); while (inputItem = _ getch ()> = '1' & inputItem <= '5') {switch (inputItem) {ca Se '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 ("enter the correct option \ 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] = {"withdrawal ", "deposit", "query Remainder "," Change Password "," back "}; 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 ("current balance: % d \ n press any key to return ", Current_user-> Money); break; case '4': system (" cls "); InputPassword (Current_user-> UserAccount, newPassword ); system ("cls "); Printf ("Enter again to confirm the password \ n, press any key to continue to enter"); _ 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", newPassword, confirmpassword ); printf ("\ n password changed! ");} Else {system (" cls "); printf (" inconsistent two passwords ");} 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); // initialization data 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 ("three-time password input error, swallowed \ n");} else {write1_file (fp, path); system ("cls "); printf ("data saved successfully") ;}}_ getch () ;}int main () {startATM (); return 0 ;}