‘壹’ 一个C语言程序,编译、链接都没问题,但一运行就出错,高手知道下~~
改好了:
#include <stdio.h>
#include <stdlib.h>
typedef struct dxs
{
int exp,coef;
struct dxs *next;
}oxs,*o_xs;
/*****输入函数*****/
void build(int i,o_xs pa)
{
int j,sum_oxs,exps=0,coefs=0;
oxs *p;
pa->coef=coefs;
pa->exp=exps;
pa->next=NULL;
p=pa;
printf("输入第%d个多项式的个数\n",i);
scanf("%d",&sum_oxs);
for(j=sum_oxs;j>0;j--)
{
printf("输入系数和指数");
scanf("%d%d",&coefs,&exps);
p->coef=coefs;
p->exp=exps;
p->next=pa;
pa=p;
p=(oxs*)malloc(sizeof(oxs));
}
free(p);
}
/*****输出函数*****/
void show(o_xs pa)
{
while(pa!=NULL)
{
printf("%d\t%d\t",pa->coef,pa->exp);
pa=pa->next;
}
printf("\n");
}
/*****排序函数(升序)*****/
void list_oxs(o_xs pa)
{
oxs tt,tt0;
oxs *p=pa,*q=pa;
tt.coef=p->coef;
tt.exp=p->exp;
while(p!=NULL)
{
while(q!=NULL)
{
if(q->exp<tt.exp)
{
tt0.exp=tt.exp;tt0.coef=tt.coef;
tt.exp=q->exp;tt.coef=q->coef;
q->exp=tt0.exp;q->coef=tt0.coef;
}
q=q->next;
}
tt0.exp=tt.exp;tt0.coef=tt.coef;
tt.exp=p->exp;tt.coef=p->coef;
p->exp=tt0.exp;p->coef=tt0.coef;
p=p->next;
q=p;
}
}
/*****多项式相加函数*****/
void add(o_xs pa,o_xs pb)
{
int sum_coef;
oxs *p,*q,*u,*pre;
pre=pa;
u=pb;
p=pa->next;
q=pb->next;
while((p!=NULL)&&(q!=NULL))
{
oxs *p,*q,*u,*pre;
if(p->exp>q->exp)
{
u=q;
pre->next=u;
u->next=p;
pre=u;
q=q->next;
}
else if(p->exp<q->exp)
{
pre=p;
p=p->next;
}
else
{
sum_coef=p->coef+q->coef;
if(sum_coef==0)
{
u=p;
p=p->next;
free(u);
u=q;
q=q->next;
free(u);
}
else
{
p->coef=sum_coef;
pre=p;
p=p->next;
u=q;
q=q->next;
free(u);
}
}
if(q!=NULL)
{
pre->next=q;
}
}
}
/*****main函数*****/
void main()
{
oxs a,b;
build(1,&a);
list_oxs(&a);
show(&a);
build(2,&b);
list_oxs(&b);
show(&a);
add(&a,&b);
show(&a);
}