编程实现一元多项式的加法运算。
时间: 2023-05-17 17:03:34 浏览: 111
可以使用数组来表示一元多项式,数组下标表示指数,数组元素表示系数。具体实现如下:
```python
def add_poly(poly1, poly2):
"""
实现一元多项式的加法运算
:param poly1: 一元多项式1,数组表示
:param poly2: 一元多项式2,数组表示
:return: 一元多项式1和2的和,数组表示
"""
res = [0] * (max(len(poly1), len(poly2)))
for i in range(len(poly1)):
res[i] += poly1[i]
for i in range(len(poly2)):
res[i] += poly2[i]
return res
```
例如,对于多项式 $2x^3 + 3x^2 + 4x + 5$ 和 $x^2 + 2x + 1$,可以表示为数组 $[5, 4, 3, 2]$ 和 $[1, 2, 1]$,分别代表系数从低到高的多项式系数。调用 `add_poly([5, 4, 3, 2], [1, 2, 1])`,得到结果为 $2x^3 + 4x^2 + 6x + 6$,对应的数组为 $[6, 6, 4, 2]$。
相关问题
c语言编程实现一元多项式加法运算,以(0,0)作为输入结束。
#include <stdio.h>
#define MAX_TERMS 100
typedef struct {
float coef; // 系数
int exp; // 指数
} Term;
void input_poly(Term poly[]);
void add_poly(const Term poly1[], const Term poly2[], Term result[]);
void print_poly(const Term poly[]);
int main() {
Term poly1[MAX_TERMS], poly2[MAX_TERMS], result[MAX_TERMS];
printf("请输入第一个多项式:\n");
input_poly(poly1);
printf("请输入第二个多项式:\n");
input_poly(poly2);
add_poly(poly1, poly2, result);
printf("两个多项式相加的结果为:\n");
print_poly(result);
return 0;
}
void input_poly(Term poly[]) {
int i = 0;
while (1) {
printf("请输入第%d项的系数和指数:", i + 1);
scanf("%f%d", &poly[i].coef, &poly[i].exp);
if (poly[i].coef == 0 && poly[i].exp == 0) {
break;
}
i++;
}
}
void add_poly(const Term poly1[], const Term poly2[], Term result[]) {
int i = 0, j = 0, k = 0;
while (poly1[i].coef != 0 || poly1[i].exp != 0 || poly2[j].coef != 0 || poly2[j].exp != 0) {
if (poly1[i].exp > poly2[j].exp) {
result[k] = poly1[i];
i++;
} else if (poly1[i].exp < poly2[j].exp) {
result[k] = poly2[j];
j++;
} else {
result[k].coef = poly1[i].coef + poly2[j].coef;
result[k].exp = poly1[i].exp;
i++;
j++;
}
k++;
}
result[k].coef = 0;
result[k].exp = 0;
}
void print_poly(const Term poly[]) {
int i = 0;
while (poly[i].coef != 0 || poly[i].exp != 0) {
if (i > 0 && poly[i].coef > 0) {
printf("+");
}
if (poly[i].coef != 1 && poly[i].coef != -1) {
printf("%.2f", poly[i].coef);
} else if (poly[i].coef == -1) {
printf("-");
}
if (poly[i].exp == 0) {
printf("%.2f", poly[i].coef);
} else if (poly[i].exp == 1) {
printf("x");
} else {
printf("x^%d", poly[i].exp);
}
i++;
}
printf("\n");
}
c语言编程实现一元多项式的加法运算
```
#include <stdio.h>
#include <stdlib.h>
typedef struct PolyNode *Polynomial;
struct PolyNode{
int coef; // 系数
int expon; // 指数
Polynomial link; // 下一项
};
void Attach(int c, int e, Polynomial *pRear){
Polynomial P;
// 新建节点
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->coef = c;
P->expon = e;
P->link = NULL;
// 插入节点
(*pRear)->link = P;
*pRear = P;
}
Polynomial PolyAdd(Polynomial P1, Polynomial P2){
Polynomial P, Rear, t1, t2;
int sum;
// 新建头节点
P = (Polynomial)malloc(sizeof(struct PolyNode));
Rear = P;
t1 = P1->link;
t2 = P2->link;
while(t1 && t2){
if(t1->expon == t2->expon){ // 指数相等
sum = t1->coef + t2->coef;
if(sum) // 系数不为0
Attach(sum, t1->expon, &Rear);
t1 = t1->link;
t2 = t2->link;
}
else if(t1->expon > t2->expon){ // P1中指数较大
Attach(t1->coef, t1->expon, &Rear);
t1 = t1->link;
}
else{ // P2中指数较大
Attach(t2->coef, t2->expon, &Rear);
t2 = t2->link;
}
}
// 将未处理完的项接到结果多项式中
for(; t1; t1 = t1->link)
Attach(t1->coef, t1->expon, &Rear);
for(; t2; t2 = t2->link)
Attach(t2->coef, t2->expon, &Rear);
// 删除头节点
Rear->link = NULL;
P = P->link;
free(P1);
free(P2);
return P;
}
void PrintPoly(Polynomial P){
if(!P){
printf("0 0\n");
return;
}
while(P){
printf("%d %d", P->coef, P->expon);
P = P->link;
if(P)
printf(" ");
}
printf("\n");
}
int main(){
Polynomial P1, P2, PP, PS;
// 新建多项式P1
P1 = (Polynomial)malloc(sizeof(struct PolyNode));
P1->link = NULL;
Attach(5, 0, &P1);
Attach(2, 1, &P1);
Attach(-3, 2, &P1);
// 新建多项式P2
P2 = (Polynomial)malloc(sizeof(struct PolyNode));
P2->link = NULL;
Attach(7, 1, &P2);
Attach(-2, 2, &P2);
Attach(4, 4, &P2);
// 输出多项式P1和P2
printf("P1: ");
PrintPoly(P1);
printf("P2: ");
PrintPoly(P2);
// 多项式相加
PP = PolyAdd(P1, P2);
// 输出相加结果
printf("P1 + P2: ");
PrintPoly(PP);
return 0;
}
```
阅读全文