1. 程式人生 > >用類寫一個簡單的通訊錄

用類寫一個簡單的通訊錄

#include <iostream>
#include <string>
using namespace std;
class addressbook
{
private:
 string Name;
 string Num;
 string Address;
 string Phone;
  bool sex;
 int Age;
 addressbook *next;
public:
 addressbook()
 {
Name="no name";
Num="no num";
sex=wu;
Age=0;
Address="no address";
Phone="no phone";
next=NULL;
 }
 void setname()
 {
cout<<"輸入姓名"<<endl;
cin>>Name;
 } 
 string getname()
 {
return Name;
 }
 void setnum()
 {
cout<<"輸入編號"<<endl;
cin>>Num;
 } 
 string getnum()
 {
return Num;
 }
 void setsex()
 {
int x;cout<<"輸入0或1,0代表男性,1代表女性"<<endl;
cin>>x;

 }
 string getsex()
 {
return (sex==Man)?"Man":"Woman";
 }
 void setage()
 {
cout<<"輸入年齡"<<endl;
cin>>Age;
 }
 int getage()
 {
return Age;
 }
 void setaddress()
 {
cout<<"輸入地址"<<endl;
cin>>Address;
 }
 string getaddress()
 {
return Address;
 }
 void setphone()
 {
cout<<"輸入電話號碼"<<endl;cin>>Phone;
 }
 string getphone()
 {
return Phone;
 }
 void setnext(addressbook *people)
 {
next=people;
 }
 addressbook* getnext()
 {
return next;
 }


};
int main()
{
 addressbook *head=new addressbook();
 addressbook *st[999];
 int i; 
 int n;
 cout<<"請輸入指令 0,1,2,3,4,5"<<endl;
 cout<<"0、退出通訊錄系統"<<endl;
 cout<<"1、 錄入功能"; 
 cout<<"性別的輸入應是男或女";
 cout<<"輸入的年齡應在0~100之間"<<endl;
 cout<<"2、 增加功能"<<endl;
 cout<<"3、 刪除功能"<<endl;
 cout<<"4、 查詢功能"<<endl;
 cout<<"5、 修改功能"<<endl;
 cout<<"請輸入:"<<endl;
 cin>>n;
 while(!(0<=n&&n<=5)
 {
  cin>>n;
 }
 while(n)
 {
   switch (n)
   {
  case 1:
{
    int num;
    cout<<"請輸入需要錄入的學生人數"<<endl;
    cin>>num;
    
    for(i=0;i<num;i++)
{
     st[i]=new addressbook();
     st[i]->setname();
     st[i]->setage();
     st[i]->setnum();
     st[i]->setphone();
     st[i]->setaddress();
     st[i]->setsex();
     st[i]->setnext(head->getnext());
     head->setnext(st[i]);
    }
    break;
   }
  case 2:
{
    addressbook* p;
    p=new addressbook();
    p->setname();
    p->setage();
    p->setnum();
    p->setphone();
    p->setaddress();
    p->setsex();
    p->setnext(head->getnext());
    head->setnext(p);
    break;
      }
  case 3:{
     string number;
     cout<<"輸入要刪除的人的編號"<<endl;
     cin>>number;
     addressbook* p;
addressbook* q;
     p=head;
     if(head->getnext()==NULL) 
cout<<"通訊錄為空"<<endl;
     while(p->getnum()!=number)
{
      q=p;
      p=p->getnext();
     }
     if(p->getnext()==NULL) 
      cout<<"查無此人"<<endl;
     else
{
      q->setnext(p->getnext());
      p->~addressbook();
      cout<<"編號為"<<number<<"的人已經刪除"<<endl;
     }
     break; 
      }


  case 4:
 {
     string na;
     cout<<"請輸入姓名"<<endl;
     cin>>na;
     addressbook * p=head->getnext();
     while(p)
{
      if(p->getname()==na) break;
      p=p->getnext();
     }
     if(p==NULL) 
cout<<"查無此人"<<endl;
     else
{
      cout<<"修改姓名"<<endl;
      p->setname();
      cout<<"修改編號"<<endl;
      p->setnum();
      cout<<"修改年齡"<<endl;
      p->setage();
      cout<<"修改地址"<<endl;
      p->setaddress();
      cout<<"修改性別"<<endl;
      p->setsex();
      cout<<"修改電話號碼"<<endl;
      p->setphone();
      cout<<"修改完畢"<<endl;
     }
     break;
      }     
 }
 cout<<"請輸入指令 0,1,2,3,4,5"<<endl;
 cout<<"0、退出通訊錄系統"<<endl;
 cout<<"1、 錄入功能"; 
 cout<<"性別的輸入應是男或女";
 cout<<"輸入的年齡應在0~100之間"<<endl;
 cout<<"2、 增加功能"<<endl;
 cout<<"3、 刪除功能"<<endl;
 cout<<"4、 查詢功能"<<endl;
 cout<<"5、 修改功能"<<endl;
 cout<<"請輸入:"<<endl;
 cin>>n;
  }
}