#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct tongxunlu
{
int num;
string name;
string address;
int tel;
int youbian;
string email;
};
void insertdata();//添加数据
void deletdata();//删除数据
void checkdata();//查询数据
void resetdata();//数据排序
tongxunlu data[40];
int main()
{
cout<<"请输入要进行的操作:"<<endl;
cout<<"1:添加 2:删除 3:查询 4:排序 5:建立新文件"<<endl;
int i;
cin>>i;
switch(i)
{
case 1:
{
ifstream infile("tongxunlu.dat",ios::in);
if(! infile)
{
cerr<<"open error,请建立文件"<<endl;
exit(1);
}
tongxunlu data[40];
for(int b=0;b<40;b++)
{
infile>>data[b].num;
infile>>data[b].name;
infile>>data[b].address;
infile>>data[b].tel;
infile>>data[b].youbian;
infile>>data[b].email;
}
insertdata();
};break;
case 2:deletdata();break;
case 3:checkdata();break;
case 4:resetdata();break;
case 5:
{
ofstream outfile("tongxunlu.dat",ios::out);
if(! outfile)
{
cerr<<"open error";
exit(1);
}
tongxunlu data[40];
for(int i=0;i<40;i++)
{
data[i].num=0;
data[i].name="0";
data[i].address="0";
data[i].tel=0;
data[i].youbian=0;
data[i].email="0";
}
cout<<"文件建立完成"<<endl;
};break;
default:cout<<"error\n";
}
return 0;
}
//输入数据
void insertdata()
{
ofstream outfile("tongxunlu.dat",ios::out);
if(! outfile)
{
cerr<<"open error";
exit(1);
}
int i;
cout<<"请输入学生编号,当输入0时添加数据结束"<<endl;
cin>>i;
while(i!=0)
{
data[i-1].num=i;
cout<<"请输入学生姓名:";
cin>>data[i-1].name;
cout<<"请输入学生住址:";
cin>>data[i-1].address;
cout<<"请输入学生电话:";
cin>>data[i-1].tel;
cout<<"请输入学生邮编:";
cin>>data[i-1].youbian;
cout<<"请输入学生email:";
cin>>data[i-1].email;
cout<<"请输入下一个编号:";
cin>>i;
}
for(int c=0;c<40;c++)
{
outfile<<data[c].num<<endl;
outfile<<data[c].name<<endl;
outfile<<data[c].address<<endl;
outfile<<data[c].tel<<endl;
outfile<<data[c].youbian<<endl;
outfile<<data[c].email<<endl;
}
outfile.close();
cout<<"数据输入完毕"<<endl;
}
//删除数据
void deletdata()
{
ifstream infile("tongxunlu.dat",ios::in);
if(!infile)
{
cerr<<"open error,请建立新文件"<<endl;
exit(1);
}
tongxunlu data[40];
for(int b=0;b<40;b++)
{
infile>>data[b].num;
infile>>data[b].name;
infile>>data[b].address;
infile>>data[b].tel;
infile>>data[b].youbian;
infile>>data[b].email;
cout<<data[b].num<<endl;
}
int i,j;
string n;
cout<<"请选择:"<<endl<<"1:按编号删除 2:按姓名删除"<<endl;
cin>>i;
if(i==1)
{
cout<<"请输入学生编号:";
cin>>j;
cout<<endl;
if(j==0) cout<<"无此学生编号"<<endl;
else if(data[j-1].num==0) cout<<"无该学生信息"<<endl;
else
{
data[j-1].num=0;
cout<<"学生信息删除完毕"<<endl;
}
}
if(i==2)
{
cout<<"请输入学生姓名:";
cin>>n;
cout<<endl;
if(n=="0") cout<<"无该学生信息"<<endl;
else if(n!="0")
{
for(int m=0;m<40;m++)
{
if(data[m].name=="0") continue;
if(data[m].name==n)
{
data[m].name="0";
cout<<"学生信息删除完毕"<<endl;
break;
}
else continue;
}
}
}
else cout<<"error\n";
ofstream outfile("tongxunlu.dat",ios::out);
if(! outfile)
{
cerr<<"open error";
exit(1);
}
for(int c=0;c<40;c++)
{
outfile<<data[c].num;
outfile<<data[c].name;
outfile<<data[c].address;
outfile<<data[c].tel;
outfile<<data[c].youbian;
outfile<<data[c].email;
}
outfile.close();
}
//查询数据
void checkdata()
{
ifstream infile("tongxunlu.dat",ios::in);
if(! infile)
{
cerr<<"open error,请建立新文件"<<endl;
exit(1);
}
tongxunlu data[40];
for(int b=0;b<40;b++)
{
infile>>data[b].num;
infile>>data[b].name;
infile>>data[b].address;
infile>>data[b].tel;
infile>>data[b].youbian;
infile>>data[b].email;
cout<<data[b].num<<endl;
}
int a;
cout<<"请选择:"<<endl<<"1:全部查询 2:按条件查询"<<endl;
cin>>a;
if(a==1)
{
for(int j=0;j<40;j++)
{
if(data[j].num==0) continue;
else
{
cout<<setw(10)<<data[j].num;
cout<<setw(10)<<data[j].name;
cout<<setw(10)<<data[j].address;
cout<<setw(10)<<data[j].tel;
cout<<setw(10)<<data[j].youbian;
cout<<setw(10)<<data[j].email<<endl;
}
}
cout<<"数据输出完毕"<<endl;
}
else if(a==2)
{
int j;
cout<<"请选择:"<<endl<<"1:按编号查询 2:按姓名查询"<<endl;
cin>>j;
if(j==1)
{
int k;
cout<<"请输入编号:";
cin>>k;
cout<<endl;
if(data[k-1].num==0) cout<<"无此学生信息"<<endl;
else
{
cout<<setw(40)<<data[k-1].num;
cout<<setw(40)<<data[k-1].name;
cout<<setw(40)<<data[k-1].address;
cout<<setw(40)<<data[k-1].tel;
cout<<setw(40)<<data[k-1].youbian;
cout<<setw(40)<<data[k-1].email<<endl;
cout<<"数据输出完毕"<<endl;
}
}
else if(j==2)
{
string n;
cout<<"请输入学生姓名:";
cin>>n;
cout<<endl;
for(int l=0;l<40;l++)
{
if(data[l].num==0) continue;
else if(data[l].name==n)
{
cout<<setw(40)<<data[l].num;
cout<<setw(40)<<data[l].name[20];
cout<<setw(40)<<data[l].address;
cout<<setw(40)<<data[l].tel;
cout<<setw(40)<<data[l].youbian;
cout<<setw(40)<<data[l].email<<endl;
cout<<"数据输出完毕"<<endl;
break;
}
else continue;
}
}
else cout<<"error\n";
}
else cout<<"error\n";
}
//数据排序
void resetdata()
{
ifstream infile("tongxunlu.dat",ios::in);
if(! infile)
{
cerr<<"open error"<<endl;
exit(1);
}
tongxunlu data[40];
for(int b=0;b<40;b++)
{
infile>>data[b].num;
infile>>data[b].name;
infile>>data[b].address;
infile>>data[b].tel;
infile>>data[b].youbian;
infile>>data[b].email;
cout<<data[b].num<<endl;
}
int i;
cout<<"请选择:"<<endl<<"1:按编号排序 2:按姓名排序"<<endl;
cin>>i;
if(i==1)
{
for(int j=0;j<40;j++)
{
if(data[j].num==0) continue;
else
{
cout<<setw(40)<<data[j].num;
cout<<setw(40)<<data[j].name;
cout<<setw(40)<<data[j].address;
cout<<setw(40)<<data[j].tel;
cout<<setw(40)<<data[j].youbian;
cout<<setw(40)<<data[j].email<<endl;
}
}
cout<<"数据输出完毕"<<endl;
}
else if(i==2)
{
tongxunlu temp;
for(int a=0;a<39;i++)
{
for(int b=38;b>=a;b--)
{
if(data[b].name>data[b+1].name)
{
temp=data[b+1];
data[b+1]=data[b];
data[b]=temp;
}
}
}
for(int c=0;c<40;c++)
{
if(data[c].name=="0") continue;
else
{
cout<<setw(40)<<data[c].num;
cout<<setw(40)<<data[c].name[20];
cout<<setw(40)<<data[c].address;
cout<<setw(40)<<data[c].tel;
cout<<setw(40)<<data[c].youbian;
cout<<setw(40)<<data[c].email<<endl;
}
}
cout<<"数据输出完毕"<<endl;
}
else cout<<"error\n";
}