图书管理系统1

来源:互联网 发布:开发游戏的软件 编辑:程序博客网 时间:2024/06/05 08:29

Entry.h

#ifndef _ENTRY_H_
#define _ENTRY_H_

#include <string>

using namespace std;

/*this is the base class of Entry*/
enum EntryType
{
 NOTDEFINE = 0,
 BOOKENTRY,    //books
 NEWSPAPERENTRY,   //Newspaper
 ELECENTRY    //Electronic
};

class Entry
{
public:
 Entry(int nEntryType):m_nEntryType(nEntryType) {}

 virtual void print() = 0;
 virtual int encode(string& str) = 0;
 virtual int decode(const string& str) = 0;

 inline static int getEntryType(string& str)
 {
  size_t nPos = str.find(" ");
  string strLeft = str.substr(0, nPos);
  int nEntryType = atoi(strLeft.c_str());
  str = str.substr(nPos);
  return nEntryType;
 }

public:
 int m_nEntryType;
 string m_strTitle;
 string m_strAuthors;
};


class BookEntry : public Entry
{
public:

 BookEntry():Entry(BOOKENTRY) {}

 virtual void print();
 virtual int encode(string& str);
 virtual int decode(const string& str);

public:
 string m_strPubPlace;
 string m_strPubName;
 string m_strPubYear;
};

class NewsPaperEntry : public Entry
{
public:
 NewsPaperEntry():Entry(NEWSPAPERENTRY) {}

 virtual void print();
 virtual int encode(string& str);
 virtual int decode(const string& str);

public:
 string m_strNewsTitle;
 string m_strPubDate;
 int m_nPageNum;
};

class ElecEntry : public Entry
{
public:
 ElecEntry():Entry(ELECENTRY) {}

 virtual void print();
 virtual int encode(string& str);
 virtual int decode(const string& str);

public:
 string m_strWebTile;
 string m_strPostDate;
 string m_strOrgName;
 string m_strAccessDate;
};

#endif

 

 

Entry.cpp

#include "Entry.h"
#include <stdio.h>
#include <iostream>

using namespace std;

void BookEntry::print()
{
 char buf[1024] = {0};
 sprintf(buf,
   "%-15s %-15s %-15s %-15s %-15s",
   m_strTitle.c_str(),
   m_strAuthors.c_str(),
   m_strPubPlace.c_str(),
   m_strPubName.c_str(),
   m_strPubYear.c_str());
 cout << buf << endl;
}

int BookEntry::encode(string& str)
{
 char buf[1024] = {0};
 sprintf(buf,
  "%d %-15s %-15s %-15s %-15s %-15s",
  m_nEntryType,
  m_strTitle.c_str(),
  m_strAuthors.c_str(),
  m_strPubPlace.c_str(),
  m_strPubName.c_str(),
  m_strPubYear.c_str());

 str = buf;
 return 0;
}

int BookEntry::decode(const string& str)
{
 char cTitleBuf[512] = {0};
 char cAuthorBuf[512] = {0};
 char cPubPlace[512] = {0};
 char cPubName[512] = {0};
 char cPubYear[512] = {0};
 sscanf(str.c_str(), "%s %s %s %s %s", cTitleBuf, cAuthorBuf, cPubPlace, cPubName, cPubYear);
 m_strTitle = cTitleBuf;
 m_strAuthors = cAuthorBuf;
 m_strPubPlace = cPubPlace;
 m_strPubName = cPubName;
 m_strPubYear = cPubYear;

 return 0;
}

void NewsPaperEntry::print()
{
 char buf[1024] = {0};
 sprintf(buf,
  "%-15s %-15s %-15s %-15s %-15d",
  m_strTitle.c_str(),
  m_strAuthors.c_str(),
  m_strNewsTitle.c_str(),
  m_strPubDate.c_str(),
  m_nPageNum);
 cout << buf << endl;
}

int NewsPaperEntry::encode(string& str)
{
 char buf[1024] = {0};
 sprintf(buf,
  "%d %-15s %-15s %-15s %-15s %-15d",
  m_nEntryType,
  m_strTitle.c_str(),
  m_strAuthors.c_str(),
  m_strNewsTitle.c_str(),
  m_strPubDate.c_str(),
  m_nPageNum);

 str = buf;
 return 0;
}

int NewsPaperEntry::decode(const string& str)
{
 char cTitleBuf[512] = {0};
 char cAuthorBuf[512] = {0};
 char cNewsTitle[512] = {0};
 char cPubDate[512] = {0};
 int nPageNum = 0;
 sscanf(str.c_str(), "%s %s %s %s %d", cTitleBuf, cAuthorBuf, cNewsTitle, cPubDate, &nPageNum);
 m_strTitle = cTitleBuf;
 m_strAuthors = cAuthorBuf;
 m_strNewsTitle = cNewsTitle;
 m_strPubDate = cPubDate;
 m_nPageNum = nPageNum;
 return 0;
}

void ElecEntry::print()
{
 char buf[1024] = {0};
 sprintf(buf,
  "%-15s %-15s %-15s %-15s %-15s %-15s",
  m_strTitle.c_str(),
  m_strAuthors.c_str(),
  m_strWebTile.c_str(),
  m_strPostDate.c_str(),
  m_strOrgName.c_str(),
  m_strAccessDate.c_str());
 cout << buf << endl;
}

int ElecEntry::encode(string& str)
{
 char buf[1024] = {0};
 sprintf(buf,
  "%d %-15s %-15s %-15s %-15s %-15s %-15s",
  m_nEntryType,
  m_strTitle.c_str(),
  m_strAuthors.c_str(),
  m_strWebTile.c_str(),
  m_strPostDate.c_str(),
  m_strOrgName.c_str(),
  m_strAccessDate.c_str());

 str = buf;
 return 0;
}

int ElecEntry::decode(const string& str)
{
 char cTitleBuf[512] = {0};
 char cAuthorBuf[512] = {0};
 char cWebTile[512] = {0};
 char cPostDate[512] = {0};
 char cOrgName[512] = {0};
 char cAccessDate[512] = {0};
 sscanf(str.c_str(), "%s %s %s %s %s %s", cTitleBuf, cAuthorBuf, cWebTile, cPostDate, cOrgName, cAccessDate);
 m_strTitle = cTitleBuf;
 m_strAuthors = cAuthorBuf;
 m_strWebTile = cWebTile;
 m_strPostDate = cPostDate;
 m_strOrgName = cOrgName;
 m_strAccessDate = cAccessDate;
 return 0;
}

 

Bibliographic.h

#ifndef _BIBLIO_GRAPHIC_H_
#define _BIBLIO_GRAPHIC_H_

#include "Entry.h"
#include <map>
#include <list>
#include <string>

using namespace std;

class BiblioGraphic
{
public:
 BiblioGraphic(string strFilePath):m_strFilePath(strFilePath) {}
 ~BiblioGraphic();

 int load();

 void save();
 
 int add(string strTitle, Entry* pEntry);

 int del(string strTitle);

 list<Entry*> find(string strTitle);

 void show();

 int num() { return m_store.size(); }

 static Entry* genEntryFactory(int nEntryType);
 
private:
 //use multmap to store the entries
 multimap<string, Entry*> m_store;
 string m_strFilePath;
};

#endif

 

BiblioGraphic.cpp

#include "Bibliographic.h"
#include <fstream>
#include <iostream>

using namespace std;

/*
class BiblioGraphic
{
public:
BiblioGraphic(string strFilePath):m_strFilePath(strFilePath) {}
~BiblioGraphic();
int load();
void save();
int add(string strTitle, Entry* pEntry);
int del(string strTitle);
list<Entry*> find(string strTitle);
void show();
int num();

private:
//use multmap to store the entries
multimap<string, Entry*> m_store;
string m_strFilePath;
};
*/

BiblioGraphic::~BiblioGraphic()
{
 for (multimap<string, Entry*>::iterator it = m_store.begin();
  it != m_store.end();
  ++it)
 {
  delete it->second;
 }
}

int BiblioGraphic::load()
{  
 ifstream in("input.txt");
 if (!in)
 {
  cout << "some error happened" << endl;
  return -1;
 }
 string str;
 while (getline(in, str))
 {
  int nType = Entry::getEntryType(str);
  Entry* pEntity = BiblioGraphic::genEntryFactory(nType);
  if (NULL == pEntity)
  {
   cout << "get unknow line"<< endl;
   continue;
  }
  pEntity->decode(str);

  m_store.insert(pair<string, Entry*>(pEntity->m_strTitle, pEntity));
  
 }

 in.close();
 cout << "load end" << endl;
 return 0;
}

void BiblioGraphic::save()
{
 ofstream of("input.txt");
 for (multimap<string, Entry*>::iterator it = m_store.begin();
  it != m_store.end();
  ++it)

 {
  string str;
  it->second->encode(str);
  of << str << endl;
 }
 of.close();
}

int BiblioGraphic::add(string strTitle, Entry* pEntry)
{
 m_store.insert(pair<string, Entry*>(strTitle, pEntry));
 cout << "insert success" << endl;
 return 0;
}

int BiblioGraphic::del(string strTitle)
{
 multimap<string, Entry*>::iterator it;
 int nNum = m_store.count(strTitle);
 if (0 == nNum)
 {
  cout << "del failed, not this Title" << strTitle << endl;
  return -1;
 }
 else if (1 == nNum)
 {
  it = m_store.find(strTitle);
  m_store.erase(it);
 }
 else
 {
  it = m_store.find(strTitle);
  for (int i=0; i<nNum; ++i)
  {
   it->second->print();
   char c = 0;
   cout << "do you want to delete it ? (y/n)" << endl;
   cin >> c;
   if (c == 'y' || c == 'Y')
   {
    m_store.erase(it++);
   }
   else
   {
    it++;
   }
  }
 }
 return 0;
}

list<Entry*> BiblioGraphic::find(string strTitle)
{
 list<Entry*> lst;

 multimap<string, Entry*>::iterator it;
 int nNum = m_store.count(strTitle);
 it = m_store.find(strTitle);
 for (int i=0; i<nNum; ++i)
 {
  lst.push_back(it->second);
  it++;
 }
 return lst;
}

void BiblioGraphic::show()
{
 for (multimap<string, Entry*>::iterator it = m_store.begin();
  it != m_store.end();
  ++it)
 {
  it->second->print();
 }
}

Entry* BiblioGraphic::genEntryFactory(int nEntryType)
{
 Entry* p = NULL;
 switch (nEntryType)
 {
 case BOOKENTRY:
  p = new BookEntry();
  break;

 case NEWSPAPERENTRY:
  p = new NewsPaperEntry();
  break;

 case ELECENTRY:
  p = new ElecEntry();
  break;

 default:
  break;
 }
 return p;
}

 

main.cpp

#include "Bibliographic.h"
#include <iostream>

using namespace std;

void help()
{
 cout << "h-Help. Print a help Message"<< endl;
 cout << "Q-Quit. Quit the program"<< endl;
 cout << "L-List. List all the BiblioGraphic"<< endl;
 cout << "A-Add.  Add an entry to the list."<< endl;
 cout << "D-Delete. Delete an entry by Title." << endl;
}

int main(int argc, char** argv)
{
 BiblioGraphic biblio("input.txt");

 biblio.load();
 cout << "h-Help. Print a help Message"<< endl;
 cout << "Q-Quit. Quit the program"<< endl;
 cout << "L-List. List all the BiblioGraphic"<< endl;
 cout << "A-Add.  Add an entry to the list."<< endl;
 cout << "D-Delete. Delete an entry by Title." << endl;

 while (1)
 {
  cout << ">>";
  char c;
  c =  getchar();
  if (c == '\n')
  {
   continue;
  }
  switch (c)
  {
  case 'Q':
  case 'q':
   cout << "quit the program."<< endl;
   biblio.save();
   return 0;
   break;

  case 'H':
  case 'h':
   help();
   break;

  case 'L':
  case 'l':
   biblio.show();
   break;

  case 'A':
  case 'a':
   { 
    string strTitle;
    string strAuhors;
    cout << "please input the title:";
    cin >> strTitle;
    cout << "please input the authors:";
    cin >> strAuhors;
    cout << "whitch kind of entry do you want to add, (a/books, b/newspaper, c/elec, other/books)"<< endl;
    char c = 0;
    cin >> c;
    Entry* p = NULL;
    if (c == 'a')
    {
     BookEntry* p1 = new BookEntry();
     
     cout << "please input the publication:";
     cin >> p1->m_strPubPlace;
     cout << "please input the pubName:";
     cin >> p1->m_strPubName;
     cout << "please input the pubYear";
     cin >> p1->m_strPubYear;
     p = p1;
     
    }
    else if (c == 'c')
    {
     NewsPaperEntry* p2 = new NewsPaperEntry();
     cout << "please input the NewsTitle:";
     cin >> p2->m_strNewsTitle;
     cout << "please input the PubDate:";
     cin >> p2->m_strPubDate;
     cout << "please input the pageNum";
     cin >> p2->m_nPageNum;
     p = p2;
    }
    else
    {
     ElecEntry* p3 = new ElecEntry();
     cout << "please input the WebTile:";
     cin >> p3->m_strWebTile;
     cout << "please input the PostDate:";
     cin >> p3->m_strPostDate;
     cout << "please input the OrgName";
     cin >> p3->m_strOrgName;
     cout << "please input the accessDate:";
     cin >> p3->m_strAccessDate;
     p = p3;
    }
    p->m_strTitle = strTitle;
    p->m_strAuthors = strAuhors;

    biblio.add(strTitle, p);
   }
   break;

  case 'D':
  case 'd':
   {
    string str;
    cout << "please input the title: "<< endl;
    cin >> str;
    biblio.del(str);
   }
   break;

  default:
   break;
  }
 }

 return 0;
}

 


 

 

0 0
原创粉丝点击