用顺序表实现图书管理系统 数据结构作业

版权声明:转载请注明原文地址即可,要是本文对您有些许帮助的话,请您在下方点个赞,谢谢啦ヾ(o◕∀◕)ノヾ https://blog.csdn.net/qq_33583069/article/details/88953106
/*
	Author:Skysys
	School:NCEPU
*/
#include<bits/stdc++.h>
using namespace std;
////////BASIC INFO//////////
#define ISBN_LENGTH 13
#define NAME_LENGTH 20
#define PRESS_LENGTH 20
#define AUTHOR_LENGTH 20
typedef  struct{
	char book_number[ISBN_LENGTH];
	char book_name[NAME_LENGTH];
	char pub_press[PRESS_LENGTH];
	char author_name[AUTHOR_LENGTH];
	double book_price;
}Book;
typedef struct{
	Book* data;
	int size;
	int length;
}List;
bool initList(List* it,int tSize){
	it->data=(Book*)malloc(sizeof(Book));
	if(it->data==NULL){cerr<<"NO SPACE";return 0;}
	it->size=tSize; it->length=0; return 0;
}
bool insertList(List* it,int id,Book tBook){
	if(id<1||id>it->length+1){cerr<<"WRONG POSITION.";return 0;}
	if(it->length>=it->size){cerr<<"NO SPACE.";return 0;}
	it->data[it->length++]=tBook; return 1;
}
bool deleteItem(List* it,char* ISBN){
	if(it->length==0){cerr<<"NO DATA.";return 0;}
	for(int i=0;i<it->length;i++)
		if(strcmp(ISBN,it->data[i].book_number)==0){
			for(int j=i;j<it->length;j++)it->data[i]=it->data[i+1];
			it->length--; return 1;
		}
	return 0;
}
bool displayList(List* it){
	for(int i=0;i<it->length;i++){
		Book& p = it->data[i];
		printf("[ID#%d %s %s %s %s %lf]\n",i+1,p.book_number,p.book_name,
			p.pub_press,p.author_name,p.book_price);
	}
}
#define SIZE 100
int main(){ 
	List *it=(List*)malloc(sizeof(List));
	Book tBook;		char ch;	int cnt=0;	initList(it,SIZE);
	do{
		cout<<"#DATA Format: book_number(str) book_name(str) pub_press(str) author_name(str) book_price(double)"<<endl;
		cout<<"[INPUT]>>>";
		cin>>tBook.book_number>>tBook.book_name>>tBook.pub_press
		   >>tBook.author_name>>tBook.book_price;getchar();
		if(!insertList(it,++cnt,tBook)){cerr<<"INSERT ERROR";return 0;};
		cout<<"[Notice]Push Y/y for continue,others for break."<<endl;
		cout<<"[INPUT]>>>";
		ch=getchar();
	}while(ch=='Y'||ch=='y');
#define TEST_DEL
#define TEST_DEL	
	char ISBN[ISBN_LENGTH];
	cin>>ISBN;
	deleteItem(it,ISBN);
	displayList(it);
#define endif
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33583069/article/details/88953106