一元多项式的乘法与加法运算

本文详细介绍了如何在C语言中定义多项式结构,包括其组成部分(系数和指数),并展示了如何使用冒泡排序算法对多项式的项进行降序排列。此外,还提供了多项式求和和乘法的函数实现,最后给出了一个主函数示例来演示这些操作。
摘要由CSDN通过智能技术生成
#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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值