说明:
(1)下面的代码,只演示了利用链表作为存储结构的可选处理方法,本讲提到的其他方面的拓展,请感兴趣做下去的同学自行使用相关技术组合起来,形成一个完整的系统。
(2)运行程序,登录用户名和密码,请阅读程序,从程序中找出。建议建立多文件项目,将代码拷贝到IDE中看。
(3)本程序由我的2011级学生刘镇参加企业组织的实训中完成,原文在:点击打开链接
Record.h
#ifndef HEADER_RECORD //条件编译 #define HEADER_RECORD #include <string> using namespace std; class Record { private: int number; string userName; string passWord; double balance; int flag; public: Record(); void set_number(int number); void set_userName(string userName); void set_passWord(string passWord); void set_balance(double balance); void set_flag(int flag); int get_number(); string get_userName(); string get_passWord(); double get_balance(); int get_flag(); void display_Record(); }; #endif
Record.cpp
#include "Record.h" #include <iostream> using namespace std; Record::Record() { this->number = 0; this->userName = ""; this->passWord = ""; this->balance = 0; this->flag = -1; } void Record::set_number(int number) { this->number = number; } void Record::set_userName(string userName) { this->userName = userName; } void Record::set_passWord(string passWord) { this->passWord = passWord; } void Record::set_balance(double balance) { this->balance = balance; } void Record::set_flag(int flag) { this->flag = flag; } int Record::get_number() { return this->number; } string Record::get_userName() { return this->userName; } string Record::get_passWord() { return this->passWord; } double Record::get_balance() { return this->balance; } int Record::get_flag() { return this->flag; } void Record::display_Record() //用于显示打印信息 { cout << "打印用户信息中..." << endl; cout << "+---------------------------------------+" << endl; cout << "|" << "账号" << '\t' << "|" << "户名" << '\t' << "|" << "密码" << '\t' << "|" << "余额" << '\t' << "|" << "状态" << '\t' << "|"<< endl; cout << "|----------------------------------------" << endl; cout << "|" << this->number << '\t' << "|" << this->userName << '\t' << "|" << this->passWord << '\t' << "|" << this->balance << '\t' << "|" ; if(this->flag == 1) { cout << "正常"; } else { cout << "销户"; } cout << '\t' << "|" << endl; cout << "|---------------------------------------+" << endl; }
Node.h
#ifndef HEADER_NODE #define HEADER_NODE #include "Record.h" using namespace std; class Node { private: Record * record; Node * next; public: Node(); ~Node(); void set_record(Record * record); void set_next(Node * next); Record * get_record(); Node * get_next(); void display_Node(); }; #endif
Node.cpp
#include "Node.h" #include <iostream> using namespace std; Node::Node() { this->record = NULL; this->next = NULL; } Node::~Node() { delete this->record; this->record = NULL; this->next = NULL; } void Node::set_record(Record * record) { this->record = record; } void Node::set_next(Node * next) { this->next = next; } Record * Node::get_record() { return this->record; } Node * Node::get_next() { return this->next; } void Node::display_Node() { cout << "打印用户信息中..." << endl; if(this->record != NULL) //当出现空链表时执行 { Record * r = this->record; r->display_Record(); } else { cout << "记录为空..." << endl; } cout << "Next * : " << this->next << endl; cout << "结点结束..." << endl; }
LinkList.h
#ifndef HEADER_LINKLIST #define HEADER_LINKLIST #include "Node.h" using namespace std; class Linklist { private: Node * head; int len; public: Linklist(); ~Linklist(); void set_head(Node * head); void set_len(int len); Node * get_head(); int get_len(); Node * make_node(Record * record); void insert_node(Node * node); Node * find_node(int number); void display_Linklist(); }; #endif
LinkList.cpp
#include "Linklist.h" #include <iostream> using namespace std; Linklist::Linklist() { this->head = NULL; this->len = 0; } Linklist::~Linklist() { Node * p, * q; p = this->head; while(p != NULL) { q = p; p = p->get_next(); delete q; } this->head = NULL; this->len = 0; } void Linklist::set_head(Node * head) { this->head = head; } void Linklist::set_len(int len) { this->len = len; } Node * Linklist::get_head() { return this->head; } int Linklist::get_len() { return this->len; } Node * Linklist::make_node(Record * record) { Node * node = new Node(); node->set_record(record); node->set_next(NULL); return node; } void Linklist::insert_node(Node * node) { Node * p = this->head; if(p == NULL) { this->head = node; this->len++; return; } while(p->get_next() != NULL) { p = p->get_next(); } p->set_next(node); this->len++; return; } Node * Linklist::find_node(int number) { Node * p = this->head; while(p != NULL) { Record * r = p->get_record(); if(r->get_number() == number) { return p; //返回找到的结点 } else { p = p->get_next(); } } return p; //return NULL; } void Linklist::display_Linklist() { cout << "打印用户信息中..." << endl; Node * p = this->head; if(p == NULL) { cout << "链表为空..." << endl; cout << "链长: " << this->len << endl; cout << "记录结束..." << endl; return; } while(p != NULL) { p->display_Node(); p = p->get_next(); } cout << "链长: " << this->len << endl; cout << "链表结束..." << endl; }
Bank.h
#ifndef HEADER_BANK #define HEADER_BANK #include "Linklist.h" using namespace std; class Bank { private: Linklist * list; int account_number; public: Bank(); ~Bank(); void set_list(Linklist * list); Linklist * get_list(); Linklist * make_linklist(); Record * make_record(string username, string password, double balance); int creat_number(); int get_number(); string get_userName(); string get_passWord(); double get_balance(); Record * find_record(int account_number); void open_account(); void close_account(); void find_account(); void deposit(); void withdraw(); void transfer_account(); }; #endif
Bank.cpp
#include <conio.h> #include "Bank.h" #include <iostream> #include <cstdio> using namespace std; Bank::Bank() { this->list = this->make_linklist(); this->account_number = 10000; //未开户 } Bank::~Bank() //将调用所有类的析构函数 { delete this->list; this->list = NULL; } void Bank::set_list(Linklist *list) { this->list = list; } Linklist * Bank::get_list() { return this->list; } Linklist * Bank::make_linklist() { return new Linklist(); } Record * Bank::make_record(string username, string password, double balance) { Record * r = new Record(); int number = this->creat_number(); r->set_number(number); r->set_passWord(password); r->set_userName(username); r->set_balance(balance); r->set_flag(1); return r; } int Bank::creat_number() { return this->account_number = this->account_number + 1; } int Bank::get_number() { int number; cout << "请输入账号..." << endl; cin >> number; cout << "账号: " << number << endl; return number; } string Bank::get_userName() { string username; cout << "请输入户名..." << endl; cin >> username; cout << "户名: " << username << endl; return username; } string Bank::get_passWord() { string password; cout << "请输入密码..." << endl; cin >> password; cout << "密码: " << password << endl; return password; } double Bank::get_balance() { double balance; cout << "请输入金额..." << endl; cin >> balance; cout << "金额:" << balance << endl; return balance; } Record * Bank::find_record(int account_number) { Node * n = this->list->find_node(account_number); if(n != NULL) { return n->get_record(); } return NULL; } //开户功能: void Bank::open_account() { // 获取用户输入的账户信息 string userName = this->get_userName(); string passWord = this->get_passWord(); double balance = this->get_balance(); // 使用输入数据创建Record对象 Record * r = this->make_record(userName, passWord, balance); // 创建Node结点,封装Record对象 Node * n = this->list->make_node(r); // 将Node结点插入到Linklist链表 this->list->insert_node(n); //显示账户信息 r->display_Record(); cout << "开户成功!按任意键退出..." << endl; getch(); return; } //销户功能: void Bank::close_account() { // 获取销户账号 int Number = this->get_number(); // 查找账户 Record * back_record = this->find_record(Number); if(back_record == NULL) { cout << "查无此帐户!按任意键退出..." << endl; getchar(); return; } //获取账户密码,判断密码是否正确 string passWord = this->get_passWord(); if(passWord != back_record->get_passWord()) { cout << "密码不正确!按任意键退出..." << endl; getchar(); return; } //判断账户状态 if(back_record->get_flag() == 0) { cout << "该帐户已经销户,不能再次销户!" << endl; getchar(); return; } //获取用户输入的选择信息,确认是否销户 string userAnswer; cout << "确认销户? [1:销户| 其他:不销户] " << endl; cin >> userAnswer; //销户 if(userAnswer == "1") { back_record->set_flag(0); back_record->set_balance(0); back_record->display_Record(); cout << "销户成功!按任意键退出..." << endl; getchar(); return; } cout << "销户用户取消销户!" << endl; return; } //查询功能: void Bank::find_account() { // 获取查询账号 int Number = this->get_number(); // 查找账户 Record * back_record = this->find_record(Number); if(back_record == NULL) { cout << "查无此帐户!按任意键退出..." << endl; getch(); return; } //获取账户密码,判断密码是否正确 string passWord = this->get_passWord(); if(passWord != back_record->get_passWord()) { cout << "密码不正确!按任意键退出..." << endl; getch(); return; } //显示查询到的用户信息 back_record->display_Record(); cout << "查询成功!按任意键退出..." << endl; getch(); return; } //存款功能: void Bank::deposit() { //获取存款账号 int Number = this->get_number(); //查找账号 Record * back_record = this->find_record(Number); //判断用户是否存在 if(back_record == NULL) { cout << "查无此帐户!按任意键退出..." << endl; getch(); return; } //显示账户信息 back_record->display_Record(); //判断账户是否销户 if(back_record->get_flag() == 0) { cout << "该帐户已经销户!按任意键退出..." << endl; getch(); return; } double cash = 0; cout << "输入存款金额: " << endl; cin >> cash; cout << "存款金额为:" << cash << endl; cash = back_record->get_balance() + cash; back_record->set_balance(cash); //显示账户信息 back_record->display_Record(); cout << "存款结束! 按任意键退出..." << endl; getch(); return; } //取款功能: void Bank::withdraw() { // 获取取款账号 int Number = this->get_number(); // 查找账户 Record * back_record = this->find_record(Number); if(back_record == NULL) { cout << "查无此帐户!按任意键退出..." << endl; getch(); return; } //获取账户密码,判断密码是否正确 string passWord = this->get_passWord(); if(passWord != back_record->get_passWord()) { cout << "密码不正确!按任意键退出..." << endl; getch(); return; } // 确定用户账户状态 if(back_record->get_flag() == 0) { cout << "该帐户已经销户!按任意键退出..." << endl; getch(); return; } double cash = 0; cout << "请输入取款金额:" << endl; cin >> cash; if(cash > back_record->get_balance()) { cout << "账户余额不足!按任意键退出..." << endl; getch(); return; } cash = back_record->get_balance() - cash; back_record->set_balance(cash); //显示账户信息 back_record->display_Record(); cout << "取款结束! 按任意键退出..." << endl; getch(); return; } //转账功能: void Bank::transfer_account() { // 获取转入账号 int number; cout << "请输入转出账号:" << endl; cin >> number; // 查找账号 Record * back_record = this->find_record(number); if(back_record == NULL) { cout << "查无此帐户!按任意键退出..." << endl; getch(); return; } //获取账户密码,判断密码是否正确 string passWord; cout << "请输入账户密码:" << endl; cin >> passWord; if(passWord != back_record->get_passWord()) { cout << "密码不正确!按任意键退出..." << endl; getch(); return; } // 确定用户账户状态 if(back_record->get_flag() == 0) { cout << "该帐户已经销户!按任意键退出..." << endl; getch(); return; } //获取转账账号 int transfer_number; cout << "请输入转入账号:" << endl; cin >> transfer_number; //查找转账账户 Record * transfer_record = this->find_record(transfer_number); if(back_record == NULL) { cout << "查无此帐户!按任意键退出..." << endl; getch(); return; } // 确定用户账户状态 if(back_record->get_flag() == 0) { cout << "该帐户已经销户!按任意键退出..." << endl; getch(); return; } //获取转账金额 double cash = 0; cout << "请输入转入金额:" << endl; cin >> cash; //判断转账金额是否合法 if(cash > back_record->get_balance()) { cout << "账户余额不足!按任意键退出..." << endl; getch(); return; } //资金转账 transfer_record->set_balance(transfer_record->get_balance() + cash); back_record->set_balance(back_record->get_balance() - cash); //显示当前帐户信息及转账账户信息 cout << "转出帐户信息:" << endl; back_record->display_Record(); cout << "转入账户信息:" << endl; transfer_record->display_Record(); //转账结束 cout << "转账结束! 按任意键退出..." << endl; getch(); return; }
Main.cpp
#include "Bank.h" #include <iostream> #include <cstdlib> using namespace std; void welcome(); void login(Bank * bank); void mainmenu(Bank * bank); int main() { Bank * bank = new Bank(); welcome(); login(bank); return 0; } void welcome() { cout << "+--------------------------------------+" << endl; cout << "| |" << endl; cout << "| 欢迎使用储蓄综合业务平台 |" << endl; cout << "| |" << endl; cout << "+--------------------------------------+" << endl; } void mainmenu(Bank * bank) { int choice = -1; do { cout << endl; cout << "+---------------------------------+" << endl; cout << "| 开户请按1 取款请按5 |" << endl; cout << "| 销户请按2 转账请按6 |" << endl; cout << "| 查询请按3 退出请按0 |" << endl; cout << "| 存款请按4 |" << endl; cout << "+---------------------------------+" << endl; cout << "请输入您的选择: " << endl; cin >> choice; switch(choice) { case 0: exit(0); case 1: bank->open_account(); break; case 2: bank->close_account(); break; case 3: bank->find_account(); break; case 4: bank->deposit(); break; case 5: bank->withdraw(); break; case 6: bank->transfer_account(); break; default: cout << "无效选项!" << endl; break; } } while(true); } void login(Bank * bank) { string admin_userName; string admin_passWord; for(int i = 1; i <= 3; ++i) { cout << endl; cout << "请输入您用的用户名: " << endl; cin >> admin_userName; cout << "请输入您的密码: " << endl; cin >> admin_passWord; if((admin_userName == "admin") && (admin_passWord == "admin")) { mainmenu(bank); } else { if(i < 2) { cout << "用户名或密码输入错误,请重新输入!" << endl; } else { cout << "连续输错用户名或密码,退出系统!" << endl; } } } }