[求助]关与结构的问题
程序的目的是将学生的学号进行由小到大的排序。小弟用边输入边排序的方法,但不知为何不能正确执行,虽然能通过编译,想很很久也想不到问题出在哪?望各位大大帮帮忙,正在学习C++
程序如何:
#include<iostream.h>
class Student
{
public:
Student()
{
setdate();
}
void setdate()
{
int a;
cout<<"输入所要建立的学生学号:";
cin>>a;
ID=a;
cout<<"输入学生的姓名:"<<flush;
cin>>name;
float b;
cout<<"输入A课程的成绩:";
cin>>b;
chinese=b;
float c;
cout<<"输入B课程的成绩:";
cin>>c;
math=c;
float d;
cout<<"输入C课程的成绩:";
cin>>d;
english=d;
total=english+chinese+math;
average=total/3;
}
int getID()
{
return ID;
}
char* getname()
{
return name;
}
float getchin()
{
return chinese;
}
float getmath()
{
return math;
}
float geteng()
{
return english;
}
float gettotal()
{
return total;
}
float getaverage()
{
return average;
}
protected:
int ID;
char name[20];
float chinese;
float math;
float english;
float total;
float average;
}; //类定义
struct Date
{
Student student;
Date *next;
}; //结构定义
Date *List()
{
int n=5;
Date *head; // 链首定义
head=NULL;
Date *temp;
Date *privoit; //临时变量
Date *head1;
Date *ps; //结点
ps=new Date;
if(head==NULL)
{
head=ps;
}
cout<<endl;
ps=new Date;
if(ps->student.getID()<head->student.getID())
{
temp=head;
head=ps;
ps=temp;
}
head->next=ps;
ps->next=NULL;//初始链
for(int i=0;i<n-2;i++)
{
cout<<endl;
ps=new Date;
if(ps->student.getID()<head->student.getID())
{
temp=head;
head=ps;
ps=temp;
}
privoit=head->next;
head->next=ps;
ps->next=privoit;
head1=head->next; //插入到head后面,再跟后面的元素比较大小
for(int j=0;j<=i;j++)
{
head1=head1->next;
if(ps->student.getID()>head1->student.getID())
{
temp=head1;
head1=ps;
ps=temp;
head1=ps;
}
else
break;
}
}
return (head);
}
void Print(Date *k)
{
cout<<"学号 姓名 A科成绩 B科成绩 C科成绩 总分成绩 平均成绩 "<<endl;
while(k!=NULL)
{
cout<<k->student.getID()<<" "<<k->student.getname()<<" "<<k->student.getchin()<<" "<<k->student.getmath()
<<" "<<k->student.geteng()<<" "<<k->student.gettotal()<<" "<<k->student.getaverage()<<endl;
k=k->next;
}
}
void main()
{
Print(List());
}