600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 数据结构——图书信息管理系统的顺序表实现

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

时间:2021-05-30 09:46:31

相关推荐

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

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

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。