![]() |
#2
peach54602013-07-15 16:06
|

#include<iostream>
#include<fstream>
#include<string>
#include"dianhuabu.hpp"
using namespace std;
string x;
int main(){
cout<<"欢迎使用电话簿管理系统!"<<endl;
cout<<"请输入你想打开的电话簿文件号码。"<<endl;
cin>>x;
fstream inout(x.c_str(),fstream::in|fstream::out);
fr *head,*p1,*p2;
int l=0;
//从文件读取数据建立链表
while(!inout.eof()){
++l;
p1=new fr;
inout>>p1->name>>p1->numb;
if(l==1){p1->next=NULL;head=p1;}
else p2->next=p1;
p2=p1;
}p2->next=NULL;
inout.close();
int m;
do{
cout<<"请输入你的选择。"<<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;
cin>>m;
string a,b,c,s;
fr *n;
switch (m){
case 1:
n=new fr;
cout<<"请输入联系人姓名及其联系方式。"<<endl;
cin>>n->name>>n->numb;
n->next=NULL;
head= inse(head,n);
break;
case 2:
cout<<"请输入需要修改的联系人姓名。"<<endl;
cin>>a;
cout<<"请输入联系人新的联系方式。"<<endl;
cin>>b;
head=rese(head,a,b);
break;
case 3:
cout<<"请输入需要查询的联系人姓名。"<<endl;
cin>>s;
cout<<endl;
quse(head,s);
break;
case 4:
cout<<"请输入需要删除的联系人姓名。"<<endl;
cin>>c;
head=dese(head,c);
break;
case 5:
exse(head);
break;
}
} while(m>=1&&m<=5);
ofstream out(x.c_str(),ofstream::out);
while(head!=NULL)
{
out<<head->name<<" "<<head->numb<<endl;
head=head->next;
}
out.close();
delete p1;
return 0;
}
#include<fstream>
#include<string>
#include"dianhuabu.hpp"
using namespace std;
string x;
int main(){
cout<<"欢迎使用电话簿管理系统!"<<endl;
cout<<"请输入你想打开的电话簿文件号码。"<<endl;
cin>>x;
fstream inout(x.c_str(),fstream::in|fstream::out);
fr *head,*p1,*p2;
int l=0;
//从文件读取数据建立链表
while(!inout.eof()){
++l;
p1=new fr;
inout>>p1->name>>p1->numb;
if(l==1){p1->next=NULL;head=p1;}
else p2->next=p1;
p2=p1;
}p2->next=NULL;
inout.close();
int m;
do{
cout<<"请输入你的选择。"<<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;
cin>>m;
string a,b,c,s;
fr *n;
switch (m){
case 1:
n=new fr;
cout<<"请输入联系人姓名及其联系方式。"<<endl;
cin>>n->name>>n->numb;
n->next=NULL;
head= inse(head,n);
break;
case 2:
cout<<"请输入需要修改的联系人姓名。"<<endl;
cin>>a;
cout<<"请输入联系人新的联系方式。"<<endl;
cin>>b;
head=rese(head,a,b);
break;
case 3:
cout<<"请输入需要查询的联系人姓名。"<<endl;
cin>>s;
cout<<endl;
quse(head,s);
break;
case 4:
cout<<"请输入需要删除的联系人姓名。"<<endl;
cin>>c;
head=dese(head,c);
break;
case 5:
exse(head);
break;
}
} while(m>=1&&m<=5);
ofstream out(x.c_str(),ofstream::out);
while(head!=NULL)
{
out<<head->name<<" "<<head->numb<<endl;
head=head->next;
}
out.close();
delete p1;
return 0;
}
dianhuabu.hpp

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class fr{
public:
fr *inse(fr *f,fr*x);//插入
fr *rese(fr *f,string a,string b);//修改
void *quse(fr *f,string a);//查询
fr *dese(fr *f,string a);//删除
void *exse(fr *f);//输出
string name;
string numb;
fr *next;
};
fr *inse(fr *f,fr*x){
fr *head;
head=f;
while((head->next)!=NULL)
{head=head->next; }
head->next=x;
x->next=NULL;
return f;
}
fr *rese(fr *f,string a,string b){
fr*head;
head=f;
while(head->name!=a){
head=head->next;
}
head->numb=b;
return f;
}
void quse(fr *f,string a){
fr*head;
head=f;
while(head->name!=a){
head=head->next;
}
cout<<head->name<<" "<<head->numb<<endl;
}
fr *dese(fr *f,string a){
fr *head,*mark;
head=f;
if(f->name==a)
f=f->next;
else{
while((head->name)!=a){
mark=head;
head=head->next;
}
mark->next=head->next; }
return f;
}
void *exse(fr *f){
fr *head;
head=f;
while(head!=NULL){
cout<<head->name<<" "<<head->numb<<endl;
head=head->next;
}
}
#include<string>
#include<fstream>
using namespace std;
class fr{
public:
fr *inse(fr *f,fr*x);//插入
fr *rese(fr *f,string a,string b);//修改
void *quse(fr *f,string a);//查询
fr *dese(fr *f,string a);//删除
void *exse(fr *f);//输出
string name;
string numb;
fr *next;
};
fr *inse(fr *f,fr*x){
fr *head;
head=f;
while((head->next)!=NULL)
{head=head->next; }
head->next=x;
x->next=NULL;
return f;
}
fr *rese(fr *f,string a,string b){
fr*head;
head=f;
while(head->name!=a){
head=head->next;
}
head->numb=b;
return f;
}
void quse(fr *f,string a){
fr*head;
head=f;
while(head->name!=a){
head=head->next;
}
cout<<head->name<<" "<<head->numb<<endl;
}
fr *dese(fr *f,string a){
fr *head,*mark;
head=f;
if(f->name==a)
f=f->next;
else{
while((head->name)!=a){
mark=head;
head=head->next;
}
mark->next=head->next; }
return f;
}
void *exse(fr *f){
fr *head;
head=f;
while(head!=NULL){
cout<<head->name<<" "<<head->numb<<endl;
head=head->next;
}
}
文件
1.dat
jim 138292929229
tom 134222233223
jim 152222343222
只有本站会员才能查看附件,请 登录
我插入一个联系人之后
在让输出现有的链表
为什么插入的和原来链表最后一个之间有一个空行。