数据结构——图书信息管理系统的顺序表实现

大部分代码来自严蔚敏老师的《数据结构》教材,但对于书上的伪代码,有编译运行不了的地方,我进行了修改,使得程序可以正常运行,功能正常使用。

代码利用顺序表制作了一个图书信息管理系统,可以进行增、删、查、看等功能。

初成代码,如果有bug, 还望指出。

#include <bits/stdc++.h>
using namespace std;
 
#define OK true
#define ERROR false
#define Status bool
#define ElemType Book
#define MAXSIZE 10000 + 10
 
//定义图书类。信息包括编号,名字,价格
typedef struct{
	char no[20];
	char name[50];
	float price;
}Book;
//定义顺序表
typedef struct{
	Book *elem;
	int length;
}SqList;
//初始化顺序表
Status InitList(SqList &L){
	L.elem = new ElemType[MAXSIZE];
	if(!L.elem) exit(OVERFLOW);
	L.length = 0;
	return OK;
}
//取出顺序表中某个位置的图书的信息
Status GetElem(SqList L, int i, ElemType &e){
	if(i < 1 || i > L.length) return ERROR;
	e = L.elem[i-1];
	return OK;
}
//判断是否是同一本书
Status CompareElem(Book a, Book b){
	if(strcmp(a.name, b.name)==0 && strcmp(a.no, b.no) == 0 && a.price == b.price)
        return true;
    return false;
}
//查找某本书的位置
int LocateElem(SqList L, ElemType e){
	for(int i = 0; i < L.length; i++)
		if(CompareElem(L.elem[i], e)) return i + 1;
	return 0;
}
//插入
Status ListInsert(SqList &L, int i, ElemType e){
	if((i < 1) || (i > L.length+1)) return ERROR;
	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;
	++L.length;
	return OK;
}
//删除
Status ListDelete(SqList &L, int i){
	if((i < 1) || (i > L.length)) return ERROR;
	for(int j = i; j <= L.length-1; j++)
		L.elem[j-1] = L.elem[j];
	--L.length;
	return OK;
}
//打印图书表
void ShowList(SqList &L){
	for(int i = 0; i < L.length; i++){
		printf("id : %s name : %s price : %f\n", L.elem[i].no, L.elem[i].name, L.elem[i].price);
	}
}
 
int main(){
	printf("Welcome to use the library management system!\n");
	printf("The system is implemented by sequence table.\n");
	SqList L;
	InitList(L);
	printf("Sequence table initialization was successful!\n");
	printf("Please enter the book information:\n");
	int num;
	printf("Total number of books: num = ");
	scanf("%d", &num);
	for(int i = 0; i < num; i++){
        printf("please input the information of %d th book :\n", i+1);
		Book x;
		scanf("%s %s %f", x.no, x.name, &x.price);
		if(ListInsert(L, i+1, x)) printf("input successfully!\n");
		else printf("Input failure\n");
	}
	printf("All book information has been entered.\n");
	ShowList(L);
	printf("Please select the operation you need.\n");
	printf("Enter a number you selected operation:\n");
	printf("0 : End the program.\n");
	printf("1 : Print all books information.\n");
	printf("2 : Insert a book.\n");
	printf("3 : Delete a book.\n");
	printf("4 : Search a book.\n");
	printf("5 : Get i-th book.\n");
	int op;
	while(scanf("%d", &op) && op){
        if(op == 1) ShowList(L);
        else if(op == 2) {
            printf("which position you want to put your book?\n");
            printf("Position : \n");
            int pos;
            scanf("%d", &pos);
            printf("please input the information of the book :\n");
            Book x;
            scanf("%s %s %f", x.no, x.name, &x.price);
            if(ListInsert(L, pos, x)) printf("input successfully!\n");
            else printf("Input failure\n");
        }
        else if(op == 3) {
            printf("Do you want to delete the book on which position\n");
            printf("Position : \n");
            int pos;
            scanf("%d", &pos);
            if(ListDelete(L, pos)) printf("Delete successfully!\n");
            else printf("Delete failure\n");
        }
        else if(op == 4) {
            printf("please input the information of the book :\n");
            Book x;
            scanf("%s %s %f", x.no, x.name, &x.price);
            int pos = LocateElem(L, x);
            printf("The position of your book is %d \n", pos);
        }
        else if(op == 5) {
			Book x;
			int id;
			printf("please input the id you want to get:\n");
			scanf("%d", &id);
			bool flag = GetElem(L, id, x);
			if(!flag) printf("The book is not exist.\n");
			else{
				printf("The %d-th book information:\n", id);
				printf("%s %s %f\n", x.no, x.name, x.price);
			}
		} 
        else printf("ERROR\n\n");
        printf("Enter a number you selected operation:\n");
	}
	printf("Thanks for using!\n");
	printf("\t\t\tauthor : LingTree\n");
	return 0;
}

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值