首页 服务器 编程 必备知识 搜索引擎
站内搜索
最近浏览
推荐文章
热文排行

将STL改成链表式


#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Contacts
{
public:
string getName() const;
string getSex() const;
string getAddress() const;
string getID() const;
string getTelnumber() const;
string getE_mail() const;

void setName(const string& s) {  name = s; }
void setSex(const string& s) { sex = s; }
void setAddress(const string& s) { address = s; }
void setID(const string& s) { ID = s; }
void setTelnumber(const string& s) { telnumber =s; }
void setE_mail(const string& s) { E_mail= s; }
private:
string name;
string sex;
string address;
string ID;
string telnumber;
string E_mail;
};
string Contacts::getAddress() const
{
return address;
}

string Contacts::getE_mail() const
{
return address;
}

string Contacts::getID() const
{
return ID;
}

string Contacts::getName() const
{
return name;
}

string Contacts::getSex() const
{
return sex;
}

string Contacts::getTelnumber() const
{
return telnumber;
}

typedef struct ST_CONTACT_LIST
{
Contacts* pCont;
ST_CONTACT_LIST* pPrev;
ST_CONTACT_LIST* pNext;
} contact_list_st;

class ContactMgr
{
public:
ContactMgr();
~ContactMgr();
void Insert();
void Edit();
void Delete();
void DisplayAll();
void QueryRes();
private:
contact_list_st* pContactList;
contact_list_st* pTail;
int counter;
contact_list_st* Query(string& name, contact_list_st* plist);
contact_list_st* exactQuery(string& name, contact_list_st* plist);
void single_Diplay(Contacts* pcon) const;
Contacts* Input();
};

ContactMgr::ContactMgr()
{
pContactList = NULL;
pTail = NULL;
counter = 0;
}

ContactMgr::~ContactMgr()
{
contact_list_st* ptmp = pContactList;
while(NULL != ptmp)
{
  pContactList = ptmp->pNext;
  delete ptmp;
  ptmp = pContactList;
}
}

Contacts* ContactMgr::Input()
{
Contacts* ptmp = new Contacts();
if(NULL == ptmp)
{
  cout << "Apply memory failed" << endl;
  exit(1);
}
string s;

cout << "Name: ";
cin >> s;
ptmp->setName(s);

cout << "Sex: ";
cin >> s;
ptmp->setSex(s);

cout << "Address: ";
cin >> s;
ptmp->setAddress(s);

cout << "ID: ";
cin >> s;
ptmp->setID(s);

cout << "TelNumber: ";
cin >> s;
ptmp->setTelnumber(s);

cout << "Email: ";
cin >> s;
ptmp->setE_mail(s);

return ptmp;
}

void ContactMgr::single_Diplay(Contacts* pcon) const
{
if(NULL == pcon)
{
  cout << "Input contact is NULL" << endl;
  return;
}
cout << setw(10) << "Name: " << pcon->getName() << endl;
cout << setw(10) << "Sex: " << pcon->getSex() << endl;
cout << setw(10) << "Address: " << pcon->getAddress() << endl;
cout << setw(10) << "ID: " << pcon->getID() << endl;
cout << setw(10) << "TelNumber: " << pcon->getTelnumber() << endl;
cout << setw(10) << "Email: " << pcon->getE_mail() << endl;
}

void ContactMgr::Insert()
{
Contacts* pnewcon = Input();
string q_name = pnewcon->getName();
if (exactQuery(q_name, pContactList) != NULL)
{
  cout << "\n----------------------------------------" << endl;
  cout << "ERROR: Try to insert an exsiting contact person!" << endl;
  cout << "-----------------------------------------\n" << endl;
  return ;
}

contact_list_st* pnew = new contact_list_st;
if(NULL == pnew)
{
  cout << "Apply memory failed" << endl;
  exit(1);
}
memset(pnew, 0, sizeof(contact_list_st));
pnew->pCont = pnewcon;
if(NULL == pContactList)
{
  pContactList = pnew;
  pTail = pnew;
}
else
{
  pTail->pNext = pnew;
  pnew->pPrev = pTail;
  pTail = pnew;
}
counter++;
cout << "\n----------------------------------------" << endl;
cout << "Insert successsfully! " << endl;
cout << "----------------------------------------\n" << endl;
}

contact_list_st* ContactMgr::Query(string& name, contact_list_st* plist)
{
contact_list_st* ptmp = plist;
while(NULL != ptmp)
{
  string s = ptmp->pCont->getName();
  if(s.find(name) != string::npos)
  {
   break;
  }
  ptmp = ptmp->pNext;
}

return ptmp;
}

contact_list_st* ContactMgr::exactQuery(string& name, contact_list_st* plist)
{
contact_list_st* ptmp = plist;
while(NULL != ptmp)
{
  string s = ptmp->pCont->getName();
  if(s.compare(name) == 0)
  {
   break;
  }
  ptmp = ptmp->pNext;
}

return ptmp;
}

void ContactMgr::Edit()
{
string name;
cout << "Input the contact person's name you want to edit: " << endl;
cin >> name;


contact_list_st* ptmp;
if ((ptmp = exactQuery(name, pContactList)) != NULL)
{
  cout << "Input new contact: " << endl;
  string s;
  cout << "Sex: ";
  cin >> s;
  ptmp->pCont->setSex(s);

   
  cout << "Address: ";
  cin >> s;
  ptmp->pCont->setAddress(s);

  cout << "ID: ";
  cin >> s;
     ptmp->pCont->setID(s);
 
 
 
  cout << "TelNumber: ";
  cin >> s;
  ptmp->pCont->setTelnumber(s);
 

  cout << "Email: ";
  cin >> s;
  ptmp->pCont->setE_mail(s);
 
 

  cout << "\n----------------------------------------" << endl;
  cout << "Edit successfully! " << endl;
  cout << "----------------------------------------\n" << endl;
}
else
{
        cout << "\n----------------------------------------" << endl;
  cout << "Contact person not found !" << endl;
  cout << "----------------------------------------\n" << endl;
}
}

void ContactMgr::Delete()
{
string name;
cout << "Input the contact person's name you want to edit: " << endl;
cin >> name;


contact_list_st* ptmp;
if ((ptmp = exactQuery(name, pContactList)) != NULL)
{
  if(NULL != ptmp->pNext)
  {
   ptmp->pNext->pPrev = ptmp->pPrev;
  }
  if(NULL != ptmp->pPrev)
  {
   ptmp->pPrev->pNext = ptmp->pNext;
  }
  if(pContactList == ptmp)
  {
   pContactList = ptmp->pNext;
  }
  if(pTail = ptmp)
  {
   if(NULL != ptmp->pPrev)
   {
    pTail = ptmp->pPrev;
   }
   else
   {
    pTail = pContactList;
   }
  }

  delete ptmp->pCont;
  delete ptmp;
  counter--;
  cout << "\n----------------------------------------" << endl;
  cout << "Delete successfully! " << endl;
  cout << "----------------------------------------\n" << endl;
}
else
{
        cout << "\n----------------------------------------" << endl;
  cout << "Contact person not found !" << endl;
  cout << "----------------------------------------\n" << endl;
}
}

void ContactMgr::DisplayAll()
{
system("cls");
cout << "\n\nThere are totally " << counter << " records in the Concacts! " << endl;
contact_list_st* ptmp = pContactList;
while(NULL != ptmp)
{
 
  cout << "\n----------------------------------------" << endl;
  single_Diplay(ptmp->pCont);
  cout << "----------------------------------------\n" << endl;
  ptmp = ptmp->pNext;
  system("pause");
  system("cls");

}
}

void ContactMgr::QueryRes()
{
string name;
cout << "Enter the name you want to Query: ";
cin >> name;

struct ST_CONTACT_FOUND_RESULT
{
  contact_list_st* pcon;
  ST_CONTACT_FOUND_RESULT* pNext;
};
ST_CONTACT_FOUND_RESULT* pcon_vec = NULL;
contact_list_st* ptmp = pContactList;
int num = 0;
while(NULL != ptmp)
{
  ptmp = Query(name, ptmp);
  if(NULL != ptmp)
  {
   num++;
   ST_CONTACT_FOUND_RESULT* pnew = new ST_CONTACT_FOUND_RESULT;
   if(NULL == pnew)
   {
    cout << "Apply memory failed" << endl;
    exit(1);
   }
   memset(pnew, 0, sizeof(ST_CONTACT_FOUND_RESULT));
   pnew->pcon = ptmp;
   if(NULL == pcon_vec)
   {
    pcon_vec = pnew;
   }
   else
   {
    ST_CONTACT_FOUND_RESULT* pt = pcon_vec;
    while(NULL != pt->pNext)
    {
     pt = pt->pNext;
    }
    pt->pNext = pnew;
   }
   ptmp = ptmp->pNext;
  }
 
}

cout << "\n" << num << " records found!" << endl;

ST_CONTACT_FOUND_RESULT* pt = pcon_vec;
while(NULL != pt)
{
  cout << "----------------------------------------" << endl;
  single_Diplay(pt->pcon->pCont);
  cout << "----------------------------------------\n" << endl;
  ST_CONTACT_FOUND_RESULT* pdel = pt;
  pt = pt->pNext;
  delete pdel;
}
}

int main()
{
ContactMgr mgr;
cout << "***************************************" << endl;
cout << "* Welcome to the easy Contact Manager!*" << endl;
cout << "***************************************" << endl;


bool flag = true;
while (flag)
{
  cout << "\n\nenter the action U want to take";
  cout << "\n'I' for inser, \n'E' for edit, \n'D' for delete,\n'Q' for query, \n'S' for show all, \nothers for out(exit):" ;
 
  char action;
  cin >> action;
  switch (action)
  {
  case ('I'):
  case ('i'): mgr.Insert();break;
  
  case ('E'):
  case ('e'): mgr.Edit();break;
  
  case ('D'):
  case ('d'): mgr.Delete();break;
  
  case ('Q'):
  case ('q'): mgr.QueryRes();break;
  
  case ('S'):
  case ('s'): mgr.DisplayAll();break;
  
  default:
   flag = false;break;
  }
       
}
return 0;
}
[wangjy17908]
添加时间:2010-07-29
版权所有(C)2005-2015