找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1967|回复: 5

[求助]关与结构的问题

[复制链接]
发表于 2009-11-3 03:18:42 | 显示全部楼层 |阅读模式 IP:江苏扬州
程序的目的是将学生的学号进行由小到大的排序。
小弟用边输入边排序的方法,但不知为何不能正确执行,虽然能通过编译,想很很久也想不到问题出在哪?望各位大大帮帮忙,正在学习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());
}
发表于 2009-11-3 03:18:43 | 显示全部楼层 IP:江苏扬州
temp=head;
head=ps;
ps=temp; ???

楼主的链表的结构看着头晕
回复

使用道具 举报

发表于 2009-11-3 03:18:44 | 显示全部楼层 IP:江苏扬州
哦,这个是交换链首的代码啊!temp作为交换两个指针的临时变量啊!
回复

使用道具 举报

发表于 2009-11-3 03:18:45 | 显示全部楼层 IP:江苏扬州
LZ,错的我没话说.
我终于明白有时候改代码比写代码还难
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后面,再跟后面的元素比较大小

if(ps->student.getID()>head1->student.getID())
{
temp=head1;
head1=ps;
ps=temp;
head1=ps;
}
else
break;
这俩地方错了,自己看看吧.有看不懂的语句.我试图改了,但他一环套一环.非常不规范
希望楼主抓抓代码规范
回复

使用道具 举报

发表于 2009-11-3 03:18:46 | 显示全部楼层 IP:江苏扬州
汗...不好意思song4大哥,麻烦到你了!顺便问一下,该如何代码规范啊?
你可以加我吗?QQ:199462359,跟你学学东西...嘻....
回复

使用道具 举报

发表于 2009-11-3 03:18:47 | 显示全部楼层 IP:江苏扬州
112021999
msn song4@163.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 13:26 , Processed in 0.130081 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表