数据结构课程设计报告
Posted yuzipei1132
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构课程设计报告相关的知识,希望对你有一定的参考价值。
一.问题描述
小明是一个计算机专业top student,祝贺他毕业了。并准备到银行参加工作。上班第一天,经理叫他编制一个实现一个活期储蓄处理程序,算作考查。上班第一天, 一定要给领导一个好印象,小明二话没说,就答应了。现要你是小明了,请完成如下题目功能。储户开户、销户、存入、支出活动频繁,系统设计要求:(1)能比较迅速地找到储户的帐户,以实现存款、取款记账;
(2)能比较简单,迅速地实现插入和删除,以实现开户和销户的需要。
二.问题分析
本程序需要先建立节点类模板 (YinHang),然后使用链表的基本操作应用实现各个程序的实现,头插法先建立链表使之成当前操作数据基础,然后可以使用节点的删除进行储户的销户,结点的插入的应用成为程序的开户(注意:建立新的用户要先确认新建的用户号不可以语之前的用户号码相同, 引起系统错误!本程序应用链表节点的比较改进程序。)。储户的存取也是应用节点的比较查找之后进行的账户钱数加减。
三.逻辑结构和存储结构设计
逻辑结构是线性结构,方便存储银行开户信息。存储结构是双链表,能够从任意点开始向前向后快速检索用户资料,操作灵活。方便查找
四.代码分析
1.建立头文件YinHang.h
建立用户结构体
struct YongHu
string Name; //姓名
string Sex; //性别
string ID; //ID
string Number; //账号
string Password; //密码
long long Money; //金额
YongHu *pre, *next; //双链表前驱后继指针
YongHu(string, string, string, string, long long); //用户构造函数
;
银行类的声明
class YinHang
public:
YinHang();
void KaiHu(); //开户
void ChaKan(); //查看
void Cha_Shan(); //查看并删户
void Cha_Cun(); //查看并存钱
void Cha_Qu(); //查看并取钱
private:
YongHu* ShuRu(); //输入
YongHu* ChaZhao(); //查找
void ShanChu(YongHu*); //删户
void CunQian(YongHu*); //存钱
void QuQian(YongHu*); //取钱
YongHu *Head;
YongHu *Rear;
;
2.建立一个源文件YinHang.cpp,该文件包括类YinHang中成员的定义
(1)#include"YinHang.h"在本源文件中包含头文件YinHang.h。
(2)构造函数YinHang()
YinHang::YinHang() :
Head(nullptr), Rear(nullptr)
Head = new YongHu("#", "#", "#", "#", 0); //建立头结点
Rear = new YongHu("#", "#", "#", "#", 0);
Head->next = Rear;
Rear->pre = Head;
cout << "系统启动成功" << endl;
定义一个空节点
(3)用户输入定义
YongHu* YinHang::ShuRu()
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 ID;
cout << "请输入您帐户号码:" << ends;
cin >> ID;
string password;
cout << "请输入您的密码:" << ends;
cin >> password;
long long money;
cout << "请输入您的账户余额:" << ends;
cin >> money;
YongHu *a = new YongHu(name, sexual, ID, password,money);
return a;
(4)开户。通过if语句来录入多个用户信息。使用头插法一个个插入:p->next = Head->next
Head->next = p p->next->pre = p p->pre = Head
void YinHang::KaiHu()
YongHu *p = nullptr;
while (true)
system("cls");
cout << "是否继续录入:Y/N" << endl;
char chosse = 'Y';
cin >> chosse;
if (chosse == 'n' || chosse == 'N')
break;
p = ShuRu();
p->next = Head->next;
Head->next = p;
p->next->pre = p;
p->pre = Head;
(5)用户查找定义
YongHu* YinHang::ChaZhao()
cout << "1.名字搜索" << endl;
cout << "2.账号搜索" << endl;
cout << "请输入查询方式:" << ends;
unsigned choice = 0;
cin >> choice;
YongHu *p = nullptr;
system("cls");
if (choice == 1)
string name;
cout << "请输入姓名:" << ends;
cin >> name;
p = Head->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 = Head->next;
while (p != nullptr&&p->Number != number)
p = p->next;
if (p == nullptr)
cout << "无该用户!" << endl;
system("pause");
return p;
(6)查看用户信息,通过指针指向查找函数来查看用户信息
void YinHang::ChaKan()
system("cls");
cout << "请按照提示进行查询操作" << endl;
YongHu *p = ChaZhao();
if (p != nullptr)
system("cls");
cout << "账户信息:" << endl;
cout << "姓名:" << p->Name << endl;
cout << "身份证号:" << p->ID << endl;
cout << "账号:" << p->Number << endl;
cout << "密码:" << p->Password << endl;
cout << "余额:" << p->Money << endl;
system("pause");
(7)删除用户信息,通过a->next->pre = a->pre a->pre->next = a->next 使a释放从而删除a
void YinHang::ShanChu(YongHu *a)
a->next->pre = a->pre;
a->pre->next = a->next;
delete a;
cout << "删除成功!" << endl;
system("pause");
void YinHang::Cha_Shan()
system("cls");
cout << "请按照提示进行删除前的查询操作" << endl;
YongHu *p = ChaZhao();
if (p != nullptr)
ShanChu(p);
else
system("cls");
cout << "删除失败!" << endl;
system("pause");
(8)存钱
void YinHang::CunQian(YongHu *a)
cout << "请输入存入金额:" << ends;
long long money = 0;
cin >> money;
a->Money += money;
system("cls");
cout << "存入成功!" << endl;
system("pause");
void YinHang::Cha_Cun()
system("cls");
cout << "请按照提示进行存钱前的查询操作" << endl;
YongHu *p = ChaZhao();
if (p != nullptr)
CunQian(p);
else
system("cls");
cout << "存钱失败!" << endl;
system("pause");
(9)取钱
void YinHang::QuQian(YongHu *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 YinHang::Cha_Qu()
system("cls");
cout << "请按照提示进行取钱前的查询操作" << endl;
YongHu *p = ChaZhao();
if (p != nullptr)
QuQian(p);
else
system("cls");
cout << "存钱失败!" << endl;
system("pause");
3.建立一个源程序文件main.cpp,该文件包括主函数
使用switch-case算法来调用相应的函数
#include "YinHang.h"
#include <iostream>
using namespace std;
void main()
YinHang yh;
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 << "----------------请输入你的选择:----------" << endl;
cin >> choice;
switch (choice)
case 0:exit(-1); break;
case 1:yh.KaiHu(); break;
case 2:yh.Cha_Shan(); break;
case 3:yh.ChaKan(); break;
case 4:yh.Cha_Cun(); break;
case 5:yh.Cha_Qu(); break;
default:
cout << "非法输入!" << endl;
system("pause");
break;
五.时间复杂度和空间复杂度分析
操作都由头指针head开始进行逐个节点的搜索,所以所有函数的时间复杂度皆为T(n)=O(n). 空间复杂度为S(n)=O(1)。
六.源代码
头文件:
#ifndef YinHang_H
#define YinHang_H
#include <string>
using namespace std;
struct YongHu //建立用户结构体
string Name; //姓名
string Sex; //性别
string ID; //ID
string Number; //账号
string Password; //密码
long long Money; //金额
YongHu *pre, *next; //双链表前驱后继指针
YongHu(string, string, string, string, long long); //用户构造函数
;
class YinHang
public:
YinHang();
void KaiHu(); //开户
void ChaKan(); //查看
void Cha_Shan(); //查看并删户
void Cha_Cun(); //查看并存钱
void Cha_Qu(); //查看并取钱
private:
YongHu* ShuRu(); //输入
YongHu* ChaZhao(); //查找
void ShanChu(YongHu*); //删户
void CunQian(YongHu*); //存钱
void QuQian(YongHu*); //取钱
YongHu *Head;
YongHu *Rear;
;
#endif
源文件YinHang.cpp:
#include "YinHang.h"
#include <iostream>
using namespace std;
YongHu::YongHu
(string name, string sex, string ID, string password, long long money) : //形参列表
Name(name), Sex(sex), ID(ID), //构造函数初始化列表
Number(ID), Password(password), Money(money),
pre(nullptr), next(nullptr)
system("cls");
cout << "开户成功" << endl;
cout << "姓名:" << Name << endl;
cout << "账号:" << Number << endl;
system("pause");
YinHang::YinHang() :
Head(nullptr), Rear(nullptr)
Head = new YongHu("#", "#", "#", "#", 0); //建立头结点
Rear = new YongHu("#", "#", "#", "#", 0);
Head->next = Rear;
Rear->pre = Head;
system("cls");
cout << "系统启动成功" << endl;
system("pause");
system("cls");
YongHu* YinHang::ShuRu()
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 ID;
cout << "请输入您帐户号码:" << ends;
cin >> ID;
string password;
cout << "请输入您的密码:" << ends;
cin >> password;
long long money;
cout << "请输入您的账户余额:" << ends;
cin >> money;
YongHu *a = new YongHu(name, sexual, ID, password,money);
return a;
void YinHang::KaiHu()
YongHu *p = nullptr;
while (true)
system("cls");
cout << "是否继续录入:Y/N" << endl;
char chosse = 'Y';
cin >> chosse;
if (chosse == 'n' || chosse == 'N')
break;
p = ShuRu();
p->next = Head->next;
Head->next = p;
p->next->pre = p;
p->pre = Head;
YongHu* YinHang::ChaZhao()
cout << "1.名字搜索" << endl;
cout << "2.账号搜索" << endl;
cout << "请输入查询方式:" << ends;
unsigned choice = 0;
cin >> choice;
YongHu *p = nullptr;
system("cls");
if (choice == 1)
string name;
cout << "请输入姓名:" << ends;
cin >> name;
p = Head->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 = Head->next;
while (p != nullptr&&p->Number != number)
p = p->next;
if (p == nullptr)
cout << "无该用户!" << endl;
system("pause");
return p;
void YinHang::ChaKan()
system("cls");
cout << "请按照提示进行查询操作" << endl;
YongHu *p = ChaZhao();
if (p != nullptr)
system("cls");
cout << "账户信息:" << endl;
cout << "姓名:" << p->Name << endl;
cout << "身份证号:" << p->ID << endl;
cout << "账号:" << p->Number << endl;
cout << "密码:" << p->Password << endl;
cout << "余额:" << p->Money << endl;
system("pause");
void YinHang::ShanChu(YongHu *a)
a->next->pre = a->pre;
a->pre->next = a->next;
delete a;
cout << "删除成功!" << endl;
system("pause");
void YinHang::Cha_Shan()
system("cls");
cout << "请按照提示进行删除前的查询操作" << endl;
YongHu *p = ChaZhao();
if (p != nullptr)
ShanChu(p);
else
system("cls");
cout << "删除失败!" << endl;
system("pause");
void YinHang::CunQian(YongHu *a)
cout << "请输入存入金额:" << ends;
long long money = 0;
cin >> money;
a->Money += money;
system("cls");
cout << "存入成功!" << endl;
system("pause");
void YinHang::Cha_Cun()
system("cls");
cout << "请按照提示进行存钱前的查询操作" << endl;
YongHu *p = ChaZhao();
if (p != nullptr)
CunQian(p);
else
system("cls");
cout << "存钱失败!" << endl;
system("pause");
void YinHang::QuQian(YongHu *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 YinHang::Cha_Qu()
system("cls");
cout << "请按照提示进行取钱前的查询操作" << endl;
YongHu *p = ChaZhao();
if (p != nullptr)
QuQian(p);
else
system("cls");
cout << "存钱失败!" << endl;
system("pause");
源文件main.Cpp:
#include "YinHang.h"
#include <iostream>
using namespace std;
void main()
YinHang yh;
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 << "----------------请输入你的选择:----------" << endl;
cin >> choice;
switch (choice)
case 0:exit(-1); break;
case 1:yh.KaiHu(); break;
case 2:yh.Cha_Shan(); break;
case 3:yh.ChaKan(); break;
case 4:yh.Cha_Cun(); break;
case 5:yh.Cha_Qu(); break;
default:
cout << "非法输入!" << endl;
system("pause");
break;
七.程序运行结果
开户
开户成功
查找用户
存钱
取钱
八.心得
经过了一个学期数据结构的学习,感觉自己对数据结构还是没有很好地掌握。但是通过数据结构这门课程,现在写代码都回自己先确定好思路与框架,不再像以前那样想到哪里写到哪里,正如老师所说的重要的是思想,虽然这门课已经结课了,但还是会自己通过其他方面继续学习。
写这课程设计遇到了很多问题,通过上网查资料和同学们的帮忙总算可以运行了,由于自己水平的关系虽然还是有很多瑕疵,但是检查完后自己会去完善好这份报告,找出自己的错误再去研究,相信通过这份报告能够让我对编程有了更近一步了解。
参考文献:数据结构学习指导与实验指导(C++版)
以上是关于数据结构课程设计报告的主要内容,如果未能解决你的问题,请参考以下文章
编写一个类似银行账户的程序,属性:账号 储户姓名 地址 存款余额 利率。方法:存款 取款查询余额计算利息
2020-2021-5 20175229张智敏 《课程设计个人报告》