各位高手 帮帮忙 怎么改case '6'才能实现(6)按座机号码查询功能
代码如下
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
class tul
{
char name[20];char xingbie[2];char znum[11];char snum[11];char id[8];
public:
tul() { }
tul(char n[20],char x[2],char z[11],char s[11],char i[8])
{
strcpy(name,n);
strcpy(xingbie,x);
strcpy(znum,z);
strcpy(snum,s);
strcpy(id,i);
}
friend void main();
};
void main()
{
cout<<"********************************************************************\n";
cout<<"\n";
cout<<" ******** 欢迎来到通讯录管理系统 ********\n"<<endl;
cout<<"********************************************************************\n";
cout<<"请选择您需要的服务!"<<endl;
cout<<"操作:"<<endl;
cout<<"********** (0)数据录入 **********"<<endl;
cout<<"********** (1)增加数据 **********"<<endl;
cout<<"********** (2)删除数据 **********"<<endl;
cout<<"********** (3)修改数据 **********"<<endl;
cout<<"********** (4)查看通讯录中的数据 **********"<<endl;
cout<<"查询:"<<endl;
cout<<"********** (5)按姓名查询 **********"<<endl;
cout<<"********** (6)按座机号码查询 **********"<<endl;
cout<<"********** (7)按手机号码查询 **********"<<endl;
cout<<"********** (8)按单位名字简称查询 **********"<<endl;
cout<<"********** (9)退出 **********"<<endl;
cout<<"选择相关操作请输入相对的括号里的阿拉伯数字!"<<endl;
char p;char w;
tul *s[100];
ofstream *file[100];
int i=0;
int j=0;
bool flag2=0;
do
{
cin>>p;
if((p>='0'&&p<='9'))
flag2=1;
else
cout<<"指令错误!请重新输入:"<<endl;
}while(flag2==0);
do{
switch(p)
{
case '0':
{
char c;
char name[20];char xingbie[2];char znum[11];char snum[11];char id[8];
do{
cout<<"请输入您要增加的通讯人姓名:"<<endl;
cin>>name;
cout<<"请输入您要增加的通讯人性别:"<<endl;
cin>>xingbie;
cout<<"请输入您要增加的通讯人的座机号码:"<<endl;
cin>>znum;
cout<<"请输入您要增加的通讯人的手机号码:"<<endl;
cin>>snum;
cout<<"请输入您要增加的通讯人单位名字的简称"<<endl;
cin>>id;
file[j]=new ofstream("d:\\通讯录.txt",ios::ate);
*file[j]<<"姓名"<<name<<" "<<"性别"<<xingbie<<" "<<"座机号码"<<znum<<" "<<"手机号码"<<snum<<" "<<"单位名字简称"<<id<<" "<<endl;
fstream outfile1,infile; //作一个文本文档在文件夹中用于显示所录入的数据
outfile1.open("通讯录.txt",ios::out|ios::app);
if(!file)
{
cout<<"通讯录.txt can not open.\n";
return;
}
outfile1<<"姓名"<<name<<" "<<"性别"<<xingbie<<" "<<"座机号码"<<znum<<" "<<"手机号码"<<snum<<" "<<"单位名字简称"<<id<<" "<<endl;
outfile1.close();
infile.open("通讯录.txt",ios::in);
if(!file) //判断文件是否打开
{
cout<<"通讯录.txt can not open.\n";
return;
}
char ch;
while(infile.get(ch))
cout<<ch;
cout<<endl;
infile.close();
j++;
s[i]=new tul( name, xingbie,znum,snum,id);
i++;
cout<<"数据录入成功,想继续录入吗(y/n)"<<endl;
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
else
flag2=1;
}while(flag2==0);
}while(c=='y');
break;
}
case '1':
{
char name[20];char xingbie[2];char znum[11];char snum[11];char id[8];
char c;
do
{
cout<<"请输入您要增加的通讯人姓名:"<<endl;
cin>>name;
cout<<"请输入您要增加的通讯人性别:"<<endl;
cin>>xingbie;
cout<<"请输入您要增加的通讯人的座机号码:"<<endl;
cin>>znum;
cout<<"请输入您要增加的通讯人的手机号码:"<<endl;
cin>>snum;
cout<<"请输入您要增加的通讯人单位名字的简称"<<endl;
cin>>id;
file[j]=new ofstream("d:\\通讯录.txt",ios::ate);
*file[j]<<"姓名"<<name<<" "<<"性别"<<xingbie<<" "<<"座机号码"<<znum<<" "<<"手机号码"<<snum<<" "<<"单位名字简称"<<id<<" "<<endl;
fstream outfile1,infile; //作一个文本文档在文件夹中用于显示所录入的数据
outfile1.open("通讯录.txt",ios::out|ios::app);
if(!file)
{
cout<<"通讯录.txt can not open.\n";
return;
}
outfile1<<"姓名"<<name<<" "<<"性别"<<xingbie<<" "<<"座机号码"<<znum<<" "<<"手机号码"<<snum<<" "<<"单位名字简称"<<id<<" "<<endl;
outfile1.close();
infile.open("通讯录.txt",ios::in);
if(!file) //判断文件是否打开
{
cout<<"通讯录.txt can not open.\n";
return;
}
char ch;
while(infile.get(ch))
cout<<ch;
cout<<endl;
infile.close();
j++;
s[i]=new tul( name, xingbie,znum,snum,id);
i++;
cout<<"数据录入成功,想继续录入吗(y/n)"<<endl;
cin>>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
}while(c=='y');
break;
}
case '6':
{
char znum[11];
char c;
string jiadian,line,str,ne;
do
{
fstream fin("通讯录.txt");
fstream *p;
fstream outfile("修改.txt",ios::trunc|ios::out); //作一个文本文档在文件夹中用于显示所录入的数据
cout<<"请输入您要查询的通讯人的座机号码:"<<endl;
cin>>znum;
jiadian=znum;
while(!fin.eof() )
{
getline(fin,line);
istringstream stream(line);
ne = line.substr(0,line.find(" ",0));//找到名字,判断是否相等
if(ne != "座机号码"+jiadian)
{
fseek(*p,32,1);
cout<<fin.tellp()<<endl;
cout<<line<<endl;
}
}
fin.close();
cout<<"数据查找成功,还要继续查找吗?(y/n)"<<endl;
cin>>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
}while(c=='y');
};break;
case '9':
{
exit(0);
break;
}
}
cout<<"您想继续进行其他操作吗?(y/n)"<<endl;
bool flag4=0;
do
{
cin>>w;
if(w!='y'&&w!='n')
cout<<"指令错误!请重新输入!"<<endl;
else
flag4=1;
}while(flag4==0);
if(w=='n') exit(0);
if(w=='y')
cout<<"请输入操作代码(0 数据录入/1 增加数据/2 删除数据/3 修改数据/4 输出通讯录中所有的数据/5 按姓名查询/6 按座机号码查询/7 按手机号码查询/8 按单位名字简称查询/9退出)"<<endl;
cin>>p;
}while(w=='y');
for(int x=0;x<i;x++)
{
delete s[x];
cout<<"delete all members!"<<endl;
}
}
.txtz中
姓名qw 性别a 座机号码qwer 手机号码qwrt 单位名字简称we
姓名we 性别a 座机号码dddd 手机号码hjmn 单位名字简称we
姓名er 性别a 座机号码ffff 手机号码erhg 单位名字简称we
姓名rt 性别a 座机号码gggg 手机号码ddfd 单位名字简称we
姓名tw 性别a 座机号码hhhh 手机号码sczx 单位名字简称we
姓名fw 性别a 座机号码jjjj 手机号码gggr 单位名字简称we
姓名vw 性别a 座机号码kkkk 手机号码sdfd 单位名字简称we
姓名sw 性别a 座机号码llll 手机号码aszx 单位名字简称we
例如:输入座机号码hhhh
显示:姓名tw 性别a 座机号码hhhh 手机号码sczx 单位名字简称we