//電話本
//by lecky
//2006-2-15
#include<iOStream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
class person
{
private:
string name,sex,age,number;
public:
int setname(string s)
{
name=s;
return 0;
}
int setsex(string s)
{
sex=s;
return 0;
}
int setage(string s)
{
age=s;
return 0;
}
int setnumber(string s)
{
number=s;
return 0;
}
string getname()
{
return name;
}
string getsex()
{
return sex;
}
string getage()
{
return age;
}
string getnumber()
{
return number;
}
bool Operator==(const person &p)
//操作符重載,用於按姓名查找的時候判定對象的相等條件
{
if(name==p.name)
return true;
return false;
}
};
char s=’0’;
vector<person> note;
vector<person>::iterator it//迭代器;
person temp;
string name,sex,age,number,str;
int add()//添加聯系人函數
{
char q;
do
{
cout << "請輸入姓名:";
cin >> str;
temp.setname (str);
cout << endl;
cout << "請輸入性別:";
cin >> sex;
temp.setsex (sex);
cout << endl;
cout << "請輸入年齡:";
cin >> age;
temp.setage(age);
cout << endl;
cout << "請輸入號碼:";
cin >> number;
temp.setnumber (number);
cout << endl;
note.push_back (temp);
//cout<<note[0].getname()<<endl;
cout << "繼續輸入嗎?[Y/N]";
cin >> q;
}while((q!=’n’)&&(q!=’N’));
return 0;
}
int update(string sp)//查找,編輯,刪除函數
{
char q;
temp.setname(sp);
//cout << note[0].getname() << endl;
it=find(note.begin (),note.end (),temp);
if(it!=note.end())
{
cout << "姓名:" << it->getname() << endl;
cout << "性別:" << it->getsex() << endl;
cout << "年齡:" << it->getage() << endl;
cout << "號碼:" << it->getnumber() << endl;
}
else {cout << "查無此人" << endl;}
if(s==’3’)
{
cout << "請輸入要編輯的信息,若不修改請輸n" << endl;
cout << "姓名";
cin >> name;
cout << endl;
cout << "姓別";
cin >> sex;
cout << endl;
cout << "年齡";
cin >> age;
cout << "號碼";
cin >> number;
cout << endl;
if ((name!="n")&&(name!="N"))
it->setname(name);
if ((sex!="n")&&(sex!="N"))
it->setsex(sex);
if ((age!="n")&&(age!="N"))
it->setage(age);
if ((number!="n")&&(number!="N"))
it->setnumber(name);
}
if(s==’4’)
{
cout << "你確定要刪除?[Y/N]" ;
cin >> q;
if((q!=’n’)&&(q!=’N’))
{
note.erase(it);
}
}
return 0;
}
int main()
{
string want;
//string str;
do
{
cout << "電話本 24班 李仕翰" << endl;
cout << "[1] 添加聯系人" << endl;
cout << "[2] 查找聯系人" << endl;
cout << "[3] 編輯聯系人" << endl;
cout << "[4] 刪除聯系人" << endl;
cout << "[5] 退出電話本" << endl;
cout << "請選擇[1-5]";
cin >> s;
switch (s)
{
case ’1’:
add();
break;
case ’2’:
cout << "請輸入要查找的聯系人姓名:";
cin >> want;
update(want);
break;
case ’3’:
cout << "請輸入要編輯的聯系人姓名:";
cin >> want;
update(want);
break;
case ’4’:
cout << "請輸入要刪除的聯系人姓名:";
cin >> want;
update(want);
break;
case ’5’:
cout << "再見啦!我一路向北,離開有你的氣味..." << endl;
break;
default:
cout << "請輸入1-5之間的數字!" << endl;
}
}while(s!=’5’);
return 0;
}