数据结构—图书管理系统(顺序表实现)

第一次数据结构作业,好好写写

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 1e5+50;

struct book {
	string id;
	string name;
	double price;
	book () {}
	book (string _id,string _name,double _price) { id = _id; name = _name; price = _price;}
};

typedef struct {
	book* elem;
	int length;
} sqtable;

int init(sqtable &s) { //
	s.elem = new book[maxn];
	if ( !s.elem ) exit(-2);
	s.length = 0;
	return 1;
}

int delect(sqtable &s,int pos) {
	if ( s.length <= 0 || s.length < pos ) return 0;
	for (int i = pos ; i <= s.length - 1 ; i ++ ) {
		s.elem[i] = s.elem[i+1];
	} 
	s.length --;
	return 1;
}

int insert(sqtable &s,int pos,book val) {
	if ( s.length + 1 == pos ) {
		s.length ++; s.elem[s.length] = val; return 1;
	}
	if ( s.length >= maxn - 1 || pos > s.length ) return 0;
	for (int i = s.length ; i >= pos ; i -- ) {
		s.elem[i+1] = s.elem[i];
	}
	s.elem[pos] = val;
	++ s.length;
	return 1; 
}

void Print(sqtable &s) {
	if ( s.length == 0 ) printf("-----顺序表为空-----\n");
	else {
		for (int i = 1 ; i <= s.length ; i ++ ) {
			cout << "id = "<<s.elem[i].id << " -- "<< "name = "<< s.elem[i].name << " -- "<< "price = "<< s.elem[i].price<< endl;
		}
	} 
}

int Scanner(int length,sqtable &s) {
	if ( s.length + length > maxn - 1) return 0;
	printf("*************************** 请依次输入 id -- name -- price (每行一个信息) *******************************\n");
	for (int i = s.length + 1 ; i <= s.length + length ; i ++ ) {
		cin >> s.elem[i].id >> s.elem[i].name >> s.elem[i].price ;
	} 
	s.length += length; 
	return 1;
}

int findder(sqtable &s,double price) {
	for (int i = 1 ; i <= s.length ; i ++) {
		if ( s.elem[i].price == price ) return i;
	}
	return 0;
}

int main() {
	int length ,val ; 
	double price;
	book temp; 
	while(true) {
		int choose = -1;
		puts("------------欢迎来到图书管理系统-------------");
		puts("");
		puts("**********************************************");
		puts("*******************1. 建立********************");
		puts("*******************2. 输入********************");
		puts("*******************3. 查找********************");
		puts("*******************4. 插入********************");
		puts("*******************5. 删除********************");
		puts("*******************6. 输出********************");
		puts("*******************0. 退出********************");
		puts("**********************************************");
		puts("");
		sqtable s;
		while(cin >> choose && choose) {
			if ( choose == 1 ) {
				if(!init(s)) printf("-----内存分配失败-----\n");
				else printf("-----内存分配成功-----\n");
			} else if ( choose == 2 ) {
				printf("请输入需要输入的信息的长度 : \n");
				cin >> length;
				if ( Scanner(length ,s ) ) {
					printf("-----输入完毕-----\n");
				} else {
					printf("----- error : 内存超限 -----\n");
				}
			} else if ( choose == 3 ) {
				printf("请输入一个书的价格信息:\n");
				cin >> price;
				val = findder(s ,price );
				if ( val ) {
					printf("该书的信息为: ");
					cout << "id = "<< s.elem[val].id<< " -- "<< "name = "<< s.elem[val].name<< " -- "<< "price = "<< s.elem[val].price<< endl; 
				} else {
					printf("-----对不起:没有该书的消息-----\n");
				}
			} else if ( choose == 4 ) {
				printf("请输入插入的书本位置和信息 (id--name--price):\n");
				cin >> val >> temp.id >> temp.name >> temp.price;
				if ( insert(s ,val ,temp ) ) {
					printf("-----插入成功-----\n");
				} else {
					printf("-----插入失败-----\n");
				}
			} else if ( choose == 5 ) {
				printf("请输入删除书本的位置:\n");
				cin >> val;
				if ( delect(s ,val ) ) {
					printf("-----删除成功-----\n");
				} else {
					printf("-----删除失败-----\n"); 
				}
			} else if ( choose == 6 ) {
				Print(s);
				printf("-----输出完毕-----\n"); 
			} 
		}
		printf("-----谢谢使用-----\n您还需要继续使用吗?(1.继续使用,2.停止使用)");
		cin >> val;
		if ( val == 1 ) continue;
		else if ( val == 2 ) break; 
	}
	return 0;
}
  • 10
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于顺序表图书管理系统是一种使用顺序表数据结构来存储和管理图书馆的读者信息、书籍信息以及借还书信息的系统。顺序表是一种线性数据结构,它将元素按照一定顺序依次存储在连续的内存空间中。 在基于顺序表图书管理系统中,可以使用一个一维数组来实现顺序表,数组的每个元素对应图书馆中的一本书籍或一个读者的信息。通过数组的下标可以快速访问和操作对应的元素。通常,系统会预留一些空间来存储新增的书籍和读者信息。 图书管理系统可以提供一些基本的功能,比如: 1. 添加书籍和读者信息:将新的书籍或读者信息添加到顺序表中,同时更新顺序表的长度。 2. 删除书籍和读者信息:从顺序表中删除指定的书籍或读者信息,同时更新顺序表的长度。 3. 查询书籍和读者信息:通过书籍名称、读者姓名等关键字在顺序表中查找对应的书籍或读者信息。 4. 借还书操作:记录读者借书和还书的信息,并更新顺序表中相应书籍的状态。 通过基于顺序表图书管理系统,图书馆可以更高效地管理读者和书籍的信息,提供更好的借阅服务。同时,顺序表作为一种简单而有效的数据结构,能够满足大多数图书馆的需求。<span class="em">1</span> #### 引用[.reference_title] - *1* [数据结构课程设计图书信息管理系统报告(顺序表)(模板)](https://download.csdn.net/download/masteryidashi/10675178)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值