简易通讯录类实现原理

#include <bits/stdc++.h>
using namespace  std;
class Informat
{
private:
    string phone;
    string address;
    string name;
public:
    Informat():name(""),phone("12345"),address("Anywhere"){}
    Informat(string a,string b,string q):name(a),address(q){ setpho(b);}
    void setpho(string a)
    {
        while(a.length()!=11&&a.length()!=5){
            cout<<"Please Key Again"<<endl;
            cin>>a;
        }
        phone=a;
    }
    void setname(string a){ name=a;}
    void setaddr(string a){address=a;}
    string getpho() const{return phone;}
    string getname()const{return name;}
    string getaddr()const{return address;}
    friend ostream&operator<<(ostream &out,const Informat &ob);
    friend istream&operator>>(istream &in, Informat &ob);
};
ostream&operator<<(ostream &out,const Informat &ob){
    out<<ob.name<<" "<<ob.phone<<" "<<ob.address<<" ";
    return out;
}
istream &operator>>(istream &in,Informat &ob)
{
    in>>ob.name>>ob.phone;
    while(ob.phone.length()!=11&&ob.phone.length()!=5)
    {
            cout<<"Please Key Again"<<endl;
            in>>ob.phone;
    }
    in>>ob.address;
    return in;
}
/*int main()
{
    Informat demo("李彦宏","1","中国北京");
    cout<<demo<<endl;
    Informat demo2;
    string tem1,tem2,tem3;
   cin>>demo;
   cout<<demo.getname()<<' '<<demo.getpho()<<' '<<demo.getaddr()<<endl;
   cin>>tem1>>tem2>>tem3;
    demo2.setname(tem1);
    demo2.setpho(tem2);
    demo2.setaddr(tem3);
    cout<<demo2<<endl;;
}*/
class Contacts
{
    vector<Informat>infor;
    map<string,int>na;
    map<string,int>ph;
public:
    Contacts()
    {
            load();
    }
    void create()
    {
        Informat tem;
        cin>>tem;
        infor.push_back(tem);
        if(!na.insert(make_pair(tem.getname(),infor.size()-1)).second)
        {
            cout<<infor[na.find(tem.getname())->second]<<endl;
            cout<<"Please Key \"Yes\" To Creat name(2) Or Key\"No\"  Update and overwrite "<<endl;
            string flag;
            cin>>flag;
            while(flag!="NO"&&flag!="No"&&flag!="no"&&flag!="YES"&&flag!="yes"&&flag!="Yes") cin>>flag;
            if(flag=="NO"||flag=="No"||flag=="no")
            {
                na.erase(tem.getname());
                na.insert(make_pair(tem.getname(),infor.size()-1));
            }
            else {
                tem.setname(tem.getname()+" ");
                na.insert(make_pair(tem.getname(),infor.size()-1));
            }
        }
        if(!ph.insert(make_pair(tem.getpho(),infor.size()-1)).second)
        {
            cout<<infor[ph.find(tem.getpho())->second]<<endl;
            cout<<"Please Key \"Yes\" To Creat Phone(2) Or Key\"No\"  Update and overwrite "<<endl;
            string flag;
            cin>>flag;
            while(flag!="NO"&&flag!="No"&&flag!="no"&&flag!="YES"&&flag!="yes"&&flag!="Yes") cin>>flag;
            if(flag=="NO"||flag=="No"||flag=="no")
            {
                na.erase(tem.getpho());
                na.insert(make_pair(tem.getpho(),infor.size()-1));
            }
            else {
                tem.setpho(tem.getpho()+"2");
                na.insert(make_pair(tem.getpho(),infor.size()-1));
            }
        }
    }
    void find1(string a)
    {
        if(ph.find(a)!=ph.end()&&infor[ph.find(a)->second].getpho()!="Null"){
            cout<<infor[ph.find(a)->second]<<endl;
            if(na.find(a)!=na.end()&&infor[na.find(a)->second].getname()!="Null"){
                cout<<infor[na.find(a)->second]<<endl;
                return ;
            }
        }
      else  if(na.find(a)!=na.end()&&infor[na.find(a)->second].getname()!="Null"){
            cout<<infor[na.find(a)->second]<<endl;
        }
        else cout<<"查无此人/号"<<endl;
    }
    void modifyaddr(string a )
    {
        string w;
        if(ph.find(a)!=ph.end()&&infor[ph.find(a)->second].getpho()!="Null"){
        	cout<<infor[ph.find(a)->second]<<endl;
           	cin>>w;
           	{
               if(w=="No"||w=="no"||w=="NO")
			   	if(na.find(a)!=na.end()&&infor[na.find(a)->second].getname()!="Null"){
        			cout<<infor[na.find(a)->second]<<endl;
        			cin>>w;
					infor[na.find(a)->second].setaddr(w);
					return ;
				}
               else {
                    infor[na.find(a)->second].setaddr(w);
                    return ;
               }
          	 }
        }
        if(na.find(a)!=na.end()&&infor[na.find(a)->second].getname()!="Null"){
			cout<<infor[na.find(a)->second]<<endl;
			cin>>w;
			infor[na.find(a)->second].setaddr(w);
			return ;
		}
        else cout<<"查无此人"<<endl;
    }
    void modifyname(string a )
    {
        string w;
        if(na.find(a)!=na.end()&&infor[na.find(a)->second].getname()!="Null"){
        cout<<infor[na.find(a)->second]<<endl;
        cin>>w;
        infor[na.find(a)->second].setname(w);
        na.insert(make_pair(w,na.find(a)->second));
        na.erase(a);
        }
        else cout<<"查无此人"<<endl;
    }
    void modifyphone(string a )
    {
        string w;
        if(ph.find(a)!=ph.end()&&infor[ph.find(a)->second].getpho()!="Null"){
        cout<<infor[ph.find(a)->second]<<endl;
        cin>>w;
        infor[ph.find(a)->second].setpho(w);
        ph.insert(make_pair(w,ph.find(a)->second));
        ph.erase(a);
        }
        else cout<<"查无此号"<<endl;
    }
    void delph(string a)
    {
         if(ph.find(a)!=ph.end()&&infor[ph.find(a)->second].getpho()!="Null")
         {
              infor[ph.find(a)->second].setpho("Null");
              infor[ph.find(a)->second].setname("Null");
         }
          else cout<<"查无此号"<<endl;
    }
    void delna(string a)
    {
         if(na.find(a)!=na.end()&&infor[na.find(a)->second].getname()!="Null")
         {
              infor[na.find(a)->second].setpho("Null");
              infor[na.find(a)->second].setname("Null");
         }
          else cout<<"查无此人"<<endl;
    }
    ~ Contacts()
    {
            save();
    }
    void load()
    {
        ifstream in("d:\\shuju.txt",ios::in);
        infor.clear();
        na.clear();
        ph.clear();
        if(!in)  return ;
        Informat ob;
        while(in>>ob)
        {
            infor.push_back(ob);
            na.insert(make_pair(ob.getname(),infor.size()-1));
            ph.insert(make_pair(ob.getpho(),infor.size()-1));
            in.close();
        }
    }
    void save()
    {
        ofstream out("d:\\shuju.txt",ios::out);
        for(auto po=infor.begin();po!=infor.end();po++)
        {
            if((*po).getname()!="Null")      out<<*po;
        }
        out.close();
    }
};
int main()
{
        Contacts ob;
        ob.create();
        ob.create();
        ob.create();
        string tem;
        cin>>tem;
        ob.find1(tem);
        cin>>tem;
        ob.find1(tem);
        cin>>tem;
      ob.  modifyaddr(tem);
        ob.find1(tem);
        cin>>tem;
      ob.  modifyaddr(tem);
        ob.find1(tem);
        cin>>tem;
       ob. modifyname(tem);
        ob.find1(tem);
        cin>>tem;
      ob.  modifyname(tem);
        ob.find1(tem);
        cin>>tem;
       ob. modifyphone(tem);
        ob.find1(tem);
        cin>>tem;
        ob. modifyphone(tem);
        ob.find1(tem);
        cin>>tem;
        ob. delph(tem);
        ob.find1(tem);
        cin>>tem;
        ob.  delph(tem);
        ob.find1(tem);
        cin>>tem;
        ob.  delna(tem);
        ob.find1(tem);
        cin>>tem;
        ob.delna(tem);
        ob.find1(tem);
}
上一篇:1.3 Starting a New Job 1.3.4 Sample Test(III)


下一篇:python_005_while循环,列表的高级操作,生成器的概念,字典