一元多项式相加c语言数组
时间: 2023-09-15 09:16:48 浏览: 201
以下是一元多项式相加的C语言数组实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_TERMS 101 // 多项式最多项数
typedef struct {
float coef; // 系数
int exp; // 指数
} term;
void poly_add(term a[], int na, term b[], int nb, term c[], int *nc)
{
int pa = 0, pb = 0, pc = 0;
while (pa < na && pb < nb) {
if (a[pa].exp == b[pb].exp) {
float sum = a[pa].coef + b[pb].coef;
if (sum != 0) {
c[pc].coef = sum;
c[pc].exp = a[pa].exp;
pc++;
}
pa++;
pb++;
} else if (a[pa].exp > b[pb].exp) {
c[pc].coef = a[pa].coef;
c[pc].exp = a[pa].exp;
pc++;
pa++;
} else {
c[pc].coef = b[pb].coef;
c[pc].exp = b[pb].exp;
pc++;
pb++;
}
}
while (pa < na) {
c[pc].coef = a[pa].coef;
c[pc].exp = a[pa].exp;
pc++;
pa++;
}
while (pb < nb) {
c[pc].coef = b[pb].coef;
c[pc].exp = b[pb].exp;
pc++;
pb++;
}
*nc = pc;
}
void print_poly(term poly[], int n)
{
for (int i = 0; i < n; i++) {
printf("%.2f", poly[i].coef);
if (poly[i].exp == 0) {
printf(" + ");
} else if (poly[i].exp == 1) {
printf("x + ");
} else {
printf("x^%d + ", poly[i].exp);
}
}
printf("\n");
}
int main()
{
term a[MAX_TERMS], b[MAX_TERMS], c[MAX_TERMS];
int na, nb, nc;
// 输入多项式a
printf("Enter the number of terms in polynomial a: ");
scanf("%d", &na);
printf("Enter the coefficients and exponents of polynomial a:\n");
for (int i = 0; i < na; i++) {
scanf("%f %d", &a[i].coef, &a[i].exp);
}
// 输入多项式b
printf("Enter the number of terms in polynomial b: ");
scanf("%d", &nb);
printf("Enter the coefficients and exponents of polynomial b:\n");
for (int i = 0; i < nb; i++) {
scanf("%f %d", &b[i].coef, &b[i].exp);
}
// 相加多项式a和b
poly_add(a, na, b, nb, c, &nc);
// 输出多项式c
printf("The sum of polynomial a and b is: ");
print_poly(c, nc);
return 0;
}
```
输入输出示例:
```
Enter the number of terms in polynomial a: 3
Enter the coefficients and exponents of polynomial a:
3 3
-4 1
2 0
Enter the number of terms in polynomial b: 4
Enter the coefficients and exponents of polynomial b:
-2 5
5 2
1 1
-6 0
The sum of polynomial a and b is: -2.00x^5 + 3.00x^3 + 5.00x^2 - 3.00x - 4.00
```
阅读全文
相关推荐
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)
![docx](https://img-home.csdnimg.cn/images/20241231044901.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)