📄 telsystem.cpp
字号:
#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <iomanip>
using namespace std;
typedef map < string, pair<string, int > > TelMap;
typedef map <int , string> TelNameMap;
void printmenu();//打印系统菜单
void Add(); //添加新订户的详细信息
void Modify(); //修改现有订户的详细信息
void Del(); //删除现有订户的详细信息
void DisTel(); //显示根据电话号码查出的订户详细信息
void DisName(); //显示根据订户名查出的订户详细信息
void ViewDir(); //查看目录,显示所有订户详细信息
TelMap telmap; //保存订户详细信息的map
TelNameMap telname; //保存订户号码和姓名的map,用于基于号码的查找
int main()
{
fstream outfile("telefon.txt", ios::out);
int n;
printmenu();
while(cin>>n && n != 7)
{
switch(n)
{
case 1:
system("cls");
cout<<"\t\t"<<"ADD NEW SUBSCRIBER DETAILS"<<endl;
Add();
cout<<"Subscriber details have been added"<<endl;
printmenu();
continue;
case 2:
system("cls");
Modify();
cout<<"Subscriber details have been modified"<<endl;
printmenu();
continue;
case 3:
system("cls");
cout<<"\t\t"<<"DELETE SUBSCRIBER DETAILS"<<endl;
Del();
cout<<"Subscriber details have been deleted"<<endl;
printmenu();
continue;
case 4:
system("cls");
DisTel();
printmenu();
continue;
case 5:
system("cls");
DisName();
printmenu();
continue;
case 6:
system("cls");
ViewDir();
printmenu();
continue;
default:
system("cls");
cout<<"The REEOR number"<<endl;
printmenu();
continue;
}
cout<<endl;
}
TelMap::iterator pos = telmap.begin ();
for(int i = 1; pos != telmap.end(); ++pos, ++i) //写入文件
{
outfile<<"Record"<<i<<"\t\t"<<pos->first<<"\t"<<pos->second.first<<"\t"<<pos->second.second<<endl;
}
cout<<"写入文件成功"<<endl;
return 0;
}
void printmenu() //打印系统菜单
{
cout<<"\t\t"<<"TELEPHONE DIRECTORY SYSTEM"<<endl;
cout<<"\t"<<"1. Add new subscriber details"<<endl;
cout<<"\t"<<"2. Modify existing subscriber details"<<endl;
cout<<"\t"<<"3. Delete existing subscriber details"<<endl;
cout<<"\t"<<"4. Display subscriber details based on telephone number"<<endl;
cout<<"\t"<<"5. Display subscriber details based on subscriber name"<<endl;
cout<<"\t"<<"6. View Directory"<<endl;
cout<<"\t"<<"7. Quit"<<endl;
cout<<endl<<"Enter choice:";
}
void Add() //添加新订户的详细信息
{
string name, addr;
int tel;
cout<<"Enter Subscriber Name(upto 30 chars): ";
cin>>name;
cout<<"Enter Subscriber Address(upto 50 chars): ";
cin>>addr;
cout<<"Enter Telephone Number: ";
while(cin>>tel)
{
if(tel< 4000000 ||tel> 4999999 )
{
cout<<"\t"<<"Subscriber telephone number must be between 4000000 and 4999999 "<<endl;
cout<<"Enter Telephone Number:";
}
else
break;
}
telmap.insert(make_pair(name, make_pair(addr, tel))); //存入订户详细信息map
telname.insert(make_pair(tel, name)); //存入号码,姓名map
cout<<endl;
}
void Modify() //修改现有订户的详细信息
{
Del();
cout<<"ENTER NEW RECORD DETAILS"<<endl;
Add();
}
void Del() //删除现有订户的详细信息
{
string name;
cout<<"Enter Subscriber Name(upto 30 chars):";
cin>>name;
TelMap::iterator pos = telmap.find(name);
if(pos == telmap.end() )
{
cout<<"No Such Subscriber"<<endl;
return;
}
cout<<"Name: "<<pos->first<<endl;
cout<<"Address: "<<pos->second.first<<endl;
cout<<"Telphone: "<<pos->second.second<<endl;
TelNameMap::iterator p = telname.find(pos->second.second);
telname.erase(p);
telmap.erase(pos);
cout<<endl;
}
void DisTel() //显示根据电话号码查出的订户详细信息
{
int tel;
cout<<"Enter Telephone Number:";
cin>>tel;
TelNameMap::iterator p = telname.find(tel);
if(p == telname.end())
{
cout<<"No Such Subscriber"<<endl;
return;
}
TelMap::iterator pos = telmap.find(p->second);
cout<<"\t\t"<<"REQUESTED SUBSCRIBER DETAILS"<<endl;
cout<<"Name: "<<pos->first<<endl;
cout<<"Address: "<<pos->second.first<<endl;
cout<<"Telphone: "<<pos->second.second<<endl;
cout<<endl;
}
void DisName() //显示根据订户名查出的订户详细信息
{
string name;
cout<<"Enter Subscriber Name(upto 30 chars):";
cin>>name;
TelMap::iterator pos = telmap.find(name);
if(pos == telmap.end())
{
cout<<"No Such Subscriber"<<endl;
return;
}
cout<<"\t\t"<<"REQUESTED SUBSCRIBER DETAILS"<<endl;
cout<<"Name: "<<pos->first<<endl;
cout<<"Address: "<<pos->second.first<<endl;
cout<<"Telphone: "<<pos->second.second<<endl;
cout<<endl;
}
void ViewDir() //查看目录,显示所有订户详细信息
{
TelMap::iterator pos = telmap.begin ();
cout<<"\t\t"<<"The Subscriber Details List"<<endl;
for(int i = 1; pos != telmap.end(); ++pos, ++i)
{
cout<<"Record"<<i<<"\t\t"<<pos->first<<"\t"<<pos->second.first<<"\t"<<pos->second.second<<endl;
}
cout<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -