#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Prompt user Action
void alert (char *content);
/*
Check that the input is legal
Maxnum: Maximum range 5 (1-5)
*/
int getvalidoperation (int maxnum);
Check balance
void query ();
Exit program
void quit (int status);
Whether to continue the operation
void Iscontinue ();
Enter password
BOOL Inputpassword ();
Withdrawal
void Takemoney ();
Defining Global Variables
int Totalmoney = 1000;//Total Amount
int password = 12345;//password
BOOL islogined = false; Record the status of the login
int main (int argc, const char * argv[]) {
int operation = 0; Record Final selection
while (1) {
Prompt user Action
Alert ("1. Enter the password \n2. Withdrawal \n3. query \n4. Exit \ n");
Judging whether it is legal
Operation = Getvalidoperation (4);
BOOL result;
Handling of user actions
Switch (operation) {
Case 1:
Enter password
result = Inputpassword ();
if (result = = False) {
Password Error exit
Quit (exit_success);
} else {
Record status
Islogined = true;
}
Ask if you want to continue operation
Iscontinue ();
Break
Case 2:
Withdrawal
Takemoney ();
Iscontinue ();
Break
Case 3:
Inquire
Query ();
Iscontinue ();
Break
Case 4:
Exit
Quit (exit_success);
Break
Default
Break
}
}
return 0;
}
void alert (char *content) {
printf ("*****************\n");
printf ("%s", content);
printf ("*****************\n");
}
int getvalidoperation (int maxnum) {
int operation;//record user input actions
Prompt user to select action
printf ("Please select operation:");
scanf ("%d", &operation);
Judging whether it is legal
while (Operation < 1 | | operation > Maxnum) {
printf ("Input error, please re-enter:");
scanf ("%d", &operation);
}
Already got a legit one.
return operation;
}
void quit (int status) {
Show a bit of content to the user
Alert ("Welcome to next use \ n");
Exit (status);
}
void query () {
printf ("*****************\n");
printf ("Balance of $%d\n", Totalmoney);
printf ("*****************\n");
}
void Iscontinue () {
char choice;
printf ("Do you want to continue?" ( y/n): ");
Throw away the last input ' \ n '
GetChar ();
Get the input for this time
scanf ("%c", &choice);
while (choice! = ' Y ' && choice! = ' n ') {
printf ("Input error (y/n):");
GetChar ();
scanf ("%c", &choice);
}
if (choice = = ' n ') {
Quit (exit_success);
}
}
BOOL Inputpassword () {
int inputedpassword = Int32_max;
int totalwrongtime = 3;
do {
printf ("%s:", Inputedpassword = = Int32_max?) Please enter the password ":" Password error please re-enter ");
scanf ("%d", &inputedpassword);
Consume One Chance
totalwrongtime--;
} while ((Inputedpassword! = password) && totalwrongtime > 0);
Determine if the password is entered correctly
if (Inputedpassword! = password) {
return false;
} else{
return true;
}
}
void Takemoney () {
To determine if a password has been entered
if (islogined = = False) {
No login
BOOL result = Inputpassword ();
if (result = = False) {
Password Error exit
Quit (exit_success);
} else {
Record status
Islogined = true;
}
} else {
1. Hint Limit
Alert ("1.$100\n2.$200\n3.$500\n4. return \ n");
Judging input
int operation = Getvalidoperation (4);
int Outmoney = 0;
Switch (operation) {
Case 1:
100
Outmoney = 100;
Break
Case 2:
200;
Outmoney = 200;
Break
Case 3:
500
Outmoney = 500;
Break
Case 4:
Return
Break
Default
Break
}
Show withdrawal Amount
if (Totalmoney < Outmoney) {
Alert ("Insufficient balance \ n");
} else{
printf ("***************\n");
printf ("Withdrawal%d balance%d\n", Outmoney, Totalmoney-= Outmoney);
printf ("***************\n");
}
}
}
The realization of 1207--ATM automatic teller machine