注意:请新建“bank.txt”且在bank.txt中输入“x x 1”
main.cpp
/***********************************************************
* 版权所有 (C)2016, ZhouGuoliang。
* *
文件名称: main.cpp
* 文件标识: 无
* 其它说明: 无
* 当前版本: V1.0
* 作 者: 周国亮
* 完成日期: 20161228
* *
**********************************************************/
#include"bank.h"
Bank bank;
int main()
{
bank.readAccountput();
while(1)
{
system("color 1A");
system("cls");
show();
int n;
cin>>n;
if(n == 1)
{
bank.append();
}
if (n == 2)
{
bank.del();
}
if (n == 3)
{
bank.query();
}
if (n == 4)
{
bank.cunqv();
}
if (n == 5)
{
bank.shuchu();
}
if(n==6)
{
system("cls");
cout<<"Bye-bye";
break;
}
}
return 0;
}
bank.h
/***********************************************************
* 版权所有 (C)2016, ZhouGuoliang。
* *
文件名称: bank.h
* 文件标识: 无
* 内容摘要:
class Account
{
public:
Account();
Account(string a,string b,double bal);
void saving(double a);
void getOutMoney(double a);
void showme();
string getname();
string getId();
double getmoney();
Account *next;
string Account_name;
string name;
d
* 其它说明: 无
* 当前版本: V1.0
* 作 者: 周国亮
* 完成日期: 20161228
* *
**********************************************************/
#ifndef BANK_H_INCLUDED
#define BANK_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
#include<iomanip>
#include<stdlib.h>
#include<stdio.h>
class Account
{
public:
Account();
Account(string a,string b,double bal);
void saving(double a);
void getOutMoney(double a);
void showme();
string getname();
string getId();
double getmoney();
Account *next;
string Account_name;
string name;
double balance ;
};
class Bank
{
public:
Bank();
void append();
bool baocun();
bool duqv;
void del();
void query();
void cunqv();
void shuchu();
void addlist(Account *acc);
void saveAccountFile() ;
void readAccountput();
Account *accountList;
};
void show();
#endif // BANK_H_INCLUDED
bank.cpp
/***********************************************************
* 版权所有 (C)2016, ZhouGuoliang。
* *
文件名称: bank.cpp
* 文件标识: 无
* 内容摘要:
void Bank::append()
{
system("cls");
string str1,str2;
cout << "请输入用户账号" << endl;
cin>>str1;
cout << "请输入开户人姓名" << endl;
cin>>str2;
Account *acc = new Account(str1,str2,0);
cout<<"增加账户成功"<<endl;
addlist(acc);
saveAccountFile();
system("pause");
}
* 其它说明: 无
* 当前版本: V1.0
* 作 者: 周国亮
* 完成日期: 20161228
* *
**********************************************************/
#include"bank.h"
Account::Account()
{
balance = 0;
}
Account::Account(string a,string b,double bal)
{
Account_name = a;
name = b;
balance = bal;
}
void Account::saving(double a)
{
balance = balance + a;
}
void Account::getOutMoney(double a)
{
if(a > balance)
cout<<"余额不足"<<endl;
else
{
balance = balance - a;
cout << "已取出" << a << "元" <<endl;
}
}
void Account::showme()
{
cout << "用户账号为" << Account_name << endl;
cout << "开户人姓名" << name <<endl;
cout << setiosflags(ios::fixed)<<setprecision(6)<<"账户余额为" << balance <<endl;
}
string Account::getId()
{
return Account_name;
}
string Account::getname()
{
return name;
}
double Account::getmoney()
{
return balance;
}
void Bank::addlist(Account *acc)
{
Account *p;
if(accountList==NULL)
{
accountList = acc;
acc->next =NULL;
}
else
{
p=accountList;
while(p)
{
if(p->next==NULL)
{
p->next=acc;
acc->next =NULL;
}
p= p->next;
}
}
}
Bank::Bank()
{
accountList = NULL;
}
void Bank::append()
{
system("cls");
string str1,str2;
cout << "请输入用户账号" << endl;
cin>>str1;
cout << "请输入开户人姓名" << endl;
cin>>str2;
Account *acc = new Account(str1,str2,0);
cout<<"增加账户成功"<<endl;
addlist(acc);
saveAccountFile();
system("pause");
}
void Bank::del()
{
system("cls");
string n;
cout << "请输入要注销的用户账号" << endl;
cin>>n;
Account *p=accountList;
Account *a=NULL;
Account *b=accountList;
int j=0,i=0;
while(p)
{
if(p->getId() == n)
{
j=1;
if(i==0)
{
a=accountList;
accountList=accountList->next;
}
else
{
a=p;
b->next=p->next;
}
cout<<"注销成功"<<endl;
}
b=p;
p=p->next;
i=1;
}
if(j==0)
cout<<"没有这个账号"<<endl;
delete a;
saveAccountFile();
system("pause");
}
void Bank::query()
{
system("cls");
string n;
cout <<"请输入您要查询的用户账号" << endl;
cin>>n;
Account *p=accountList;
int j=0;
while(p)
{
if(p->getId()== n )
{j=1;p->showme();break;}
p=p->next;
}
if(j==0)
{cout<<"没有这个账号"<<endl;}
system("pause");
}
void Bank::cunqv()
{
system("cls");
string n;
cout<<"请输入您要存取款的账号"<<endl;
cin>>n;
Account *p=accountList;
int j=0;
while(p)
{
if(p->getId() == n)
{
j=1;
int choice;
cout<<"1.取款"<<endl;
cout<<"2.存款"<<endl;
cout<<"3.返回"<<endl;
cout<<"请选择"<<endl;
cin>>choice;
if(choice == 1)
{
double jine;
cout<<"请输入取款金额"<<endl;
cin >> jine;
p->getOutMoney(jine);
p->showme();
}
if(choice == 2)
{
double qkuan;
cout<<"请输入存款金额"<<endl;
cin>>qkuan;
p->saving(qkuan);
p->showme();
}
if(choice==3)
{break;}
}
p=p->next;
}
if(j==0)
cout<<"账号输入错误"<<endl;
saveAccountFile();
system("pause");
}
void Bank::shuchu()
{
system("cls");
Account *p=accountList;
if(p)
{
while(p)
{
p->showme();
p=p->next;
}
}
else
{
cout<<"请先建立帐号!"<<endl;
}
system("pause");
}
void show()
{
cout<<"----------------------------------"<<endl;
cout << " 1.增加账户"<< endl;
cout << " 2.删除账户" <<endl;
cout << " 3.查询账户" <<endl;
cout << " 4.取款和存款" <<endl;
cout << " 5.查询所有账户" <<endl;
cout << " 6.退出" <<endl;
cout << " 请选择" <<endl;
cout<<"----------------------------------"<<endl;
}
void Bank::saveAccountFile() //保存用户信息到文件
{
FILE *fp;
Account *p=accountList;
char str1[32],str2[32],str[32];
if((fp=fopen("bank.txt","w"))==NULL)
{
printf("没有创建!");
exit(1);
}
while(p)
{
int i;
for( i=0;i<p->Account_name.length();i++)
str1[i] = p->Account_name[i];
str1[i] = '\0';
for( i=0;i<p->name.length();i++)
str2[i] = p->name[i];
str2[i] = '\0';
sprintf((char*)str,"%f",p->getmoney());
fprintf(fp,"%s %s %s \n",str1,str2,str);
p=p->next;
}
fclose(fp);
}
void Bank::readAccountput() //运行前把文件内容读取到电脑内存
{
FILE *fp;
char x[32];
fp=fopen("bank.txt","rb"); //以只读方式打开当前目录下的.txt
if(fp==NULL)
{
printf("无法打开文件\n");
exit(0); //-------- 终止程序 ---------
}
int i=0;
while(!feof(fp))
{
fscanf(fp,"%s %s %s",x,x,x);
i++;
}
fclose(fp);
FILE *FP;
FP=fopen("bank.txt","rb"); //以只读方式打开当前目录下的.txt
char str2[32],str1[32],str3[32];
double str;
FP=fopen("bank.txt","rb"); //以只读方式打开当前目录下的.txt
if(FP==NULL)
{
printf("无法打开文件\n");
exit(0); //-------- 终止程序 ---------
}
int j=1;
int b=i-1;
while(!feof(FP))
{
fscanf(FP,"%s %s %s",str1,str2,str3);
str=atoi(str3);
Account *p = new Account(str1,str2,str);
addlist(p);
p=p->next;
if(j==b)
break;
j++;
}
fclose(FP); //----关闭文件-----
}