标题:C++程序,求高手指教,不胜感激!!!!!!!!!!
只看楼主
loveshuang
Rank: 9Rank: 9Rank: 9
来 自:湖北武汉
等 级:蜘蛛侠
帖 子:270
专家分:1198
注 册:2010-11-14
结帖率:100%
已结贴  问题点数:20 回复次数:7 
C++程序,求高手指教,不胜感激!!!!!!!!!!
为什么执行中collegestudent的信息无法输出???


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct information{
    string name;            
    string address;
    string IDNO;
    string position;
    struct information* next;
};

class person{
private:
    information* list;
public:
    person(){}
    person(information* L){list=L;}
    void traverse();
    void destroy();
    ~person(){cout<<"析构函数被调用,person类的对象被释放!"<<endl;}
};
void person::traverse()
{
    information* p;
    cout<<"该组人员的信息为:"<<endl;
    while(p)
    {
        p=list->next;
        cout<<left<<setw(10)<<p->name
            <<setw(10)<<p->address
            <<setw(10)<<p->IDNO
            <<setw(10)<<p->position<<endl;
        list=list->next;
    }
}

void person::destroy()
{
    information* p;
    while(list)
    {
        p=list;
        list=list->next;
        delete p;
    }
}

class collegestudent:virtual public person
{
public:
    collegestudent(){}

    collegestudent(string s1,information* k):person(k){schoolnumber=s1;}
    string get1(){return schoolnumber;}
private:
    string schoolnumber;
};
class proffessor:virtual public person
{
public:
    proffessor(){}
    proffessor(string s2){marrycondition=s2;}
    proffessor(information* k):person(k){}
    string get2(){return marrycondition;}
private:
    string marrycondition;
};

class Doctor:public collegestudent,public proffessor
{
public:
    Doctor(){}
    Doctor (string s1,string s2,information* k):collegestudent(s1,k),proffessor(s2){}
    ~Doctor(){cout<<"析构函数被调用,Doctor类对象被释放!";}
};

int main()
{
    struct information*head1,*head2,*head3,*head4,*p,*q,*r,*s;
    int n,i;
    cout<<"现在设计第一组person的信息,请输入第一组人的人数:";
    cin>>n;
    head1=new information();
    head1->next=NULL;
    cout<<"请输入第一组人的相关信息:"<<endl;
    for(i=0;i<n;i++)
    {
        p=new information;
        cout<<"姓名:";
        cin>>p->name;
        cout<<"地址:";
        cin>>p->address;
        cout<<"身份证号:";
        cin>>p->IDNO;
        cout<<"身份:";
        cin>>p->position;
        p->next=head1->next;
        head1->next=p;
        cout<<endl;
    }
    person p1(head1);
    p1.traverse();
    p1.destroy();




    cout<<"现在设计第一组collegestudent的信息,请输入第一组人的人数:";
    cin>>n;
    head2=new information();
    head2->next=NULL;
    cout<<"请输入第一组collegestudent的相关信息:"<<endl;
    for(i=0;i<n;i++)
    {
        q=new information;
        cout<<"姓名:";
        cin>>q->name;
        cout<<"地址:";
        cin>>q->address;
        cout<<"身份证号:";
        cin>>q->IDNO;
        cout<<"身份:";
        cin>>q->position;
        q->next=head2->next;
        head2->next=q;
        cout<<endl;
    }
    collegestudent p2("0904681325",head2);
    p2.traverse();
    cout<<"学号:"<<p2.get1();
    p2.destroy();







    cout<<"现在设计第一组proffessor的信息,请输入第一组人的人数:";
    cin>>n;
    head3=new information;
    head3->next=NULL;
    cout<<"请输入第一组proffessor的相关信息:"<<endl;
    for(i=0;i<n;i++)
    {
        r=new information();
        cout<<"姓名:";
        cin>>r->name;
        cout<<"地址:";
        cin>>r->address;
        cout<<"身份证号:";
        cin>>r->IDNO;
        cout<<"身份:";
        cin>>r->position;
        r->next=head3->next;
        head3->next=r;
        cout<<endl;
    }
    proffessor p3("未婚");
    proffessor p4(head3);

    p4.traverse();
    cout<<"婚姻状况:"<<p4.get2();
    p4.destroy();




    cout<<"现在设计第一组Doctor的信息,请输入第一组人的人数:";
    cin>>n;
    head4=new information();
    head4->next=NULL;
    cout<<"请输入第一组Doctor的相关信息:"<<endl;
    for(i=0;i<n;i++)
    {
        s=new information;
        cout<<"姓名:";
        cin>>s->name;;
        cout<<"地址:";
        cin>>s->address;
        cout<<"身份证号:";
        cin>>s->IDNO;
        cout<<"身份:";
        cin>>s->position;
        s->next=head4->next;
        head4->next=s;
        cout<<endl;
    }
    Doctor p5("0904681326","已婚",head4);
    p5.traverse();
    cout<<"学号:"<<p5.get1()<<endl;
    p5.destroy();
    return 0;
}
搜索更多相关主题的帖子: 感激 指教 
2010-11-18 16:51
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
得分:5 
我是试过了。
可以输出。
但是输出完了后,你的程序就被操作系统给终止了。

你好好看看你有没有做了一些违规的操作。

南国利剑
2010-11-18 18:50
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
得分:5 
while(p)
    {
        p=list->next;
        cout<<left<<setw(10)<<p->name
            <<setw(10)<<p->address
            <<setw(10)<<p->IDNO
            <<setw(10)<<p->position<<endl;
        p=p->next;    ///这里指针乱了
    }

                                         
===========深入<----------------->浅出============
2010-11-18 21:40
loveshuang
Rank: 9Rank: 9Rank: 9
来 自:湖北武汉
等 级:蜘蛛侠
帖 子:270
专家分:1198
注 册:2010-11-14
得分:0 
回复 3楼 laoyang103
谢谢啦!!!
2010-11-18 22:39
loveshuang
Rank: 9Rank: 9Rank: 9
来 自:湖北武汉
等 级:蜘蛛侠
帖 子:270
专家分:1198
注 册:2010-11-14
得分:0 
回复 2楼 南国利剑
嗯,,,有违规的错误,呵呵呵。。
2010-11-18 22:39
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:605
专家分:2489
注 册:2009-4-8
得分:5 
程序代码:
// try.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct information{
    information();
    string name;            
    string address;
    string IDNO;
    string position;
    information* next;
};
information::information(){next = 0;}
class person{
private:
    information* list;
public:
    person(){}
    person(information* L){list=L;}
    void traverse();
    void destroy();
    ~person(){cout<<"析构函数被调用,person类的对象被释放!"<<endl;}
};
void person::traverse()
{
    information* p = list;
    cout<<"该组人员的信息为:"<<endl;
    do
    {
        cout<<left<<setw(10)<<p->name
            <<setw(10)<<p->address
            <<setw(10)<<p->IDNO
            <<setw(10)<<p->position<<endl;
        p=p->next;
    }while(p);
}

void person::destroy()
{
    information* p;
    while(list)
    {
        p=list;
        list=list->next;
        delete p;
    }
}

class collegestudent:virtual public person
{
public:
    collegestudent(){}

    collegestudent(string s1,information* k):person(k){schoolnumber=s1;}
    string get1(){return schoolnumber;}
private:
    string schoolnumber;
};
class proffessor:virtual public person
{
public:
    proffessor(){}
    proffessor(string s2){marrycondition=s2;}
    proffessor(information* k):person(k){}
    string get2(){return marrycondition;}
private:
    string marrycondition;
};

class Doctor:public collegestudent,public proffessor
{
public:
    Doctor(){}
    Doctor (string s1,string s2,information* k):collegestudent(s1,k),proffessor(s2){}
    ~Doctor(){cout<<"析构函数被调用,Doctor类对象被释放!";}
};

int main()
{
    struct information*head1,*head2,*head3,*head4,*p,*q,*r,*s;
    int n,i;
    cout<<"现在设计第一组person的信息,请输入第一组人的人数:";
    cin>>n;
    head1 = p = new information;
  //  head1->next = 0;
    cout<<"请输入第一组人的相关信息:"<<endl;
    for(i=0;i<n;i++)
    {
        if(i){ p->next=new information; p = p->next;}
        cout<<"姓名:";
        cin>>p->name;
        cout<<"地址:";
        cin>>p->address;
        cout<<"身份证号:";
        cin>>p->IDNO;
        cout<<"身份:";
        cin>>p->position;
  //      p->next = 0;
        cout<<endl;
    }
    person p1(head1);
    p1.traverse();
    p1.destroy();




    cout<<"现在设计第一组collegestudent的信息,请输入第一组人的人数:";
    cin>>n;
    head2=new information();
    head2->next=NULL;
    cout<<"请输入第一组collegestudent的相关信息:"<<endl;
    for(i=0;i<n;i++)
    {
        q=new information;
        cout<<"姓名:";
        cin>>q->name;
        cout<<"地址:";
        cin>>q->address;
        cout<<"身份证号:";
        cin>>q->IDNO;
        cout<<"身份:";
        cin>>q->position;
        q->next=head2->next;
        head2->next=q;
        cout<<endl;
    }
    collegestudent p2("0904681325",head2);
    p2.traverse();
    cout<<"学号:"<<p2.get1();
    p2.destroy();







    cout<<"现在设计第一组proffessor的信息,请输入第一组人的人数:";
    cin>>n;
    head3=new information;
    head3->next=NULL;
    cout<<"请输入第一组proffessor的相关信息:"<<endl;
    for(i=0;i<n;i++)
    {
        r=new information();
        cout<<"姓名:";
        cin>>r->name;
        cout<<"地址:";
        cin>>r->address;
        cout<<"身份证号:";
        cin>>r->IDNO;
        cout<<"身份:";
        cin>>r->position;
        r->next=head3->next;
        head3->next=r;
        cout<<endl;
    }
    proffessor p3("未婚");
    proffessor p4(head3);

    p4.traverse();
    cout<<"婚姻状况:"<<p4.get2();
    p4.destroy();




    cout<<"现在设计第一组Doctor的信息,请输入第一组人的人数:";
    cin>>n;
    head4=new information();
    head4->next=NULL;
    cout<<"请输入第一组Doctor的相关信息:"<<endl;
    for(i=0;i<n;i++)
    {
        s=new information;
        cout<<"姓名:";
        cin>>s->name;;
        cout<<"地址:";
        cin>>s->address;
        cout<<"身份证号:";
        cin>>s->IDNO;
        cout<<"身份:";
        cin>>s->position;
        s->next=head4->next;
        head4->next=s;
        cout<<endl;
    }
    Doctor p5("0904681326","已婚",head4);
    p5.traverse();
    cout<<"学号:"<<p5.get1()<<endl;
    p5.destroy();
    return 0;
}
改好了一部分,没时间再帮你测试下面的部分,楼主先前应该是学C语言的吧,有空可以看看C++的编程方格。你的链表没处理好,可以使用标准库里的。

迭代的是人,递归的是神。
2010-11-19 08:23
xjm2008
Rank: 1
等 级:新手上路
帖 子:2
专家分:5
注 册:2010-11-19
得分:5 
学习c##怎么开始呀?
2010-11-19 09:22
sycy135
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-8-18
得分:0 
代码没问题啊!是不是你的编译有问题。我建议使用英文的
2011-08-21 09:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-326150-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.425941 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved