#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<iomanip>
using namespace std;
class Contact {
public:
void input()
{
cin.get();
cout << "姓 名:";
//cin>>name;
getline(cin,name);
cout << "地 址:";
getline(cin,address);
cout << "电话号码:";
getline(cin,tele);
cout << "移动电话:";
getline(cin,phone);
cout << "电子邮件:";
getline(cin,emall);
}
void output()
{
cout <<name<<"";
cout <<address<<"";
cout <<tele<<"";
cout <<phone<<"";
cout <<emall<<"";
cout <<endl;
}
void del()
{
del_flag=0;
}
void reco()
{
del_flag=1;
}
int del_flag;
string name;//姓名
string address;//地址
string tele;//电话号码
string phone;//移动电话
string emall;//电子邮件
};
Contact contactor[200];
int order=0;
void show_mainmenu()
{
cout << " \t\t\t\t 通讯录管理系统 " << endl;
cout << " \t\t\t\t1:联系人信息添加" << endl;
cout << " \t\t\t\t2:联系人信息查询" << endl;
cout << " \t\t\t\t3:联系人信息修改" << endl;
cout << " \t\t\t\t4:联系人信息删除" << endl;
cout << " \t\t\t\t5:联系人信息保存" << endl;
cout << " \t\t\t\t6:联系人信息读取" << endl;
cout << " \t\t\t\t7:联系人信息恢复" << endl;
cout << " \t\t\t\t8:联系人信息显示" << endl;
cout << " \t\t\t\t9:退出" << endl;
cout << "请选择你的一个操作: ";
}
void show_form()
{
cout <<"姓 名"<<"";
cout <<"地 址"<<"";
cout <<"电话号码"<<"";
cout <<"移动电话"<<"";
cout <<"电子邮件"<<"";
cout <<endl;
}
void input_items()
{
char c;
while(1)
{
cout << " \t\t\t\t 联系人添加 " << endl;
cout << " \t\t\t\t 1添加 " << endl;
cout << " \t\t\t\t 2退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
if('1' == c)
{
contactor[order].input();
while(1)
{
cout << "是否添加(y<Y>/n<N>):";
cin>>c;
if('y' == c||'Y' == c)
{
contactor[order].del_flag=1;
order++;
cout << "---------添加成功---------"<<endl;
break;
}
else if('n' == c||'N' == c)
{
contactor[order].del_flag=0;
break;
}
}
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void inquiry()
{
string inquiry_name;
int i;
char c;
while(1)
{
cout << " \t\t\t\t 联系人信息查询 " << endl;
cout << " \t\t\t\t 1:查询 " << endl;
cout << " \t\t\t\t 2:退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
cin.get();
if('1' == c)
{
cout << "请输入查询联系人姓名:";
getline(cin,inquiry_name);
for(i=0;i<=order;i++)
{
if((contactor[i].name == inquiry_name)&&(contactor[i].del_flag == 1))
{
show_form();
contactor[i].output();
break;
}
}
if(i>order)
{
cout << "没有该联系人信息!"<< endl;
}
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void modify()
{
string inquiry_name;
Contact ap;
int i;
char c;
while(1)
{
cout << " \t\t\t\t 联系人信息修改 " << endl;
cout << " \t\t\t\t 1:修改 " << endl;
cout << " \t\t\t\t 2:退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
cin.get();
if('1' == c)
{
cout << "请输入修改联系人姓名:";
getline(cin,inquiry_name);
for(i=0;i<=order;i++)
{
if((contactor[i].name == inquiry_name)&&(contactor[i].del_flag == 1))
{
show_form();
contactor[i].output();
ap=contactor[i];
contactor[i].input();
while(1)
{
cout << "是否保存修改(y<Y>/n<N>):";
cin>>c;
if('y' == c||'Y' == c)
{
order++;
contactor[i].del_flag=1;
cout << "---------修改成功---------"<<endl;
break;
}
else if('n' == c||'N' == c)
{
contactor[i]=ap;
break;
}
else
{
cout << "上一次输入错误!" << endl;
}
}
break;
}
}
if(i>order)
{
cout << "没有该联系人信息!"<< endl;
}
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void delet()
{
string inquiry_name;
int i;
char c;
while(1)
{
cout << " \t\t\t\t 联系人信息删除 " << endl;
cout << " \t\t\t\t 1:删除 " << endl;
cout << " \t\t\t\t 2:退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
cin.get();
if('1' == c)
{
cout << "请输入删除联系人姓名:";
getline(cin,inquiry_name);
for(i=0;i<=order;i++)
{
if((contactor[i].name == inquiry_name)&&(contactor[i].del_flag == 1))
{
show_form();
contactor[i].output();
while(1)
{
cout << "是否删除(y<Y>/n<N>):";
cin>>c;
if('y' == c||'Y' == c)
{
contactor[i].del();
cout << "---------删除成功---------"<<endl;
break;
}
else if('n' == c||'N' == c)
{
break;
}
else
{
cout << "上一次输入错误!" << endl;
}
}
break;
}
}
if(i>order)
{
cout << "没有该联系人信息!"<< endl;
}
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void save_item()
{
int i;
char c;
while(1)
{
cout << " \t\t\t\t 联系人信息保存 " << endl;
cout << " \t\t\t\t 1:保存 " << endl;
cout << " \t\t\t\t 2:退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
cin.get();
if('1' == c)
{
ofstream outfile("联系人.txt", ios::app);
if (!outfile)
{
cerr << "open error!" << endl;
exit(0);
}
for (i = 0; i <order; i++)
{
if(1 == contactor[i].del_flag)
{
outfile << contactor[i].name << " " << contactor[i].address << " " << contactor[i].tele
<< " " << contactor[i].phone << " " << contactor[i].emall<<endl;
}
}
outfile.close();
cout << "---------保存成功---------"<<endl;
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void read_item()
{
void entir_item();
int i;
char c;
while(1)
{
cout << " \t\t\t\t 联系人信息读取 " << endl;
cout << " \t\t\t\t 1:读取 " << endl;
cout << " \t\t\t\t 2:退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
cin.get();
if('1' == c)
{
ifstream infile("联系人.txt", ios::in);
for (i = order; i <=150; i++)
{
infile >>contactor[i].name >>contactor[i].address >>contactor[i].tele
>>contactor[i].phone>>contactor[i].emall;
contactor[i].del_flag=1;
if(contactor[i].name == "")
{
break;
}
order++;
}
infile.close();
if(i != 0)
{
cout << "---------读取成功---------"<<endl;
cout << "是否显示读取数据(y<Y>/n<N>):";
cin>>c;
cin.get();
if('y' == c || 'Y' == c)
{
entir_item();
}
}
else
{
cout << "---------读取失败---------"<<endl;
}
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void reco_delet()
{
string inquiry_name;
int i;
char c;
while(1)
{
cout << " \t\t\t\t 联系人信息恢复 " << endl;
cout << " \t\t\t\t 1:恢复 " << endl;
cout << " \t\t\t\t 2:退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
cin.get();
if('1' == c)
{
cout << "请输入恢复职工联系人姓名:";
getline(cin,inquiry_name);
for(i=0;i<=order;i++)
{
if((contactor[i].name == inquiry_name)&&(contactor[i].del_flag == 0))
{
show_form();
contactor[i].output();
while(1)
{
cout << "是否恢复(y<Y>/n<N>):";
cin>>c;
if('y' == c||'Y' == c)
{
contactor[i].reco();
cout << "---------恢复成功---------"<<endl;
break;
}
else if('n' == c||'N' == c)
{
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
break;
}
}
if(i>order)
{
cout << "没有该联系人信息!"<< endl;
}
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void entir_item()
{
show_form();
int i;
for(i=0;i<order;i++)
{
if(contactor[i].del_flag == 1)
{
contactor[i].output();
}
}
}
int main()
{
int c;
while(1)
{
show_mainmenu();
cin>>c;
cin.get();
if (c == 9)
{
cout << "---------退出成功---------" << endl;
break;
}
else
{
switch (c)
{
case 1:
input_items();
break;
case 2:
inquiry();
break;
case 3:
modify();
break;
case 4:
delet();
break;
case 5:
save_item();
break;
case 6:
read_item();
break;
case 7:
reco_delet();
break;
case 8:
entir_item();
system("cls");
break;
default:
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
break;
}
}
}
return 0;
}
可以运行,但是修改联系人alter()函数改完之后如果不在选5保存,关闭程序再打开选6就根本没改,如果保存了,则关掉再打开选6会发现有两份信息,一份是修改前的,一份是修改后的,此时若不关掉程序继续选择读取联系人信息,则显示的信息又是上次的两份复制,
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<iomanip>
using namespace std;
class Contact {
public:
void input()
{
cin.get();
cout << "姓 名:";
//cin>>name;
getline(cin,name);
cout << "地 址:";
getline(cin,address);
cout << "电话号码:";
getline(cin,tele);
cout << "移动电话:";
getline(cin,phone);
cout << "电子邮件:";
getline(cin,emall);
}
void output()
{
cout <<name<<"";
cout <<address<<"";
cout <<tele<<"";
cout <<phone<<"";
cout <<emall<<"";
cout <<endl;
}
void del()
{
del_flag=0;
}
void reco()
{
del_flag=1;
}
int del_flag;
string name;//姓名
string address;//地址
string tele;//电话号码
string phone;//移动电话
string emall;//电子邮件
};
Contact contactor[200];
int order=0;
void show_mainmenu()
{
cout << " \t\t\t\t 通讯录管理系统 " << endl;
cout << " \t\t\t\t1:联系人信息添加" << endl;
cout << " \t\t\t\t2:联系人信息查询" << endl;
cout << " \t\t\t\t3:联系人信息修改" << endl;
cout << " \t\t\t\t4:联系人信息删除" << endl;
cout << " \t\t\t\t5:联系人信息保存" << endl;
cout << " \t\t\t\t6:联系人信息读取" << endl;
cout << " \t\t\t\t7:联系人信息恢复" << endl;
cout << " \t\t\t\t8:联系人信息显示" << endl;
cout << " \t\t\t\t9:退出" << endl;
cout << "请选择你的一个操作: ";
}
void show_form()
{
cout <<"姓 名"<<"";
cout <<"地 址"<<"";
cout <<"电话号码"<<"";
cout <<"移动电话"<<"";
cout <<"电子邮件"<<"";
cout <<endl;
}
void input_items()
{
char c;
while(1)
{
cout << " \t\t\t\t 联系人添加 " << endl;
cout << " \t\t\t\t 1添加 " << endl;
cout << " \t\t\t\t 2退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
if('1' == c)
{
contactor[order].input();
while(1)
{
cout << "是否添加(y<Y>/n<N>):";
cin>>c;
if('y' == c||'Y' == c)
{
contactor[order].del_flag=1;
order++;
cout << "---------添加成功---------"<<endl;
break;
}
else if('n' == c||'N' == c)
{
contactor[order].del_flag=0;
break;
}
}
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void inquiry()
{
string inquiry_name;
int i;
char c;
while(1)
{
cout << " \t\t\t\t 联系人信息查询 " << endl;
cout << " \t\t\t\t 1:查询 " << endl;
cout << " \t\t\t\t 2:退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
cin.get();
if('1' == c)
{
cout << "请输入查询联系人姓名:";
getline(cin,inquiry_name);
for(i=0;i<=order;i++)
{
if((contactor[i].name == inquiry_name)&&(contactor[i].del_flag == 1))
{
show_form();
contactor[i].output();
break;
}
}
if(i>order)
{
cout << "没有该联系人信息!"<< endl;
}
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void modify()
{
string inquiry_name;
Contact ap;
int i;
char c;
while(1)
{
cout << " \t\t\t\t 联系人信息修改 " << endl;
cout << " \t\t\t\t 1:修改 " << endl;
cout << " \t\t\t\t 2:退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
cin.get();
if('1' == c)
{
cout << "请输入修改联系人姓名:";
getline(cin,inquiry_name);
for(i=0;i<=order;i++)
{
if((contactor[i].name == inquiry_name)&&(contactor[i].del_flag == 1))
{
show_form();
contactor[i].output();
ap=contactor[i];
contactor[i].input();
while(1)
{
cout << "是否保存修改(y<Y>/n<N>):";
cin>>c;
if('y' == c||'Y' == c)
{
order++;
contactor[i].del_flag=1;
cout << "---------修改成功---------"<<endl;
break;
}
else if('n' == c||'N' == c)
{
contactor[i]=ap;
break;
}
else
{
cout << "上一次输入错误!" << endl;
}
}
break;
}
}
if(i>order)
{
cout << "没有该联系人信息!"<< endl;
}
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void delet()
{
string inquiry_name;
int i;
char c;
while(1)
{
cout << " \t\t\t\t 联系人信息删除 " << endl;
cout << " \t\t\t\t 1:删除 " << endl;
cout << " \t\t\t\t 2:退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
cin.get();
if('1' == c)
{
cout << "请输入删除联系人姓名:";
getline(cin,inquiry_name);
for(i=0;i<=order;i++)
{
if((contactor[i].name == inquiry_name)&&(contactor[i].del_flag == 1))
{
show_form();
contactor[i].output();
while(1)
{
cout << "是否删除(y<Y>/n<N>):";
cin>>c;
if('y' == c||'Y' == c)
{
contactor[i].del();
cout << "---------删除成功---------"<<endl;
break;
}
else if('n' == c||'N' == c)
{
break;
}
else
{
cout << "上一次输入错误!" << endl;
}
}
break;
}
}
if(i>order)
{
cout << "没有该联系人信息!"<< endl;
}
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void save_item()
{
int i;
char c;
while(1)
{
cout << " \t\t\t\t 联系人信息保存 " << endl;
cout << " \t\t\t\t 1:保存 " << endl;
cout << " \t\t\t\t 2:退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
cin.get();
if('1' == c)
{
ofstream outfile("联系人.txt", ios::app);
if (!outfile)
{
cerr << "open error!" << endl;
exit(0);
}
for (i = 0; i <order; i++)
{
if(1 == contactor[i].del_flag)
{
outfile << contactor[i].name << " " << contactor[i].address << " " << contactor[i].tele
<< " " << contactor[i].phone << " " << contactor[i].emall<<endl;
}
}
outfile.close();
cout << "---------保存成功---------"<<endl;
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void read_item()
{
void entir_item();
int i;
char c;
while(1)
{
cout << " \t\t\t\t 联系人信息读取 " << endl;
cout << " \t\t\t\t 1:读取 " << endl;
cout << " \t\t\t\t 2:退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
cin.get();
if('1' == c)
{
ifstream infile("联系人.txt", ios::in);
for (i = order; i <=150; i++)
{
infile >>contactor[i].name >>contactor[i].address >>contactor[i].tele
>>contactor[i].phone>>contactor[i].emall;
contactor[i].del_flag=1;
if(contactor[i].name == "")
{
break;
}
order++;
}
infile.close();
if(i != 0)
{
cout << "---------读取成功---------"<<endl;
cout << "是否显示读取数据(y<Y>/n<N>):";
cin>>c;
cin.get();
if('y' == c || 'Y' == c)
{
entir_item();
}
}
else
{
cout << "---------读取失败---------"<<endl;
}
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void reco_delet()
{
string inquiry_name;
int i;
char c;
while(1)
{
cout << " \t\t\t\t 联系人信息恢复 " << endl;
cout << " \t\t\t\t 1:恢复 " << endl;
cout << " \t\t\t\t 2:退出 " << endl;
cout << "请输入你的选择:";
cin>>c;
cin.get();
if('1' == c)
{
cout << "请输入恢复职工联系人姓名:";
getline(cin,inquiry_name);
for(i=0;i<=order;i++)
{
if((contactor[i].name == inquiry_name)&&(contactor[i].del_flag == 0))
{
show_form();
contactor[i].output();
while(1)
{
cout << "是否恢复(y<Y>/n<N>):";
cin>>c;
if('y' == c||'Y' == c)
{
contactor[i].reco();
cout << "---------恢复成功---------"<<endl;
break;
}
else if('n' == c||'N' == c)
{
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
break;
}
}
if(i>order)
{
cout << "没有该联系人信息!"<< endl;
}
}
else if('2' == c)
{
system("cls");
break;
}
else
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
}
}
void entir_item()
{
show_form();
int i;
for(i=0;i<order;i++)
{
if(contactor[i].del_flag == 1)
{
contactor[i].output();
}
}
}
int main()
{
int c;
while(1)
{
show_mainmenu();
cin>>c;
cin.get();
if (c == 9)
{
cout << "---------退出成功---------" << endl;
break;
}
else
{
switch (c)
{
case 1:
input_items();
break;
case 2:
inquiry();
break;
case 3:
modify();
break;
case 4:
delet();
break;
case 5:
save_item();
break;
case 6:
read_item();
break;
case 7:
reco_delet();
break;
case 8:
entir_item();
system("cls");
break;
default:
{
cout << "上一次输入错误!" << endl;
cin.clear();
fflush(stdin);
}
break;
}
}
}
return 0;
}
可以运行,但是修改联系人alter()函数改完之后如果不在选5保存,关闭程序再打开选6就根本没改,如果保存了,则关掉再打开选6会发现有两份信息,一份是修改前的,一份是修改后的,此时若不关掉程序继续选择读取联系人信息,则显示的信息又是上次的两份复制,