#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
class book
{
public:
book();//默认构造函数
char inter_face();//首页
void add_person();//添加联系人
void del_person();//删除联系人
void show_all();//显示所有联系人
void alter();//修改信息
void select();//查询联系人
void save_new();//保存新增加的联系人
private:
string name;//姓名
string address;//地址
string number;//电话号码
string post;//邮编
string qq;//QQ号
};
book::book()
{
name = "\0";
address = "\0";
number = "\0";
post = "\0";
qq = "\0";
}
//首页
char book::inter_face()
{
system("cls");
cout <<endl;
cout<<"\t\t★★★★★★★★★★★★★★★★★★★★★★★★★★" <<endl
<<"\t\t※ ※" <<endl
<<"\t\t※ 通 迅 录 ※" <<endl
<<"\t\t※ ※" <<endl
<<"\t\t※ 1. 添加新联系人. 4. 修改信息. ※" <<endl
<<"\t\t※ ※" <<endl
<<"\t\t※ 2. 删除联系人. 5. 查询联系人. ※" <<endl
<<"\t\t※ ※" <<endl
<<"\t\t※ 3. 显示所有联系人. 6. 关闭通迅录. ※" <<endl
<<"\t\t※ ※" <<endl
<<"\t\t★★★★★★★★★★★★★★★★★★★★★★★★★★" <<endl <<endl
<<"\t\t\t选择 :" ;
char choose;
cin >>choose;
fflush(stdin);
return choose;
}
//添加联系人
void book::add_person()
{
cout <<endl <<"根据下面提示输入新联系人信息" <<endl <<endl
<<"姓名 :" ;
cin >>name;
fflush(stdin);
cout <<"电话 : ";
cin >>number;
fflush(stdin);
cout <<"QQ : ";
cin >>qq;
fflush(stdin);
cout <<"邮编 : ";
cin >>post;
fflush(stdin);
cout <<"地址 : ";
cin >>address;
fflush(stdin);
save_new();
cout <<endl <<endl <<"新联系人信息已经保存好!!!!!" <<endl <<endl;
system("pause");
}
//删除联系人
void book::del_person()
{
ofstream outData("temp.txt", ios::out); //以输出方式打开文件,并使之与outdata建立联系
ifstream inData("pbook.txt", ios::in); //以输入方式打开文件,并使之与indata建立联系
if (!outData || !inData) //打开文件失败
{
cout <<endl <<"对不起!!!!找不到文件!!!!" <<endl;
system("pause");
return;
}
string sign;
cout <<endl <<"你要删除输入姓名或电话号 :";
cin >>sign;
string str1;
bool flag = true;
string str;
while (inData >>name >>number) //while循环找到要输入或删除的姓名或号码
{
getline(inData, str); //将文件流中剩下的内容读入到字符串(str指一个数据除去name和number后所剩的数据)
if ((sign==name) || (sign==number))
{
cout <<endl <<"你想删除的联系人:" <<endl <<endl;
cout <<str1 <<endl; //输出一行空格
cout <<setiosflags(ios::left) <<setw(17) <<name
<<" " <<number <<str <<endl;
flag = false;
break; //break语句跳出for循环,continue语句跳出当前语句继续循环
}
outData <<setiosflags(ios::left) <<setw(17)<<name //输出数据左对齐,且设置字段宽度为17,并将名字和号码输出到文件temp.txt中
<<" " <<number <<str <<endl;
}
if (flag)
{
cout <<endl <<"对不起!!!联系人中没你找的人!!!!" <<endl <<endl;
}
else //如果查找成功,即找到了与之相同的姓名或号码
{
while (getline(inData, str)) //保证所有数据都输出
//getline函数的作用是从输入流中读入所有剩下的数据(注:并不提供输出到显示器)
{
outData <<str <<endl; //将读取的字符数据输出到pbook.txt中
}
outData.close();
inData.close();
ofstream out("pbook.txt", ios::out);
ifstream in("temp.txt", ios::in);
if (!out || !in)
{
cout <<endl <<"对不起不能打开文件!!!" <<endl <<endl;
system("pause");
return;
}
while (getline(in, str))
{
out <<str <<endl;
}
out.close();
in.close();
cout <<endl <<"这个人的信息已经从你的通迅录中删除!!!" <<endl <<endl;
}
system("pause");
}
//显示所有联系人
void book::show_all()
{
ifstream inData("pbook.txt",ios::in); //打开文件pbook.txt,并使之与输入文件流建立联系
if (!inData)
{
cout <<endl <<"对不起!!!!没有找到文件!!!!!" <<endl;
system("pause");
return;
}
bool flag = true;
string record;
while (getline(inData, record)) // while是先判断后执行,do......while是先执行后判断
{
if (flag)
{
cout <<endl <<"所有联系人信息如下: "<<endl;
}
cout <<record <<endl;
flag = false;
}
if (flag)
{
cout <<endl <<"你的通迅录中没有联系人!!!!!" <<endl <<endl;
}
else
{
cout <<endl <<"所有联系人已经全部显示出来!!!!!" <<endl <<endl;
}
system("pause");
}
//修改信息
void book::alter()
{
ofstream outData("temp.txt", ios::out);
ifstream inData("pbook.txt", ios::in); //
if (!outData || !inData)
{
cout <<endl <<"对不起!!!!找不到文件!!!!" <<endl;
system("pause");
return;
}
string sign;
cout <<endl <<"你要修改输入姓名或电话号 :";
cin >>sign;
string str1;
bool flag = true;
string str;
while (inData >>name >>number)
{
getline(inData, str);
if ((sign==name) || (sign==number))
{
cout <<endl <<"你想修改的联系人:" <<endl <<endl;
cout <<str1 <<endl;
cout <<setiosflags(ios::left) <<setw(17) <<name
<<" " <<number <<str <<endl;
cout <<endl <<"请根据下面提示修改信息 : " <<endl;
cout <<"姓名 :" ;
cin >>name;
fflush(stdin);
cout <<"电话号:";
cin >>number;
fflush(stdin);
cout <<"QQ :";
cin >>qq;
fflush(stdin);
cout <<"邮编 :";
cin >>post;
fflush(stdin);
cout <<"地址 :";
cin >>address;
fflush(stdin);
save_new();
flag = false;
break;
}
outData <<setiosflags(ios::left) <<setw(17) <<name
<<" " <<number <<str <<endl;
}
if (flag)
{
cout <<endl <<"对不起!!!联系人中没你找的人!!!!" <<endl <<endl;
}
else
{
while (getline(inData, str)) //如果找到了你要查找的联系人
{
outData <<str <<endl;
}
outData.close();
inData.close();
ofstream out("pbook.txt", ios::out);
ifstream in("temp.txt", ios::in);
if (!out || !in)
{
cout <<endl <<"对不起不能打开文件!!!" <<endl <<endl;
system("pause");
return;
}
while (getline(in, str))
{
out <<str <<endl;
}
out.close();
in.close();
cout <<endl <<"这个人的信息已经在你的通迅录中修改!!!" <<endl <<endl;
}
system("pause");
}
//查询联系人
void book::select()
{
ifstream inData("pbook.txt",ios::in);
if (!inData)