level 1
这个是个通讯录的源代码,在删除联系人那里出了问题,求解决方法
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <iomanip.h>
#include <fstream.h>
//--------------------------------------------------------------节点类的定义ok
class node
{
char name[15];
int op,hp;
char mp[15];
char em[20];
node *next;
public:
node(char *n="a",int o=0,int h=0,char *m="b",char *e="c",node *ne=NULL)//初始化
{
if(n)strcpy(name,n);
if(m)strcpy(mp,m);
if(e)strcpy(em,e);
next=ne;
op=o;hp=h;
}
void show()//显示节点数据
{
cout<<setw(10)<<name<<setw(11)<<op<<"(0)"<<setw(11)<<hp<<"(H)"<<setw(14)<<mp<<"(M)"<<' '<<em<<endl;
}
void setname(char *s)//重置姓名
{strcpy(name,s);}
void setop(int x)//重置office电话
{op=x;}
void sethp(int x)//重置home电话
{hp=x;}
void setmp(char *s)//重置mobile电话
{strcpy(mp,s);}
void setem(char *s)//重置email
{strcpy(em,s);}
friend class addr;
friend istream& operator>>(istream &,node *);//>>的重载
};
//--------------------------------------------------------------节点定义结束
//--------------------------------------------------------------链表创建ok
class addr
{
node *head;
node *tail;
int t;
public:
addr(node *h=NULL,node *ta=NULL,int tag=1)
{head=h;tail=ta;t=tag;}
void addtail(node *p)//--------------------------------------------------------------添加到尾部ok
{
tail->next=p;
tail=p;
}
void addsort(node *p)//--------------------------------------------------------------按序添加ok
{
2010年06月17日 14点06分
1
level 1
node *p1,*p2;
p1=head;p2=head;
if(t==1)//姓名插入
{
if (head==NULL){head=p;tail=head;}
else if (strcmp(p->name,head->name)==-1){p->next=head;head=p;}
else if(strcmp(p->name,tail->name)>=0) {tail->next=p;tail=p;}
else
{
while (strcmp(p2->name,p->name)==-1)//p2.name<p.name
{
p1=p2;
p2=p2->next;
}
p->next=p2;
p1->next=p;
}
}
if (t==2)//office电话插入
{
if (head==NULL){head=p;tail=head;}
else if (p->op<=head->op){p->next=head;head=p;}
else if(p->op>=tail->op) {tail->next=p;tail=p;}
else
{
while (p->op>=p2->op)//p2.op<p.op
{
p1=p2;
p2=p2->next;
2010年06月17日 14点06分
2
level 1
}
p->next=p2;
p1->next=p;
}
}
}
void sort()//--------------------------------------------------------------排序ok
{
addr s2;
s2.sett(t);
node *p=head;
while(p)
{
node *p1=new node;
node *p2;
strcpy(p1->name,p->name);
strcpy(p1->mp,p->mp);
strcpy(p1->em,p->em);
p1->op=p->op;
p1->hp=p->hp;
s2.addsort(p1);
p2=p;
p=p->next;
delete p2;
}
s2.showall();
head=s2.head;
s2.head=NULL;
tail=s2.tail;
s2.tail=NULL;
}
node *lookup(char *na)//--------------------------------------------------------------查找ok
{
node *p1;
p1=head;
while (p1)
{
if (strcmp(p1->name,na)==0) break;
else p1=p1->next;
}
2010年06月17日 14点06分
3
level 1
return(p1);
}
void deletea(char *na)//--------------------------------------------------------------删除ok
{
node *p,*p1;
p=lookup(na);
char s;
cin>>s;
if (p->name&&p!=head)
{
p1=head;
while(p1->next!=p)p1=p1->next;
p1->next=p->next;
}
if(p==head) {head=p->next;}
if (p->next==NULL) {tail=p1;p1->next=NULL;}
if (p->name) delete p;
}
void showall()//--------------------------------------------------------------显示全部节点+每屏显示十个ok
{
node *p1=head;int k=0;
system("cls");
if(p1==NULL) cout<<"No information"<<endl;
while (p1)
{
k++;
p1->show();
p1=p1->next;
if(k%10==0){system("pause");system("cls");}
}
}
void sett(int tag){t=tag;}//--------------------------------------------------------------重置tag ok
int gett(){return(t);}//--------------------------------------------------------------取tag ok
node *gethead(){return head;}//--------------------------------------------------------------取head ok
void creatlist(char *filename)//--------------------------------------------------------------文件读>>重载ok
{
ifstream in(filename);
if(!in){cout<<"wrong name"<<endl;}
else
2010年06月17日 14点06分
4
level 1
{ int n;
in>>n;
for(int i=1;i<=n;i++)
{
node *s=new node;
in>>s;
addsort(s);
}
}
in.close();
}
void writelist(char *filename)//--------------------------------------------------------------文件写ok
{
ofstream out(filename);
if (head)
{
node *p1=head;
while (p1!=tail)
{
out<<setw(10)<<p1->name<<setw(11)<<p1->op<<"(0)"<<setw(11)<<p1->hp<<"(H)"<<setw(14)<<p1->mp<<"(M)"<<' '<<p1->em<<endl;
p1=p1->next;
}
out<<setw(10)<<tail->name<<setw(11)<<tail->op<<"(0)"<<setw(11)<<tail->hp<<"(H)"<<setw(14)<<tail->mp<<"(M)"<<' '<<tail->em<<endl;
out.close();
}
}
void writelistbin()//--------------------------------------------------------------文件二进制写ok
{
ofstream out("data.bin",ios::out|ios::binary);
if (head)
{
node *p1=head;
2010年06月17日 14点06分
5
level 1
while (p1)
{
int l;
l=strlen(p1->name);
out.write((char *)&p1->name,l);out<<' ';
out.write((char *)&p1->op,4);out<<' ';
out.write((char *)&p1->hp,4);out<<' ';
l=strlen(p1->mp);
out.write((char *)&p1->mp,l);out<<' ';
l=strlen(p1->em);
out.write((char *)&p1->em,l);out<<endl;
p1=p1->next;
}
out.close();
}
}
~addr()
{
node *p1=head;
while(head)
{
p1=head;
head=head->next;
delete p1;;
}
}
};
//---------------------------------------------------------------------创建链表结束
istream& operator>>(istream &in,node *s)//------------------------------------->>的重载
{
in>>s->name>>s->op>>s->hp>>s->mp>>s->em;
return in;
}
addr s1;
void add(void)//--------------------------------------------------------------菜单定义1 添加
{
node *s=new node;
cin>>s;
s1.addsort(s);
}
void del()//-------------------------------------------------------------------菜单定义2 删除
{
char name[15];
cin>>name;
2010年06月17日 14点06分
6
level 1
s1.deletea(name);
}
void find()//------------------------------------------------------------------菜单定义4 查找
{
char name[15];int c;
node *p;
cout<<"1.By name"<<endl;
cout<<"2.By fristname"<<endl;
cin>>c;
cout<<"Input the key word:"<<endl;
cin>>name;
if(c==1)
{
p=s1.lookup(name);
if(p)p->show();
else cout<<"Sorry,can' find !"<<endl;
}
if(c==2)s1.lookup(name);
}
void modify()//--------------------------------------------------------------------菜单定义5 修改
{
char name[15];node *p;
char *ss[5]={"1.Change name",
"2.change office phone",
"3.Change home phone",
"4.Change mobile phone",
"5.Change email"};
cin>>name;
p=s1.lookup(name);
if(p)
{
p->show();
cout<<"Pick your choice:"<<endl;
for (int j=0;j<5;j++)cout<<ss[j]<<endl;
int i;
cout<<"Choice:";
cin>>i;
switch (i)
{
case 1:
char sn[15];
cout<<"input the new name:";
cin>>sn;
p->setname(sn);
p->show();
if (s1.gett()==1)s1.sort();
break;
case 2:
int so;
cout<<"input the new number:";
cin>>so;
p->setop(so);
2010年06月17日 14点06分
7
level 1
p->show();
if (s1.gett()==2)s1.sort();
break;
case 3:
int sh;
cout<<"input the new number:";
cin>>sh;
p->sethp(sh);
p->show();
break;
case 4:
char sm[15];
cout<<"input the new number:";
cin>>sm;
p->setmp(sm);
p->show();
break;
case 5:
char se[20];
cout<<"input the new email:";
cin>>se;
p->setem(se);
p->show();
break;
}
}else cout<<"srooy,can't find!"<<endl;
}
void addtxt()//--------------------------------------------------------------菜单定义6 批量添加
{
char name[15];
cin>>name;
s1.creatlist(name);
}
void writetxt()//--------------------------------------------------------------菜单定义7 批量写出
{
char name[15];
cin>>name;
s1.writelist(name);
}
void addsort()//--------------------------------------------------------------菜单定义8 排序
{
int k=s1.gett();
char ss;
if(k==1)
{
cout<<"Sort by name,do you want to change? [Y//N]"<<endl;
cin>>ss;
if (ss=='Y'||ss=='y') {s1.sett(2);s1.sort();}
}
if(k==2)
{
cout<<"Sort by officenumber,do you want to change? [Y//N]"<<endl;
cin>>ss;
if (ss=='Y') {s1.sett(1);s1.sort();}
2010年06月17日 14点06分
8
level 1
}
}
//---------------------------------------------------------------------------菜单定义及调用ok
int menu()
{
char *m[9]={"1.Add Record",
"2.Del Record",
"3.Show All",
"4.Search",
"5.Change",
"6.Add from Text",
"7.Write to Text",
"8.Sort",
"9.Quit"};
int i;
char c;
do
{
system("cls");
cout<<"What do you want:"<<endl;cout<<endl;
for (i=0;i<9;i++)cout<<m[i]<<endl;
cout<<"input(1_9):";
cin>>c;
}while (c<'0'||c>'9');
return(c-'0');
}
void work()
{
for(;;)
{
switch(menu())
{
case 1:
cout<<"Please write down the imformation:"<<endl;
add();
break;
case 2:
cout<<"Please write down the name:"<<endl;
del();
break;
case 3:
if(s1.gethead())s1.showall();
else cout<<"No record!"<<endl;
system("pause");
2010年06月17日 14点06分
9
level 1
break;
case 4:
cout<<"Please write down a name:"<<endl;
find();
system("pause");
break;
case 5:
cout<<"Please write down a name:"<<endl;
modify();
system("pause");
break;
case 6:
cout<<"Please input the text's name:";
addtxt();
break;
case 7:
cout<<"Please input the text's name:";
writetxt();
break;
case 8:
addsort();
system("pause");
break;
case 9:
s1.writelist("data1.txt");
s1.writelistbin();
exit(0);
}
}
}
//----------------------------------------------------------------------菜单调用结束
//----------------------------------------------------------------------主程序
void main()
{
s1.creatlist("data.dat");
work();
}
2010年06月17日 14点06分
10