#include<fstream>
#include<algorithm>
#include<cstdio>
#include<conio.h>
#include"person.h"
using namespace std;
/*************************************************************************************/
void help()
{
cout << "******************************************************************" << endl;
cout << "* Introduction *" << endl;
cout << "* 这是一个通讯录软件,请通过命令控制软件,并根据向导完成数据 *" << endl;
cout << "* 注意:录入的数据保存在D:\\Adress Book.txt文件中,请勿修改 *" << endl;
cout << "* 如果录入相同的名称,新建的名册会将旧的覆盖 *" << endl;
cout << "* 名字中不能含有空格 *" << endl;
cout << "* 命令大小写通用 *" << endl;
cout << "* 只能用名称/号码搜索到的结果修改原数据 *" << endl;
cout << "* version 1.0 by GTX *" << endl;
cout << "******************************************************************" << endl;
cout << "请重新选择功能" << endl;
}
vector<int> stovec(string Get)
{
vector<int> v;
string::iterator p;
for(p = Get.begin();p != Get.end();p++)
v.push_back(*p - '0') ;
return v;
}
bool Get(map<string,person> &book,string &Name)
{
string GetWord;
vector<int> GetCode;
char get;
cout << "请输入联系人的姓名:";
cin >> GetWord;
cout << "请输入电话:";
Name = GetWord;
getchar();
while( (get = getchar()) != '\n')
{
if( !(get >= 48 && get <= 57) )
{
cout << "error";
GetCode.push_back(0);
book[Name].GetName_telephone(GetWord,GetCode);//创建一个带清空的
return false;
}
GetCode.push_back(get - '0');
}
book[Name].GetName_telephone(GetWord,GetCode);//得到电话和姓名
cout << "性别(M/F):";
cin >> get;
while( book[Name].GetSex(get) != true)//得到性别
{
cout << "性别(M/F):";
cin >> get;
}
GetWord.clear();
GetCode.clear();
cout << "请输入地址:";
cin >> GetWord;
cout << "请输入邮编:";
getchar();
while( (get = getchar()) != '\n')
{
if( !(get >= 48 && get <= 57) )
{
cout << "error" << endl;
return false;
}
GetCode.push_back(get - '0');
}
book[Name].Getaddress(GetWord);
book[Name].Getmailcode(GetCode);
GetWord.clear();
GetCode.clear();
cout << "请输入邮箱地址:";
cin >> GetWord;
cout << "请输入QQ号:";
getchar();
while( (get = getchar()) != '\n')
{
if( !(get >= 48 && get <= 57) )
{
cout << "error" << endl;
return false;
}
GetCode.push_back(get - '0');
}
book[Name].Getmailbox(GetWord);
book[Name].GetQQ(GetCode);
GetWord.clear();
cout << "请输入组别:";
cin >> GetWord;
book[Name].Getgroup(GetWord);
return true;
}
bool Get(map<string,person> &book,ifstream &infile)//从文件获取通讯信息的函数,得到NAME,遍历MAP,不存在就装进MAP
{
char onoff;
infile >> onoff;
if(onoff != 'N')
return false;
infile.seekg(0);
size_t pos;
string Getword;
string item;
string Name;
while( infile >> Getword )
{
if( Getword[0] == '*')
continue;
pos = Getword.find( ":" );
item = Getword.substr(0,pos);
Getword = Getword.substr(pos + 1);
if( item == "Name" )
{
Name = Getword;//改进方向:检测已有的MAP
}
else if( item == "PhoneNumber" )
{
vector<int> phonenumber = stovec(Getword);
book[Name].GetName_telephone(Name,phonenumber);
}
else if( item == "Sex" )
{
char sex = *(Getword.begin());
book[Name].GetSex(sex);
}
else if( item == "Adress" )
{
book[Name].Getaddress(Getword);
}
else if( item == "Postcode" )
{
vector<int> code = stovec(Getword);
book[Name].Getmailcode(code);
}
else if( item == "Mailbox" )
{
book[Name].Getmailbox(Getword);
}
else if( item == "QQ" )
{
vector<int> QQ = stovec(Getword);
book[Name].GetQQ(QQ);
}
else if( item == "Group" )
{
book[Name].Getgroup(Getword);
}
}
return true;
}
void s_change(char button,map<string,person> &book,map<string,person>::iterator &search)
{
switch(button)
{
case 'd':
{
remove("D:\\Address Book.txt");
ofstream outfile("D:\\Address Book.txt");
outfile.close();
book.erase(search);
map<string,person>::iterator p;
for(p = book.begin();p != book.end();p++)
(*p).second.Out();
break;
}
case 'D':
{
remove("D:\\Address Book.txt");
ofstream outfile("D:\\Address Book.txt");
outfile.close();
book.erase(search);
map<string,person>::iterator p;
for(p = book.begin();p != book.end();p++)
(*p).second.Out();
break;
}
case 's':
{
char button2;
string SGet,SGetnumber;
cout << "请选择要修改的部分:" << endl;
cout << "\t1.姓名/电话\t2.性别\t3.地址\t4.邮编\t5.邮箱\t6.QQ\t7.类别" << endl;
button2 = _getch();
if( (*search).second.rewise(button2,SGet,SGetnumber) == false)
{
book[SGet].GetName_telephone(SGet,stovec(SGetnumber));
(*search).second.personcopy(book,SGet);
book.erase(search);
book[SGet].OutScreen();
remove("D:\\Address Book.txt");
ofstream outfile("D:\\Address Book.txt");
outfile.close();
map<string,person>::iterator p;
for(p = book.begin();p != book.end();p++)
(*p).second.Out();
break;
}
else
{
remove("D:\\Address Book.txt");
ofstream outfile("D:\\Address Book.txt");
outfile.close();
map<string,person>::iterator p;
for(p = book.begin();p != book.end();p++)
(*p).second.Out();
break;
}
}
case 'S':
{
char button2;
string SGet,SGetnumber;
cout << "请选择要修改的部分:" << endl;
cout << "\t1.姓名/电话\t2.性别\t3.地址\t4.邮编\t5.邮箱\t6.QQ\t7.类别" << endl;
button2 = _getch();
if( (*search).second.rewise(button2,SGet,SGetnumber) == false)
{
book[SGet].GetName_telephone(SGet,stovec(SGetnumber));
(*search).second.personcopy(book,SGet);
book.erase(search);
book[SGet].OutScreen();
remove("D:\\Address Book.txt");
ofstream outfile("D:\\Address Book.txt");
outfile.close();
map<string,person>::iterator p;
for(p = book.begin();p != book.end();p++)
(*p).second.Out();
break;
}
else
{
remove("D:\\Address Book.txt");
ofstream outfile("D:\\Address Book.txt");
outfile.close();
map<string,person>::iterator p;
for(p = book.begin();p != book.end();p++)
(*p).second.Out();
break;
}
}
case 'q':break;
case 'Q':break;
default:break;
}
return;
}
/*************************************************************************************/
int main()
{
map<string,person> book;
char menu;
fstream filecheck("D:\\Address Book.txt");
if(!filecheck)
{
ofstream newfile;
newfile.open("D:\\Address Book.txt");
newfile.close();
}
cout << "功能选择:" << endl;
cout << "\tN:录入\tF:查询/修改\tP:全部显示\tH:帮助\tQ:退出" << endl;
menu = 0;
while( menu != 'q' && menu != 'Q')
{
menu = _getch();
if(menu == 'n' || menu == 'N')
{
string Name;
if( Get(book,Name) )
{
book[Name].Out();
cout << "请继续选择功能" << endl;
}
else
{
book.erase( book.find(Name) );//清空操作
cout << "请重新选择功能!" << endl;
}
}
else if(menu == 'f' || menu == 'F')
{
cout << "\tM:按姓名/电话查找\tA:按地址查找\tG:按组别查找" << endl;
menu = _getch();
if(menu == 'm' || menu == 'M')
{
ifstream infile("D:\\Address Book.txt");
if(!infile)
{
cout << "打开文件失败!" << endl;
return 1;
}
if( Get(book,infile) == true )
{
bool if_find = false;
bool nif_find = false;
char button;
string Name_phone;
map<string,person>::iterator search;
cout << "请输入姓名或