zoukankan      html  css  js  c++  java
  • 数据结构—图书管理系统(顺序表实现)

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

    #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("-----顺序表为空-----
    ");
    	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 (每行一个信息) *******************************
    ");
    	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("-----内存分配失败-----
    ");
    				else printf("-----内存分配成功-----
    ");
    			} else if ( choose == 2 ) {
    				printf("请输入需要输入的信息的长度 : 
    ");
    				cin >> length;
    				if ( Scanner(length ,s ) ) {
    					printf("-----输入完毕-----
    ");
    				} else {
    					printf("----- error : 内存超限 -----
    ");
    				}
    			} else if ( choose == 3 ) {
    				printf("请输入一个书的价格信息:
    ");
    				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("-----对不起:没有该书的消息-----
    ");
    				}
    			} else if ( choose == 4 ) {
    				printf("请输入插入的书本位置和信息 (id--name--price):
    ");
    				cin >> val >> temp.id >> temp.name >> temp.price;
    				if ( insert(s ,val ,temp ) ) {
    					printf("-----插入成功-----
    ");
    				} else {
    					printf("-----插入失败-----
    ");
    				}
    			} else if ( choose == 5 ) {
    				printf("请输入删除书本的位置:
    ");
    				cin >> val;
    				if ( delect(s ,val ) ) {
    					printf("-----删除成功-----
    ");
    				} else {
    					printf("-----删除失败-----
    "); 
    				}
    			} else if ( choose == 6 ) {
    				Print(s);
    				printf("-----输出完毕-----
    "); 
    			} 
    		}
    		printf("-----谢谢使用-----
    您还需要继续使用吗?(1.继续使用,2.停止使用)");
    		cin >> val;
    		if ( val == 1 ) continue;
    		else if ( val == 2 ) break; 
    	}
    	return 0;
    }
    
  • 相关阅读:
    小程序返回顶部top滚动
    创建对象的几种模式
    前端基础常识
    三行代码让页面中的所有元素增添不同颜色的外边框
    纯css制作小三角
    设计表单
    纯css制作三级菜单
    三栏-中栏流动布局
    三栏固定布局(为栏设定内边距和边框)
    ie8以下不兼容h5新标签的解决方法
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745924.html
Copyright © 2011-2022 走看看