您的位置:首页 > 其它

图书馆信息管理系统(顺序表)

2019-06-07 18:31 330 查看
//已补全
/* 图书信息管理系统:要求完成查找、插入、删除、修改、排序、计数功能*/
#include<stdio.h>
#include<iostream.h>
#include<malloc.h>
#include<cstdlib>//包含exit头文件
#include<math.h>
#include<string.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef int Status;
typedef struct
{
char no[20];//图书ISBN
char name[50];//名字
float price;//价格
}Book;//书结构体
typedef struct
{
Book *elem;//空间基地址
int length;//表长
}SqList;
Status InitList(SqList &L)
{//初始化
L.elem=(Book *)malloc(MAXSIZE*sizeof(Book));
if(!L.elem) exit(OVERFLOW);//空间分配失败
L.length=0;
return OK;
}
Status MakeList(SqList &L)
{//测试用 赋值
cout<<"输入图书ISBN,名字,价格"<<endl;
while(L.length<MAXSIZE)
{/*#表示输入结束,不过会让表尾被#占用
注意表长实际上是包括了这一部分的,虽然表面看不出来*/
cin>>L.elem[L.length].no>>
L.elem[L.length].name>>
L.elem[L.length].price;
if(strcmp(L.elem[L.length].no ,"#")!=0)
{
L.length++;
}else
{
L.elem[L.length].price=0;//有别的方法让这玩意儿正常一点就好了
break;
}
}
cin.clear();
cin.ignore(2,0);
return OK;
}
Status SearchList(SqList &L,Book &e)
{//查找,若找到相同ISBN则返回序号i+1
for(int i=0;i<L.length;i++)
{
if(strcmp(L.elem[i].no,e.no)==0)
{
return i+1;//返回序号,真混乱
}
}
return ERROR;//查无此书
//实际查找也可以通过书名,这个想法就留给需要的时候再说
}
void InsertList(SqList &L,int i,Book e)
{//插入
if(i<1||i>L.length+1) return ERROR;//插入位置不合法
if(L.length==MAXSIZE) return ERROR;//存储空间满
for(int j=L.length;j>=i-1;j--)
{
L.elem[j+1].price=L.elem[j].price;
strcpy(L.elem[j+1].name,L.elem[j].name);
strcpy(L.elem[j+1].no,L.elem[j].no);
}
strcpy(L.elem[i-1].name,e.name);
strcpy(L.elem[i-1].no,e.no);
L.elem[i-1].price=e.price;
L.length++;
return OK;
}

void DeleteList(SqList &L,int i)
{//删除第i个元素
if(i<1||i>L.length)return ERROR;
for(int j=i-1;j<L.length;j++)
{
L.elem[j].price=L.elem[j+1].price;
strcpy(L.elem[j].name,L.elem[j+1].name);
strcpy(L.elem[j].no,L.elem[j+1].no);
}
--L.length;
return OK;
}

void KaiteiList(SqList &L)
{//修改
Book e;
cout<<"あなたが直したい図書の番号は?"<<endl;
cin>>e.no;
int i=SearchList(L,e);
if(i)
{
Book p2;
cout<<"見つけた、变更したいデータくれ"<<endl;
cin>>p2.no>>p2.name>>p2.price;
L.elem[i-1].price=p2.price;
strcpy(L.elem[i-1].no,p2.no);
strcpy(L.elem[i-1].name,p2.name);
return OK;
}else
{
cout<<"调らべ结果なし"<<endl;
return ERROR;
}
}
void ShowList(SqList L)
{//展示
for(int i=0;i<L.length+1;i++)//为了提醒自己选择输出表尾#
{
cout<<L.elem[i].no<<"\t"<<
L.elem[i].name<<"\t"<<
L.elem[i].price<<endl;
}
}
void main()
{
SqList L;
cout<<InitList(L)<<endl;//Pass
cout<<MakeList(L)<<endl;//Pass
ShowList(L);//Pass
/*cout<<"查询结果:"<<endl;
cout<<"书名"<<L.elem[SearchList(L,e)-1].name<<endl;//Pass
Book e1;
strcpy(e1.no,"01x1x");
strcpy(e1.name,"薛定谔的狗");
e1.price=100;
InsertList(L,3,e1);//在表L的第三个位置插入元素e1
ShowList(L);//Pass
cout<<"进行删除:"<<endl;
DeleteList(L,4);
ShowList(L);//Pass */
KaiteiList(L);
ShowList(L);//Pass
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: