第1题 银行终端系统

这篇博客介绍了一个用于银行活期储蓄处理的程序设计,包括储户开户、销户、存款和取款等功能。程序采用链式存储结构,以线性结构组织数据,并通过双链表实现快速查找。此外,还分析了算法的时间复杂度和空间复杂度。

一.  问题描述

题目 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;

}

}

}

 

 

一. 心得

经过一个学期的学习,数据结构课程对我来说是有难度的。从大一开始接触c++就发现自己在编程方面并不太敏感,面对有编程,又有算法的数据结构,说实话,这学期学得很辛苦。

一路坚持下来,很多不懂都是自己把书翻了又翻才看懂的,甚至补看了很多次的大一c++书籍,勤能补拙,坚持那么久还是有收获的。从本次的数据结构的课程设计就可以看到明显的效果,之前的学习里,并没有试过把私有函数放在公有函数里调用,这次参照书本上的方法,我把功能代码都放在私有函数里,只有公有函数才能调用私有函数再实现其代码。同时,存放储户的信息是存放在结点里面,在之前回看大一的书籍时,就有看过结构体,用结构体简单方便。当然,整个课程设计都涉及了指针,指针一开始运用得不熟练,渐渐才用起来,有时出了错也不知道怎么改,只能问问语法学得比较扎实的同学,多交流比一个人盲干有效率得多。

总的来说这篇课程设计的代码很简单,但是在创建的过程已经花了很多时间和心血在上面。母不嫌子丑,同样的,我做出来的课程设计虽然很简单,但是在设计方面做得比以前好,至少懂得如何更好实现类的封装性,我就会欣赏它。多学多用,以后会做得更好的。

 

 

参考文献:《数据结构》

 

 

 

(Kriging_NSGA2)克里金模型结合多目标遗传算法求最优因变量及对应的最佳自变量组合研究(Matlab代码实现)内容概要:本文介绍了克里金模型(Kriging)与多目标遗传算法NSGA-II相结合的方法,用于求解最优因变量及其对应的最佳自变量组合,并提供了完整的Matlab代码实现。该方法首先利用克里金模型构建高精度的代理模型,逼近复杂的非线性系统响应,减少计算成本;随后结合NSGA-II算法进行多目标优化,搜索帕累托前沿解集,从而获得多个最优折衷方案。文中详细阐述了代理模型构建、算法集成流程及参数设置,适用于工程设计、参数反演等复杂优化问。此外,文档还展示了该方法在SCI一区论文中的复现应用,体现了其科学性与实用性。; 适合人群:具备一定Matlab编程基础,熟悉优化算法和数值建模的研究生、科研人员及工程技术人员,尤其适合从事仿真优化、实验设计、代理模型研究的相关领域工作者。; 使用场景及目标:①解决高计算成本的多目标优化问,通过代理模型降低仿真次数;②在无法解析求导或函数高度非线性的情况下寻找最优变量组合;③复现SCI高水平论文中的优化方法,提升科研可信度与效率;④应用于工程设计、能源系统调度、智能制造等需参数优化的实际场景。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现过程,重点关注克里金模型的构建步骤与NSGA-II的集成方式,建议自行调整测试函数或实际案例验证算法性能,并配合YALMIP等工具包扩展优化求解能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值