目录
一、问题描述…………………………………………………1
二、问题分析…………………………………………………1
三、逻辑结构和存储结构设计………………………………1
四、算法设计…………………………………………………1
五、时间复杂度和空间复杂度分析…………………………8
六、源代码……………………………………………………8
七、运行结果…………………………………………………16
八、心得………………………………………………………22
一. 问题描述
题目 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()
{
Account *p = nullptr;
while (true)
{
system("cls");
cout << "是否继续录入:Y/N" << endl;
char chosse = 'Y';
cin >> chosse;
if (chosse == 'n' || chosse == 'N')
break;
p = Scan(); 调用私有函数,获得一个新结点,传递进来
p->next = First->next;
First->next = p;
p->next->pre = p;
p->pre = First;
}
}
Account* Bank_System::Search()
{
cout << "1.名字搜索" << endl;
cout << "2.账号搜索" << endl;
cout << "请输入查询方式:" << ends;
unsigned choice = 0;
cin >> choice;
Account *p = nullptr;
system("cls");
if (choice == 1)
{
string name;
cout << "请输入姓名:" << ends;
cin >> name;
p = First->next;
while (p != nullptr&&p->Name != name)
p = p->next;
if (p == nullptr)
{
cout << "无该用户!" << endl;
system("pause");
}
}
else
{
string number;
cout << "请输入账号:" << ends;
cin >> number;
p = First->next;
while (p != nullptr&&p->Number != number)
p = p->next;
if (p == nullptr)
{
cout << "无该用户!" << endl;
system("pause");
}
}
return p;
}
void Bank_System::Watch()
{
system("cls");
cout << "请按照提示进行查询操作" << endl;
Account *p = Search();
if (p != nullptr)
{
system("cls");
cout << "账户信息:" << endl;
cout << "姓名:" << p->Name << endl;
cout << "身份证号:" << p->Identity << endl;
cout << "账号:" << p->Number << endl;
cout << "密码:" << p->Password << endl;
cout << "余额:" << p->Money << endl;
system("pause");
}
}
void Bank_System::Cut(Account *a)
{
a->next->pre = a->pre;
a->pre->next = a->next;
delete a;
cout << "删除成功!" << endl;
system("pause");
}
void Bank_System::Search_Cut()
{
system("cls");
cout << "请按照提示进行删除前的查询操作" << endl;
Account *p = Search();
if (p != nullptr)
Cut(p);
else
{
system("cls");
cout << "删除失败!" << endl;
system("pause");
}
}
void Bank_System::Deposit(Account *a)
{
cout << "请输入存入金额:" << ends;
long long money = 0;
cin >> money;
a->Money += money;
system("cls");
cout << "存入成功!" << endl;
system("pause");
}
void Bank_System::Search_Deposit()
{
system("cls");
cout << "请按照提示进行存钱前的查询操作" << endl;
Account *p = Search();
if (p != nullptr)
Deposit(p);
else
{
system("cls");
cout << "存钱失败!" << endl;
system("pause");
}
}
void Bank_System::Withdraw(Account *a)
{
cout << "请输入取出金额:" << ends;
long long money = 0;
cin >> money;
if (money > a->Money)
{
system("cls");
cout << "您的账户余额不足,取出失败!" << endl;
system("pause");
}
else
{
a->Money -= money;
system("cls");
cout << "取款成功!" << endl;
system("pause");
}
}
void Bank_System::Search_Withdraw()
{
system("cls");
cout << "请按照提示进行取钱前的查询操作" << endl;
Account *p = Search();
if (p != nullptr)
Withdraw(p);
else
{
system("cls");
cout << "存钱失败!" << endl;
system("pause");
}
}
五. 时间复杂度和空间复杂度分析.
算法中的Search函数使用了蛮力法,其时间复杂度为O(n),空间复杂度为O(n),其余的所有函数的时间复杂度为O(1),空间复杂度为O(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);//为了方便建立头尾结点的时候可以录入一个空的账户,
//用布尔类型来区分建立的是头尾结点或者是账户结点
};
#endif
#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*);
void Withdraw(Account*);
Account *First;
Account *Rear;
};
#endif
#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()
{
Account *p = nullptr;
while (true)
{
system("cls");
cout << "是否继续录入:Y/N" << endl;
char chosse = 'Y';
cin >> chosse;
if (chosse == 'n' || chosse == 'N')
break;
p = Scan();
p->next = First->next;
First->next = p;
p->next->pre = p;
p->pre = First;
}
}
Account* Bank_System::Search()
{
cout << "1.名字搜索" << endl;
cout << "2.账号搜索" << endl;
cout << "请输入查询方式:" << ends;
unsigned choice = 0;
cin >> choice;
Account *p = nullptr;
system("cls");
if (choice == 1)
{
string name;
cout << "请输入姓名:" << ends;
cin >> name;
p = First->next;
while (p != nullptr&&p->Name != name)
p = p->next;
if (p == nullptr)
{
cout << "无该用户!" << endl;
system("pause");
}
}
else
{
string number;
cout << "请输入账号:" << ends;
cin >> number;
p = First->next;
while (p != nullptr&&p->Number != number)
p = p->next;
if (p == nullptr)
{
cout << "无该用户!" << endl;
system("pause");
}
}
return p;
}
void Bank_System::Watch() {
system("cls");
cout << "请按照提示进行查询操作" << endl;
Account *p = Search();
if (p != nullptr)
{
system("cls");
cout << "账户信息:" << endl;
cout << "姓名:" << p->Name << endl;
cout << "身份证号:" << p->Identity << endl;
cout << "账号:" << p->Number << endl;
cout << "密码:" << p->Password << endl;
cout << "余额:" << p->Money << endl;
system("pause");
}
}
void Bank_System::Cut(Account *a)
{
a->next->pre = a->pre;
a->pre->next = a->next;
delete a;
cout << "删除成功!" << endl;
system("pause");
}
void Bank_System::Search_Cut()
{
system("cls");
cout << "请按照提示进行删除前的查询操作" << endl;
Account *p = Search();
if (p != nullptr)
Cut(p);
else
{
system("cls");
cout << "删除失败!" << endl;
system("pause");
}
}
void Bank_System::Deposit(Account *a) {
cout << "请输入存入金额:" << ends;
long long money = 0;
cin >> money;
a->Money += money;
system("cls");
cout << "存入成功!" << endl;
system("pause");
}
void Bank_System::Search_Deposit() {
system("cls");
cout << "请按照提示进行存钱前的查询操作" << endl;
Account *p = Search();
if (p != nullptr)
Deposit(p);
else
{
system("cls");
cout << "存钱失败!" << endl;
system("pause");
}
}
void Bank_System::Withdraw(Account *a)
{
cout << "请输入取出金额:" << ends;
long long money = 0;
cin >> money;
if (money > a->Money)
{
system("cls");
cout << "您的账户余额不足,取出失败!" << endl;
system("pause");
}
else
{
a->Money -= money;
system("cls");
cout << "取款成功!" << endl;
system("pause");
}
}
void Bank_System::Search_Withdraw() {
system("cls");
cout << "请按照提示进行取钱前的查询操作" << endl;
Account *p = Search();
if (p != nullptr)
Withdraw(p);
else
{
system("cls");
cout << "存钱失败!" << endl;
system("pause");
}
}
#include "Bank_System.h"
#include <iostream>
using namespace std;
void main()
{
Bank_System bk;
unsigned choice = 0;
while (true)
{
system("cls");
cout << "0:退出" << endl;
cout << "1:开户" << endl;
cout << "2:销户" << endl;
cout << "3:查询" << endl;
cout << "4:存钱" << endl;
cout << "5:取钱" << endl;
cout << "请输入你的选择:" << ends;
cin >> choice;
switch (choice)
{
case 0:exit(-1); break;
case 1:bk.Build(); break;
case 2:bk.Search_Cut(); break;
case 3:bk.Watch(); break;
case 4:bk.Search_Deposit(); break;
case 5:bk.Search_Withdraw(); break;
default:
cout << "非法输入!" << endl;
system("pause");
break;
}
}
}
. 程序运行结果
系统启动
界面
按1开户
按3 查询
按1 查找
销户
按名字查询