C++数据结构-图书管理系统
立即下载
资源介绍:
内容概要:使用C++编写的数据结构课程大作业:图书管理系统,实际考核优秀案例
适用人群:C++数据结构学习者,计算机方向大学生,期末考核焦虑人群
使用场景:再不写出来期末就丸辣!!!
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#pragma warning( disable : 4996 )
#define MAXNUM 30
#define MAX 100
#define OK 1
#define ERROR -1
typedef string ElementType;
typedef struct node* PNODE;
typedef int Status;
int num = 0;//解决输入现存量和库存量的错误
struct book
{
string bid, bname, writer;
int standingnum;
int totalnum;
};
//------------------------------------
//-------------------------------------------------------
//栈//重写变量类型
typedef struct Mode//栈
{
PNODE data;
Mode* next;
}Mode;
typedef Mode* QNode;
typedef Mode* LinkStack;
Status InitStack_Link(LinkStack& LS) {
//#构造一个空的顺序栈SS
LS = NULL;
return OK;
}
Status Push_Link(LinkStack& LS, PNODE x)
{
//#把元素x压入栈,编写此函数
QNode p = new Mode;
p->data = x;
p->next = LS;
LS = p;
return OK;
}
Status Pop_Link(LinkStack& LS, PNODE& x)
{
//#弹出栈顶元素,用x保存,编写此函数
if (LS == NULL)return ERROR;
QNode p = new Mode;
x = LS->data;
p = LS;
LS = LS->next;
delete p;
return OK;
}
void GetTop(LinkStack& LS, PNODE& x)
{
x = LS->data;//获取队头元素,用x记录
}
//平衡二叉树------------------------------------------------------------
typedef struct node
{
book data;
int height;
struct node* lchild, * rchild;
node() = default;
node(book k) :data(k), height(1),lchild(NULL), rchild(NULL) {}
}node, * AVLTree;
int GetHeight(AVLTree rt) { //得到高度
if (rt == NULL) return 0;
return rt->height;
}
void UpdateHeight(AVLTree rt) { //更新高度
if (rt == NULL) return;
rt->height = max(GetHeight(rt->lchild), GetHeight(rt->rchild)) + 1;
}
//左左调整(bf=2),右旋,左子节点变成父节点,其多余的右子节点变成降级节点的左子节点
void UpdateLL(AVLTree& rt) {
AVLTree pl = rt->lchild;
rt->lchild = pl->rchild;
pl->rchild = rt;
rt = pl;
UpdateHeight(rt->lchild);
UpdateHeight(rt->rchild);
UpdateHeight(rt);
}
//右右调整(bf=-2),左旋,右子节点变成父节点,其多余的左子节点变成降级节点的右子节点
void UpdateRR(AVLTree& rt) {
AVLTree pr = rt->rchild;
rt->rchild = pr->lchild;
pr->lchild = rt;
rt = pr;
UpdateHeight(rt->lchild);
UpdateHeight(rt->rchild);
UpdateHeight(rt);
}
//左右调整(bf=2),先对左子节点左旋调整为左左型,再进行左左调整
void UpdateLR(AVLTree rt) {
UpdateRR(rt->lchild);
UpdateHeight(rt->lchild->lchild);
UpdateHeight(rt->lchild->rchild);
UpdateHeight(rt->lchild);
UpdateLL(rt);
UpdateHeight(rt->lchild);
UpdateHeight(rt->rchild);
UpdateHeight(rt);
}
//右左调整(bf=-2),先对右子节点右旋调整为右右型,再进行右右调整
void UpdateRL(AVLTree& rt) {
UpdateLL(rt->rchild);
UpdateHeight(rt->rchild->lchild);
UpdateHeight(rt->rchild->rchild);
UpdateHeight(rt->rchild);
UpdateRR(rt);
UpdateHeight(rt->lchild);
UpdateHeight(rt->rchild);
UpdateHeight(rt);
}
bool InsertAVL(AVLTree& T, book data) { //插入
if (T == NULL) {
T = new node(data);
return true;
}
if (data.bid == T->data.bid)
{
T->data.standingnum += data.standingnum;//书号相同意味着增加库存量->更新data值
T->data.totalnum += data.totalnum;
return 1;
}
bool res = true;
if (data.bid < T->data.bid) {
res = InsertAVL(T->lchild, data);
if (res && GetHeight(T->lchild) - GetHeight(T->rchild) > 1) {
if (data.bid < T->lchild->data.bid) UpdateLL(T); //左左
else UpdateLR(T); //左右
}
}
else {
res = InsertAVL(T->rchild, data);
if (res && GetHeight(T->lchild) - GetHeight(T->rchild) < -1) {
if (data.bid > T->rchild->data.bid) UpdateRR(T); //右右
else UpdateRL(T); //右左
}
}
if (res) UpdateHeight(T);
return res;
}
void Delete(AVLTree& rt, AVLTree pt) { //删除节点有左右子树时处理
if (rt->rchild== NULL) {
AVLTree p = rt;
pt->data.bid = rt->data.bid;
rt = rt->lchild;
delete p;
}
else {
Delete(rt->rchild, pt);
if (GetHeight(rt->lchild) - GetHeight(rt->rchild) > 1) {
UpdateLL(rt); //左左
}
}
UpdateHeight(rt);
}
bool Delete_AVL(AVLTree& rt, book data) {//删除
if (rt == NULL) return false;
bool res = true;
if (rt->data.bid == data.bid) {
if (rt->lchild == NULL) {
rt = rt->rchild;
}
else if (rt->rchild == NULL) {
rt = rt->lchild;
}
else {
Delete(rt->lchild, rt);
}
}
else if (data.bid < rt->data.bid) {
res = Delete_AVL(rt->lchild, data);
if (res && GetHeight(rt->lchild) - GetHeight(rt->rchild) > 1) {
if (GetHeight(rt->lchild->lchild) >= GetHeight(rt->lchild->rchild)) UpdateLL(rt); //左左
else UpdateLR(rt); //左右
}
else if (res && GetHeight(rt->lchild) - GetHeight(rt->rchild) < -1) {
if (GetHeight(rt->rchild->rchild)>=GetHeight(rt->rchild->lchild)) UpdateRR(rt); //右右
else UpdateRL(rt); //右左
}
}
else {
res = Delete_AVL(rt->rchild, data);
if (res && GetHeight(rt->lchild) - GetHeight(rt->rchild) > 1) {
if (GetHeight(rt->lchild->lchild) >= GetHeight(rt->lchild->rchild)) UpdateLL(rt); //左左
else UpdateLR(rt); //左右
}
else if (res && GetHeight(rt->lchild) - GetHeight(rt->rchild) < -1) {
if (GetHeight(rt->rchild->rchild) >= GetHeight(rt->rchild->lchild)) UpdateRR(rt); //右右
else UpdateRL(rt); //右左
}
}
if (res) UpdateHeight(rt);
return res;
}
AVLTree research(AVLTree& T, book data)
{//递归查找结点
if (T == NULL)return 0;
if (T->data.bid == data.bid)return T;
if (T->data.bid > data.bid)
{
return research(T->lchild, data);
}
if (T->data.bid < data.bid)
{
return research(T->rchild, data);
}
}
void Outfile(AVLTree& T)
{//读取图书管理系统文件赋予二叉树
ifstream fin;
fin.open("C:/Users/lenovo/Desktop/图书管理系统.txt");
string line;
book p;
int i = 0;
while (getline(fin, line))
{
istringstream line3(line);//检查
getline(line3, line, ' ');
p.bid = line;
getline(line3, line, ' ');
p.bname = line;
getline(line3, line, ' ');
p.writer = line;
getline(line3, line, ' ');
p.standingnum = stoi(line);
getline(line3, line, ' ');
p.totalnum = stoi(line);
InsertAVL(T, p);
AVLTree MinRoot;
MinRoot = NULL;
}
fin.close();
}
void Infile(AVLTree T)
{ //非递归中序遍历二叉树写入文件
//覆盖,但每次都会先读出
ofstream fout;
fout.open("C:/Users/lenovo/Desktop/图书管理系统.txt", ios::out);
LinkStack LS;
InitStack_Link(LS);//初始化创建栈
PNODE p = T;//创建新的结点p指向根结点
PNODE q = new node;//创建新的结点q
while (p || LS != NULL)//树与栈不空
{
if (p)
{
Push_Link(LS, p);
p = p->lchild;
}//若树存在则将结点p压入栈并使其走向左孩子
else
{
Pop_Link(LS, q);
fout << q->data.bid << " ";
fout << q->data.bname << " ";
fout << q->data.writer << " ";
fout << q->data.standingnum << " ";
fout << q->data.totalnum << " ";
fout << endl;
p = q->rchild;
}//否则出栈输出并使其走向右孩子
}
fout.close();
}
void print(AVLTree T, int n)
{//打印凹入表
int i;
if (T)
{
print(T->rchild, n + 1);
for (i = 0; i < n; i++)
{
cout<<"\t";
}
cout << T->data.bid << endl;
print(T->lchild, n + 1);
}
}
int putinstorage(AVLTree& T)
{//入库
book data;
cout << "请输入需要入库的书号:";
cin >> data.bid;//cin.get()输入包含回车键,需要ignore()
cout << endl;
cout << "请输入需要入库的书名:";
cin >> data.bname;
cout << endl;
cou
资源文件列表:
C++数据结构-图书管理系统.zip 大约有1个文件
- ConsoleApplication1.cpp 16KB