图书管理系统 (C语言数据结构)

在这里插入图片描述

实验要求:

1、图书信息:包括ISBN、书名、主编、出版社、定价
2、功能:

(1)插入:若表中不存在新图书信息(ISBN不同),则插入(表尾)新图书信息。
(2)删除:按ISBN删除
(3)查找:按ISBN查找
(4)修改:按ISBN查找,然后修改各个属性
(5)排序:按ISBN排序
(6)计数:输出图书信息的个数
(7)导入:从TXT文件中读取已有图书信息(不同的属性之间用tab键隔开)
(8)保存:将表中现有信息保存到txt文件中
(9)打印:在屏幕上显示所有图书信息

3、扩展功能(选做):

(1)增加按书名、主编、出版社进行查找
(2)增加按书名、主编、出版社、定价排序

4、界面要求:简单的人机交互界面
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
   //头文件 
   
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define LIST_INIT_SIZE 50
#define LISTINCREMENT 10
   //宏定义,即定义常量 
   
typedef int status;
typedef struct {
	char ISBN[15];
	char bookname[30];
	char writername[10];
	char publisher[20];
	float price;
} ElemType;
typedef struct {
	ElemType *elem;
	int length;
	int listsize;	
} SqList; 
    //定义结构体类型,即  typedef 类型  类型名 
    
status  InitList_Sq(SqList &L) 	
{   //构造一个空的线性表L。
	L.elem=(ElemType *)	malloc(LIST_INIT_SIZE*sizeof(ElemType));
	//申请初始化长度的内存 
	if(!L.elem){
	//如果L的序列非空 
		exit(OVERFLOW);	
		//说明L被重定义了,抛出溢出 
	}
	L.length=0;
	//默认L内有0个元素	
	L.listsize=LIST_INIT_SIZE; 
	//默认L的容量为初始化长度 
	return OK;
} 

int LocateElem_Sq(SqList L,char ISBN[])
{    //在顺序表L中遍历查找第1个值与e.ISBN相等的元素的位序,若找到,返回其位序,否则返回0; 
	for(int i=0;i<L.length;i++){
		if(strcmp(ISBN,L.elem[i].ISBN)==0){
			return i+1;
		}
	}
	return 0;
}

status ListInsert_Sq(SqList &L,ElemType e)
{//元素e插入表尾
	ElemType *newbase;
	if(LocateElem_Sq(L,e.ISBN)){
		return ERROR;
	}
	if(L.length>=L.listsize) {
		newbase =(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
		if(!newbase){
			exit(OVERFLOW);
		}
		L.elem=newbase;
		L.listsize+=LISTINCREMENT;
	}
	L.elem[L.length]=e;
	L.length++;
	return OK;
}

status ListDelete_Sq(SqList &L,char ISBN[] ,ElemType &e)
{   //删除L中ISBN的元素,并返回删除元素; 
	int i,j;
	if((i=LocateElem_Sq(L,ISBN))==0){
		return ERROR;
	}
	
	e=L.elem[i-1];
	for(j=i;j<L.length;j++){
		L.elem[j-1]=L.elem[j];
	}
	L.length--;
	return OK;
}

status SetElem_Sq(SqList &L,int i,ElemType e)
{   //修改在i的位置的元素 
	if(i<1||i>L.length){
		return ERROR;
	}
	L.elem[i-1]=e;
	return OK;
}

void sort_ISBN(SqList &L)
{	//对表中所有元素进行排序; 
	int i,j,k;
	ElemType e;
	for(i=0;i<L.length-1;i++){
		k=i;
		for(j=i+1;j<L.length;j++){
			if(strcmp(L.elem[k].ISBN,L.elem[j].ISBN)>0){
				k=j;
			}
		}
		if(k!=i){
			e=L.elem[i];
			L.elem[i]=L.elem[k];
			L.elem[k]=e;
		}	
	}
} 

void print(SqList L)
{	//打印出所有元素
	int i;
	printf("\n图书信息为:\n");
	for(i=0;i<L.length;i++){
		printf("%-15s%-30s%-10s%-20s%-6.2f\n",L.elem[i].ISBN,L.elem[i].bookname,L.elem[i].writername,L.elem[i].publisher,L.elem[i].price); 
	}	
}

void import(SqList &L,char *filename)
{	//从文件中导入信息 
	FILE *fp;
	int i;
	char ISBN[15],bookname[30],writername[10],publisher[20];
	float price;
	if((fp=fopen(filename,"r"))==NULL){
		printf("文件不存在!\n");
		return ;
	}
	i=0;
	while(!feof(fp)){
		fscanf(fp,"%s%s%s%s%f",ISBN,bookname,writername,publisher,&price);
		strcpy(L.elem[i].ISBN,ISBN);
		strcpy(L.elem[i].bookname,bookname);
		strcpy(L.elem[i].writername,writername);
		strcpy(L.elem[i].publisher,publisher);
		L.elem[i].price=price;
		i++;
	}
	L.length=i;
	fclose(fp);
}

void save(SqList L,char *filename)
{  	//把信息保存到文件 
	FILE *fp;
	int i;
	if((fp=fopen(filename,"w"))==NULL){
		printf("文件不存在!\n");
		return ;
	} 
	for(i=0;i<L.length;i++){
		fprintf(fp,"%-15s\t%-30s\t%-10s\t%-20s\t%-6.2f\n",L.elem[i].ISBN,L.elem[i].bookname,L.elem[i].writername,L.elem[i].publisher,L.elem[i].price);
	}
	fclose(fp);
 } 


int main(){
	int ch;
	SqList L;
	char ISBN[15],filename[30];
	int i;
	ElemType e;
	InitList_Sq(L);
	printf("1. 插入   2. 删除   3. 查找   4. 修改元素   5. 按ISBN排序元素   6. 输出   7. 导入   8. 保存   9. 退出\n");
	printf("请选择:");
	scanf("%d",&ch);
	while(ch!=9){
		switch(ch){
		case 1:	printf("请输入要增加的元素");
				scanf("%s%s%s%s%f",e.ISBN,e.bookname,e.writername,e.publisher,&e.price); 
				if(ListInsert_Sq(L,e)==OK){
					printf("插入成功!当前信息为:\n");
					print(L);
				}
				else {
					printf("表中已存在该图书!\n");
				} 
				break;
		case 2:	printf("请输入删除的图书ISBN:");
				scanf("%s",ISBN);
				if(ListDelete_Sq(L,ISBN,e)==OK)
				{
					printf("删除成功!被删图书为%s\t%s\t%s\t%s\t%.2f\n",e.ISBN,e.bookname,e.writername,e.publisher,e.price);
					print(L);
				}
				else
					printf("该图书不存在!\n");
				break;
		case 3:	printf("请输入要查找的图书ISBN:");
				scanf("%s",&ISBN);
				i=LocateElem_Sq(L,ISBN);
				if(i!=0)
				{
					printf("该图书为%s\t%s\t%s\t%s\t%.2f\n",L.elem[i-1].ISBN,L.elem[i-1].bookname,L.elem[i-1].writername,L.elem[i-1].publisher,L.elem[i-1].price);
				}
				else
					printf("该图书不存在!\n");
				break;
		case 4: printf("请输入要修改的图书ISBN:");
				scanf("%s",&ISBN);
				i=LocateElem_Sq(L,ISBN);
				if(i==0){
					printf("该图书不存在!\n");
				}	
				else{
					printf("请输入图书的ISBN,书名,主编,出版商和定价");
					scanf("%s%s%s%s%f",e.ISBN,e.bookname,e.writername,e.publisher,e.price);
					SetElem_Sq(L,i,e);
					printf("修改成功,图书信息为:");
					print(L); 	
				}
				break;
		case 5:	sort_ISBN(L);
				printf("按照ISBN排序后\n");
				print(L);
				break;
		case 6: print(L);
				break;
		case 7: printf("请输入导入文件名:");
				scanf("%s",filename);
				import(L,filename);
				break;
		case 8: printf("请输入导出文件名:");
				scanf("%s",filename);
				save(L,filename);
				break;	
		default: printf("输入错误,请重新选择\n");
		}
		printf("1. 插入   2. 删除   3. 查找   4. 修改元素   5. 按ISBN排序元素   6. 输出   7. 导入   8. 保存   9. 退出\n");
		printf("请选择:");
		scanf("%d",&ch);
	}
} 
发布了79 篇原创文章 · 获赞 45 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43652327/article/details/104566111