#include<iostream>
#include<string.h>
#include<fstream>
#include<cstdlib>
using namespace std;
class phoneBook{
public:
phoneBook(){};
phoneBook(string a,string b){
name=a;
number=b;
}
~phoneBook(){}
void GetName(string a){name=a;}
void GetNumber(string a){number=a;}
string ReturnName(){return name;}
string ReturnNumber(){return number;}
void input();
void store();
void seek();
void change();
void Delete();
void read();
void storeNew();
private:
string name;
string number;
};
phoneBook p[100];
void phoneBook::input(){ //添加信息 姓名电话号码
int i=0;
char name[30];
string number;
cout<<"请输入信息[姓名为#代表输入信息结束]:"<<endl;
cout<<"姓名:";
cin>>name;
p[i].GetName(name);
while(p[i].ReturnName()!="#"){
cout<<"电话号码:";
cin>>number;
p[i].GetNumber(number);
i++;
cout<<"姓名:";
cin>>name;
p[i].GetName(name);
}
store();
cout<<endl;
}
void phoneBook::seek(){ //输入姓名查找电话号码
string temp;
int j=0;
cout<<"请输入要查找的姓名:";
cin>>temp;
for(int i=0;i<=99;i++){
if(p[i].ReturnName()==temp){
cout<<"该用户电话号码为:"<<p[i].ReturnNumber()<<endl<<endl;
}
else if((p[i].ReturnName())!=temp){
j++;
if(j==100)
cout<<"该用户不存在"<<endl<<endl;
}
}
}
void phoneBook::change(){ //修改指定信息
char temp[20];
string a,b;
int j=0;
cout<<"请输入要修改的姓名:";
cin>>temp;
for(int i=0;i<=99;i++){
if((p[i].ReturnName())==temp){
cout<<"修改信息:\n姓名:";
cin>>a;
p[i].GetName(a);
cout<<"电话号码:" ;
cin>>b;
p[i].GetNumber(b);
cout<<"修改成功"<<endl<<endl;
}
else if((p[i].ReturnName())!=temp){
j++;
if(j==100)
cout<<"该用户不存在"<<endl<<endl;
}
}
this->store();
}
void phoneBook::Delete(){ //删除指定电话信息
string temp;
int i=0,j=0,truth=0;
cout<<"请输入要删除的姓名:";
cin>>temp;
while(p[i].ReturnName()!="#"){
if((p[i].ReturnName())==temp){
truth=1;
while((p[i+1].ReturnName())!="#"){
p[i]=p[i+1];
i++;
}
p[i]=p[i+1];
cout<<"删除成功"<<endl<<endl;
}
i++;
}
if(truth==0){
cout<<"该用户不存在"<<endl<<endl;
}
this->store();
}
void phoneBook::store(){ //写入文件
ofstream font("电话本.txt");
for(int i=0;i<=99;i++){
if(p[i].ReturnName()!="#")
font<<"姓名:"<<p[i].name<<endl;
font<<"电话号码:"<<p[i].number<<endl;
}
font.close();
}
void phoneBook::storeNew(){ //写入指定文件
char ch,filename2[30];
cout<<"请输入用来储存电话信息的文件名:";
cin>>filename2;
getchar();
ofstream font2(filename2);
ifstream font("电话本.txt");
while(font.get(ch)){
font2<<ch;
}
font2.close();
cout<<"信息保存成功,请关闭程序后打开文件查看"<<endl<<endl;
}
void phoneBook::read(){ //从指定文件中读取
char filename1[30];
int i=0;
cout<<"请输入要读取电话信息的文件名:";
cin>>filename1;
getchar();
ifstream font1(filename1,ios_base::in);
while(font1>>name>>number)
{
p[i]=phoneBook(name,number);
p[i].GetName(name);
p[i].GetNumber(number);
i++;
}
font1.close();
cout<<"文件读取成功"<<endl<<endl;
}
int main(){
int n,truth=1;
phoneBook ptr;
while(truth==1){
cout<<"请输入您的选择:"<<endl<<"1--添加姓名电话信息"<<endl<<"2--查找指定电话信息"<<endl;
cout<<"3--删除指定电话号码"<<endl<<"4--修改指定电话信息"<<endl<<"5--从指定文件读取电话信息"<<endl;
cout<<"6--将电话信息保存至指定文件(若不选择此项信息将会储存在默认文件“电话本.txt”中)"<<endl;
cout<<"7--退出电话本"<<endl<<"[选择]:";
cin>>n;
cout<<endl;
switch (n) {
case 1:
ptr.input();
break;
case 2:
ptr.seek();
break;
case 3:
ptr.Delete();
break;
case 4:
ptr.change();
break;
case 5:
ptr.read();
break;
case 6:
ptr.storeNew();
break;
case 7:
truth=0;
break;
default:
cout<<"您的输入有误,请重新输入"<<endl<<endl;
}
}
cout<<"感谢您的使用,您现在可以打开文件查看电话信息。祝您生活愉快,再见!"<<endl<<endl;
system("pause");
return 0;
}