以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  C++达人进,帮我看下问题出在哪?紧急。救命。。。  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=42184)


--  作者:kidd231
--  发布时间:1/11/2007 10:46:00 AM

--  C++达人进,帮我看下问题出在哪?紧急。救命。。。
期末考试试验的题目

电话本管理系统

没有语法错误,但是程序没法进行下去。。

帮忙看看,谢谢。

#include<iostream>
#include<iomanip>
#include<string.h>
using namespace std;
struct data
{
int year;int month;int day;
};

struct friends
{
char name[10];
char sex;
char tel[12];
data birthday;
friends* next;
};

friends* create()
{

int n;
struct friends *head,*p1,*p2;
n=0;
p1=p2=new friends;
cout<<"输入我朋友的名字(输入\"#\"表示结束):"<<endl;
cin>>p1->name;
cout<<"输入我朋友的性别,电话,生日(输入\"#\"表示结束):"<<endl;
cin>>p1->sex;
cin>>p1->tel;
cin>>p1->birthday.year>>p1->birthday.month>>p1->birthday.day;
head=NULL;
while(strcmp(p1->name,"#")!=0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=new friends;
cout<<"输入我朋友的名字(输入\"#\"表示结束):"<<endl;
cin>>p1->name;
if(strcmp(p1->name,"#")!=0)
{
cout<<"继续输入我朋友的性别,电话,生日(输入\"#\"表示结束):"<<endl;
cin>>p1->sex;
cin>>p1->tel;
cin>>p1->birthday.year>>p1->birthday.month>>p1->birthday.day;
}
}
p2->next=NULL;
return head;
}
void print(struct friends *head)
{
struct friends *p;
p=head;
cout<<setw(11)<<"name"<<setw(5)<<"sex"<<setw(12)<<"telNO."<<setw(16)<<"birthday"<<endl;
while(p==NULL)
{
cout<<setw(11)<<p->name<<setw(4)<<p->sex<<setw(16)<<p->tel<<" ";
cout<<setw(5)<<p->birthday.year;
cout<<setw(3)<<p->birthday.month;
cout<<setw(2)<<p->birthday.day<<endl;
p=p->next;
}
}

struct friends *DELETE(struct friends *head,char name[])
{
struct friends *p1,*p2;
if(head==NULL)
{cout<<"list null!DO NOT DELETE!";
return(head);
}
p1=head;
while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
p2->next=p1->next;
delete p1;
cout<<"delete:"<<name;
}
else
cout<<name<<"has not been found!"<<endl;
return head;
}
struct friends *insert(struct friends *head,struct friends *frd)
{
int n=0;
struct friends *p0,*p1,*p2;
p1=head;
p0=frd;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
while(strcmp(p0->name,p1->name)>0&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p0->name,p1->name)<=0)
{
if(head==p1)
{
head=p0;
p0->next=p1;
}
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{
p1->next=p0;
p0->next=NULL;
}
n++;
return head;
}

void main()
{
char del[10];
friends *s,*t;
s=create();
print(s);
cout<<"input the name you want to delete:";
cin>>del;
s=DELETE(s,del);
cout<<endl;
print(s);
cout<<"input the name you want to inserted:";
t=new friends;
cin>>t->name>>t->sex>>t->tel>>t->birthday.year>>t->birthday.month>>t->birthday.day;
s=insert(s,t);
print(s);
delete t;
}


--  作者:一分之千
--  发布时间:1/11/2007 11:31:00 AM

--  
第一 名字没有必要使用#结束吧?设置一个标志久可以了。
第二  这里该一下
         void print(struct friends *head)
{
 struct friends *p;
 p=head;
 cout<<setw(11)<<"name"<<setw(5)<<"sex"<<setw(12)<<"telNO."<<setw(16)<<"birthday"<<endl;
 while(p->name!=NULL) //这个地方判断条件不对
 {
第三  这里也应该判断一下 考虑只有一条内容的时候
 if(strcmp(p1->name,name)==0)
 {
  p2->next=p1->next; //这里
  delete p1;
  cout<<"delete:"<<name;
 }

先改改看。。。。


--  作者:卷积内核
--  发布时间:1/11/2007 2:35:00 PM

--  
struct data
{
 char year[4];char month[2];char day[2]; //接收键盘为字符
};

struct friends
{
 char name[10];
 char sex[2]; //此处没有空间
 char tel[12];
 data birthday;
 friends* next;
};

你这里定义出错了,你要给你输入的字符分配相应的空间。


--  作者:longshentailang
--  发布时间:1/12/2007 5:50:00 PM

--  
if(strcmp(p1->name,name)==0)
{
    //这里面应该考虑多种情况
   //当内容只有一条的时候,应该也有多种情况
}

从你的代码来看,主要问题在delete例程,print例程也看看


--  作者:longshentailang
--  发布时间:1/12/2007 5:53:00 PM

--  
还有我觉得你应该对day和month进行判断和限制,如day最多为31天,month为12个月。
而且打印格式也要调整一下了
--  作者:卷积内核
--  发布时间:1/13/2007 1:59:00 PM

--  
以下是引用longshentailang在2007-1-12 17:50:00的发言:
if(strcmp(p1->name,name)==0)
  {
     //这里面应该考虑多种情况
    //当内容只有一条的时候,应该也有多种情况
  }

从你的代码来看,主要问题在delete例程,print例程也看看


你调试一下就知道他问什么了,这些并不重要。


--  作者:longshentailang
--  发布时间:1/13/2007 4:29:00 PM

--  
调试了一下,发现问题比较多,集中在delete例程中。
--  作者:longshentailang
--  发布时间:1/13/2007 4:44:00 PM

--  

具体代码如下:

#include<iostream>
#include<iomanip>
#include<string.h>

using namespace std;

struct data
{
 int year;int month;int day;
};

struct friends
{
 char name[10];
 char sex;
 char tel[12];
 data birthday;
 friends* next;
};

friends* create()
{
 
 int n;
 struct friends *head,*p1,*p2;
 n=0;
 p1=p2=new friends;

 cout<<"输入我朋友的名字(输入\"#\"表示结束):"<<endl;
 cin>>p1->name;
  
 cout<<"输入我朋友的性别,电话,生日(输入\"#\"表示结束):"<<endl;
 cin>>p1->sex;
 
 cin>>p1->tel;
  
 cin>>p1->birthday.year>>p1->birthday.month>>p1->birthday.day;

 head=NULL;
 while(strcmp(p1->name,"#")!=0)
 {
  n++;
  if(n==1)
   head=p1;
  else
   p2->next=p1;
  p2=p1;
  p1=new friends;  
  cout<<"输入我朋友的名字(输入\"#\"表示结束):"<<endl;

  cin>>p1->name;
  if(strcmp(p1->name,"#")!=0)
  {
   cout<<"继续输入我朋友的性别,电话,生日(输入\"#\"表示结束):"<<endl;
   cin>>p1->sex;
   cin>>p1->tel;
   cin>>p1->birthday.year>>p1->birthday.month>>p1->birthday.day;
  }
 }
 p2->next=NULL;
 
 return head;
}
void print(struct friends *head)
{
 struct friends *p;
 p=head;
 
 cout<<setw(7)<<"name"<<setw(5)<<"sex"<<setw(11)<<"telNO."<<setw(13)<<"birthday"<<endl;
 
 while(p!=NULL)
 {
  cout<<setw(5)<<p->name<<setw(6)<<p->sex<<setw(11)<<p->tel<<" ";
  cout<<setw(10)<<p->birthday.year;
  cout<<setw(3)<<p->birthday.month;
  cout<<setw(3)<<p->birthday.day<<endl;
  
  if (p == NULL)
  {
   break;
  }
  p=p->next;
 }
}

struct friends *DELETE(struct friends *head,char name[])
{
 struct friends *p1,*p2;
 
 p2=head; 
 if(head==NULL)
 {
  cout<<"list null!DO NOT DELETE!";
  return(head);
 }
 p1=head;
      
 while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
 {
  p2=p1;
  p1=p1->next;
 }
      
 if(strcmp(p1->name,name)==0)
 {
  if (p1->next == NULL)
  {
   if (p1 == p2)
   {
    delete p1;
   
   p2->next = NULL;
   
   head =NULL;
   }
   else
   {
    delete p1;
    p2->next=NULL;
   }
  }
  else if (p2->next==p1)
  {
   p2->next=p1->next;
   delete p1;
  }
  else
  {
   p2=p1->next;
   delete p1;
   p1 = p2;
   head = p2;
  }
  cout<<"delete:"<<name;
 }
 else
  cout<<name<<"has not been found!"<<endl;
 
 return head;
}
struct friends *insert(struct friends *head,struct friends *frd)
{
 int n=0;
 struct friends *p0,*p1,*p2;
 p1=head;
 p0=frd;
 if(head==NULL)
 {
  head=p0;
  p0->next=NULL;
  return head;
 }
 else
  while(strcmp(p0->name,p1->name)>0&&p1->next!=NULL)
  {
   p2=p1;
   p1=p1->next;
  }
  if(strcmp(p0->name,p1->name)<=0)
  {
   if(head==p1)
   {
    head=p0;
    p0->next=p1;
   }
   else
   {
    p2->next=p0;
    p0->next=p1;
   }
  }
  else
  {
   p1->next=p0;
   p0->next=NULL;
  }
  n++;
  return head;
}

void main()
{
 char del[10] = {0};
 friends *s,*t;
 s=create();
 print(s);
 cout<<"input the name you want to delete:";
 cin>>del;
 s=DELETE(s,del);
 cout<<endl;
 if (s==NULL)
 {
  cout<<"Nothing left to delete..."<<endl;
 }
 print(s);
 cout<<"input the name you want to inserted:";
 t=new friends;
 cin>>t->name>>t->sex>>t->tel>>t->birthday.year>>t->birthday.month>>t->birthday.day;
 s=insert(s,t);
 print(s);
 delete t;
}
我只修改了delete例程,同时对print例程中的打印格式修改了一下。


--  作者:longshentailang
--  发布时间:1/13/2007 4:47:00 PM

--  

以下是我进行测试的结果:

输入我朋友的名字(输入"#"表示结束):
a
输入我朋友的性别,电话,生日(输入"#"表示结束):
m
5555
1999 10 22
输入我朋友的名字(输入"#"表示结束):
b
继续输入我朋友的性别,电话,生日(输入"#"表示结束):
w
6666
2000 01 02
输入我朋友的名字(输入"#"表示结束):
c
继续输入我朋友的性别,电话,生日(输入"#"表示结束):
m
5989
2002 11 12
输入我朋友的名字(输入"#"表示结束):
d
继续输入我朋友的性别,电话,生日(输入"#"表示结束):
w
9999
2005 05 05
输入我朋友的名字(输入"#"表示结束):
e
继续输入我朋友的性别,电话,生日(输入"#"表示结束):
m
9977
2004 02 26
输入我朋友的名字(输入"#"表示结束):
f
继续输入我朋友的性别,电话,生日(输入"#"表示结束):
w
4444
2007 01 13
输入我朋友的名字(输入"#"表示结束):
#
   name  sex     telNO.     birthday
    a     m       5555       1999 10 22
    b     w       6666       2000  1  2
    c     m       5989       2002 11 12
    d     w       9999       2005  5  5
    e     m       9977       2004  2 26
    f     w       4444       2007  1 13
input the name you want to delete:c
delete:c
   name  sex     telNO.     birthday
    a     m       5555       1999 10 22
    b     w       6666       2000  1  2
    d     w       9999       2005  5  5
    e     m       9977       2004  2 26
    f     w       4444       2007  1 13
input the name you want to inserted:A
m 3322 2008 11 25
   name  sex     telNO.     birthday
    A     m       3322       2008 11 25
    a     m       5555       1999 10 22
    b     w       6666       2000  1  2
    d     w       9999       2005  5  5
    e     m       9977       2004  2 26
    f     w       4444       2007  1 13


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
234.009ms