程序示例精选
C语言银行管理系统
如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!
前言
这篇博客针对<<C语言银行管理系统>>编写代码,代码整洁,规则,易读。 学习与应用推荐首选。
文章目录
一、所需工具软件
二、使用步骤
1. 引入库
2. 创建结构体
3. 创建元素操作函数
4. 运行结果
三、在线协助
一、所需工具软件
1. Visual Studio
2. C/C++
二、使用步骤
1.引入库
代码如下(示例):
#include<stdlib.h>
#include<iostream>
#include <string>
#include<fstream>
using namespace std;
class consumer;
2.创建结构体
代码如下(示例):
class YH //银行类
{
public:
void set_account();//银行开户功能
void del_account();//注销账户功能
void transfer(int);//转账功能
void enter_account();//进入用户个人信息功能
void addmoney(int, float);//存款功能
void exitYH();//退出系统
void functionshow();
void save();
void load();//功能界面
protected:
consumer* account[20];
static int acnum;//账户数
};
class consumer :public YH//用户类,继承银行类的属性
{
public:
friend class YH;
consumer(int id, string Name, int Number, double IN, string Company, string Address, string PassWord, float m)
{
ID = id; name = Name; number = Number; in = IN; company = Company; address = Address; money = m; = PassWord;
}
consumer()
{
ID = 0; name = '0'; number = 0; in = 0; company = '0'; address = '0'; money = 0; = '0';
}
int get_id() { return ID; }
void savemoney();//取钱
string () { return ; }//取得密码
void display();
void fetchmoney();//取钱
void ();
void add_money(float);//计算余额
void dec_money(float);//计算余额
float get_money();//卡卡转帐
private:
int ID;//开户帐号
string ; //用户密码
string name; //用户姓名
float money;//开户金额
int number; string company; string address; double in;
};
3.创建元素操作函数:
代码如下(示例):
void YH::save()
{
ofstream outfile("bankdat.txt", ios::out);//以输出方式打开文件bankdat.dat接收从内存输出的数据
int n = 0;
outfile << acnum << " ";
for (n = 0; n < acnum; n++)
{
outfile << account[n]->ID << " ";//把信息写入磁盘文件bankdat.dat
outfile << account[n]->money << " ";
outfile << account[n]->name << " ";
outfile << account[n]-><< " ";
outfile << account[n]->number << " ";
outfile << account[n]->company << " ";
outfile << account[n]->address << " ";
outfile << account[n]->in << " ";
}
outfile.close();
}
/*读入用户信息功能实现*/
void YH::load()
{
ifstream infile("bankdat.txt", ios::in);//以输入方式打开文件
if (!infile)
{
cerr << "读取错误!" << endl;
return;
}
int n = 0;
int id, m;
string nam, passw; int number; string company; string address; double in;
infile >> acnum;
for (n = 0; n < acnum; n++)//全部读入
{
infile >> id;//从磁盘文件bankdat.dat读入信息
infile >> m;
infile >> nam;
infile >> passw;
infile >> company;
infile >> number;
infile >> address;
infile >> in;
account[n]->;
consumer* acc = new consumer(id, nam, number, in, company, address, passw, m); //每读入一个n开辟一段内存
account[n] = acc; //赋值首地址
}
infile.close();
cout << "系统正在正常运行,您可以办理以下业务:" << endl;
}
/*转账功能实现*/
void YH::transfer(int x)
{
int id;
cout << "请输入帐号:";
cin >> id;
int flag = 1;
int i = 0;
while ((i < acnum) && (flag)) //查找要转入的账号
{
if (id == account[i]->get_id()) flag = 0;
else i++;
}
if (flag)
{
cout << "帐号不存在!" << endl << endl;
return;
}
float b;
cout << endl << "请输入你要转帐的金额:";
cin >> b;
while (b <= 0)
{
cout << "请输入正确的数字!" << endl;
cout << "→";
cin >> b;
}
if (account[x]->get_money() < b) //调用友元类consumer的公有成员函数
cout << "对不起,金额不够!!" << endl;
else { account[x]->dec_money(b); account[i]->add_money(b); }
cout << "转账成功!!" << endl;
return;
}
/*账户金额计算*/
void consumer::add_money(float x)
{
money = x + money;
}
void consumer::dec_money(float x)
{
money = money - x;
}
void YH::addmoney(int x, float y)
{
account[x]->money = account[x]->money - y;
}
float consumer::get_money()
{
return money;
}
int main()
{
YH yh;
yh.functionshow();
return 0;
}
/*主界面显示*/
void YH::functionshow()
{
int n;
do
{
system("cls");
load();
cout << " ___________________________________________________________" << endl;
cout << "| |" << endl;
cout << "| 1:开户 |" << endl;
cout << "| |" << endl;
cout << "| 2:账户登录 |" << endl;
cout << "| |" << endl;
cout << "| 3:账户注销 |" << endl;
cout << "| |" << endl;
cout << "| 4:退出系统 |" << endl;
cout << "|___________________________________________________________|" << endl;
cout << endl << "请您输入你要进行的相应操作前的数字:" << endl;
cout << "→";
cin >> n;
while (n < 1 || n>4)
{
cout << "您输入的数字有误!请重新输入!" << endl;
cout << "→";
cin >> n;
}
switch (n)
{
case 1:set_account();//开户
break;
case 2:enter_account();//登录
break;
case 3:del_account();//注销
break;
case 4:exitYH();//退出
break;
}
cin.get();//输入流类istream的成员函数
} while (true);
}
void YH::enter_account()
{
int id;
cout << "请输入帐号:";
cin >> id;
int flag = 1;
int i = 0;//__page_break__
while ((i < acnum) && (flag)) //循环查找
{
if (id == account[i]->get_id()) flag = 0; else i++;
}
if (flag)
{
cout << "帐号不存在!" << endl;
return;
}
cout << "请输入密码:";
string passw;
cin >> passw;
if (passw != account[i]->()) return;//返回到登录界面
account[i]->display(); cin.get(); cin.get();
int n;
do {
system("cls");
cout << "请选择下列操作:" << endl;
cout << " ________________________________________________" << endl;
cout << "| |" << endl;
cout << "| 1.查看账户信息 |" << endl;
cout << "| |" << endl;
cout << "| 2.取款 |" << endl;
cout << "| |" << endl;
cout << "| 3.存款 |" << endl;
cout << "| |" << endl;
cout << "| 4.修改密码 |" << endl;
cout << "| |" << endl;
cout << "| 5.转账 |" << endl;
cout << "| |" << endl;
cout << "| 6.返回 |" << endl;
cout << "|________________________________________________|" << endl;
cout << "→";
cin >> n;
switch (n)
{
case 1: account[i]->display(); break;
case 2: account[i]->fetchmoney(); save(); break;//从2-5功能,每执行一次调用一次save函数,重新写入数据
case 3:account[i]->savemoney(); save(); break;
case 4:account[i]->(); save(); break;
case 5:transfer(i); save(); break;
case 6:return;
}cin.get(); cin.get();
}
while (1);
}
void YH::set_account()
{
int id;
string nam;
string passw;
float m;
string company; string address; int number; double in;
cout << "请输入开户账号:" << endl;
cin >> id;
cout << "请输入开户人姓名:" << endl;
cin >> nam;
cout << "请输入开户密码:" << endl;
cin >> passw;
cout << "请输入存入金额:" << endl;
cin >> m;
cout << "请输入开户人电话:" << endl;
cin >> number;
cout << "请输入开户人公司:" << endl;
cin >> company;
cout << "请输入开户人地址:" << endl;
cin >> address;
cout << "请输入开户人身份证号码:" << endl;
cin >> in;
while (m <= 0)
{
cout << "请输入正确的数字!" << endl;
cin >> m;
}
consumer* acc = new consumer(id, nam, number, in, company, address, passw, m);
account[acnum] = acc;
cout << "您好,你已开户成功,请牢记您的账号和密码,注意账号安全!" << endl << endl;
acnum++;
save();
cin.get();
return;
}
void YH::del_account()
{
int id;
cout << endl << "请输入您要注销的帐户号:";
cin >> id;
int flag = 1;
int i = 0;
while ((i < acnum) && (flag)) //循环查找
{
if (id == account[i]->get_id())
{
flag = 0;
}
else
{
i++;
}
}
if (flag)
{
cout << "对不起,你输入的该帐号不存在或有误,请重新输入!" << endl;
return; //返回到登陆界面
}
for (int j = i; j < acnum; j++) //所有被删号后的数据重新存储
{
account[j] = account[j + 1];
}
account[acnum - 1] = NULL;
acnum--; //账号总数自减一次
cout << "您好,您的账号已成功注销!" << endl << endl;
save();
cin.get();
return;
}
void consumer::()
{
string pwd, repwd;
cout << "请输入新密码:";
cin >> pwd;
cout << "请确认新密码:";
cin >> repwd;
if (pwd != repwd)
cout << "对不起,您输入的两次密码不一致,按输入键返回上一层菜单!" << endl;
else
cout << "您好,您的密码已修改成功,请牢记并注意账号安全!" << endl; cin.get();
}
void consumer::fetchmoney()
{
float m;
char ch;
do
{
cout << endl << "请输入您的取款金额:" << "¥>" << endl;
cin >> m;
while (m <= 0)
{
cout << "请您输入正确的金额!" << endl;
cout << "→";
cin >> m;
}
if (money < m)
{
cout << "对不起,您的余额不足!" << endl;
}
else
{
money = money - m;
cout << endl << "操作成功,请拿好您的钱!" << endl;
}
cout << "是否要继续该项操作:(Y/N) "
<< endl;
cout << "→";
cin >> ch;
while (ch != 'n' && ch != 'N' && ch != 'Y' && ch != 'y')//选择错误时判定
{
cout << "→";
cin >> ch;
}
} while (ch == 'y' || ch == 'Y');
}
void consumer::savemoney()//存钱函数功能实现
{
float c;
char ch;
do
{
cout << endl << "请输入要存入的金额:" << "¥>" << endl;
cin >> c;
while (c <= 0)
{
cout << "对不起您输入的金额错误,请重新输入!" << endl;
cout << "→";
cin >> c;
}
money = money + c;
cout << "操作已成功!" << endl;
cout << "是否要继续该项操作:(Y/N) " << endl;
cout << "→";
cin >> ch;
while (ch != 'n' && ch != 'N' && ch != 'Y' && ch != 'y')
{
cout << "→";
cin >> ch;
}
} while (ch == 'y' || ch == 'Y');
}
int YH::acnum = 0;
void consumer::display()//用户信息界面
{
system("cls");
cout << "_____________________________________________" << endl;
cout << "| |" << endl;
cout << "| 用户姓名:" << name << endl;
cout << "| |" << endl;
cout << "| 帐号:" << ID << endl;
cout << "| |" << endl;
cout << "| 余额:" << money << endl;
cout << "| |" << endl;
cout << "| 返回(Enter键) |" << endl;
cout << "| |" << endl;
cout << "|____________________________________________|" << endl;
cout << "→";
}
void YH::exitYH()//退出系统
{
cout << endl << "感谢您对银行的支持,欢迎下次光临!" << endl;
exit(0);
}
4.运行结果如下:
三、在线协助:
如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!