/*简易电话薄管理系统
本程序纯属应付期末验收。
程序一共两个类:
Contact_person 描述电话薄中的联系人信息
System 系统程序,包含新建和查找这两个功能
其中查找到联系人后,可进行删除和修改
没有写文件操作,这样每次都必须重新新建联系人
读者可自行添加文件操作,已提供count记录电话薄中联系人的个数
以及flag标记,方便文件的读入及重写
程序几乎没有容错能力,不能处理非法输入
不能处理联系人姓名相同的情况*/
#include <cstdio>
#include <string>
#include <iostream>
#include <map>
using namespace std;
map <string,int> system_map;
class Contact_person{
private:
int ID;//该联系人的数字编号
bool flag;//标记该联系人是否已经被删除
string name,tel,remark;//remark为备注
public:
void assign(int id,string n,string t,string r){
ID = id;
name = n;
tel = t;
remark = r;
}
void print(){
cout << "Name :" << name << endl;
cout << "Tel :" << tel << endl;
cout << "Remark:" << remark << endl;
}
void delate(){
flag = 0;
}
};
Contact_person p[1000];
class System{
private:
int count;//记录目前电话薄中有多少联系人
void show_sys(){
cout << "***********************************" << endl;
cout << "1 . 新建联系人" << endl;
cout << "2 . 查找联系人" << endl;
cout << "3 . 退出" << endl ;
cout << " 请输入指令:" << endl;
cout << "***********************************" << endl;
}
void show_search(){
cout << "***********************" << endl;
cout << "1 . 修改该联系人" << endl;
cout << "2 . 删除该联系人" << endl;
cout << "3 . 退出" << endl;
cout << "请输入指令" << endl;
cout << "***********************" << endl;
}
void build(){
string name,tel,remark;
cout << "请输入联系人姓名" << endl;
cin >> name;
cout << "请输入电话" << endl;
cin >> tel;
cout << "请输入备注"<< endl;
cin >> remark;
p[count].assign(count,name,tel,remark);
cout << "\n新建成功!\n" << endl;
p[count].print();
system_map[name] = count;
count++;
return;
}
void search(string s){
int tag;
while(1){
int id;
if(system_map.count(s) != 0){
id = system_map[s];
cout << "\n查找成功!\n" << endl;
p[id].print();
}
else{
cout << "\n抱歉,您要查找的联系人不存在,请重新选择指令\n" << endl;
return;
}
show_search();
cin >> tag;
if(tag == 1){
string name,tel,remark;
cout << "请输入联系人姓名" << endl;
cin >> name;
cout << "请输入电话" << endl;
cin >> tel;
cout << "请输入备注"<< endl;
cin >> remark;
p[id].assign(id,name,tel,remark);
cout << "\n修改成功!\n" << endl;
p[id].print();
return;
}
else if(tag == 2){
system_map.erase(s);
p[id].delate();
cout << "\n删除成功!" << endl;
return;
}
else if(tag == 3){
return;
}
else{
cout << "输入错误,请重新输入" << endl;
}
}
}
void command(){
int tag;
while(1){
show_sys();
cin >> tag;
if(tag == 1){
build();
}
else if(tag == 2){
string text;
cout << "请输入姓名:" << endl;
cin >> text;
search(text);
}
else if(tag == 3){
return;
}
else
cout << "输入错误,请重新输入" << endl;
}
}
public:
System(int c){
count = c;
command();
}
};
int main(){
System s(0);
return 0;
}