注册 登录
编程论坛 C++教室

一个链表问题求高手找错

红糖水 发布于 2013-04-26 20:29, 480 次点击
#include <iostream>
#include <string>
using namespace std;
class student
{
    public:
    student();
    void aver(int math,int english,int c,int computer);
    void showdate();
    student *next;
    private:
    string name;
    string sex;
    int ID;
    int math;
    int english;
    int c;
    int computer;
    int average;
};
student::student()
{
    cin>>name>>sex>>ID>>math>>english>>c>>computer;
    aver(math,english,c,computer);
}
void student::aver(int math,int english,int c,int computer)
{
    average=(math+english+c+computer)/4;
}
void student::showdate()
{
    cout<<name<<sex<<ID<<math<<english<<c<<computer<<average;
}
int main()
{
    student *head,*p1,*p2;
    int n;
    cout<<"Please Input the Number of Students:\n";
    cin>>n;
    cout<<"Please input"<< n <<"student info: Name  ID  Sex  Math  English  C  Computer"<<endl;
    p1=new student;
    head=NULL;
    p2=p1;
    for(int i=1;i<=n;i++)
    {
        if(head==NULL)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=new student;
    }
    p2->next=NULL;
    for(int i=1;i<=n;i++)
    {
        head->showdate();
        head=head->next;
    }
    delete []p1;
}
1 回复
#2
邓士林2013-04-26 21:07
cout<<"Please input"<< n <<"student info: Name  ID  Sex  Math  English  C  Computer"<<endl;
    p1=new student;
    head=NULL;
    p2=p1;
    for(int i=1;i<=n;i++)
    {
        if(head==NULL)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=new student;
    }
    p2->next=NULL;
你这一部分都没有输入数据啊!你怎么往下进行的,另外还有几个个别小错误,给你修改了下:
#include <iostream>
#include <string>
using namespace std;
class student
{
    public:
    student();
    void aver(int math,int english,int c,int computer);
    void showdate();
    student *next;
    private:
    string name;
    string sex;
    int ID;
    int math;
    int english;
    int c;
    int computer;
    int average;
};
student::student()
{
    cin>>name>>sex>>ID>>math>>english>>c>>computer;
    aver(math,english,c,computer);
}
void student::aver(int math,int english,int c,int computer)
{
    average=(math+english+c+computer)/4;
}
void student::showdate()
{
    cout<<name<<sex<<ID<<math<<english<<c<<computer<<average;
}
int main()
{
    student *head,*p1,*p2;
    int n;
    cout<<"Please Input the Number of Students:\n";
    cin>>n;
    cout<<"Please input"<< n <<"student info: Name  ID  Sex  Math  English  C  Computer"<<endl;
    p1=new student;
    head=NULL;
    p2=p1;
    for(int i=1;i<=n;i++)
    {
        if(head==NULL)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=new student;
    }
    p2->next=NULL;
    for(i=1;i<=n;i++)
    {
        head->showdate();
        head=head->next;
    }
    delete []p1;
    return 0;
}
1