数据结构一元多项式计算 第3页
2.4 流程图 如图2所示。
图2 一元多项式输入功能模块的流程图
2.5 源程序
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<conio.h>
typedef struct pnode
{float coef;
int exp;
struct pnode
}pnode;
pnode * creat()
{
int m;float n; pnode *head,*rear,*s;
head=(pnode *)malloc(sizeof(pnode));
rear=head;
printf("input coef:");
scanf("%f",&n);
printf("input exp:");
scanf("%d",&m);
while(n!=0)
{s=(pnode *)malloc(sizeof(pnode));
s->coef=n;
s->exp=m;
s->next=NULL;
rear->next=s;
rear=s;
printf("input coef:");
scanf("%f",&n);
printf("input exp:");
scanf("%d",&m);
}
head=head->next;
return head;
}
void tiao_zheng(pnode *head)
{
pnode *p,*q,*t;float temp;
p=head;
while(p!=NULL)
{q=p;
t=q->next;
while(t!=NULL)
{if(t->exp>q->exp)
q=t;
t=t->next;
}
temp=p->coef;p->coef=q->coef;q->coef=temp;
temp=p->exp;p->exp=q->exp;;
p=p->next;
}
}
void display(pnode *head)
{
pnode *p;int one_time=1; p=head;
while(p!=NULL)
{
if(one_time==1)
{if(p->exp==0)
printf("%f",p
else if(p->coef==1||p->coef==-1)
printf("x^%d",p->exp);
else if(p->coef>0)
printf("%fx^%d",p->coef,p->exp);
else if(p->coef<0)
printf("%fx^%d",p->coef,p->exp);
one_time=0;
}
else{
if(p->exp==0)
{if(p->coef>0)
printf("+%f",p->coef);
}
else if(p->coef==1)
printf("+x^%d",p->exp);
else if(p->coef==-1)
printf("x^%d",p->exp);
else if(p->coef>0)
printf("+%fx^%d",p->coef,p->exp);
else if(
}
p=p->next;
}
printf("\n");
}
pnode * add(pnode *heada,pnode *headb)
{
需要完整内容的请联系QQ3249114,本文免费,转发请注明源于www.751com.cn
r=headc;
while(p!=NULL&&q!=NULL)
{if(p->exp==q->exp)
{x=p->coef+q->coef;
if(x!=0)
{s=(pnode *)malloc(sizeof(pnode));
s->coef=x;
s->exp=p->exp;
r->next=s;
r=s;
}
q=q->next;p=p->next;
}
else if(p->exp<q->exp)
{s=(pnode *)malloc(sizeof(pnode));
s->coef=q->coef;
s->exp=q->exp;
r->next=s;
r=s;
q=q->next;
}
else
上一页 [1] [2] [3] [4] [5] [6] [7] 下一页