#include <iostream>
#include <fstream>
#include <string>
#include<iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;
typedef struct {
string num;
string name;
double price;
}Book;
typedef struct LNode {
Book data;
struct LNode* next;
}LNode, * LinkList;
Status InitList(LinkList& L)
{//构造一个空的单链表L
L =new LNode;//生成新节点作为头节点,用头指针L指向头节点
L->next = NULL;//头结点的指针域置空
return OK;
}
Status GetElem( LinkList L, int i, Book& e)
{//在带头节点的单链表L中根据序号1获取元素的值,用e返回L中第i个数据元素的值
LinkList p = L->next; int j = 1;//初始化,p指向首元节点,计数器j的初值赋为1
while (p && j < i)//顺链域向后扫描,直到p为空或p指向第i个元素
{
p = p->next;//p指向下一个节点
++j;//计数器相应的加1
}
if (!p || j > i) return ERROR;//i值不合法
e = p->data;//取第一个节点的数据域
return OK;
}
Status LocateElem(LinkList L, Book e)
{//在带头节点的单链表L中查找值为e的元素
LinkList p = L->next;//初始化,p指向首元节点
while (p)//顺链域向后扫描,直到p为空或p所指结点的数据等于e
{
if (p->data.price == e.price)
cout << p->data.name << endl;
p = p->next;//p指向下一个节点
}
return OK;//查找成功返回值为e的地址节点p,查找失败p为NULL
}
Status ListInsert(LinkList& L, int i,Book e)
{//在带头节点的单链表L中第i个位置插入值为e的新节点
LinkList p = L; int j = 0;
while (p && (j < i - 1))
{
p = p->next; ++j;
}//查找第i-1个结点,p指向该结点
if (!p || j > i - 1) return ERROR;
LinkList s=new LNode;//生成新节点*s
s->data = e;//将结点*s的数据域置为e
s->next = p->next; // 将结点 * s的指针指向结点ai
p->next = s;//将结点*p的指针域指向结点*s
return OK;
}
Status ListDelete(LinkList& L, int i)
{//在带头结点的单链表L中,删除第i个元素
LinkList p = L; int j = 0;
while ((p->next) && (j < i - 1))
{
p = p->next; ++j;
}//查找第i-1个结点,p指向该结点
if(!(p->next) || (j > i - 1)) return ERROR;
LinkList q= p->next;//临时保存被删结点的地址以备释放
p->next = q->next;
delete q;//改变删除结点前驱结点的指针域
return OK;//释放删除结点的空间
}
void CreateList_R(LinkList& L)
{
LinkList p, r;
string head_1, head_2, head_3;
L = new LNode;
L->next = NULL;
r = L;
int length = 0;
fstream file;
file.open("book.txt");
if (!file)
{
cout << "未找到相关文件,无法打开!" << endl;
exit(ERROR);
}
file >> head_1 >> head_2 >> head_3;
while (!file.eof()) {
p = new LNode;
file >> p->data.num >> p->data.name >> p->data.price;
p->next = NULL;
r->next = p;
r = p;
length++;
}
cout << "输入book.text 信息完毕\n";
file.close();
}
int main() {
LinkList p,L;
L=NULL;
int i = 0, temp, a, c, choose;
Book e;
string head_1, head_2, head_3;
cout << "1,建立\n";
cout << "2,输入\n";
cout << "3,取值\n";
cout << "4,查找\n";
cout << "5,插入\n";
cout << "6,删除\n";
cout << "7,输出\n";
cout << "0,退出\n\n";
choose = -1;
while (choose != 0)
{
cout << "请选择:";
cin >> choose;
switch (choose)
{
case 1:
{
if (InitList(L))
cout << "成功建立顺序表\n\n";
else
cout << "顺序表建立失败\n\n";
break;
}
case 2:
{
CreateList_R(L);
break;
}
case 3: {
cout << "请输入一个位置用来取值;\n";
cin >> i;
temp = GetElem(L, i, e);
if (temp != 0) {
cout << "查找成功\n";
cout << "第" << i << "本图书的信息是:\n";
cout << left << setw(15) << e.num << "\t" << left << setw(50)
<< e.name << "\t" << left << setw(5) << e.price << endl
<< endl;
}
else
cout << "查找失败!位置超出范围\n\n";
break; }
case 4: {
cout << "请输入所要查找价格: ";
cin >> e.price;
LocateElem(L, e);
break; }
case 5: {
cout << "请输入插入的位置和书本信息,包括:编号 书名 价格:";
cin >> a;
cin >> e.num >> e.name >> e.price;
if (ListInsert(L, a, e))
cout << "插入成功.\n\n";
else
cout << "插入失败.\n\n";
break; }
case 6: {
cout << "请输入所要删除的图书的位置:";
cin >> c;
if (ListDelete(L, c))
cout << "删除成功,\n\n";
else
cout << "删除失败,\n\n";
break; }
case 7: {
cout << "当前图书系统信息\n";
p = L->next;
while (p) {
e = p->data;
cout << left << setw(15) << e.num << "\t" << left << setw(50)
<< e.name << "\t" << left << setw(5) << e.price << endl;
p = p->next;
}
break;
}
}
}
return 0;
}
链式图书信息管理系统
最新推荐文章于 2024-08-23 00:15:00 发布