一. 问题描述
题目 1.小明是一个计算机专业top student,祝贺他毕业了。并准备到银行参加工作。上班第一天,经理叫他编制一个实现一个活期储蓄处理程序,算作考查。上班第一天, 一定要给领导一个好印象,小明二话没说,就答应了。现要你是小明了,请完成如下题目功能。储户开户、销户、存入、支出活动频繁,系统设计要求:(1)能比较迅速地找到储户的帐户,以实现存款、取款记账;
二. 问题分析
根据问题,首先解决用户的存储问题,用一个简单方便的方法存储储户的信息;再者开始寻找销户和存入及支出的问题,这些问题都设计到一些平常的操作,类似于删除和修改值(使用流程控制语句)
及查找。
三. 逻辑结构和存储结构设计
逻辑结构:线性结构。是一一对应的关系,一个储户对应一个结构体。
存储结构:采用链式存储结构。使用双链表能方便找到每个结点的前驱和后继,每个结点都是储户的信息。
四. 算法设计
用头文件定义结点,该结点存放储户信息,
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <string>
using std::string;
struct Account
{
string Name;
string Sex;
string Identity;
string Number;
string Password; //(密码)
long long Money;
Account *pre, *next;
Account(string, string, string, string,long long);
Account(string, string, string, string, long long, bool);
//为了方便建立头尾结点的时候可以录入一个空的账户,
//用布尔类型来区分建立的是头尾结点或者是账户结点
头文件定义区别头结点和尾结点以及客户结点的函数
#include "Account.h"
#include <iostream>
using namespace std;
Account::Account
(string name, string sex, string identity, string password,long long money) :
Name(name), Sex(sex), Identity(identity),
Number(Identity), Password(password), Money(money),
pre(nullptr), next(nullptr)
{
system("cls");
cout << "开户成功" << endl;
cout << "姓名:" << Name << endl;
cout << "账号:" << Number << endl;
system("pause");
}
Account::Account
(string name, string sex, string identity, string password, long long money, bool) :
Name(name), Sex(sex), Identity(identity),
Number(Identity), Password(password), Money(money),
pre(nullptr), next(nullptr){}
用头文件定义功能函数(共有函数调用私有函数,保证类的封装性)
#ifndef BANK_SYSTEM
#define BANK_SYSTEM
struct Account;
class Bank_System
{
public:
Bank_System(); 功能函数
void Build(); 建立用户
void Watch(); 查询用户
void Search_Cut(); 删除用户
void Search_Deposit(); 存钱
void Search_Withdraw(); 取钱
private:
Account* Scan(); 建立用户
Account* Search(); 查找用户
void Cut(Account*); //Account*是指针形参,指向要存储删除的用户
void Deposit(Account*); Account*是指针形参,指向要存钱的用户,
void Withdraw(Account*);Account*是指针形参,指向要取钱的用户,
Account *First; 头指针
Account *Rear; 尾指针
};
#endif
cpp文件调用头文件Bank_System.和hAccount.h来实现各个功能,是函数的具体实现
#include "Bank_System.h"
#include <iostream>
#include "Account.h"
using namespace std;
Bank_System::Bank_System() :
First(nullptr), Rear(nullptr)
{
First = new Account("#", "#", "#", "#", 0, true);
Rear = new Account("#", "#", "#", "#", 0, true);
First->next = Rear;
Rear->pre = First;
system("cls");
cout << "系统启动成功" << endl;
system("pause");
system("cls");
}
Account* Bank_System::Scan() 定义用户的函数的详细情况
{
system("cls");
string name;
cout << "请输入您的姓名:" << ends;
cin >> name;
unsigned sex = 0;
string sexual;
while (sex != 1 && sex != 2)
{
cout << "请选择您的性别:1:女,2:男" << ends;
cin >> sex;
if (sex == 1)
sexual = "女";
else sexual = "男";
}
string identity;
cout << "请输入您的身份证号:" << ends;
cin >> identity;
string password;
cout << "请输入您的密码:" << ends;
cin >> password;
long long money;
cout << "请输入您的账户余额:" << ends;
cin >> money;
Account *a = new Account(name, sexual, identity, password,money);
return a;
}
void Bank_System::Build()
{
Accoun