数据结构课程设计图书管理系统

/*
1.查找:根据指定的ISBN或书名查找相应图书的有关信息,并返回该图书在表中的位置序号。
2.插入:插入一种新的图书信息。
3.删除:删除一种图书信息。
4.修改:根据指定的ISBN号,修改图书的价格。
5.排序:将图书按照价格由低到高进行排序。
6.计数:统计图书表中的图书数量。
*/
#include<stdio.h>//standard input output
#include<string.h>//
using namespace std;
#include<malloc.h> //
#include<stdlib.h>//standard libary
#define MAXSIZE 10000//图书表可能达到的最大长度 
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#include<iostream>
#include<cstring> 
typedef int Status;
typedef struct{   //构造结构体Book,描述书的信息 
	char no[20];
	char name[50];
	float price;	
}Book;
typedef struct{
  Book *elem; //下一个book的地址,指针指向的是下一个书的地址 
  int length; //表的长度 
}SqList;
//struct 结构体名 *变量名
//结构体指针获取结构体成员
//(*ptr).structMember  ptr->structMember   .的运算符高于*()不可以取掉 
//用&L做实参,&L是结构体Sqlist的地址。
Status InitList_Sq(SqList &L)    //构造一 个空的顺序表L   
{
    L.elem=new Book[MAXSIZE];   //为顺序表分配空间      
    if(!L.elem) exit(OVERFLOW);           //存储分配失败
    L.length=0;				     //空表长度为0
    return OK;
}
//L定义为Sqlist类型的指针变量, 
int GetElem(SqList L,int i,Book &e) //按照位置进行取值,获取第i本书的信息 
{
  if (i<1||i>L.length) return ERROR;   //判断i值是否合理,若不合理,返回ERROR
  e=L.elem[i-1];   //第i-1的单元存储着第i个数据   访问顺序表中序号为i的元素 
  return OK;
} 

//e定义为Book类型的指针变量,在顺序表L中查找与e相等的元素, 
int LocateELem(SqList L,Book e) //按照ISBN查找 
{
  	for (int i=0;i< L.length;i++)
  	{
      	int flag_cmp=strcmp(L.elem[i].no,e.no);//相等为0 通过顺序表中的第i个元素的信息的no信息和书中no的信息 
      	if (flag_cmp==0) //如果找到返回1 ,并输出 
		  {
		  	cout<<"获取到的书目信息为:"; 
		  	cout<<L.elem[i].no<<" "<<L.elem[i].name<<" "<<L.elem[i].price<<endl;
		    return 1; 
		  } 
	}             
 	 return 0;// 未找到返回0 
}

Status ListInsert_Sq(SqList &L,int i ,Book e)//插入 
{
   if(i<1 || i>L.length+1) return ERROR;	         //i值不合法
   if(L.length==MAXSIZE) return ERROR;    //当前存储空间已满     
   for(int j=L.length-1;j>=i-1;j--) 
       L.elem[j+1]=L.elem[j];    //插入位置及之后的元素后移
    L.elem[i-1]=e;                     //将新元素e放入第i个位置
    ++L.length;		     	//表长增1
    return OK;
} 


Status ListDelete_Sq(SqList &L,int i) //删除
{
	int j; 
	if(i<1 || i>L.length+1){
		printf("删除位置不合法");
		return ERROR; 
	}
	for(j=i; j<=L.length;j++)
		L.elem[j-1]=L.elem[j];//L->elem[k-1]=L->elem[k];
	L.length--;                //L->length--
	return OK; 
}

Status ListModify_Sq(SqList &L,int i,Book e)//修改 
{
	if(i<1||i>L.length+1){
		printf("修改位置不合法");
		return ERROR;
	}  
    L.elem[i-1]=e; 
	 //将修改后的元素e放入第i个位置 }  //第i-1的单元存储着第i个数据   访问顺序表中序号为i的元素 
    return OK;	
} 

int main()
{
	/*第一步 创建结构体变量,进行初始化*/
	SqList L;     //SqList结构体的别名,通过别名可以直接定义结构体变量L 
	int Init_flag=InitList_Sq(L); 
	cout<<"第一步 创建结构体变量,进行初始化"<<endl;
	if(Init_flag==1)
		cout<<"Init OK!"<<endl;
	else
		cout<<"Init ERROE!"<<endl;
		
    /*第二步 给顺序表赋值*/
    cout<<"第二步 构造书的基本信息"<<endl;
	for(int i=0;i<5;i++)
	{
		cin>> L.elem[i].no>>L.elem[i].name>>L.elem[i].price;//cin和scanf一样输入no name price 
	} 
	L.length=5; //顺序表长度为5 
	cout<<"The BookList info is:"<<endl;  //打印输出这本书的信息是: 
	cout<<"ISBN"<<"   "<<"书名"<<"   "<<"定价"<<endl;  //打印输出ISBN  和书名 和定价 
	for(int j=0;j<5;j++)
		cout<<L.elem[j].no<<" "<<L.elem[j].name<<" "<<L.elem[j].price<<endl;
	cout<<"书目信息输入完毕!";	
	cout<<endl;
	/*第三步 按照位置进行查找,获取第i本书的信息*/
    cout<<"第三步 按照位置进行查找,获取第i本书的信息"<<endl;
	int k,getElem_flag;
	Book value;
	cout<<"please input Book_List getElem_k:";
	cin>>k;
    getElem_flag=GetElem(L,k,value);
	if( getElem_flag==1)
		cout<<"获取到的书目信息为:"<<value.no<<" "<<value.name<<" "<<value.price<<endl;
	else
		cout<<"输入的位置越界!"<<endl;
	/*第四步 按照ISBN获取书的信息*/
    cout<<"第四步 按照ISBN获取书的信息"<<endl;
	int local_flag;
	Book e;
	cout<<"please input this Book_ISBN :";
	cin>>e.no;
    local_flag=LocateELem(L,e);
	if(local_flag==0)
		cout<<"查无该书!"<<endl;
		
    /*第五步 插入运算*/
	cout<<"第五步 在位置i处插入元素e"<<endl;
	int Insert_i,Insert_flag;
	Book  Insert_value; 
	cout<<"please input Insert_i:";
	cin>>Insert_i;
	cout<<"please input Insert_value:";
	cin>>Insert_value.no>>Insert_value.name>>Insert_value.price;
	Insert_flag=ListInsert_Sq(L,Insert_i,Insert_value);
    if(Insert_flag)
	{  //插入成果循环输出L中元素
		cout<<"Insert OK!"<<endl;
		cout<<"SqList L Length is:"<<L.length<<endl;
		for(int h=0;h<L.length;h++)
		cout<<L.elem[h].no<<" "<<L.elem[h].name<<" "<<L.elem[h].price<<endl;
	    cout<<endl;
	}

		else
		cout<<"Insert ERROR!"<<endl;
		
	/*第六步 删除运算*/
	cout<<"第六步 在位置i处删除元素e"<<endl;
	int Delete_i,Delete_flag;
	Book Delete_value;
	cout<<"please input Delete_i:";
	cin>>Delete_i;
	Delete_flag=ListDelete_Sq(L,Delete_i);
	if(Delete_flag)
	{  //删除成果循环输出L中元素
		cout<<"Delete OK!"<<endl;
		cout<<"SqList L Length is:"<<L.length<<endl;
		for(int h=0;h<L.length;h++)
		cout<<L.elem[h].no<<" "<<L.elem[h].name<<" "<<L.elem[h].price<<endl;
	    cout<<endl;
	}
	else
		cout<<"Delete ERROR!"<<endl;
		
	/*第七步 修改运算*/ 
	cout<<"第七步 在位置i处修改的元素e"<<endl;
	int Modify_i,Modify_flag;
	Book  Modify_value; 
	cout<<"please input Modify_i:";
	cin>>Modify_i;
	cout<<"please input Modify_value:";
	cin>>Modify_value.no>>Modify_value.name>>Modify_value.price;
	Modify_flag=ListModify_Sq(L,Modify_i,Modify_value);
    if(Modify_flag)
	{  //修改成果循环输出L中元素
		cout<<"Modify OK!"<<endl;
		cout<<"SqList L Length is:"<<L.length<<endl;
		for(int h=0;h<L.length;h++)
		cout<<L.elem[h].no<<" "<<L.elem[h].name<<" "<<L.elem[h].price<<endl;
	    cout<<endl;
	}
	
	/*第八步 排序运算*/	
	cout<<"第八步 排序元素e的价格"<<endl;
	int i,j;
	Book t;
	for(i=0;i<L.length-1;i++){
		for(j=0;j<L.length-1-i;j++){
			if(L.elem[j].price>L.elem[j+1].price){
				t=L.elem[j];
				L.elem[j]=L.elem[j+1];
				L.elem[j+1]=t;
			}
		}
	}
	for(i=0;i<L.length;i++)
	cout<<L.elem[i].no<<" "<<L.elem[i].name<<" "<<L.elem[i].price<<endl;
	cout<<endl;
	
	/*第九步 计数运算*/ 
	cout<<"当前表的长度为:"<<L.length<<endl; 
	return OK;
}

运行结果:

 

 

  • 6
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kiki,

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

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

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

打赏作者

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

抵扣说明:

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

余额充值