C language bank savings system source code
- 2020-06-03 07:22:52
- OfStack
This paper shares the source code of C language bank savings system and realizes various functions of the bank for your reference. The specific content is as follows
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
void openaccount();// Open an account
void save();// deposit
void withdraw();// withdrawals
void showAccount();// The query
void transferAccounts();// transfer
void reportLoss(); // Report the loss of
void cancelLoss(); // Cancel the report the loss of
void cancelAccount(); // The cancellation of the account
void updatePassword(); // Change password
int load();// Log in to your account
int accountnum=0;// Defines global variables that are used to calculate the number of accounts
int nowaccount=0;// Login successful account
struct Bank
{
char account[20]; // account
char password[10]; // password
char name[20]; // The user name
int balance;// The account balance
int state;// state
}; // The structure of the body
struct Bank banks[100];
int load()
{
int right=0,i=0,j;
int r1,r2;
char ch;
char account1[20]= {0};
char password1[10]= {0};
printf(" landing \n Please enter your account number: \n");
scanf("%s",account1);
printf(" Please enter your password: \n");
while((ch=getch())!='\r')
{
password1[i++]=ch;
putchar('*');
}
for(j=0; j<accountnum; j++)
{
r1=strcmp(account1,banks[j].account);
r2=strcmp(password1,banks[j].password);
if(r1==0&&r2==0)
{
nowaccount=j;
if(banks[j].state==0)
{
printf(" Login successful! \n");// Login successful
right=1;
}
else if(banks[j].state==1)
{
printf(" Your account is in the state of loss reporting , Please solution to hang \n");
right=3;
}
else if(banks[j].state==2)
{
printf(" The account has been sold \n");
right=2;
}
break;
}
}
if(right==0)
printf(" Login failed! \n");
return right;
}
int main()
{
int i=0,num;
char a[100];
FILE *fp;
if((fp=fopen("account.txt","r"))==NULL)
{
if((fp=fopen("account.txt","w"))==NULL)
{
printf(" Failure! \n");
exit(1);
}
}
while(fscanf(fp,"%s%s%s%d%d",banks[i].account,banks[i].name,banks[i].password,&banks[i].balance,&banks[i].state)!=EOF)
{
accountnum++;
i++;
}
fclose(fp);
printf(" Welcome to my bank \n");
while(1)
{
printf(" Please select business :\n");
printf("1. Open an account 2. deposit 3. withdrawals \n");
printf("4. The query 5. transfer 6. Report the loss of \n");
printf("7. Solution to hang 8. Pin households 9. To close \n");
printf("-1. exit \n");
scanf("%s",a);
num=atoi(a);
if(num==1)
openaccount();// Open an account
else if(num==2)
save();// deposit
else if(num==3)
withdraw();// withdrawals
else if(num==4)
showAccount();// The query
else if(num==5)
transferAccounts();// transfer
else if(num==6)
reportLoss(); // Report the loss of
else if(num==7)
cancelLoss(); // Solution to hang
else if(num==8)
cancelAccount(); // The cancellation of the account
else if(num==9)
updatePassword(); // Change password
else if(num==-1)
{
printf(" Welcome back next time \n");
break;
}
else
printf(" Sorry, there is no such service \n");
}
if((fp=fopen("account.txt","w"))==NULL)
{
printf(" Failure! \n");
exit(1);
}
for(i=0; i<accountnum; i++)
{
fprintf(fp,"%s\t%s\t%s\t%d\t%d\n",banks[i].account,banks[i].name,banks[i].password,banks[i].balance,banks[i].state);
}
fclose(fp);
return 0;
}
void openaccount()
{
int i=0,j=0;
int r;
char ch;
char password1[10]= {0};
printf(" Account ( 10 Digits) : \n");
scanf("%s",banks[accountnum].account);
while(banks[accountnum].account[i]!='\0')
i++;
if(i!=10)
{
printf(" Input account error \n");
return;
}
i=0;
printf(" Name: \n");
scanf("%s",banks[accountnum].name);
printf(" Password: \n");
while((ch=getch())!='\r')
{
banks[accountnum].password[i++]=ch;
putchar('*');
}
printf("\n");
printf(" Enter your password again: \n");
while((ch=getch())!='\r')
{
password1[j++]=ch;
putchar('*');
}
printf("\n");
r=strcmp(banks[accountnum].password,password1);
if(r!=0)
{
printf(" The password is not the same twice, the account opening fails! \n");
return;
}
banks[accountnum].balance=0;
banks[accountnum].state=0;
accountnum++;
printf(" Successful opening an account \n");
}
void save()
{
if(load()!=1)
return;
char addmoney[10];// Deposit amount
int money;// Converts the character type to int type
int i=0;
printf(" Deposit amount :\n");
scanf("%s",addmoney);
while(addmoney[i]!='\0')
{
if(addmoney[i]<'0'||addmoney[i]>'9')
{
printf(" Input format error! \n");
return;
}
i++;
}
while(addmoney[i]!='\0')
{
if(i>9)
{
printf(" The amount of overrun \n");
return;
}
i++;
}
money=atoi(addmoney);
if(money==0)
{
printf(" Deposit failure \n");
return;
}
else
{
banks[nowaccount].balance+=money;
printf(" Deposit successfully \n");
}
}
void withdraw()
{
if(load()!=1)
return;
char reducemoney[10];// Withdrawal amount
int money,i=0;
printf(" Please enter the withdrawal amount \n");
scanf("%s",reducemoney);
while(reducemoney[i]!='\0')
{
if(reducemoney[i]<'0'||reducemoney[i]>'9')
{
printf(" Input format error! \n");
return;
}
i++;
}
while(reducemoney[i]!='\0')
{
if(i>9)
{
printf(" The amount of overrun \n");
return;
}
i++;
}
money=atoi(reducemoney);
if(money==0)
{
printf(" Withdrawals failure \n");
return;
}
else
{
if(money>banks[nowaccount].balance)
printf(" Your account doesn't have such a large balance , Withdrawals failure !\n");
else
{
banks[nowaccount].balance-=money;
printf(" Withdrawals success \n");
}
}
}
void showAccount()
{
if(load()!=1&&load()!=2)
return;
printf(" Your account information is as follows: \n");
printf(" Account: %s\t Balance: %d\n",banks[nowaccount].account,banks[nowaccount].balance);
}
void transferAccounts()
{
if(load()!=1)
return;
char account1[20];
int i,judge=0,money,a;
char tramoney[10];
printf(" Please enter the account into which you transferred \n");
scanf("%s",account1);
for(i=0; i<accountnum; i++)
{
if(strcmp(account1,banks[i].account)==0)
{
a=i;
judge=1;
break;
}
}
if(judge!=1)
{
printf(" This account was not found \n");
return;
}
printf(" Please enter the transfer amount \n");
scanf("%s",tramoney);
while(tramoney[i]!='\0')
{
if(tramoney[i]<'0'||tramoney[i]>'9')
{
printf(" Input format error! \n");
return;
}
i++;
}
while(tramoney[i]!='\0')
{
if(i>9)
{
printf(" The amount of overrun \n");
return;
}
i++;
}
money=atoi(tramoney);
if(money==0)
{
printf(" Transfer failed \n");
return;
}
else
{
if(money>banks[nowaccount].balance)
printf(" Your account doesn't have such a large balance , Transfer failed !\n");
else
{
banks[nowaccount].balance-=money;
banks[a].balance+=money;
printf(" Transfer success \n");
}
}
}
void reportLoss()
{
if(load()!=1)
return;
banks[nowaccount].state=1;
printf(" Report the loss of success \n");
}
void cancelLoss()
{
int judge=0;
if(load()==3)
{
printf(" Click to unhook immediately 1 , please press to exit 0\n");
scanf("%d",&judge);
if(judge==1)
{
banks[nowaccount].state=0;
printf(" Solution to hang success \n");
}
}
else if(load()==1)
printf(" Account is normal \n");
}
void cancelAccount()
{
int r;
r=load();
if(r==2)
{
printf(" The account has been sold \n");
return;
}
else if(r==3||r==0)
return;
else if(r==1)
{
printf(" Please withdraw the balance of your account \n");
banks[nowaccount].state=2;
banks[nowaccount].balance=0;
printf(" Cancellation of success \n");
}
}
void updatePassword()
{
char newpassword1[10]= {0},newpassword2[10]= {0};
int i=0,j=0,r;
char ch;
if(load()!=1)
return;
printf(" Please enter a new password \n");
while((ch=getch())!='\r')
{
newpassword1[i++]=ch;
putchar('*');
}
printf("\n");
printf(" Enter your password again: \n");
while((ch=getch())!='\r')
{
newpassword2[j++]=ch;
putchar('*');
}
printf("\n");
r=strcmp(newpassword1,newpassword2);
printf("r:%d",r);
if(r!=0)
{
printf(" Two passwords are not the same, change the secret failed! \n");
return;
}
i=0;
while(newpassword1[i]!='\0')
{
banks[nowaccount].password[i]=newpassword1[i];
i++;
}
printf(" Changes the secret success \n");
}