媒體庫管理系統

//這個是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();//完成顯示功能
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章