I have little talent and learning , Just learning c Language soon , It's also my first blog , If there are any deficiencies in the following contents, you are welcome to correct them .
use c Easy language implementation ATM system : Should ATM common 10 Functions 1, Open an account 2, deposit 3, withdraw money 4, query 5, transfer accounts 6, report the loss 7, Uncoupling 8, Account cancellation
9, Change density 10, sign out ( All the code is at the end )
General idea : Complete the output and multiple use of the main interface through printing and dead cycle , Call various functions through functions , In order to use function call variables, the variables we want to operate on should be defined globally , To implement multiple user operations, you should use array or structure variables to complete the definition of global variables ( This code uses structure variables , It is also possible to use only arrays ), The implementation of specific functions will be described later .
Main function ( Output and multiple use of the main interface ): Pair via keyboard a Make an assignment to select the calling function , Then call multiple times through an endless loop .
int main() { int j=1,a; for(j=1;j<=10;j++) { zh[j].money=0; zh[j].status=0; }
printf(" Welcome to the system \n"); while(control==1) {
printf("===============================================\n");
printf(" Please select your action \n"); printf("1, Open an account 2, deposit 3, withdraw money 4, query 5, transfer accounts \n"); printf("6, report the loss 7, Uncoupling
8, Account cancellation 9, Change density 10, sign out \n");
printf("===============================================\n"); printf(" The selected action is :");
scanf_s("%d",&a); menu(a); } return 0; } int menu(int a) { switch(a) { case
1:authoring();;break; case 2:save_money();break; case 3:get_money();break; case
4:inquire();break; case 5:shift_money();break; case 6:lose();break; case
7:undo();break; case 8:eliminate();break; case 9:change_password();break; case
10:close();break; } return 0; }
Define structure : To achieve the above functions, an account should have
account number , user name , secret , balance , State these variables , Defined here 10 Users zh【10】 Of course, you can define more .( The structure is used here , Of course, you can also define an array of global variables , Using structs here makes the program more readable )
typedef struct { int account; // account number char username[10]; // user name int password; // password
double money; // money int status; // state :0 normal 1 report the loss 2 Account cancellation }Account; Account zh[10];
// establish 10 Users
Define other variables and functions : The variable that controls the end of the loop is the condition of the loop , Through function void
close Changing its value can end the dead cycle and achieve the purpose of closing . The variable of control order is to find the corresponding variable during operation , It is used to find the array or structure to be operated on . The password used for confirmation is only used in the account opening function, so it is defined separately . Here 10 You need to judge the existence and status of the user name for many times in each function , So write it alone 2 A function does this 2 Two functions are easy to use .
#include<stdio.h> int control=1; // Control end variable int order=1; // Variables controlling order int
confirmpassword[10]; // Password for confirmation int menu(int a); // menu void authoring(); // Create user
void save_money(); // save money void get_money(); // Withdraw money void inquire(); // query void
shift_money(); // transfer accounts void lose(); // report the loss void undo(); // Uncoupling void eliminate(); // Account cancellation
void change_password(); // Change density void close(); // sign out int judge_exist(int a);
// Determine whether the user name exists enter one user name , Returns the value of the user name, if any , If there is no output prompt, return 0 int judge_status(int a); // Judge user status
enter one user name , If normal return 0, Return if loss is reported 1 And prompt , If frozen, return 2 And prompt
Realization of specific functions : It is not specified here 10 The realization of three functions , Select the most representative 3 Two functions are described , The implementation method of other functions is similar . The money and status of each user have been defined in the main function . Money is 0, The status is also 0 Both normal .
Account opening function function void authoring()
: Each account is assigned to a user's account , Every time you finish it order Self addition enables you to assign a value to the next user's account when you call the function next time .
void authoring() { zh[order].account=1000+order; printf(" Opening an account .....\n");
printf(" account number :%d\n",zh[order].account); printf(" Enter the name of the head of household :");
scanf_s("%s",&zh[order].username); printf(" Please input a password :");
scanf_s("%d",&zh[order].password); printf(" Please confirm the password again :");
scanf_s("%d",&confirmpassword[order]);
if(zh[order].password==confirmpassword[order]) { printf(" Account opened successfully !\n\n"); order++;
} else { printf(" Inconsistent passwords, account opening failed \n\n"); zh[order].password=0;
confirmpassword[order]=0; } }
Transfer function void shift_money()
This function is the most complex in this code , adopt if Statement to judge the assignment. If it is correct, carry out relevant operations , If not, prompt and return , Through many times if Judge step by step . Because the transfer system involves 2 Therefore, users made multiple judgments .
void shift_money() { int
exist2,status2,account2,password2,order2,account3,exist3,order3,status3; double
money2; printf(" Please enter your account number :"); scanf_s("%d",&account2);
exist2=judge_exist(account2); if(exist2!=0) { order2=account2-1000;
status2=judge_status(zh[order2].status); if(status2==0) { printf(" Please input a password :");
scanf_s("%d",&password2); if(password2==zh[order2].password) {
printf(" Name of head of household :%s\n",zh[order2].username); printf(" Please enter the transfer account number :");
scanf_s("%d",&account3); exist3=judge_exist(account3); if(exist3!=0) {
order3=account3-1000; status3=judge_status(zh[order3].status); if(status3==0) {
printf(" Please enter the amount you want to transfer :"); scanf_s("%lf",&money2);
zh[order2].money=zh[order2].money-money2;
zh[order3].money=zh[order3].money+money2; if(zh[order2].money>=0) {
printf(" Transfer succeeded , Your balance is %lf\n\n",zh[order2].money); } else { printf(" Sorry, your credit is running low , Transfer failed \n\n");
zh[order2].money=zh[order2].money+money2;
zh[order3].money=zh[order3].money-money2; } } } } else { printf(" Password error \n\n"); }
} } }
Account cancellation function :void eliminate() Simple judgment and modification of variables. The account number is changed here , report the loss , Uncoupling , Change density , It's almost the same as this. It's just different from the modified variables .
void eliminate() { int exist2,account2,password2,order2; printf(" Please enter your account number :");
scanf_s("%d",&account2); exist2=judge_exist(account2); if(exist2!=0) {
order2=account2-1000; printf(" Please input a password :"); scanf_s("%d",&password2);
if(password2==zh[order2].password) { printf(" Account closed successfully \n\n"); zh[order2].account=0;
} else { printf(" Password error \n\n"); } } }
Original code :
#include<stdio.h> int control=1; // Control end variable int order=1; // Variables controlling order int
confirmpassword[10]; // Password for confirmation int menu(int a); // menu void authoring(); // Create user
void save_money(); // save money void get_money(); // Withdraw money void inquire(); // query void
shift_money(); // transfer accounts void lose(); // report the loss void undo(); // Uncoupling void eliminate(); // Account cancellation
void change_password(); // Change density void close(); // sign out int judge_exist(int a);
// Determine whether the user name exists enter one user name , Returns the value of the user name, if any , If there is no output prompt, return 0 int judge_status(int a); // Judge user status
enter one user name , If normal return 0, Return if loss is reported 1 And prompt , If frozen, return 2 And prompt typedef struct { int account; // account number char
username[10]; // user name int password; // password double money; // money int status; // state :0 normal
1 report the loss 2 Account cancellation }Account; Account zh[10]; // establish 10 Users int main() { int j=1,a;
for(j=1;j<=10;j++) { zh[j].money=0; zh[j].status=0; } printf(" Welcome to sunrise bank \n");
while(control==1) {
printf("===============================================\n");
printf(" Please select your action \n"); printf("1, Open an account 2, deposit 3, withdraw money 4, query 5, transfer accounts \n"); printf("6, report the loss 7, Uncoupling
8, Account cancellation 9, Change density 10, sign out \n");
printf("===============================================\n"); printf(" The selected action is :");
scanf_s("%d",&a); menu(a); } return 0; } int menu(int a) { switch(a) { case
1:authoring();;break; case 2:save_money();break; case 3:get_money();break; case
4:inquire();break; case 5:shift_money();break; case 6:lose();break; case
7:undo();break; case 8:eliminate();break; case 9:change_password();break; case
10:close();break; } return 0; } void authoring() {
zh[order].account=1000+order; printf(" Opening an account .....\n");
printf(" account number :%d\n",zh[order].account); printf(" Enter the name of the head of household :");
scanf_s("%s",&zh[order].username); printf(" Please input a password :");
scanf_s("%d",&zh[order].password); printf(" Please confirm the password again :");
scanf_s("%d",&confirmpassword[order]);
if(zh[order].password==confirmpassword[order]) { printf(" Account opened successfully !\n\n"); order++;
} else { printf(" Inconsistent passwords, account opening failed \n\n"); zh[order].password=0;
confirmpassword[order]=0; } } void save_money() { int
exist2,status2,account2,password2,order2; double money2; printf(" Please enter your account number :");
scanf_s("%d",&account2); exist2=judge_exist(account2); if(exist2!=0) {
order2=account2-1000; status2=judge_status(zh[order2].status); if(status2==0) {
printf(" Please input a password :"); scanf_s("%d",&password2); if(password2==zh[order2].password)
{ printf(" Name of head of household :%s\n",zh[order2].username); printf(" Please enter the deposit amount :");
scanf_s("%lf",&money2); zh[order2].money=zh[order2].money+money2;
printf(" Your current balance is :%lf\n\n",zh[order2].money); } else { printf(" Password error \n\n"); } } }
} void get_money() { int exist2,status2,account2,password2,order2; double
money2; printf(" Please enter your account number :"); scanf_s("%d",&account2);
exist2=judge_exist(account2); if(exist2!=0) { order2=account2-1000;
status2=judge_status(zh[order2].status); if(status2==0) { printf(" Please input a password :");
scanf_s("%d",&password2); if(password2==zh[order2].password) {
printf(" Name of head of household :%s\n",zh[order2].username); printf(" Please enter withdrawal amount :");
scanf_s("%lf",&money2); zh[order2].money=zh[order2].money-money2;
if(zh[order2].money>=0) { printf(" Withdrawal succeeded , Your current balance is :%lf\n\n",zh[order2].money); }
else { printf(" Withdrawal failed due to insufficient balance \n\n"); zh[order2].money=zh[order2].money+money2; } }
else { printf(" Password error \n\n"); } } } } void inquire() { int
account2,exist2,order2,password2; printf(" Please enter your account number :"); scanf_s("%d",&account2);
exist2=judge_exist(account2); if(exist2!=0) { order2=exist2-1000;
printf(" Please input a password :"); scanf_s("%d",&password2); if(password2==zh[order2].password)
{ switch(zh[order2].status) { case 0:printf(" account number :%d
full name :%s\n",account2,zh[order2].username); printf(" state : normal
balance :%lf\n\n",zh[order2].money); break; case 1:printf(" account number :%d
full name :%s\n",account2,zh[order2].username); printf(" state : report the loss
balance :%lf\n\n",zh[order2].money); break; case 2:printf(" account number :%d
full name :%s\n",account2,zh[order2].username); printf(" state : Account cancellation
balance :%lf\n\n",zh[order2].money); break; } } else { printf(" Password error \n\n"); } } } void
shift_money() { int
exist2,status2,account2,password2,order2,account3,exist3,order3,status3; double
money2; printf(" Please enter your account number :"); scanf_s("%d",&account2);
exist2=judge_exist(account2); if(exist2!=0) { order2=account2-1000;
status2=judge_status(zh[order2].status); if(status2==0) { printf(" Please input a password :");
scanf_s("%d",&password2); if(password2==zh[order2].password) {
printf(" Name of head of household :%s\n",zh[order2].username); printf(" Please enter the transfer account number :");
scanf_s("%d",&account3); exist3=judge_exist(account3); if(exist3!=0) {
order3=account3-1000; status3=judge_status(zh[order3].status); if(status3==0) {
printf(" Please enter the amount you want to transfer :"); scanf_s("%lf",&money2);
zh[order2].money=zh[order2].money-money2;
zh[order3].money=zh[order3].money+money2; if(zh[order2].money>=0) {
printf(" Transfer succeeded , Your balance is %lf\n\n",zh[order2].money); } else { printf(" Sorry, your credit is running low , Transfer failed \n\n");
zh[order2].money=zh[order2].money+money2;
zh[order3].money=zh[order3].money-money2; } } } } else { printf(" Password error \n\n"); }
} } } void lose() { int exist2,account2,password2,order2; printf(" Please enter your account number :");
scanf_s("%d",&account2); exist2=judge_exist(account2); if(exist2!=0) {
order2=account2-1000; printf(" Please input a password :"); scanf_s("%d",&password2);
if(password2==zh[order2].password) { printf(" Successful loss reporting , The card has been reported lost \n\n");
zh[order2].status=1; } else { printf(" Password error \n\n"); } } } void undo() { int
exist2,account2,password2,order2; printf(" Please enter your account number :"); scanf_s("%d",&account2);
exist2=judge_exist(account2); if(exist2!=0) { order2=account2-1000;
printf(" Please input a password :"); scanf_s("%d",&password2); if(password2==zh[order2].password)
{ printf(" Uncoupling succeeded \n\n"); zh[order2].status=0; } else { printf(" Password error \n\n"); } } }
void eliminate() { int exist2,account2,password2,order2; printf(" Please enter your account number :");
scanf_s("%d",&account2); exist2=judge_exist(account2); if(exist2!=0) {
order2=account2-1000; printf(" Please input a password :"); scanf_s("%d",&password2);
if(password2==zh[order2].password) { printf(" Account closed successfully \n\n"); zh[order2].account=0;
} else { printf(" Password error \n\n"); } } } void change_password() { int
exist2,account2,order2; printf(" Please enter your account number :"); scanf_s("%d",&account2);
exist2=judge_exist(account2); if(exist2!=0) { order2=account2-1000;
printf(" Authenticating ....\n"); printf(" Verification succeeded. Please enter a new password :");
scanf_s("%d",&zh[order2].password); printf(" Encryption change succeeded \n\n"); } } void close() {
control=2; } int judge_exist(int a) { int i,j=0; for(i=1;i<=10;i++) {
if(a==zh[i].account) { j=1; break; } } if(j==0) { a=0; printf(" Sorry, the user does not exist \n\n");
} return a; } int judge_status(int a) { if(a==0) { return 0; } if(a==1) {
printf(" The user has reported the loss \n\n"); return 1; } if(a==2) { printf(" The user has been frozen \n\n"); return 2; }
return 0; }
That's all , I hope it will help you .
Technology