用C++编写求多项式的和与积的算法,要求如下

来源:百度知道 编辑:UC知道 时间:2024/09/20 22:48:26
a. 要求从键盘分别输入2个多项式的系数以及最高次幂;
b. 通过重载操作符+和*,c. 完成多项式的和与积的计算;
d. 输出运算结果;

//稀疏多项式运算
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

typedef struct polynode
{
int coef; //多项式的系数
int exp; //指数
struct polynode *next;
}node;

node *create() //用尾插法建立一元多项式的链表
{
node *h,*r,*s;
int c,e;
h=(node*)malloc(sizeof(node));
r=h;
printf("coef:");
scanf("%d",&c);
printf("exp: ");
scanf("%d",&e);
while(c!=0) //输入系数为0时,多项式的输入结束
{
s=(node*)malloc(sizeof(node));
s->coef=c;
s->exp=e;
r->next=s;
r=s;
printf("coef:");
scanf("%d",&c);
printf("exp: ");
scanf("%d",&e);
}
r->next=NULL;
return(h);
}