Address.h

[php] #ifndef ADDRESS_H #define ADDRESS_H #include #include #include #include using namespace std;

class Address { public: Address() {Status = true;} string GetName() const; string GetPhone() const; bool isEmpty(); void Enter(string &name, string &phone); void Set(); void Clear(); private: bool Status; char Name[31]; char Phone[21]; };

#endif [/php]

AddressBook.h

[php] #ifndef ADDRESSBOOK_H #define ADDRESSBOOK_H #include “Address.h” #include #include #include #include using namespace std; const int ItemNum = 100;

class AddressBook { public: AddressBook() {Num = 0;} void Enter(); void Erase(); void Load(); void Save(); void List(); void SearchByName(); //按姓名模糊查询 void SearchByPhone(); //按电话模糊查询 void ChangeByName(); //按姓名修改号码 private: Address Item[ItemNum]; int Num; int FindFree(); };

#endif [/php]

Address.cpp

[php] #include “Address.h” #include #include #include #include using namespace std;

string Address::GetName() const { string s = Name; return s; }

string Address::GetPhone() const { string s = Phone; return s; }

bool Address::isEmpty() {return Status;}

void Address::Enter(string &name, string &phone) { int len = name.length(); if(len > 30) len = 30; name.copy(Name, len, 0);

Name[len] = 0;

len = phone.length();
if(len > 20)
	len = 20;
phone.copy(Phone, len, 0);

Phone[len] = 0; }

void Address::Set() {Status = false;} void Address::Clear() {Status = true;} [/php]

AddressBook.cpp

[php] #include “Address.h” #include “AddressBook.h” #include using namespace std;

ostream &operator « (ostream &output, const Address &addr) { output « setiosflags(ios::left) « setw(31) « addr.GetName() « setw(21) « addr.GetPhone(); return output; }

int AddressBook::FindFree() { int t; for(t = 0; t < ItemNum; t++) if(Item[t].isEmpty()) return t; return -1; }

void AddressBook::Enter() { string name, phone;

int pointer;
pointer = FindFree();
if(pointer == -1)
	cout << "电话簿已记满";
else
{
	if(Num == 0)
		cout << "\n电话簿为空!" << endl;
	else
		cout << "\n电话簿中有" << Num << "条通信录" << endl;
	cout << "\n***************添加联系人*****************\n\n";
	cout << "输入姓名:";
	cin >> name;
	cout << "输入电话号码:";
	cin >> phone;

	Item[pointer].Enter(name, phone);
	Item[pointer].Set();

	cout << "\n已添加如下联系人:\n" << endl;
	cout << "姓名:" << name << endl;
	cout << "号码:" << phone << endl;

	Num++;
} }

void AddressBook::Erase() { int t; string name;

if(Num == 0)
	cout << "\n电话簿为空!" << endl;
else
	cout << "\n电话簿中有" << Num << "条通信录" << endl;

if(!Num)
	cout << "电话簿已空!\n";
else
{
	cout << "\n***************删除联系人*****************\n\n";
	cout << "输入姓名:";
	cin >> name;

	for(t = 0; t < ItemNum; t++)
	{
		if(!Item[t].isEmpty() && Item[t].GetName() == name)
		{
			Item[t].Clear();
			Num--;
			cout << "已经删除" << name << "的通信录。\n";
			break;
		}
	}

	if(t == ItemNum)
	{
		cout << "该名字不存在\n";
		cin.get();
	}
} }

void AddressBook::List() { int t, i, j; Address list[ItemNum], temp;

if(!Num)
	cout << "电话簿已空!" << endl;
else
{
	if(Num == 0)
		cout << "\n电话簿为空!" << endl;
	else
		cout << "\n电话簿中有" << Num << "条通信录" << endl;
	for(t = 0; t < ItemNum; t++)
		list[t] = Item[t];

	for(i = 0; i < ItemNum; i++)
	{
		for(j = ItemNum - 1; j > i; j--)
			if(list[j - 1] .GetName() > list[j].GetName())
			{
				temp = list[j - 1];
				list[j - 1] = list[j];
				list[j] = temp;
			}
	}
	cout << "\n***************联系人列表*****************\n\n";
	cout << setiosflags(ios::left) << setw(31) << "姓名" << setw(21) << "电话" << endl;
	for(t = 0; t < ItemNum; t++)
	{
		if(!list[t].isEmpty())
			cout << list[t] << endl;
	}
} }

void AddressBook::Save() { ofstream file; int i; char fname[41];

cout << "\n***************导出联系人*****************\n\n";
cout << "输入保存的文件名:";
cin >> fname;

file.open(fname, ios::out);
if(!file)
{
	cout << "\n打开文件失败!\n";
	cin.get();
}
else
{
	for(i = 0; i < ItemNum; i++)
	{
		if(file && !file.eof())
			file.write(reinterpret_cast<const char *>(&Item[i]), sizeof(Address));
	}
	file.close();
} }

void AddressBook::Load() { ifstream file;

int i;
char fname[41];

cout << "\n***************导入联系人*****************\n\n";
cout << "输入读取的文件名:";
cin >> fname;

file.open(fname, ios::in);
if(!file)
{
	cout << "打开文件失败!\n";
	cin.get();
}
else
{
	Num = 0;
	for(i = 0; i < ItemNum; i++)
	{
		if(file && !file.eof())
			file.read(reinterpret_cast<char *> (&Item[i]), sizeof(Address));
		if(!Item[i].isEmpty())
			Num++;
	}
	file.close();
} }

void AddressBook::SearchByName() { int num_sim = 0; //模糊找到的名字数 int t, i; Address list[ItemNum], temp; string name; cout « “\n****查找联系人******\n\n”; cout « “输入要查找姓名的关键字: “; cin » name;

if(!Num)
	cout << "电话簿已空!" << endl;
else
{
	for(t = 0; t < ItemNum; t++)
		list[t] = Item[t];

	for(i = 0; i < ItemNum; i++)
	{
		if(Item[i].GetName().find(name) != string::npos)
		{
			cout << "姓名:" << Item[i].GetName() << endl;
			cout << "号码:" << Item[i].GetPhone() << endl;
			num_sim++;
		}
	}
	if(num_sim == 0)
		cout << "没有找到!" << endl;
} }

void AddressBook::SearchByPhone() { int num_sim = 0; //模糊找到的名字数 int t, i; Address list[ItemNum], temp; string phone; cout « “\n****查找联系人******\n\n”; cout « “输入要查找的号码: “; cin » phone;

if(!Num)
	cout << "电话簿已空!" << endl;
else
{
	for(t = 0; t < ItemNum; t++)
		list[t] = Item[t];

	for(i = 0; i < ItemNum; i++)
	{
		if(Item[i].GetPhone().find(phone) != string::npos)
		{
			cout << "姓名:" << Item[i].GetName() << endl;
			cout << "号码:" << Item[i].GetPhone() << endl;
			num_sim++;
		}
	}
	if(num_sim == 0)
		cout << "没有找到!" << endl;
} }

void AddressBook::ChangeByName() { int t; string name, phone;

if(!Num)
	cout << "电话簿已空!" << endl;
else
{
	cout << "\n***************修改联系人*****************\n\n";
	cout << "输入要修改的姓名: ";
	cin >> name;
	cout << "输入号码: ";
	cin >> phone;

	for(t = 0; t < ItemNum; t++)
	{
		if(!Item[t].isEmpty() && Item[t].GetName() == name)
		{
			Item[t].Enter(name, phone);
			cout << "已经修改" << name << "的通信录。\n";
			cout << "修改后的信息:\n姓名:" << Item[t].GetName() << endl;
			cout << "号码:" << Item[t].GetPhone() << endl;
			break;
		}
		else if(Item[t].GetName().find(name) != string::npos)
		{
			string choice = "n";	//是否要修改
			cout << "是否要修改如下联系人信息:\n姓名:" << Item[t].GetName() << '\t';
			cout << "号码:" << Item[t].GetPhone() << endl << "(y/n):";
			cin >> choice;
			if(choice == "y" || choice == "yes" || choice == "Y" || choice == "YES")
			{
				Item[t].Enter(name, phone);
				cout << "已经修改" << name << "的通信录。\n";
				cout << "修改后的信息:\n姓名:" << Item[t].GetName() << endl;
				cout << "号码:" << Item[t].GetPhone() << endl;
				break;
			}
		}
	}

	if(t == ItemNum)
	{
		cout << "该名字不存在\n";
		cin.get();
	}
} } [/php]

main.cpp

[php] #include “Address.h” #include “AddressBook.h” #include #include #include #include using namespace std;

int menu_select();

int main() { char choice; string c; AddressBook maillist;

for(;;)
{
	choice = menu_select();

	switch(choice)
	{
	case 1:
		maillist.Enter();
		while(cout << "是否要继续添加?(y/n): " && cin >> c &&
			c == "y" || c == "yes" || c == "Y" || c == "YES")
		{
			maillist.Enter();
		}
		break;
	case 2:
		maillist.Erase();
		while(cout << "是否要继续删除?(y/n): " && cin >> c &&
			c == "y" || c == "yes" || c == "Y" || c == "YES")
		{
			maillist.Erase();
		}
		break;
	case 3:
		maillist.List();
		break;
	case 4:
		maillist.Save();
		break;
	case 5:
		maillist.Load();
		break;
	case 6:
		maillist.SearchByName();
		while(cout << "是否要继续查找?(y/n): " && cin >> c &&
			c == "y" || c == "yes" || c == "Y" || c == "YES")
		{
			maillist.SearchByName();
		}
		break;
	case 7:
		maillist.SearchByPhone();
		while(cout << "是否要继续查找?(y/n): " && cin >> c &&
			c == "y" || c == "yes" || c == "Y" || c == "YES")
		{
			maillist.SearchByPhone();
		}
		break;
	case 8:
		maillist.ChangeByName();
		while(cout << "是否要继续修改?(y/n): " && cin >> c &&
			c == "y" || c == "yes" || c == "Y" || c == "YES")
		{
			maillist.ChangeByName();
		}
		break;
	case 9:
		exit(0);
	}
}
return 0; }

int menu_select() { char c; cout « “**************\n”; cout « ” 模拟电话簿 v1.0 \n”; cout « ” \n”; cout « ” 1. 添加通信录 \n”; cout « ” 2. 删除通信录 \n”; cout « ” 3. 显示通信录 \n”; cout « ” 4. 电话簿存盘 \n”; cout « ” 5. 读出电话簿 \n”; cout « ” 6. 按名字查找 \n”; cout « ” 7. 按号码查找 \n”; cout « ” 8. 按姓名修改 \n”; cout « ” 9. 退出 \n”; cout « “**************\n”; cout « “\n请输入(1 ~ 9): “;

do
{
	cin.get(c);
}while(c < '1' || c > '9');

return c - 48; } [/php]

增加以下功能:

  • 按名模糊查询

  • 按电话模糊查询

  • 修改功能,根据姓名修改电话号码