回 帖 发 新 帖 刷新版面

主题:求助高手,链表输入输出问题。。

求助大虾,下面代码的name有问题,还有showstudent也有问题,但本菜鸟不知道出什么问题,请教各位大虾下面的程序哪里出错,要怎么改?在此先谢谢了



#include <iostream.h>
class student
{
    public:
        int num;
        char name[10];
        double china;
        double eng;
        double math;
        student *next;
        student *head;
};

student *create()
{
    int n;
    student *p,*s;
    p=new student;
    p->next=NULL;
    
    cout<<"输入学生的人数:";
    cin>>n;
    for(int i=0;i<n;i++)
    {
        s=new student;
        cout<<"输入第"<<i+1<<"学生的学号和姓名:";
        cin>>s->num;
        cin>>s->name[10];
        cout<<"输入第"<<i+1<<"学生的中英数成绩:";
        cin>>s->china;
        cin>>s->eng;
        cin>>s->math;
        s->next=p;
        p=s;
    }
    return p;
}
void showstudent(student *head)
{
    student *p;
    p=head->next;
    while(p!=NULL)
    {
        cout<<p->num;
        cout<<p->name[10];
        cout<<p->china;
        cout<<p->eng;
        cout<<p->math;
        p=p->next;
    }
    cout<<endl;
}
void main()
{
    student *head=NULL;
    head=create();
    showstudent(head);
}

回复列表 (共4个回复)

沙发

#include <iostream.h>
#include <stdio.h>
class student
{
    public:
        int num;
        char name[10];
        double china;
        double eng;
        double math;
        student *next;
};

student *create(student *head,int n)
{
    student *p,*s;
    head=new student;
    p=head;
    for(int i=0;i<n;i++)
    {
        s=new student;
        cout<<"输入第"<<i+1<<"学生的学号和姓名:"<<endl;
        cin>>s->num;
        gets(s->name);
        cout<<"输入第"<<i+1<<"学生的中英数成绩:"<<endl;
        cin>>s->china;
        cin>>s->eng;
        cin>>s->math;
        p->next=s;
        p=s;
    }
    p->next=NULL;
    return head;
}
void showstudent(student *head)
{
    student *p;
    p=head->next;
    while(p!=NULL)
    {
        puts(p->name);
        cout<<p->num<<"号:"<<endl;
        cout<<"语文:"<<p->china<<endl;
        cout<<"英语:"<<p->eng<<endl;
        cout<<"数学:"<<p->math<<endl;
        p=p->next;
    }
    cout<<endl;
}
void main()
{
    student *head;
    int n;
    cout<<"输入总人数:";
    cin>>n;
    head=create(head,n);
    showstudent(head);
}
你的链表创建有问题,流对字符的处理我不太清楚,我用了stdio里的函数输出的

板凳

非常感谢。。。我一开始是学的C++,很少用stdio这个函数。。
找了很多数据结构的书,但很多都是C语言的版本的,比较少C++版本。。

3 楼


#include <iostream>
#include<string>
using namespace std;
class student
{
    public:
        int num;
        string name;
        double china;
        double eng;
        double math;
        student *next;
        student *head;
};

student *create(student *head)
{
    int n;
    head= new student;
    student *p,*s;
    cout<<"请输入学生的学号"<<endl;
    cin>>head->num;
    cout<<"please enter the name"<<endl;
    cin>>head->name;
    cout<<"please enter the chinse<<endl";
    cin>>head->china;
    cout<<"please enter the english"<<endl;
    cin>>head->eng;
    cout<<"please enter the math"<<endl;
    cin>>head->math;
    head->next=NULL;
    cout<<"输入学生的人数:";
    cin>>n;
    p=head;
  for(int i=0;i<n;i++)
    {
        s=new student;
        cout<<"输入第"<<i+1<<"学生的学号和姓名:";
        cin>>s->num;
        cin>>s->name;
        cout<<"输入第"<<i+1<<"学生的中英数成绩:";
        cin>>s->china;
        cin>>s->eng;
        cin>>s->math;
        s->next=NULL;
        p->next=s;
        p=s;
    }
    return head;
}
void showstudent(student *head)
{
    student *p;
    p=head;
    while(p!=NULL)
    {
        cout<<p->num<<" ";
        cout<<p->name<<"  ";
        cout<<p->china<<"  ";
        cout<<p->eng<<"  ";
        cout<<p->math<<"  ";
        cout<<endl;
        p=p->next;
    }
    cout<<endl;
}
void main()
{
    student *head=NULL;
    head=create(head);
    showstudent(head);
}

4 楼


发现很多人都不喜欢用注释

比如修改LZ的程序

我觉得还是在修改的地方加上注释比较好

呵呵



我来回复

您尚未登录,请登录后再回复。点此登录或注册