#include<stdio.h>
#include<stdlib.h>
typedef struct term {
int coef;
int exp;
} term;
#define MAXSIZE 10000
typedef struct {
term terms[MAXSIZE];
int term_num;
} polynomial;
void print_polynomial(polynomial* poly) {
for (int i = 0; i < poly->term_num; i++) {
if (i == 0) {
printf("%d %d", poly->terms[i].coef, poly->terms[i].exp);
}
else {
printf(" %d %d", poly->terms[i].coef, poly->terms[i].exp);
}
}
}
void sortPolynomial(polynomial* poly) {
// 使用冒泡排序对多项式的项按指数降序排序
for (int i = 0; i < poly->term_num - 1; i++) {
for (int j = 0; j < poly->term_num - i - 1; j++) {
if (poly->terms[j].exp < poly->terms[j + 1].exp) {
// 交换两个项
term temp = poly->terms[j];
poly->terms[j] = poly->terms[j + 1];
poly->terms[j + 1] = temp;
}
}
}
}
polynomial* sum(polynomial poly1, polynomial poly2) {
int i = 0, j = 0, k = 0;
polynomial* polysum = (polynomial*)malloc(sizeof(polynomial));
polysum->term_num = 0;
while (i < poly1.term_num && j < poly2.term_num) {
if (poly1.terms[i].exp > poly2.terms[j].exp) {
polysum->terms[k++] = poly1.terms[i++];
}
else if (poly1.terms[i].exp < poly2.terms[j].exp) {
polysum->terms[k++] = poly2.terms[j++];
}
else {
int sum_coef = poly1.terms[i].coef + poly2.terms[j].coef;
if (sum_coef != 0) {
polysum->terms[k].exp = poly1.terms[i].exp;
polysum->terms[k++].coef = sum_coef;
}
i++;
j++;
}
}
while (i < poly1.term_num) {
polysum->terms[k++] = poly1.terms[i++];
}
while (j < poly2.term_num) {
polysum->terms[k++] = poly2.terms[j++];
}
polysum->term_num = k;
return polysum;
}
polynomial* multiply(polynomial poly1, polynomial poly2) {
polynomial* polyproduct = (polynomial*)malloc(sizeof(polynomial));
polyproduct->term_num = 0;
for (int i = 0; i < poly1.term_num; i++) {
for (int j = 0; j < poly2.term_num; j++) {
int coef = poly1.terms[i].coef * poly2.terms[j].coef;
int exp = poly1.terms[i].exp + poly2.terms[j].exp;
int found = 0;
for (int k = 0; k < polyproduct->term_num; k++) {
if (polyproduct->terms[k].exp == exp) {
polyproduct->terms[k].coef += coef;
// 如果系数相加后为0,则删除该项
if (polyproduct->terms[k].coef == 0) {
// 将当前项后面的所有项往前移动一位
for (int m = k; m < polyproduct->term_num - 1; m++) {
polyproduct->terms[m] = polyproduct->terms[m + 1];
}
polyproduct->term_num--; // 更新项数
}
found = 1;
break;
}
}
if (!found && coef != 0) {
polyproduct->terms[polyproduct->term_num].exp = exp;
polyproduct->terms[polyproduct->term_num].coef = coef;
polyproduct->term_num++;
}
}
}
sortPolynomial(polyproduct);
return polyproduct;
}
int main() {
int num1, num2;
polynomial poly1;
polynomial poly2;
scanf("%d", &num1);
poly1.term_num = num1;
for (int i = 0; i < num1; i++) {
scanf("%d %d", &poly1.terms[i].coef, &poly1.terms[i].exp);
}
scanf("%d", &num2);
poly2.term_num = num2;
for (int i = 0; i < num2; i++) {
scanf("%d %d", &poly2.terms[i].coef, &poly2.terms[i].exp);
}
polynomial* sumpoly = sum(poly1, poly2);
polynomial* productpoly = multiply(poly1, poly2);
if (productpoly->term_num == 0) {
printf("0 0\n");
}
else {
print_polynomial(productpoly);
printf("\n");
}
if (sumpoly->term_num == 0) {
printf("0 0\n");
}
else {
print_polynomial(sumpoly);
}
return 0;
}
一元多项式的乘法与加法运算
本文详细介绍了如何在C语言中定义多项式结构,包括其组成部分(系数和指数),并展示了如何使用冒泡排序算法对多项式的项进行降序排列。此外,还提供了多项式求和和乘法的函数实现,最后给出了一个主函数示例来演示这些操作。
摘要由CSDN通过智能技术生成