求助:求高手关于一元多项式计算的算法

jeffer365 2005-09-06 02:05:22
刚学c不久,求一元多项式计算的算法。
要求:能够按照指数降序排列建立并输出多项式;
能够完成两个多项式的相加、减,并输出结果。
...全文
273 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jeffer365 2005-09-06
  • 打赏
  • 举报
回复
感谢你们的指点~~!
snowbirdfly 2005-09-06
  • 打赏
  • 举报
回复
你最好看看书本,链表的一般操作,诸如插入结点\删除结点~~
应该不是太难~~~
snowbirdfly 2005-09-06
  • 打赏
  • 举报
回复
#include <stdio.h>
#include "conio.h"
#include <stdlib.h>
#include "malloc.h"
#define M 3//可以改变输入的多项式的项数
#define N 2

typedef struct poly *polytype;
struct poly{
float coef;
int exp;
polytype next;
};
polytype Create_Empty_Node(int );
void Input_Values(polytype ,int );
void Insert_Node(polytype ,float ,int );
void Add_Poly(polytype ,polytype );
void Sub_Poly(polytype ,polytype );
void OutPut_Results(polytype);
void OutPut_Values(polytype);
int Infact(int ,int);/*计算阶乘*/

int main()
{
polytype pa,pb;
pa = Create_Empty_Node(M);//对加法进行操作
printf("Please Input ListPoly pa:%d\n",M);
Input_Values(pa,M);
printf("A(x) =\t");
OutPut_Results(pa->next);
pb = Create_Empty_Node(N);
printf("Please Input ListPoly pb:%d\n",N);
Input_Values(pb,N);
printf("B(x) =\t");
OutPut_Results(pb->next);
Add_Poly(pa,pb);
printf("Add Results Are:\n");
printf("C(x) = A(x) + B(x) =\t");
OutPut_Results(pa->next);/*从第一个结点输出*/
OutPut_Values(pa->next);


pa = Create_Empty_Node(M);//对减法进行操作
printf("Please Input ListPoly pa:%d\n",M);
Input_Values(pa,M);
printf("A(x) =\t");
OutPut_Results(pa->next);
pb = Create_Empty_Node(N);
printf("Please Input ListPoly pb:%d\n",N);
Input_Values(pb,N);
printf("B(x) =\t");
OutPut_Results(pb->next);
Sub_Poly(pa,pb);
printf("Sub Results Are:\n");
printf("D(x) = A(x)-B(x) =\t");
OutPut_Results(pa->next);
OutPut_Values(pa->next);


/*pa = Create_Empty_Node(M);//对乘法进行操作
printf("Please Input ListPoly pa:%d\n",M);
Input_Values(pa,M);
printf("A(x) =\t");
OutPut_Results(pa->next);
pb = Create_Empty_Node(N);
printf("Please Input ListPoly pb:%d\n",N);
Input_Values(pb,N);
printf("B(x) =\t");
OutPut_Results(pb->next);
By_Poly(pa,pb);
printf("E(x) =\t");
OutPut_Results(pa->next);/*从第一个结点输出*/
/*OutPut_Values(pa->next);*/

getchar();
return 0;
}
polytype Create_Empty_Node(int n)
{
polytype p;
p = (polytype)malloc(sizeof(struct poly));
if(p != NULL){
p->coef = (float)n;
p->exp = -1;
p->next = NULL;
}
else
printf("Failed To malloc!");
return
p;
}
void Input_Values(polytype p,int t)
{
float m;
int n;
for(int i = 0 ;i < t;i++)
{
scanf("%f",&m);
scanf("%d",&n);
Insert_Node(p,m,n);
}
}

void Insert_Node(polytype p,float coef,int exp)
{
polytype q,r;
q = p;
while(q->next != NULL )/*找到链表的结尾*/
q = q->next;
r = (polytype)malloc(sizeof(struct poly));
if(r == NULL)
{
printf("The Memory Is Full!");
exit(1);
}
else{
r->coef = coef;
r->exp = exp;
r->next = NULL;
q->next = r;
}
}
void Add_Poly(polytype pa,polytype pb)
{
float x = 0;
polytype p,q,pre,u;
p = pa->next;
q = pb->next;
pre = pa;
while(p && q)
{
if(p->exp < q->exp){
pre = p;
p = p->next;
}
else if(p->exp == q->exp){
x = p->coef+q->coef;
if(x){
p->coef = x;
pre = p;
}
else{
pre->next = p->next;
free(p);
}
p = pre->next;
u = q;
q = q->next;
free(u);
}
else{
u = q->next;
q->next = p;
pre ->next = q;
pre = q;
q = u;
}
}
if(q)
pre->next = q;
free(pb);
}
void Sub_Poly(polytype pa,polytype pb)
{
float x = 0;
polytype p,q,pre,u;
p = pa->next;
q = pb->next;
pre = pa;
while(p && q)
{
if(p->exp < q->exp){
pre = p;
p = p->next;
}
else if(p->exp == q->exp){
x = p->coef-q->coef;
if(x){
p->coef = x;
pre = p;
}
else{
pre->next = p->next;
free(p);
}
p = pre->next;
u = q;
q = q->next;
free(u);
}
else{//p->coef > q->coef
u = q->next;
q->next = p;
q->coef = -q->coef;
pre ->next = q;
pre = q;
q = u;
}
}
if(q){
pre->next = q;
q->coef = -q->coef;
}
free(pb);
}
/*void By_Poly(polytype pa,polytype pb)
{*/


void OutPut_Results(polytype pa)
{
polytype p;
for(p = pa;p->next != NULL;p = p->next)
{
printf("%.1fX^%d\t+",p->coef,p->exp);
}
printf("%.1fX^%d\n",p->coef,p->exp);
}
void OutPut_Values(polytype pa)
{
polytype p;
int x;
float Sum;
Sum = 0;
printf("Please Input X:\n");
scanf("%d",&x);
for(p = pa;p != NULL;p = p->next)
{
Sum += p->coef*Infact(x,p->exp);
}
printf("The Results Is :\n");
printf("%.1f\n",Sum);
}
int Infact(int m,int n)
{
int Fact;
Fact = 1;
for(int t = 0;t < n;t++)
Fact = Fact * m;
return Fact;
}




snowbirdfly 2005-09-06
  • 打赏
  • 举报
回复
是啊~
就是链表的一般操作~~
最好自己写~~
zhouhuahai 2005-09-06
  • 打赏
  • 举报
回复
用链表啊.
楼主应该再去看看数据结构的书

70,035

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧