程序示例精选
C语言银行管理系统
如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!
前言
这篇博客针对<>编写代码,代码整洁,规则,易读。 学习与应用推荐首选。
文章目录
一、所需工具软件
二、使用步骤
1. 引入库
2.创建结构体
3. 创建元素操作函数
4. 运行结果
三、在线协助
一、所需工具软件
1.Visual Studio
2.C/C++
二、使用步骤
1.引入库
代码如下(示例):
#include#include#include #includeusing 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 <ID << " ";//把信息写入磁盘文件bankdat.datoutfile <money << " ";outfile <name << " ";outfile <<< " ";outfile <number << " ";outfile <company << " ";outfile <address << " ";outfile <in << " ";}outfile.close();}/*读入用户信息功能实现*/void YH::load(){ifstream infile("bankdat.txt", ios::in);//以输入方式打开文件if (!infile){cerr << "读取错误!" <> acnum;for (n = 0; n > 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 <> id;int flag = 1;int i = 0;while ((i get_id()) flag = 0;else i++;}if (flag){cout << "帐号不存在!" << endl << endl;return;}float b;cout << endl <> b;while (b <= 0){cout << "请输入正确的数字!" << endl;cout <> b;}if (account[x]->get_money() < b) //调用友元类consumer的公有成员函数cout << "对不起,金额不够!!" <dec_money(b); account[i]->add_money(b); }cout << "转账成功!!" <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 <> n;while (n 4){cout << "您输入的数字有误!请重新输入!" << endl;cout <> 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 <> id;int flag = 1;int i = 0;//__page_break__while ((i get_id()) flag = 0; else i++;}if (flag){cout << "帐号不存在!" << endl;return;}cout <> 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 <> 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 << "请输入开户账号:" <> id;cout << "请输入开户人姓名:" <> nam;cout << "请输入开户密码:" <> passw;cout << "请输入存入金额:" <> m;cout << "请输入开户人电话:" <> number;cout << "请输入开户人公司:" <> company;cout << "请输入开户人地址:" <> address;cout << "请输入开户人身份证号码:" <> in;while (m <= 0){cout << "请输入正确的数字!" <> 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 <> id;int flag = 1;int i = 0;while ((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 <> pwd;cout <> repwd;if (pwd != repwd)cout << "对不起,您输入的两次密码不一致,按输入键返回上一层菜单!" << endl;elsecout << "您好,您的密码已修改成功,请牢记并注意账号安全!" << endl; cin.get();}void consumer::fetchmoney(){float m;char ch;do{cout << endl << "请输入您的取款金额:" <" <> m;while (m <= 0){cout << "请您输入正确的金额!" << endl;cout <> m;}if (money < m){cout << "对不起,您的余额不足!" << endl;}else{money = money - m;cout << endl << "操作成功,请拿好您的钱!" << endl;}cout << "是否要继续该项操作:(Y/N) "<< endl;cout <> ch;while (ch != 'n' && ch != 'N' && ch != 'Y' && ch != 'y')//选择错误时判定{cout <> ch;}} while (ch == 'y' || ch == 'Y');}void consumer::savemoney()//存钱函数功能实现{float c;char ch;do{cout << endl << "请输入要存入的金额:" <" <> c;while (c <= 0){cout << "对不起您输入的金额错误,请重新输入!" << endl;cout <> c;}money = money + c;cout << "操作已成功!" << endl;cout << "是否要继续该项操作:(Y/N) " << endl;cout <> ch;while (ch != 'n' && ch != 'N' && ch != 'Y' && ch != 'y'){cout <> 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.运行结果如下:
三、在线协助:
如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!