數據結構——圖書信息管理系統的順序表實現

大部分代碼來自嚴蔚敏老師的《數據結構》教材,但對於書上的僞代碼,有編譯運行不了的地方,我進行了修改,使得程序可以正常運行,功能正常使用。

 

代碼利用順序表製作了一個圖書信息管理系統,可以進行增、刪、查、看等功能。

初成代碼,如果有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");
	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 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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章