题目概要
问题:在通讯录管理程序中,通讯录是由通讯录条目组成的.通讯录条目由姓名、多个电话(各种类型的电话)组成的。可以进行输入、输出、修改姓名、修改电话.
题目要求
- 多个电话及其类型可用字符串动态数组存储;
int telCount; string *tels; string *telType; - 用构造函数完成多个电话的初始化;
tels=new string[num]; telType=new string[num]; - 在析构函数中完成内存的回收
delete []tels; delete []telType; - 其它相应的函数也要做修改

代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
class CommEntry{
public:
CommEntry(int num)
{
telCount=num;
tels=new string[num];
telType=new string[num];
}
~CommEntry()
{
telCount=0;
delete []tels;
delete []telType;
}
int displaymenu();
void inputcommEntry();
void outputcommEntry();
void update_name(string change_name);
void update_tel(string change_tel,int inum);
void update_teltype(string change_teltype,int inum);
void update_addr(string change_addr);
private:
int telCount;
string name;
string *tels;
string *telType;
string addr;
string get_name();
string get_tel(int inum);
string get_teltype(int inum);
string get_addr();
};
int CommEntry::displaymenu()
{
int op;
cout << "-------------功能菜单-------------" << endl;
cout << " 1.输入通讯录条目" << endl;
cout << " 2.输出通讯录条目" << endl;
cout << " 3.修改通讯录条目姓名" << endl;
cout << " 4.修改通讯录条目电话" << endl;
cout << " 5.修改通讯录条目地址" << endl;
cout << " 0.退出系统" << endl;
cout << "----------------------------------" << endl;
cout << "请输入你要选择的功能编号(0-5): ";
while(cin >> op)
{
if(op>=0&&op<=5)
break;
else
cout << "输入出错,请重新输入你要选择的功能编号(0,5): ";
}
return op;
}
void CommEntry::inputcommEntry()
{
string temp_name,temp_tel,temp_addr,temp_teltype;
cout << "请输入该通讯录条目的名字: " ;
cin >> temp_name;
update_name(temp_name);
for(int i=0;i<telCount;i++)
{
printf("请输入该通讯录条目的第%d个电话和类型(下标从0开始,中间用空格隔开): ",i);
cin >> temp_tel >> temp_teltype;
update_tel(temp_tel,i);
update_teltype(temp_teltype,i);
}
cout << "请输入该通讯录条目的地址: " ;
cin >> temp_addr;
update_addr(temp_addr);
}
string CommEntry::get_name()
{
return name;
}
string CommEntry::get_tel(int inum)
{
return tels[inum];
}
string CommEntry::get_teltype(int inum)
{
return telType[inum];
}
string CommEntry::get_addr()
{
return addr;
}
void CommEntry::outputcommEntry()
{
cout << "请输入查看的通讯录条目查看的内容选项(1为名字,2为电话,3为地址,0为全部): ";
int op;
while(cin >> op)
{
if(op>=0&&op<=3)
break;
cout << "输入出错,请重新输入查看的通讯录条目查看的内容选项(1为名字,2为电话,3为地址,0为全部): ";
}
if(op==1||op==0)
{
string temp_name=get_name();
cout << "该通讯录条目的名字: " << temp_name << endl;
}
if(op==2||op==0)
{
for(int i=0;i<telCount;i++)
{
string temp_tel=get_tel(i),temp_type=get_teltype(i);
cout << "该通讯录条目的第" << i << "个电话: " << temp_tel << endl;
cout << "该通讯录条目的第" << i << "个电话类型: " << temp_type << endl;
}
}
if(op==3||op==0)
{
string temp_addr=get_addr();
cout << "该通讯录条目的地址: " << temp_addr << endl;
}
}
void CommEntry::update_name(string change_name)
{
name=change_name;
}
void CommEntry::update_tel(string change_tel,int inum)
{
tels[inum]=change_tel;
}
void CommEntry::update_teltype(string change_teltype,int inum)
{
telType[inum]=change_teltype;
}
void CommEntry::update_addr(string change_addr)
{
addr=change_addr;
}
int main()
{
int num;
cout << "请输入该通讯录条目的电话个数: " ;
cin >> num;
CommEntry data(num);
while(1)
{
int op=data.displaymenu();
if(op==1)
data.inputcommEntry();
else if(op==2)
data.outputcommEntry();
else if(op==3)
{
string temp_name;
cout << "请输入要修改的通讯录条目的名字: ";
cin >> temp_name;
data.update_name(temp_name);
}
else if(op==4)
{
int temp_num;
string temp_tel;
cout << "请输入要修改的通讯录条目第几个电话(下标从0开始): ";
cin >> temp_num;
cout << "请输入要修改的通讯录条目的电话: ";
cin >> temp_tel;
data.update_tel(temp_tel,temp_num);
}
else if(op==5)
{
string temp_addr;
cout << "请输入要修改的通讯录条目的地址: ";
cin >> temp_addr;
data.update_addr(temp_addr);
}
else if(op==0)
break;
}
return 0;
}