| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

已结贴   问题点数：17  回复次数：6

//一元多项式那个我着急用这个程序，麻烦哪位大神帮忙改下，谢谢哦
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
int coef;      //系数
int expn;      //指数
struct node *next;
}ploynom;

void main()
{
ploynom *pa,*pb,*pc;
printf("please input tne ploynom of pa:\n");
pa=creatployn();
printf("print the ploynom of pa:\n");
printployn(pa);
printf("please input tne ploynom of pb:\n");
pb=creatployn();
printf("print the ploynom of pb:\n");
printployn(pb);  printf("\n\n\n");
pc=(ploynom *)malloc(sizeof(ploynom));
pc->next=NULL;
}

ploynom *creatployn()
{
int c,e;
scanf("%d,%d",&c,&e);
while(c!=0)              //以输入系数c为0作为结束标志
{
p=(ploynom *)malooc(sizeof(ploynom));
p->coef=c;
p->expn=e;
r->next=p;         //在当前表尾作插入
r=p;
scanf("%d,%d\n",&c,&e);
}
r->next=NULL;
}

{
ploynom *qa,*qb,*q1;
qa=pa->next;          //qa,qb,初值分别指向pa,pb的第一个结点
qb=pb->next;
pc=(ploynom *)malloc(sizeof(ploynom));
pc->next=NULL;
while(qa&&qb)           //当二者均非空时
{
qc=(ploynom *)malloc(sizeof(ploynom));
if(qa->expn==qb->expn)
{
qc->coef=qa->coef+qb->coef;
if((qc->coef)!=0)             //系数之和不为0
{
qc->expn=qa->expn;
qa=qa->next;
q1=qb;                //将已经把指数和系数都传给qc后没用的qb的第一个结点放到q1中
qb=qb->next;
free(q1);               //释放掉第一个和qa系数相等的qb结点
}
else
{
q1=qa; qa=qa->next; free(q1);
q1=qb; qb=qb->next; free(q1);
}
}
else if(qa->expn<qb->expn)
{
qc->coef=qa->coef;
qc->expn=qa->expn;
q1=qa;
qa=qa->next;
free(q1);
}
else(qa->expn>qb->expn)
{
qc->coef=qb->coef;
qc->expn=qb->expn;
q1=qb;
qb=qb->next;
free(q1);
}
if((qc->coef)!=0)
{
qc->next=qc->next;
pc->next=qc;
pc=qc;
}
}
while(qa!=NULL&&qb==NULL)
{
qc=(ploynom *)malloc(sizeof(ploynom));
qc->coef=qa->coef;  qc->expn=qa->expn;
qa=qa->next;
qc->next=pc->next;
pc->next=qc;  pc=qc;
}
while(qa==NULL&&qb!=NULL)
{
qc=(ploynom *)malloc(sizeof(ploynom));
qc->coef=qb->coef;  qc->expn=qb->expn;
qb=qb->next;
qc->next=pc->next;
pc->next=qc;  pc=qc;
}
}

void printployn(ploynom *p)
{
ploynom *q=p->next;
if(!q)
printf("It's empty!!!");
while(q)
{
if(q->coef>0)   putchar('+');
if(q->coef!=1&&q->coef!=-1)
{
printf("%2d",q->coef);
if(q->expn==1)  putchar('x');
else if(q->expn)  printf("x^%d",q->expn);
}
else
{
if(q->coef==1)
{
if(q->expn==0)  putchar('1');
else if(q->expn==1)  putchar('x');
else  printf("x^%d",q->expn);
}
if(q->coef==-1)
{
if(q->expn==0)  putchar('-1');
else if(q->expn==1)  putchar('-x');
else  printf("-x^%d",q->expn);
}
}
q=q->next;
}
printf("\n");
}

得分:10
while(c!=0)              //以输入系数c为0作为结束标志
{
p=(ploynom *)malooc(sizeof(ploynom));
else(qa->expn>qb->expn)
{
qc->coef=qb->coef;
qc->expn=qb->expn;
q1=qb;
qb=qb->next;
free(q1);
}

得分:0

error C2040: 'creatployn' : 'struct node *()' differs in levels of indirection from 'int ()'
error C2223: left of '->next' must point to struct/union

得分:7

[此贴子已经被作者于2016-9-25 23:05编辑过]

得分:0

得分:0

得分:0

• 7
• 1/1页
• 1