实验内容
代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<fstream>
using namespace std;
class CommEntry{
public:
void inputcommEntry();
void inputcommEntryfile(istream &in);
void outputcommEntry();
void outputcommEntryfile(ostream &out);
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 inputAllfile(istream &in);
void outputAll();
void outputAllfile(ostream &out);
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 << " 5.输出到文件" << endl;
cout << " 6.从文件中读取数据" << endl;
cout << " 0.退出系统" << endl;
cout << "----------------------------------" << endl;
cout << "请输入你要选择的功能编号(0-6): ";
while(cin >> op)
{
if(op>=0&&op<=6)
break;
else
cout << "输入出错,请重新输入你要选择的功能编号(0-6): ";
}
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);
}
void CommEntry::inputcommEntryfile(istream &in)
{
string temp_name,temp_tel,temp_addr;
in >> temp_name >> temp_tel >> temp_addr;
update_name(temp_name);
update_tel(temp_tel);
update_addr(temp_addr);
}
void CommEntry::outputcommEntryfile(ostream &out)
{
out << "姓名: " << get_name() << " 电话: " << get_tel() << " 地址: " << get_addr() << endl;
}
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();
}
}
void Comms::inputAllfile(istream &in)
{
for(int i=0;i<Count;i++)
{
get_CommEntry(i).inputcommEntryfile(in);
}
}
void Comms::outputAllfile(ostream &out)
{
for(int i=0;i<Count;i++)
{
out << "***第" << i << "个通讯录条目***" << endl;
get_CommEntry(i).outputcommEntryfile(out);
}
}
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==5)
{
ofstream file1;
file1.open("record.txt");
data.outputAllfile(file1);
file1.close();
}
else if(op==6)
{
ifstream file1;
string file_name;
cout << "请输入读入文件名: ";
cin >> file_name;
file1.open(file_name);
data.inputAllfile(file1);
}
else if(op==0)
break;
}
return 0;
}