新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 C/C++编程思想 』 → C++达人进,帮我看下问题出在哪?紧急。救命。。。 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 4233 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: C++达人进,帮我看下问题出在哪?紧急。救命。。。 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     kidd231 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:0
      积分:52
      门派:XML.ORG.CN
      注册:2007/1/11

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给kidd231发送一个短消息 把kidd231加入好友 查看kidd231的个人资料 搜索kidd231在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看kidd231的博客楼主
    发贴心情 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;
    }


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/1/11 10:46:00
     
     一分之千 帅哥哟,离线,有人找我吗?射手座1984-11-30
      
      
      威望:1
      等级:研一(随老板参加了WWW大会还和Tim Berners-Lee合了影^_^)
      文章:632
      积分:4379
      门派:XML.ORG.CN
      注册:2006/12/31

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给一分之千发送一个短消息 把一分之千加入好友 查看一分之千的个人资料 搜索一分之千在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看一分之千的博客2
    发贴心情 
    第一 名字没有必要使用#结束吧?设置一个标志久可以了。
    第二  这里该一下
             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;
     }

    先改改看。。。。

    ----------------------------------------------
    越学越无知

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/1/11 11:31:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客3
    发贴心情 
    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;
    };

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

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/1/11 14:35:00
     
     longshentailang 帅哥哟,离线,有人找我吗?
      
      
      威望:1
      等级:计算机学士学位
      文章:325
      积分:2990
      门派:XML.ORG.CN
      注册:2006/6/20

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给longshentailang发送一个短消息 把longshentailang加入好友 查看longshentailang的个人资料 搜索longshentailang在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看longshentailang的博客4
    发贴心情 
    if(strcmp(p1->name,name)==0)
    {
        //这里面应该考虑多种情况
       //当内容只有一条的时候,应该也有多种情况
    }

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

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/1/12 17:50:00
     
     longshentailang 帅哥哟,离线,有人找我吗?
      
      
      威望:1
      等级:计算机学士学位
      文章:325
      积分:2990
      门派:XML.ORG.CN
      注册:2006/6/20

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给longshentailang发送一个短消息 把longshentailang加入好友 查看longshentailang的个人资料 搜索longshentailang在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看longshentailang的博客5
    发贴心情 
    还有我觉得你应该对day和month进行判断和限制,如day最多为31天,month为12个月。
    而且打印格式也要调整一下了
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/1/12 17:53:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客6
    发贴心情 
    以下是引用longshentailang在2007-1-12 17:50:00的发言:
    if(strcmp(p1->name,name)==0)
      {
         //这里面应该考虑多种情况
        //当内容只有一条的时候,应该也有多种情况
      }

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


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

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/1/13 13:59:00
     
     longshentailang 帅哥哟,离线,有人找我吗?
      
      
      威望:1
      等级:计算机学士学位
      文章:325
      积分:2990
      门派:XML.ORG.CN
      注册:2006/6/20

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给longshentailang发送一个短消息 把longshentailang加入好友 查看longshentailang的个人资料 搜索longshentailang在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看longshentailang的博客7
    发贴心情 
    调试了一下,发现问题比较多,集中在delete例程中。
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/1/13 16:29:00
     
     longshentailang 帅哥哟,离线,有人找我吗?
      
      
      威望:1
      等级:计算机学士学位
      文章:325
      积分:2990
      门派:XML.ORG.CN
      注册:2006/6/20

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给longshentailang发送一个短消息 把longshentailang加入好友 查看longshentailang的个人资料 搜索longshentailang在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看longshentailang的博客8
    发贴心情 

    具体代码如下:

    #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例程中的打印格式修改了一下。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/1/13 16:44:00
     
     longshentailang 帅哥哟,离线,有人找我吗?
      
      
      威望:1
      等级:计算机学士学位
      文章:325
      积分:2990
      门派:XML.ORG.CN
      注册:2006/6/20

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给longshentailang发送一个短消息 把longshentailang加入好友 查看longshentailang的个人资料 搜索longshentailang在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看longshentailang的博客9
    发贴心情 

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

    输入我朋友的名字(输入"#"表示结束):
    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

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/1/13 16:47:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/13 16:24:20

    本主题贴数9,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    113.281ms