The main use of pointers, structures, arrays, linked lists, file read
Because of the usual use of Java, so feel their own code can be seen, as far as possible with the object-oriented thinking to write, which also makes the original nearly thousands of lines of code in the program only used less than half of the code to complete.
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 ("* " );p rintf ("%d.", i+1); int str_size = 20;for (int j = 0; J <; J + +) {if (menuitem[i][j]! = ' + ') {Putchar (Menuitem[i][j]) ;} Else{putchar (");}} printf (" *\n");} printf ("***************************************************"); return 0;}
FileContrller.h for file-related operations, as well as initialization
void Writeintofile (FILE *fp, char path[]) {fopen_s (&FP, Path, "w+"), struct user *temp = (struct user *) malloc (sizeof (ST Ruct 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", "621030002235 2323 "," 123123 ", 10000); fprintf (FP,"%s%s%d\n "," 6210300022352527 "," 132132 ", 1000000); fprintf (FP,"%s%s%d\n "," 621030 3002352323 "," 123123 ", 10000); fprintf (FP,"%s%s%d\n "," 6210300202235233 "," 123123 ", 10000); fprintf (FP,"%s%s%d\n "," 62 13002323523233 "," 123123 ", 10000);} Fclose (FP);} void InitData (FILE *fp, char path[]) {/*printf ("%d", fopen_s (&FP, Path, "r+"));p rintf ("%d", fopen_s (&FP, path, "R + "));p rintf ("%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)! = EOF) {fscanf_s (FP, "%s", &user->userpassword, +); fscanf_s (FP, "%d\n", & User->money), struct user *nextuser;nextuser = (struct user *) malloc (sizeof (struct user));p rintf ("%s%s%d\n", use R->useraccount, User->userpassword, User->money); if (head = = NULL) head = User;user->next = Nextuser;user = NE Xtuser;} Fclose (FP);}
accountcontroller.h//account-related operations, find accounts, verify passwords, etc.
BOOL CheckAccount (char account[],struct user *u) {struct user *temp = (struct user *) malloc (sizeof (struct user)); struct use R *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);p rintf ("temp%s%d\n", Temp->useraccount, Temp->money); */for (int i = 0; i <; i++) {if (Account[i]!=temp->useraccount[i]) {isexist = False;break;}} if (IS Exist) {u=temp; Current_User = u; *head =* headcopy;/*printf ("findout%s\n", U->useraccount);p rintf ("afterchange%s%s\n", current_user-> UserAccount, Head->useraccount);p rintf ("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 FAL Se;}} 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) {p Utchar (');}}} 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");p rintf ("Enter user name:"); while ((Useraccount[i] = _getch ())! = ' \ R ' &&i<16) {if (useraccount[i] = = 8&&i>0) {Useraccount[i] = ' + '; Useraccount[i-1] = ' n '; i--;} else if (Useraccount[i] >= ' 0 ' && useraccount[i] <= ' 9 ') {if (I <=) i++;} System ("CLS");p rintf ("Please enter user name:"); Showaccount (UserAccount);}} void Inputpassword (char Account[],char pwd[]) {int i = 0;system ("cls");p rintf ("username:"); Showaccount (account);p rintf ("\ N Enter the password: "), while ((Pwd[i] = _getch ())! = ' \ r ') {if (pwd[i] = = 8) {Pwd[i] = ';p wd[i-1] = ' n '; i--;} else if (Pwd[i] >= ' 0 ' && pwd[i] <= ' 9 ') {if (I <= 5) i++;} System ("CLS");p rintf ("User name:"), Showaccount (account);p rintf ("\ n Please enter password:"); Showpassword (Strlen (PWD));}} void Withdraw (int count) {if (Count > Current_user->money) {printf ("Withdrawal amount exceeds deposit"); return;} else {System ("CLS");p rintf ("Withdrawal amount%d\n Press any key to return to the previous menu", Count); Current_user->money-= Count;}} void Deposit (int Count) {System ("CLS");p rintf ("Deposit amount%d\n Press any key to return to the previous menu", Count); Current_user->money + = Count;}
Main program Entrance
Main.cpp
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <conio.h>struct User *head, *current_user; #include "User.h" #include "UI.h" #include "AccountController.h" #include "FileController.h" int Inputmoney () {int result = 100;char Inputitem = ' + '; int i = 0;system ("cls");p rintf ("Input amount:"); while ((Inputitem = _getch ()) ! = ' \ r ') {System ("CLS");p rintf ("Input Amount:"), if (Inputitem >= ' 0 ' &&inputitem <= ' 9 ') {if (result! =) {result * = 10;result + = (inputItem-48) * 100;} else {result = (inputItem-48) * 100;}} if (Result >) {printf ("The withdrawal limit is"); result = 0;} printf ("\n%d", result);} return result;} int Selectmoney () {int result = 100;char Inputitem = ' + '; char moneyitem[][20] = {"100", "200", "500", "1000", "Enter Amount"};sh Owmenu (Moneyitem, 5); ((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 ("Please enter the correct option \ n");}}} BOOL Operationmenu () {char input_seletor = '% '; int money = 0;struct user *temp = (struct user *) malloc (sizeof (struct user) Char newpassword[7] = {' loginmenu[][20 '};char confirmpassword[7] = {'};char ' = {' + ', ' deposit ', ' Query balance ', ' Change password ', ' Exit card "};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 ();D eposit (money); Break;case ' 3 ': System ("CLS");p rintf ("Current balance:%d\ N Press any key to return ", Current_user->money); Break;case ' 4 ': System (" CLS "); Inputpassword (Current_user->useraccount, NewPassword), System ("CLS");p rintf ("Enter again to confirm password \ n, press any key to continue input"); _getch (); Inputpassword (Current_user->useraccount, ConfirmPassword); if (Checkpassword (NewPassword, ConfirmPassword)) {for (int i = 0; Current_user->userpassword[i]! = ' + '; i++) {current_user->userpassword[i] = newpassword[i];} printf ("%s%s", Newpassword,confirmpassword);p Rintf ("\ n Password modified successfully! ");} Else{system ("CLS");p rintf ("Two times password input inconsistency");} 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);//Initialize data int IsLogin = 0;char input_seletor = '};char '; char useraccount[100] = {' userpwd[100 ' to};char] = {'/' + ' = ' + inputpwd[100] = { '};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++;p rintf ("Password Error");}} if (IsLogin >= 2) {System ("CLS");p rintf ("Password three times input error, swallow card \ n");} Else{writeintofile (Fp,path), System ("CLS");p rintf ("Data saved successfully");}} _getch ();} int main () {STARTATM (); return 0;}
C Language Realization ATM system