//定義全局變量
ios
char num;app
//定義全局函數函數
void choice(char);spa
void add_record();code
void del_record();ci
void rep_record();get
void fin_record();string
void dis_record();it
void exi_record();io
class Person
{
public:
Person(){}
Person(string a,string b,string c,Person*d=NULL):name(a),sex(b),tel(c),next(d){}
public:
string name;
string sex;
string tel;
Person* next;
}*Linkist;
struct str{
string temp;
str*next;
}*getstr;
int main()
{
A:
cout<<" 歡迎使用通信錄系統!!!"<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"1.添加記錄"<<"\t"<<"2.刪除記錄"<<"\t"<<"3.修改記錄"<<endl;
cout<<"4.查詢記錄"<<"\t"<<"5.列出記錄"<<"\t"<<"6.退出程序"<<endl;
cout<<"-------------------------------------------------------------"<<endl;
choice(num);
goto A;
return 0;
}
void choice(char num)
{
cout<<"請輸入的所要執行操做的數字編號:";
cin>>num;
switch(num)
{
case '1':
add_record();
break;
case '2':
del_record();
break;
case '3':
rep_record();
break;
case '4':
fin_record();
break;
case '5':
dis_record();
break;
case '6':
exi_record();
break;
default:
break;
}
}
void add_record()
{
Linkist=new Person;
Linkist->next=NULL;
Person *q,*p;
p=Linkist;
cout<<"請輸入你所要添加的記錄數量:";
int num1;
cin>>num1;
cout<<"請輸入姓名、姓別、號碼:"<<endl;
for(int i=0;i<num1;i++){
q=new Person;
cin>>q->name>>q->sex>>q->tel;
q->next=NULL;
p->next=q;
p=q;
}
cout<<"是否保存以上剛輸入信息?y/n:";
char ch;
cin>>ch;
if(ch=='y'||ch=='Y')
{
ofstream outfile("person.dat",ios::app);
for(int i=0;i<num1;i++){
outfile<<Linkist->next->name<<'\t'<<Linkist->next->sex<<'\t'<<Linkist->next->tel<<endl;
Linkist=Linkist->next;
}
outfile.close();
}
delete Linkist,q;
}
void dis_record()
{
string temp;
ifstream infile("person.dat",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
cout<<"姓名\t姓別\t號碼"<<endl;
while(!infile.eof())
{
infile.seekg(0,ios::cur);
getline(infile,temp);
cout<<'\n'<<temp<<endl;
}
}
void del_record()
{
ifstream infile("person.dat",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
getstr=new str;
getstr->next=NULL;
str *p,*q;
p=getstr;
while(!infile.eof())
{
q=new str;
infile.seekg(0,ios::cur);
getline(infile,q->temp);
q->next=NULL;
p->next=q;
p=q;
}
infile.close();
ofstream outfile("person.dat",ios::out);
char finstr[20];
cout<<"請輸入你將要刪除的人的姓名:";
cin>>finstr;
while(getstr->next->next!=NULL)
{
char sc='y';
if(!(getstr->next->temp).find(finstr))
{
cout<<getstr->next->temp<<endl;
cout<<"你肯定要刪除此條記錄嗎?y/n:";
cin>>sc;
}
if(sc=='y'||sc=='Y')
if((getstr->next->temp).find(finstr))
outfile<<getstr->next->temp<<endl;
getstr=getstr->next;
}
outfile.close();
delete getstr,q;
}
void rep_record()
{
ifstream infile("person.dat",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
getstr=new str;
getstr->next=NULL;
str *p,*q;
p=getstr;
while(!infile.eof())
{
q=new str;
infile.seekg(0,ios::cur);
getline(infile,q->temp);
q->next=NULL;
p->next=q;
p=q;
}
infile.close();
ofstream outfile("person.dat",ios::out);
char finstr[20];
cout<<"請輸入你將要修改的人的姓名:";
cin>>finstr;
Person *p1=new Person;
int xg=0;
while(getstr->next->next!=NULL)
{
char sc,xz;
if(!(getstr->next->temp).find(finstr))
{
xg=1;
cout<<getstr->next->temp<<endl;
cout<<"你肯定要修改此條記錄嗎?y/n:";
cin>>sc;
}
if(sc=='y'||sc=='Y'){
cout<<"請爲記錄輸入新的值:"<<endl;
cin>>p1->name>>p1->sex>>p1->tel;
cout<<"肯定要保存新值嗎?y/n:";
sc=' ';
cin>>xz;
}
if(xz=='y'||xz=='Y')
{
outfile<<p1->name<<'\t'<<p1->sex<<'\t'<<p1->tel<<endl;
xz=' ';
}
if(xz=='n'||xz=='n')
{
outfile<<getstr->next->temp<<endl;
xz=' ';
}
if((getstr->next->temp).find(finstr))
outfile<<getstr->next->temp<<endl;
getstr=getstr->next;
}
if(xg==0)
cout<<"找不到你所要修改的數據!"<<endl;
outfile.close();
delete getstr,q,p1;
}
void fin_record()
{
ifstream infile("person.dat",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
getstr=new str;
getstr->next=NULL;
str *p,*q;
p=getstr;
while(!infile.eof())
{
q=new str;
infile.seekg(0,ios::cur);
getline(infile,q->temp);
q->next=NULL;
p->next=q;
p=q;
}
infile.close();
string finstr;
cout<<"請輸入你將要查找的人的姓名:";
cin>>finstr;
int j;
while(getstr->next->next!=NULL)
{
j=0;
if(!(getstr->next->temp).find(finstr))
{
cout<<getstr->next->temp<<endl;
break;
}
else
++j;
getstr=getstr->next;
}
if(j>0)
cout<<"沒法找到你所查詢的記錄!"<<endl;
delete getstr,q;
}
void exi_record()
{
exit(0);
}