用C语言写出多项式的乘法
时间: 2023-02-24 14:12:53 浏览: 90
下面是C语言实现多项式乘法的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_TERMS 101 /* 最多项数 */
typedef struct {
float coef; /* 系数 */
int expn; /* 指数 */
} term;
term a[MAX_TERMS]; /* 第一个多项式 */
term b[MAX_TERMS]; /* 第二个多项式 */
term c[MAX_TERMS]; /* 乘积多项式 */
int avail = 0; /* c数组的当前可用位置 */
void attach(float coefficient, int exponent) {
if (avail >= MAX_TERMS) {
printf("too many terms in the polynomial\n");
exit(1);
}
c[avail].coef = coefficient;
c[avail++].expn = exponent;
}
/* 乘积多项式的形成 */
void polynomial_multiply(int m, int n) {
int i, j, k;
avail = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
k = a[i].expn + b[j].expn;
attach(a[i].coef * b[j].coef, k);
}
}
}
/* 合并同类项 */
void polynomial_group() {
int i, j;
for (i = 0; i < avail - 1; i++) {
for (j = i + 1; j < avail; j++) {
if (c[i].expn == c[j].expn) {
c[i].coef += c[j].coef;
c[j].coef = 0;
c[j].expn = 0;
}
}
}
}
/* 多项式的输出 */
void polynomial_print(int n) {
int i;
for (i = 0; i < n; i++) {
if (c[i].coef != 0) {
if (c[i].expn == 0) {
printf("%.0f", c[i].coef);
} else {
printf("%.0fx^%d", c[i].coef, c[i].expn);
}
if (i < n - 1 && c[i + 1].coef >= 0) {
printf(" + ");
}
}
}
printf("\n");
}
int main() {
int m, n, i;
printf("Enter the number of terms of the first polyn
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)