药王孙思邈电视剧:C语言高手请帮个忙

来源:百度文库 编辑:神马品牌网 时间:2024/04/27 16:42:27
帮我看下SORT1函数算法错在哪里.其它函数都没问题,这里不解决,下面的程序没心情去做啊!各位,拜托了!
#include<stdio.h>
#include<string.h>
#include<alloc.h>
#define LEN sizeof(struct stu)
struct stu
{int num;
char name[20];
char sex[5];
int score[10];
struct stu *next;};
int n=0;
struct stu *new(int i,int j)
{int a,b=1,flag;
struct stu *head,*p1,*p2;
head=p1=p2=NULL;
do
{n+=1;
flag=0;
p1=(struct stu *)malloc(LEN);
printf("Input stu%d ID:",b++);
scanf("%d",&p1->num);
printf("Input name:");
scanf("%s",p1->name);
printf("Input sex(boy or girl):");
loop:
scanf("%s",p1->sex);
if(strcmp(p1->sex,"boy")==0||strcmp(p1->sex,"girl")==0)flag=1;
if(flag==0)
{printf("Input error,input sex again:");
goto loop;}
printf("Input scores:");
for(a=0;a<j;a++)
scanf("%d",&p1->score[a]);
if(n==1)head=p1;
else p2->next=p1;
p2=p1;}
while(b!=i+1);
p2->next=NULL;
return(head);}

struct stu *print(struct stu *head,int j)
{struct stu *p;
int a,b=1;
p=head;
printf("The records are:\n");
do
{printf("%06d %s %-4s",p->num,p->name,p->sex);
for(a=0;a<j;a++)
printf("%3d",p->score[a]);
printf("\n");
p=p->next;
b++;}
while(p!=NULL);
return(head);}

struct stu *sort1(struct stu *head,int j)
{struct stu *p0;
struct stu *p1;
struct stu *p2;
p0=p1=head;
do
{p2=p1->next;
do
{if(p1->num>p2->num)
{p0=p2;p2=p1;p1=p0;}
p0->next=p2;
p2=p2->next;}
while(p2!=NULL);
p1=p1->next;}
while(p1->next!=NULL);
print(p0,j);
}

main()
{int number,course;
struct stu *p;
printf("Input students' number:");
scanf("%d",&number);
printf("Input courses number:");
scanf("%d",&course);
p=new(number,course);
print(p,course);
sort1(p,course);}
无聊的不要回答,谢谢!
此程序是对学生成绩管理系统,要求首先输入学生人数,考试科目数,然后输入学号,姓名,性别,各科成绩.print函数显示学生考试信息.sort1函数是对学生按学号从小到大顺序排列.

问题出在你只交换了p2和p1的指针的值
这是没有意义的
if(p1->num>p2->num)
{p0=p2;p2=p1;p1=p0;}
我们的目的是交换p2和p1所指向的节点
所以
把p0=p2;p2=p1;p1=p0;
改为*p0=*p2;*p2=*p1;*p1=*p0;

就ok啦^-^

这么长呀,你也发到 www.dayi.net 试试。

嗯。初学者常犯的错误。

我很无聊