线性表顺序储存结构(图书馆管理系统)

    在建立图书馆管理系统时要建立两个结构体。一个是数据结构体,用于储存图书的数据

typedef struct
{
	string bnum;//书的编号
	string bname;//书的书名
	float price;//书的价格
}book;

   

    一个是顺序表结构体,用于储存数据结构体和数据结构体的长度,也就是用于储存书的信息和书的数量的数据表

typedef struct//顺序表结构体
{
	Elemtype* elem;//储存的数据
	int length;//记录表格的长度
}Sqlist;

    接下来定义一个Initlist(Sqlist& L)函数来在顺序表中开辟储存数据所用的空间,建立空间成功后函数要返回1,以便于判断是否成功创建了空间

Status Initlist(Sqlist& L)
{
	L.elem = new Elemtype[MAXSIZE];
	if (!L.elem)
		return 0;
	L.length = 0;
	return 1;
}

    然后定义一个creat(Sqlist& L)函数来获取数据,获取的数据储存在数据结构体中

void creat(Sqlist& L)//获取数据
{
	if (Initlist(L) == 1)//调用了Initlist后面主函数不用调了
	{
		cout << "请输入图书的信息,输入完毕按0退出" << endl;
		while (1)
		{
			cout << "请输入书籍编号" << endl;
			cin >> L.elem[L.length].bnum;
			if (L.elem[L.length].bnum == "0")
				break;
			cout << "请输入书籍名字" << endl;
			cin >> L.elem[L.length].bname;
			cout << "请输入书籍价格" << endl;
			cin >> L.elem[L.length].price;
			L.length++;
		};
	}
}

      在if函数中判断InitList(L)函数时便已经调用了该函数建立了空间,所以这里creat函数便有了建立空间并判断空间是否建立成功的作用,这样我们运用creat函数后便可以不使用InitList函数了

    定义一个printd(Sqlist& L)函数用来输出所有储存好了的数据

void printd(Sqlist& L)//输出所有数据
{
	int i = 0;
	for (i = 0; i < L.length; i++)
	{
		cout << L.elem[i].bnum << " " << L.elem[i].bname << " " << L.elem[i].price << endl;
	}
}

    这里用一个简单的for循环便能进行输出

    定义一个GetElem(Sqlist& L, string bname)函数来查询特定的数据。这里需要向函数传入用户需要查询的书名

int GetElem(Sqlist& L, string bname)//查询特定的数据
{
	int i = 0;
	for (i = 0; i < L.length; i++)
	{
		if (L.elem[i].bname == bname)
		{
			cout << "编号:" << L.elem[i].bnum << "  " << "书名:" << L.elem[i].bname << "  " << "价格:" << L.elem[i].price << endl;
			return 1;
		}
	}
	cout << "未查询到当前书籍" << endl;
	return 0;
}

    用for循环对表中的每个数据进行遍历,再for中用if来查找所需要打印出现的数据

    定义一个 ListInsert(Sqlist& L, int i, Elemtype e)函数来在用户指定的地方‘i’插入用户指定的数据‘e’

void ListInsert(Sqlist& L, int i, Elemtype e)//在i处插入特定的数据e
{
	int j = 0;
	if (i < 1 || i >= L.length + 1)//判断是否能在i位置处插入数据
	{
		cout << "无法插入" << endl;
	}
	if (L.length == MAXSIZE)//判断在现在表格是否已经满了
	{
		cout << "表格已满无法插入" << endl;
	}
	for (j = L.length - 1; j >= i - 1; j--)//i位置以及后面的位置里的数都向后移
	{
		L.elem[j + 1] = L.elem[j];
	}
	L.elem[i - 1] = e;//此时i位置空了出来,将数据存入
	L.length++;
}

     插入一个数据到顺序表的‘i’处意味着i以及i后面的数据都要向后移动来空出‘i’这个位置,再把要插入的数据放在‘i’位置

    

    定义一个 ListDelete(Sqlist& L, int i, Elemtype* e1)函数来删除用户指定位置‘i’的数据,由于要返回删除的数据让用户知晓,所以还要传入一个数据结构体指针来接收删除的数据

void ListDelete(Sqlist& L, int i, Elemtype* e1)//删除i处的数据
{
	int j = 0;
	if (L.length == 0)
	{
		cout << "该链表为空" << endl;
	}
	if (i<1 || i>L.length)//判断输入的i是否合法
	{
		cout << "输入不合法,无法删除" << endl;
	}
	*e1 = L.elem[i - 1];//将删除的元素赋给e
	if (i < L.length)//当i不是最后一位的时候
	{
		for (j = i - 1; j < L.length; j++)
		{
			L.elem[j] = L.elem[j + 1];
		}
	}
	L.length--;//若i表示最后一位长度减小1直接就删除了
	cout << "删除成功" << endl;
}

        在顺序表中删除数据就意味着将‘i’后的数据全部向前移

    相关的功能函数定义完成便可以写主函数

#include "FUNC.h"
int main()
{
	cout << "欢迎来到图书管理系统" << endl;
	int b1 = 0;
	Sqlist L;//定义一个数据表L出来
	while (1)
	{
		system("pause");
		system("cls");
		cout << "       请选择您要进行的操作" << endl;
		cout << "1.存储书籍,2.输出所有书籍  3.查询书籍  4.插入书籍  5.删除书籍  6.退出程序" << endl;
		cin >> b1;
		if (b1 == 1)//存储书籍数据
		{
			creat(L);
		}
		else if (b1 == 2)//输出所有书籍数据
		{
			printd(L);

		}
		else if (b1 == 3)//查询书籍
		{
			cout << "请输入你想查询的书籍名称" << endl;
			string GetName;
			cin >> GetName;
			GetElem(L, GetName);
		}
		else if (b1 == 4)//插入书籍
		{
			int i;
			Elemtype e;//用于储存输入的数据的临时变量
			cout << "请输入在哪个位置插入数据" << endl;
			cin >> i;
			cout << "请输入要插入书的编号,书的书名以及书的价格" << endl;
			cin >> e.bnum >> e.bname >> e.price;
			ListInsert(L, i, e);
		}
		else if (b1 == 5)
		{
			int b = 0;
			Elemtype e;//用来接收删除了的数据的结构体
			Elemtype* e1 = &e;
			cout << "请输入要删除书籍的位置" << endl;
			cin >> b;
			ListDelete(L, b, e1);
			cout << "删除的书籍为" << e.bname << endl;
		}
		else if (b1 == 6)//退出程序
		{
			break;
		}
	}
	return 0;
}

    主函数便是向函数中输入参数的过程

    主函数引用的头文件#include "FUNC.h"来源于自己创造的头文件FUNC.h,并且还有头文件FUNC.h的附属源文件FUNC.cpp

    头文件FUNC.h如下

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
#define MAXSIZE 20
typedef int Status;
typedef struct
{
	string bnum;//书的编号
	string bname;//书的书名
	float price;//书的价格
}book;
typedef book Elemtype;//要储存的数据类型
typedef struct//顺序表结构体
{
	Elemtype* elem;//储存的数据
	int length;//记录表格的长度
}Sqlist;
Status Initlist(Sqlist& L);//开辟空间
void creat(Sqlist& L);//获取数据
void printd(Sqlist& L);//输出所有数据
int GetElem(Sqlist& L, string bname);//查询特定的数据
void ListInsert(Sqlist& L, int i, Elemtype e);//在i处插入特定的数据e
void ListDelete(Sqlist& L, int i, Elemtype* e);//删除i处的数据

    源文件FUNC.cpp如下

#include "FUNC.h"
Status Initlist(Sqlist& L)
{
	L.elem = new Elemtype[MAXSIZE];
	if (!L.elem)
		return 0;
	L.length = 0;
	return 1;
}
void creat(Sqlist& L)//获取数据
{
	if (Initlist(L) == 1)//调用了Initlist后面主函数不用调了
	{
		cout << "请输入图书的信息,输入完毕按0退出" << endl;
		while (1)
		{
			cout << "请输入书籍编号" << endl;
			cin >> L.elem[L.length].bnum;
			if (L.elem[L.length].bnum == "0")
				break;
			cout << "请输入书籍名字" << endl;
			cin >> L.elem[L.length].bname;
			cout << "请输入书籍价格" << endl;
			cin >> L.elem[L.length].price;
			L.length++;
		};
	}
}
void printd(Sqlist& L)//输出所有数据
{
	int i = 0;
	for (i = 0; i < L.length; i++)
	{
		cout << L.elem[i].bnum << " " << L.elem[i].bname << " " << L.elem[i].price << endl;
	}
}
int GetElem(Sqlist& L, string bname)//查询特定的数据
{
	int i = 0;
	for (i = 0; i < L.length; i++)
	{
		if (L.elem[i].bname == bname)
		{
			cout << "编号:" << L.elem[i].bnum << "  " << "书名:" << L.elem[i].bname << "  " << "价格:" << L.elem[i].price << endl;
			return 1;
		}
	}
	cout << "未查询到当前书籍" << endl;
	return 0;
}
void ListInsert(Sqlist& L, int i, Elemtype e)//在i处插入特定的数据e
{
	int j = 0;
	if (i < 1 || i >= L.length + 1)//判断是否能在i位置处插入数据
	{
		cout << "无法插入" << endl;
	}
	if (L.length == MAXSIZE)//判断在现在表格是否已经满了
	{
		cout << "表格已满无法插入" << endl;
	}
	for (j = L.length - 1; j >= i - 1; j--)//i位置以及后面的位置里的数都向后移
	{
		L.elem[j + 1] = L.elem[j];
	}
	L.elem[i - 1] = e;//此时i位置空了出来,将数据存入
	L.length++;
}
void ListDelete(Sqlist& L, int i, Elemtype* e1)//删除i处的数据
{
	int j = 0;
	if (L.length == 0)
	{
		cout << "该链表为空" << endl;
	}
	if (i<1 || i>L.length)//判断输入的i是否合法
	{
		cout << "输入不合法,无法删除" << endl;
	}
	*e1 = L.elem[i - 1];//将删除的元素赋给e
	if (i < L.length)//当i不是最后一位的时候
	{
		for (j = i - 1; j < L.length; j++)
		{
			L.elem[j] = L.elem[j + 1];
		}
	}
	L.length--;//若i表示最后一位长度减小1直接就删除了
	cout << "删除成功" << endl;
}

    最后完成我们的顺序表(图书馆管理)的创建

  • 10
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小林想被监督学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值