当前位置: 代码迷 >> C语言 >> [求助]程序中的排序函数起作用?(链表中的)
  详细解决方案

[求助]程序中的排序函数起作用?(链表中的)

热度:270   发布时间:2006-05-06 23:00:00.0
[求助]程序中的排序函数起作用?(链表中的)

程序中出现的排序函数竟然不执行,出来的结果跟原来没有时候一样;
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct term
{
float coef;
int expn;
struct term * next;
};

struct term *Creat(char ch)
{
struct term *p, *s,*r;
float x; int y;
p=(struct term*)malloc(sizeof(struct term));
p->next=NULL;
printf("\n",ch);
scanf("%f, %d",&x,&y);
while(x!=0)
{
s=(struct term*)malloc(sizeof(struct term));
s->coef=x;
s->expn=y;
s->next=NULL;
if(p->next==NULL)
{
p->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
scanf("%f, %d",&x,&y);
}
return(p);
}
void sort(struct term *La)
{ struct term *p,*q,*s;
for(p=La->next;p;p=p->next)
{ for(q=p->next;q;q=q->next)
{if(p->expn>q->expn)
{s=(struct term*)malloc(sizeof(struct term));
s->coef=q->coef;
s->expn=q->expn;
s->next=NULL;
q->coef=p->coef;
q->expn=p->expn;
p->coef=s->coef;
p->expn=s->expn;}
else
if(p->expn==q->expn)
{s=(struct term*)malloc(sizeof(struct term));
s->coef=p->coef+q->coef;
s->expn=p->expn;
s->next=NULL;
p=s; ( 就这个函数出现错误,难道是我的指针用错了?)
s->next=p->next;
free(p);
free(q);}
else
{return;}
}
}
}

struct term *Add(struct term *f,struct term *g)
{
struct term *fg;
struct term *t,*q,*s,*r;
float m;
t=f->next;
q=g->next;
fg=r=(struct term*)malloc(sizeof(struct term));
fg->next=NULL;
while(t&&q)
{
if(t->expn==q->expn)
{
m=t->coef+q->coef;
if(m!=0)
{
s=(struct term*)malloc(sizeof(struct term));
s->coef=m;
s->expn=t->expn;
s->next=NULL;
}
t=t->next;
q=q->next;
}
else
if(t->expn<q->expn)
{
s=(struct term*)malloc(sizeof(struct term));
s->coef=t->coef;
s->expn=t->expn;
s->next=NULL;
t=t->next;
}
else
{
s=(struct term*)malloc(sizeof(struct term));
s->coef=q->coef;
s->expn=q->expn;
s->next=NULL;
q=q->next;
}

if(fg->next==NULL)
{
fg->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
}
r->next=t? t:q;
return (fg);
}


void print(struct term *f)
{
struct term *t;
t=f->next;
if(!f->next){
printf("\n"); return;
}
while(t)
{ if(t->coef>0&&f->next!=t) printf("+");
if(t->expn==0)
printf("%f",t->coef);
else
printf("%f*X^%d",t->coef,t->expn);
t=t->next;
}
printf("\n");
}
struct term *Mul(struct term *f,struct term *g)
{
struct term *h;
struct term *t,*q,*s,*r;
h=(struct term*)malloc(sizeof(struct term));
h->next=NULL;
r=(struct term*)malloc(sizeof(struct term));
r->next=NULL;
for(t=f->next;t;t=t->next)
{
for(q=g->next;q;q=q->next)
{
s=(struct term*)malloc(sizeof(struct term));
r->next=s;
s->coef=q->coef*t->coef;
s->expn=q->expn+t->expn;
s->next=NULL;
h=Add(r,h);
}

}
return(h);
}

void Open()
{
printf("*****************************\n");
printf(" phoyle's add and mulfiple \n");
printf("yun nan cai jing university ji ke 04-1 ban weiyinggui\n");
printf("*****************************\n");
printf("please choose operation:\n");
printf("0.quit\n");
printf("1.addtion of two phoyle\n");
printf("2.mutiple of two phoyle\n");
printf("3.user operate\n");
}
void Readme()
{
printf("**********user operation***********\n");
printf("1.when input only input phoyle's coef and expn.\n");
printf("2.please input as rise form.\n");
printf("3.for example \"1, 1 2, 2 0, 0\" meaning\"1*X^1+2*X^2\"\n");
printf("4.thank you for using!haha^_^\n");
}


void main()
{ struct term *f,*g,*fg;
int i;
i=-1;
Open();

while(i!=0)
{
scanf("%d",&i);
getchar();
switch(i)
{
case 0:
return;
case 1:
clrscr();
printf("please input A\n");
f=Creat('A');
sort(f);
printf("A=");
print(f);
printf("please input B\n");
g=Creat('B');
sort(g);
printf("B=");
print(g);
printf("A+B=");
fg=Add(f,g);
print(fg);
break;
case 2:
clrscr();
printf("please input A\n");
f=Creat('A');
sort(f);
printf("A=");
print(f);
printf("please input B\n");
g=Creat('B');
sort(g);
printf("B=");
print(g);
printf("A*B=");
fg=Mul(f,g);
print(fg);
break;
case 3:
clrscr();
Readme();
break;
default:
clrscr();
Open();
}
}

}


搜索更多相关的解决方案: 链表  函数  

----------------解决方案--------------------------------------------------------
  相关解决方案