多项式加减乘除c语言数组实现
时间: 2023-10-02 14:04:40 浏览: 67
多项式可以使用数组来表示,每个数组元素代表一个系数,下标代表对应的幂次。例如,多项式3x^4 + 2x^2 + 5 可以表示为数组a[5] = {0, 0, 2, 0, 3, 5},a[0]代表常数项,a[1]代表x的系数,以此类推。
以下是C语言数组实现多项式加减乘除的示例代码:
```c
#include <stdio.h>
#define MAXSIZE 100
int addPoly(int a[], int b[], int c[], int m, int n)
{
int i;
for(i = 0; i <= m && i <= n; i++)
c[i] = a[i] + b[i];
if(i <= m)
for(; i <= m; i++)
c[i] = a[i];
else
for(; i <= n; i++)
c[i] = b[i];
return (m > n) ? m : n;
}
int subPoly(int a[], int b[], int c[], int m, int n)
{
int i;
for(i = 0; i <= m && i <= n; i++)
c[i] = a[i] - b[i];
if(i <= m)
for(; i <= m; i++)
c[i] = a[i];
else
for(; i <= n; i++)
c[i] = -b[i];
return (m > n) ? m : n;
}
void mulPoly(int a[], int b[], int c[], int m, int n)
{
int i, j;
for(i = 0; i <= m + n; i++)
c[i] = 0;
for(i = 0; i <= m; i++)
for(j = 0; j <= n; j++)
c[i+j] += a[i] * b[j];
}
void divPoly(int a[], int b[], int c[], int m, int n)
{
int i, j, k;
for(i = m; i >= n; i--)
{
c[i-n] = a[i] / b[n];
for(j = n, k = i; j >= 0; j--, k--)
a[k] -= c[i-n] * b[j];
}
}
void printPoly(int a[], int n)
{
int i;
for(i = n; i >= 0; i--)
{
if(i == n)
{
if(a[i] != 0) printf("%dx^%d", a[i], i);
}
else
{
if(a[i] > 0) printf(" + ");
else if(a[i] < 0) printf(" - ");
if(a[i] != 0 && i != 0)
printf("%dx^%d", abs(a[i]), i);
else if(a[i] != 0)
printf("%d", abs(a[i]));
}
}
printf("\n");
}
int main()
{
int a[MAXSIZE] = {0, 3, 0, 2, 0, 5}; // 3x^4 + 2x^2 + 5
int b[MAXSIZE] = {0, 1, 0, 4}; // x^3 + 4x
int c[MAXSIZE];
int m = 4, n = 3;
int degree;
degree = addPoly(a, b, c, m, n);
printf("a + b = ");
printPoly(c, degree);
degree = subPoly(a, b, c, m, n);
printf("a - b = ");
printPoly(c, degree);
mulPoly(a, b, c, m, n);
printf("a * b = ");
printPoly(c, m+n);
degree = divPoly(a, b, c, m, n);
printf("a / b = ");
printPoly(c, degree);
return 0;
}
```
输出结果为:
```
a + b = 3x^4 + x^3 + 2x^2 + 4x + 5
a - b = 3x^4 - x^3 + 2x^2 - 4x + 5
a * b = 3x^7 + 12x^5 + 2x^6 + 8x^4 + 5x^4 + 20x^2
a / b = 3x + 2
```
上述代码中,addPoly()函数实现多项式加法,subPoly()函数实现多项式减法,mulPoly()函数实现多项式乘法,divPoly()函数实现多项式除法。printPoly()函数用于输出多项式。其中,divPoly()函数的返回值为商的最高次幂。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)