//这个是main.cpp中的代码 #include <iostream> #include <iomanip>//作用是调用setw()函数 #include <string> #include <stdlib.h>//使用exit()函数 #include <fstream>//使用文件输入输出流 #include "article.h" //#include "article.cpp" #include "book.h" #include "picture.h" #include "disk.h" #include "add.h" #include "del.h" using namespace std; int main() { int i;//用于菜单功能 int j;//存放物品的编号 string str;//存放物品的标题 //以下9行程序为建立链表做准备工作 struct book * bhead,*bcurrent; struct disk * dhead,*dcurrent; struct picture *phead,*pcurrent; bhead=bcurrent=NULL; dhead=dcurrent=NULL; phead=pcurrent=NULL; bhead=initbook(); dhead=initdisk(); phead=initpicture(); while(1) { cout<<endl; cout<<"*****************************媒体库管理系统*************************************"; cout<<setw(40)<<"1、添加物品"<<endl; cout<<setw(40)<<"2、显示物品"<<endl; cout<<setw(40)<<"3、查询物品"<<endl; cout<<setw(40)<<"4、统计物品"<<endl; cout<<setw(40)<<"5、删除物品"<<endl; cout<<setw(40)<<"6、编辑物品"<<endl; cout<<setw(36)<<"7、清屏"<<endl; cout<<setw(36)<<"8、退出"<<endl; cout<<"********************************************************************************"; cin>>i; switch(i) { case 1: cout<<"********************************************************************************"; cout<<setw(40)<<"1、添加书的信息"<<endl; cout<<setw(40)<<"2、添加光盘信息"<<endl; cout<<setw(40)<<"3、添加图画信息"<<endl; cout<<"********************************************************************************"; cin>>i; if(i==1) bhead=addbook(bhead); else if(i==2) dhead=adddisk(dhead); else if(i==3) phead=addpicture(phead); else { cout<<"输入有误,返回主目录!"<<endl; } break; case 2: cout<<"********************************************************************************"; cout<<setw(40)<<"1、显示书的信息"<<endl; cout<<setw(40)<<"2、显示光盘信息"<<endl; cout<<setw(40)<<"3、显示图画信息"<<endl; cout<<"********************************************************************************"; cin>>i; if(i==1) { if(bhead!=NULL) { bcurrent=bhead; while(bcurrent!=NULL) { bcurrent->bk->showbook(); bcurrent=bcurrent->next; } } else cout<<"无图书信息!"<<endl; } else if(i==2) { if(dhead!=NULL) { dcurrent=dhead; while(dcurrent!=NULL) { dcurrent->dk->showdisk(); dcurrent=dcurrent->next; } } else cout<<"无光盘信息!"<<endl; } else if(i==3) { if(phead!=NULL) { pcurrent=phead; while(pcurrent!=NULL) { pcurrent->pe->showpicture(); pcurrent=pcurrent->next; } } else cout<<"无图画信息!"<<endl; } else { cout<<"输入错误,返回主目录!"<<endl; } break; case 3: cout<<"********************************************************************************"; cout<<setw(40)<<"1、按标题查询"<<endl; cout<<setw(40)<<"2、按编号查询"<<endl; cout<<setw(40)<<"3、按类别查询"<<endl; cout<<"********************************************************************************"; cin>>i; if(i==1) { cout<<"请输入物品的标题:"<<endl; cin>>str; pcurrent=phead; while(pcurrent!=NULL) { if(pcurrent->pe->searchtitle(str)) { cout<<setw(40)<<"图画中的信息如下所示:"<<endl; pcurrent->pe->showpicture(); break; } else pcurrent=pcurrent->next; } if(pcurrent==NULL) { cout<<setw(40)<<"图画中无此信息!"<<endl; } dcurrent=dhead; while(dcurrent!=NULL) { if(dcurrent->dk->searchtitle(str)) { cout<<setw(40)<<"光盘中的信息如下所示:"<<endl; dcurrent->dk->showdisk(); break; } else dcurrent=dcurrent->next; } if(dcurrent==NULL) { cout<<setw(40)<<"光盘中无此信息!"<<endl; } bcurrent=bhead; while(bcurrent!=NULL) { if(bcurrent->bk->searchtitle(str)) { cout<<setw(40)<<"图书中的信息如下所示:"<<endl; bcurrent->bk->showbook(); break; } else bcurrent=bcurrent->next; } if(bcurrent==NULL) { cout<<setw(40)<<"图书中无此信息!"<<endl; } } else if(i==2) { if(sumnumber!=0) { cout<<"请输入物品的编号:"<<endl; cin>>j; for(i=0;i<sumnumber;i++) { if(sum[i]==j) { pcurrent=phead; while(pcurrent!=NULL) { if(pcurrent->pe->searchnumber(j)) { pcurrent->pe->showpicture(); break; } else pcurrent=pcurrent->next; } dcurrent=dhead; while(dcurrent!=NULL) { if(dcurrent->dk->searchnumber(j)) { dcurrent->dk->showdisk(); break; } else dcurrent=dcurrent->next; } bcurrent=bhead; while(bcurrent!=NULL) { if(bcurrent->bk->searchnumber(j)) { bcurrent->bk->showbook(); break; } else bcurrent=bcurrent->next; } break; } } if(i>=sumnumber) { cout<<"无此编号!"<<endl; } } else cout<<"无编号信息!"<<endl; } else if(i==3) { cout<<"请输入物品的类别(book,disk,picture,请按此格式输入一个英文字母):"<<endl; cin>>str; if(str=="book") { bcurrent=bhead; if(bcurrent==NULL) { cout<<setw(40)<<"无图书信息!"<<endl; } while(bcurrent!=NULL) { bcurrent->bk->showbook(); bcurrent=bcurrent->next; } } else if(str=="disk") { dcurrent=dhead; if(dcurrent==NULL) { cout<<setw(40)<<"无光盘信息!"<<endl; } while(dcurrent!=NULL) { dcurrent->dk->showdisk(); dcurrent=dcurrent->next; } } else if(str=="picture") { pcurrent=phead; if(pcurrent==NULL) { cout<<setw(40)<<"无图画信息!"<<endl; } while(pcurrent!=NULL) { pcurrent->pe->showpicture(); pcurrent=pcurrent->next; } } else { cout<<"输入错误,返回主目录!"<<endl; } } else { cout<<"输入错误,返回主目录!"<<endl; } break; case 4: cout<<"********************************************************************************"; cout<<setw(40)<<"1、查询图书数"<<endl; cout<<setw(40)<<"2、查询光盘数"<<endl; cout<<setw(40)<<"3、查询图画数"<<endl; cout<<"********************************************************************************"; cin>>i; cout<<setw(40)<<"物件总数为:"<<sumnumber<<endl; if(i==1) cout<<setw(40)<<"图书数:"<<bcount<<endl; else if(i==2) cout<<setw(40)<<"光盘数:"<<dcount<<endl; else if(i==3) cout<<setw(40)<<"图画数:"<<pcount<<endl; else cout<<setw(40)<<"输入错误,不能查询相应的物品!"<<endl; break; case 5: cout<<"请输入物品的编号:"<<endl; cin>>j; while(1) { if(sumnumber==0)cout<<"物品库为空!"<<endl; for(i=0;i<bcount;i++) { if(bk[i]==j) { bhead=(struct book *)del((struct article *)bhead,j);//若该编号为书的编号 bcount--; break; } else cout<<"该编号不存在!"<<endl; } for(i=0;i<dcount;i++) { if(dk[i]==j) { dhead=(struct disk *)del((struct article *)dhead,j);//若该编号为光盘的编号 dcount--; break; } else cout<<"该编号不存在!"<<endl; } for(i=0;i<pcount;i++) { if(pe[i]==j) { phead=(struct picture *)del((struct article *)phead,j);//若该编号为图画的编号 pcount--; break; } else cout<<"该编号不存在!"<<endl; } break; } break; case 6: cout<<endl; break; case 7: system("cls"); break; case 8: exit(0); break; default : cout<<"输入有误,请重新输入:"<<endl; } } return 0; } //这个是article.cpp中的代码 #include "article.h" #include <iomanip>//作用是调用setw()函数 #include <string> Article :: Article(int number,string title,string author) { this->number=number; this->title=title; this->author=author; } void Article :: show() { cout<<"********************************************************************************"; cout<<setw(35)<<"number:"<<number<<endl; cout<<setw(35)<<"title:"<<title<<endl; cout<<setw(35)<<"author:"<<author<<endl; } bool Article :: searchnumber(int number) { if(this->number==number) return true; else return false; } bool Article :: searchtitle(string str) { if(this->title==str) return true; else return false; } //这个是book.cpp中的文件 #include "article.h" #include <iomanip>//作用是调用setw()函数 #include <string> #include "book.h" Book :: Book(int number,string title,string author,string press,int page) :Article(number,title,author) { this->press=press; this->page=page; } void Book :: showbook() { show(); cout<<setw(35)<<"press:"<<press<<endl; cout<<setw(35)<<"page:"<<page<<endl; cout<<"********************************************************************************"; } //这个是disk.cpp中的文件 #include "article.h" #include <iomanip>//作用是调用setw()函数 #include <string> #include "disk.h" Disk :: Disk(int number,string title,string author,int time,string date) : Article(number,title,author) { this->time=time; this->date=date; } void Disk :: showdisk() { show(); cout<<setw(35)<<"time:"<<time<<endl; cout<<setw(35)<<"date:"<<date<<endl; cout<<"********************************************************************************"; } //这个是picture.cpp中的文件 #include "article.h" #include <iomanip>//作用是调用setw()函数 #include <string> #include "picture.h" Picture :: Picture(int number,string title,string author,int width,int length,string nationality) : Article(number,title,author) { this->width=width; this->length=length; this->nationality=nationality; } void Picture :: showpicture() { show(); cout<<setw(35)<<"width:"<<width<<endl; cout<<setw(35)<<"length:"<<length<<endl; cout<<setw(35)<<"nationality:"<<nationality<<endl; cout<<"********************************************************************************"; } //这个是add.h中的代码 #include "count.h" #include <sstream> struct article { Article * ae; struct article * next; }; struct book { Book * bk; struct book * next; }; struct disk { Disk * dk; struct disk * next; }; struct picture { Picture * pe; struct picture * next; }; struct book * initbook();//完成初始化功能 struct disk * initdisk(); struct picture * initpicture(); struct book * addbook(struct book * phead);//完成添加功能 struct disk * adddisk(struct disk * disk); struct picture * addpicture(struct picture * picture); bool count(char str,int num);//完成统计功能 struct book * initbook() { struct book * phead; phead=NULL; ifstream infile("书籍档案.txt"); string str; getline(infile,str); while(!infile.eof()) { getline(infile,str); if(str.empty())break; istringstream istring(str); int num,page; num=page=0; string title,author,press; istring>>bcount>>num>>title>>author>>press>>page; struct book * ptail=phead; if(ptail!=NULL) { while(ptail->next!=NULL) ptail=ptail->next; } struct book * d=new struct book; Book * d1=new Book(num,title,author,press,page); d->bk=d1; d->next=NULL; if(phead==NULL)phead=d; else ptail->next=d; ptail=d; sumnumber++; bk[dcount-1]=num; sum[dcount-1]=num; } infile.close(); return phead; } struct book * addbook(struct book * phead) { ofstream outfile("书籍档案.txt",ios::app);//定义一个文件输出流的对象,用于向文件中输出数据 if(!outfile) { cout<<"打开书籍档案失败!"<<endl; return phead; } struct book * ptail=phead; if(ptail!=NULL) { while(ptail->next!=NULL) ptail=ptail->next; } int i; int num; string title; string author; string press; int page; while(1) { cout<<"按1输入图书信息,按0返回主目录:"<<endl; cin>>i; if(i==0)break; if(i==1) { cout<<"输入图书编号(整数),题目,作者,出版社,页数(整数):"<<endl; getchar(); cin>>num>>title>>author>>press>>page; if(count('b',num)) { struct book * b=new struct book; Book * b1=new Book(num,title,author,press,page); b->bk=b1; b->next=NULL; if(phead==NULL)phead=b; else ptail->next=b; ptail=b; //在链表末尾添加节点后,将数据添加到文件中 outfile<<endl<<bcount<<" "<<num<<" "<<title<<" "<<author<<" "<<press<<" "<<page; outfile.close(); } else continue; } else cout<<"输入错误,重新输入!"<<endl; } return phead; } struct disk * initdisk() { struct disk * phead; phead=NULL; ifstream infile("光盘档案.txt"); string str; getline(infile,str); while(!infile.eof()) { getline(infile,str); if(str.empty())break; istringstream istring(str); int num,time; num=time=0; string title,author,date; istring>>dcount>>num>>title>>author>>time>>date; struct disk * ptail=phead; if(ptail!=NULL) { while(ptail->next!=NULL) ptail=ptail->next; } struct disk * d=new struct disk; Disk * d1=new Disk(num,title,author,time,date); d->dk=d1; d->next=NULL; if(phead==NULL)phead=d; else ptail->next=d; ptail=d; sumnumber++; dk[dcount-1]=num; sum[dcount-1]=num; } infile.close(); return phead; } struct disk * adddisk(struct disk * phead) { ofstream outfile("光盘档案.txt",ios::app);//定义一个文件输出流的对象,用于向文件中输出数据 if(!outfile) { cout<<"打开光盘档案失败!"<<endl; return phead; } //outfile<<"记录"<<" "<<"编号"<<" "<<"题目"<<" "<<"作者"<<" "<<"播放时长"<<" "<<"出品年份"<<endl; struct disk * ptail=phead; if(ptail!=NULL) { while(ptail->next!=NULL) ptail=ptail->next; } int i; int num; string title; string author; int time; string date; while(1) { cout<<"按1输入光盘信息,按0返回主目录:"<<endl; cin>>i; if(i==0)break; if(i==1) { cout<<"输入光盘编号(整数),题目,作者,播放时长(整数),出品年份:"<<endl; getchar(); cin>>num>>title>>author>>time>>date; if(count('d',num)) { struct disk * d=new struct disk; Disk * d1=new Disk(num,title,author,time,date); d->dk=d1; d->next=NULL; if(phead==NULL)phead=d; else ptail->next=d; ptail=d; //在链表末尾添加节点后,将数据添加到文件中 outfile<<endl<<dcount<<" "<<num<<" "<<title<<" "<<author<<" "<<time<<" "<<date; outfile.close(); } else continue; } else cout<<"输入错误,重新输入!"<<endl; } return phead; } struct picture * initpicture() { struct picture * phead; phead=NULL; ifstream infile("图画档案.txt"); string str; getline(infile,str); while(!infile.eof()) { getline(infile,str); if(str.empty())break; istringstream istring(str); int num,width,length; num=width=length=0; string title,author,nationality; istring>>pcount>>num>>title>>author>>width>>length>>nationality; struct picture * ptail=phead; if(ptail!=NULL) { while(ptail->next!=NULL) ptail=ptail->next; } struct picture * d=new struct picture; Picture * d1=new Picture(num,title,author,width,length,nationality); d->pe=d1; d->next=NULL; if(phead==NULL)phead=d; else ptail->next=d; ptail=d; sumnumber++; pe[pcount-1]=num; sum[pcount-1]=num; } infile.close(); return phead; } struct picture * addpicture(struct picture * phead) { ofstream outfile("图画档案.txt",ios::app);//定义一个文件输出流的对象,用于向文件中输出数据 if(!outfile) { cout<<"打开图画档案失败!"<<endl; return phead; } struct picture * ptail=phead; if(ptail!=NULL) { while(ptail->next!=NULL) ptail=ptail->next; } //outfile<<"记录"<<" "<<"编号"<<" "<<"题目"<<" "<<"作者"<<" "<<"宽"<<" "<<"长"<<" "<<"出品国籍"<<endl; int i; int num; string title; string author; int width; int length; string nationality; while(1) { cout<<"按1输入图画信息,按0返回主目录:"<<endl; cin>>i; if(i==0)break; if(i==1) { cout<<"输入图画编号(整数),题目,作者,宽(整数),长(整数),出品国籍:"<<endl; getchar(); cin>>num>>title>>author>>width>>length>>nationality; if(count('p',num)) { struct picture * p=new struct picture; Picture * p1=new Picture(num,title,author,width,length,nationality); p->pe=p1; p->next=NULL; if(phead==NULL)phead=p; else ptail->next=p; ptail=p; //在链表末尾添加节点后,将数据添加到文件中 outfile<<endl<<pcount<<" "<<num<<" "<<title<<" "<<author<<" "<<width<<" "<<length<<" "<<nationality; outfile.close(); } else continue; } else cout<<"输入错误,重新输入!"<<endl; } return phead; } //article.h #include <iostream> using namespace std; class Article { protected : int number;//此处为物品的编号,是物品不同与其他物品的唯一标识,为书,光盘,图画的共有属性 string title;//此处为物品的名称,为书,光盘,图画的共有属性 string author;//此处为物品的作者,为书,光盘,图画的共有属性 public : Article(int number,string title,string author);//构造函数,为属性初始化 void show();//此函数的功能为显示 bool searchnumber(int number); bool searchtitle(string str); }; //book.h #include <iostream> using namespace std; class Article; class Book : public Article { private : string press;//为出版社的名字 int page;//为书的页数 public : Book(int number,string title,string author,string press,int page);//构造函数,为属性初始化 void showbook();//完成显示功能 }; //count.h #define MAX 100//可以存放的物品的最大值 int bk[MAX]; int dk[MAX]; int pe[MAX]; int sum[3*MAX]; //定义全局变量,对书,光盘,图画进行统计,初始值为0; int bcount=0; int dcount=0; int pcount=0; int sumnumber=0; bool count(char str,int num) { int i; switch(str) { case 'b': for(i=0;i<sumnumber;i++) { if(sum[i]==num) { cout<<"物品的num应该唯一,输入有误,核对后重新输入"<<endl; return false; //break; } } if(i>=sumnumber) { bcount++; sumnumber++; bk[i]=num; sum[i]=num; } break; case 'd': for(i=0;i<sumnumber;i++) { if(sum[i]==num) { cout<<"物品的num应该唯一,输入有误,核对后重新输入"<<endl; return false; //break; } } if(i>=sumnumber) { dcount++; sumnumber++; dk[i]=num; sum[i]=num; } break; case 'p': for(i=0;i<sumnumber;i++) { if(sum[i]==num) { cout<<"物品的num应该唯一,输入有误,核对后重新输入"<<endl; return false; //break; } } if(i>=pcount) { pcount++; sumnumber++; pe[i]=num; sum[i]=num; } } return true; } //del.h struct article * del(struct article * ahead,int j)//j存放物品的编号 { int i; struct article * acurrent,* afront; acurrent=afront=ahead; for(i=0;i<sumnumber;i++) { if(sum[i]==j) { while(acurrent!=NULL) { if(acurrent->ae->searchnumber(j)) { if(ahead->ae->searchnumber(j)) { ahead=ahead->next; delete(acurrent->ae); sumnumber--; i--; cout<<"删除成功!"<<endl; break; } else { afront->next=acurrent->next; delete(acurrent->ae); sumnumber--; i--; cout<<"删除成功!"<<endl; break; } break; } else { afront=acurrent; acurrent=acurrent->next; } } break; } } return ahead; } //disk.h class Disk : public Article { protected : int time;//为光盘的播放时间 string date;//为光盘的出品年份 public : Disk(int number,string title,string author,int time,string date);//构造函数,为属性初始化 void showdisk();//完成显示功能 }; //picture.h class Picture : public Article { protected : int width;//画的宽 int length;//画的长 string nationality;//出品国籍 public : Picture(int number,string title,string author,int width,int length,string nationality);//构造函数,为属性初始化 void showpicture();//完成显示功能 };