题目概要
问题描述: 通讯录是由通讯录条目(不包括朋友条目,以后再考虑在通讯录中加入朋友类条目)组成的.通讯录总条目数,通讯录中现有多少条.可以输入通讯录、可以输出通讯录、可以查找、修改.
实验目的: 学习使用类与类之间的组合关系.
与之前实验的关系: 以第二个实验,通讯录条目程序为基础.
题目要求
$(1)$ 在原来通讯录条目的基础之上添加一个类$Comms$,$CommEntry$是$Comms$的数据成员.$Comms$的声明与定义可以使用单独的$.h$和$.cpp$文件;
$(2)$ 通迅录中有多个条目,所以$CommEntry$是以数组的形式出现在$Comms$中,为了能灵活处理通讯录条目的个数,可以指针的方式进行声明,然后动态的申请通讯录条目的个数,比如可以考虑在$Comms$的构造函数中进行内存的申请,在其析构函数中进行内存的回收.
$(3)$ 在$Comms$中定义两个整数: $MaxCount$, $count$,分别用了表示通讯录中条目的最多个数与已经存在的个数.
$(4)$ 在$Comms$中声明如下函数:
- $void$ $inputAll()$: 用于输入通迅录中的通讯录条目.注意在此函数中如何使用条目类的$input()$函数。
- $void$ $outputAll()$: 用于输出通讯录条目中的通讯录条目.注意在此函数中如何使用条目类的$input()$函数。
- $int$ $find(string$ $nm)$: 用于在通讯录中按姓名查找一个通讯录条目在通讯录条目在通讯录中的位置,其返回值是找到的条目在数组中一下标位置.此函数主体可以用$for$循环来完成,供参考代码如下:
- $void$ $modify(stirng$ $nm,$ $string$ $t)$: 给定一个姓名,一个电话,修改通讯录中此姓名对应的条目的电话为指定的电话.注意此函数与$find$函数的关系.
int Comms::find(string nm){ int i; i = 0; for (i=0;i<count;i++){ if ((pCe[i]).getName()==nm){ break; } } return i; }$(5)$ $Comms,CommEntry$之间的关系是组合与被组合的关系,可以用以下$UML$图来表示:

$(6)$ 程序主菜单如下,主控程序的结构与通讯录条目程序的结构类似.
$1.$ 输入通讯录
$2.$ 输出通讯录
$3.$ 查找姓名
$4.$ 修改(输入姓名,修改其电话)
$0.$ 退出
代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
class CommEntry{
public:
//int displaymenu();
void inputcommEntry();
void outputcommEntry();
void update_name(string change_name);
void update_tel(string change_tel);
void update_addr(string change_addr);
string get_name();
string get_tel();
string get_addr();
private:
string name;
string tel;
string addr;
};
class Comms{
public:
Comms(int num,int maxnum=50)
{
maxCount=maxnum;
Count=num;
pCe=new CommEntry[num];
}
~Comms()
{
maxCount=Count=0;
delete []pCe;
}
int displaymenu();
void inputAll();
void outputAll();
CommEntry& get_CommEntry(int inum);
int find(string nm);
void modify(string nm,string t);
private:
CommEntry *pCe;
int maxCount;
int Count;
};
int Comms::displaymenu()
{
int op;
cout << "-------------功能菜单-------------" << endl;
cout << " 1.输入通讯录" << endl;
cout << " 2.输出通讯录" << endl;
cout << " 3.查找通讯录姓名" << endl;
cout << " 4.修改通讯录指定名字的电话" << endl;
cout << " 0.退出系统" << endl;
cout << "----------------------------------" << endl;
cout << "请输入你要选择的功能编号(0-4): ";
while(cin >> op)
{
if(op>=0&&op<=4)
break;
else
cout << "输入出错,请重新输入你要选择的功能编号(0-4): ";
}
return op;
}
void CommEntry::inputcommEntry()
{
string temp_name,temp_tel,temp_addr;
cout << "请输入名字: " ;
cin >> temp_name;
cout << "请输入电话: " ;
cin >> temp_tel;
cout << "请输入地址: " ;
cin >> temp_addr;
update_name(temp_name);
update_tel(temp_tel);
update_addr(temp_addr);
}
CommEntry& Comms::get_CommEntry(int inum)
{
return pCe[inum];
}
int Comms::find(string nm)
{
int flag=0;
for(int i=0;i<Count;i++)
if(get_CommEntry(i).get_name()==nm)
{
flag=1;
return i;
}
return -1;//找不到该姓名
}
void Comms::modify(string nm,string t)
{
int pos=find(nm);
if(pos!=-1)
get_CommEntry(pos).update_tel(t);
else cout << "查无此人,无法修改!" << endl;
}
void Comms::inputAll()
{
for(int i=0;i<Count;i++)
{
cout << "***第" << i << "个通讯录条目***" << endl;
get_CommEntry(i).inputcommEntry();
}
}
void Comms::outputAll()
{
for(int i=0;i<Count;i++)
{
cout << "***第" << i << "个通讯录条目***" << endl;
get_CommEntry(i).outputcommEntry();
}
}
string CommEntry::get_name()
{
return name;
}
string CommEntry::get_tel()
{
return tel;
}
string CommEntry::get_addr()
{
return addr;
}
void CommEntry::outputcommEntry()
{
cout << "名字: " << get_name() << " " <<"电话: " << get_tel() << " " << "地址: " << get_addr() << endl;
}
void CommEntry::update_name(string change_name)
{
name=change_name;
}
void CommEntry::update_tel(string change_tel)
{
tel=change_tel;
}
void CommEntry::update_addr(string change_addr)
{
addr=change_addr;
}
int main()
{
int num;
cout << "请输入通讯录的总人数: " ;
cin >> num;
Comms data=Comms(num);
while(1)
{
int op=data.displaymenu();
if(op==1)
data.inputAll();
else if(op==2)
data.outputAll();
else if(op==3)
{
string temp_name;
cout << "请输入查找的姓名: " << endl;
cin >> temp_name;
int pos=data.find(temp_name);
if(pos!=-1)
data.get_CommEntry(pos).outputcommEntry();
else cout << "查无此人" << endl;
}
else if(op==4)
{
string temp_name,temp_tel;
cout << "请输入要修改的名字: " ;
cin >> temp_name;
cout << "请输入要修改的电话: ";
cin >> temp_tel;
data.modify(temp_name,temp_tel);
}
else if(op==0)
break;
}
return 0;
}