C语言实现一元多项式加法运算
#include <stdio.h>
#include <stdlib.h>
#define MAX_TERMS 101
typedef struct {
float coef;
int expon;
} PolyTerm;
void read_poly(PolyTerm poly[]);
void print_poly(PolyTerm poly[]);
void add_poly(PolyTerm poly1[], PolyTerm poly2[], PolyTerm result[]);
int main() {
PolyTerm poly1[MAX_TERMS], poly2[MAX_TERMS], result[MAX_TERMS];
printf('Enter the first polynomial:\n');
read_poly(poly1);
printf('Enter the second polynomial:\n');
read_poly(poly2);
add_poly(poly1, poly2, result);
printf('The sum of the polynomials is:\n');
print_poly(result);
return 0;
}
void read_poly(PolyTerm poly[]) {
int n;
printf('Enter the number of terms in the polynomial: ');
scanf('%d', &n);
printf('Enter the coefficients and exponents of each term:\n');
for (int i = 0; i < n; i++) {
scanf('%f %d', &poly[i].coef, &poly[i].expon);
}
}
void print_poly(PolyTerm poly[]) {
int i = 0;
while (poly[i].expon != -1) {
if (i != 0 && poly[i].coef > 0) {
printf('+');
}
printf('%.2fx^%d', poly[i].coef, poly[i].expon);
i++;
}
printf('\n');
}
void add_poly(PolyTerm poly1[], PolyTerm poly2[], PolyTerm result[]) {
int i = 0, j = 0, k = 0;
while (poly1[i].expon != -1 && poly2[j].expon != -1) {
if (poly1[i].expon == poly2[j].expon) {
result[k].coef = poly1[i].coef + poly2[j].coef;
result[k].expon = poly1[i].expon;
i++;
j++;
} else if (poly1[i].expon > poly2[j].expon) {
result[k].coef = poly1[i].coef;
result[k].expon = poly1[i].expon;
i++;
} else {
result[k].coef = poly2[j].coef;
result[k].expon = poly2[j].expon;
j++;
}
k++;
}
while (poly1[i].expon != -1) {
result[k].coef = poly1[i].coef;
result[k].expon = poly1[i].expon;
i++;
k++;
}
while (poly2[j].expon != -1) {
result[k].coef = poly2[j].coef;
result[k].expon = poly2[j].expon;
j++;
k++;
}
result[k].coef = 0;
result[k].expon = -1;
}
代码详解:
首先,定义了一个结构体 PolyTerm,用来表示一项多项式,其中 coef 表示系数,expon 表示指数。
typedef struct {
float coef;
int expon;
} PolyTerm;
然后,定义了三个函数:
read_poly():用来读取多项式的系数和指数,并将它们存储在一个结构体数组中;print_poly():用来输出多项式的表达式;add_poly():用来实现两个多项式的加法,并将结果存储在一个结构体数组中。
void read_poly(PolyTerm poly[]);
void print_poly(PolyTerm poly[]);
void add_poly(PolyTerm poly1[], PolyTerm poly2[], PolyTerm result[]);
在 main() 函数中,首先定义了三个结构体数组:poly1、poly2 和 result,分别用来存储两个多项式和它们的和。
然后,分别调用 read_poly() 函数读取多项式,调用 add_poly() 函数计算两个多项式的和,最后调用 print_poly() 函数输出结果。
int main() {
PolyTerm poly1[MAX_TERMS], poly2[MAX_TERMS], result[MAX_TERMS];
printf('Enter the first polynomial:\n');
read_poly(poly1);
printf('Enter the second polynomial:\n');
read_poly(poly2);
add_poly(poly1, poly2, result);
printf('The sum of the polynomials is:\n');
print_poly(result);
return 0;
}
read_poly() 函数的实现很简单,首先读取多项式的项数 n,然后循环读取每一项的系数和指数,并将它们存储在结构体数组 poly 中。
void read_poly(PolyTerm poly[]) {
int n;
printf('Enter the number of terms in the polynomial: ');
scanf('%d', &n);
printf('Enter the coefficients and exponents of each term:\n');
for (int i = 0; i < n; i++) {
scanf('%f %d', &poly[i].coef, &poly[i].expon);
}
}
print_poly() 函数的实现也很简单,它循环输出每一项的系数和指数,并根据情况添加正负号和幂符号。
void print_poly(PolyTerm poly[]) {
int i = 0;
while (poly[i].expon != -1) {
if (i != 0 && poly[i].coef > 0) {
printf('+');
}
printf('%.2fx^%d', poly[i].coef, poly[i].expon);
i++;
}
printf('\n');
}
add_poly() 函数的实现稍微有些复杂,它使用了类似于归并排序的思想,将两个多项式的项按指数从大到小进行合并。
具体实现方式如下:
- 使用三个指针
i、j和k分别指向两个多项式和结果多项式的当前项; - 依次比较
poly1[i]和poly2[j]的指数,将较大的项添加到结果多项式中; - 如果两个多项式的当前项的指数相等,则将它们的系数相加,并添加到结果多项式中;
- 当其中一个多项式的项已经全部添加到结果多项式中后,将另一个多项式的剩余项直接添加到结果多项式中;
- 最后,将结果多项式的最后一项的指数设为
-1,表示多项式已经结束。
void add_poly(PolyTerm poly1[], PolyTerm poly2[], PolyTerm result[]) {
int i = 0, j = 0, k = 0;
while (poly1[i].expon != -1 && poly2[j].expon != -1) {
if (poly1[i].expon == poly2[j].expon) {
result[k].coef = poly1[i].coef + poly2[j].coef;
result[k].expon = poly1[i].expon;
i++;
j++;
} else if (poly1[i].expon > poly2[j].expon) {
result[k].coef = poly1[i].coef;
result[k].expon = poly1[i].expon;
i++;
} else {
result[k].coef = poly2[j].coef;
result[k].expon = poly2[j].expon;
j++;
}
k++;
}
while (poly1[i].expon != -1) {
result[k].coef = poly1[i].coef;
result[k].expon = poly1[i].expon;
i++;
k++;
}
while (poly2[j].expon != -1) {
result[k].coef = poly2[j].coef;
result[k].expon = poly2[j].expon;
j++;
k++;
}
result[k].coef = 0;
result[k].expon = -1;
}
原文地址: https://www.cveoy.top/t/topic/lRHL 著作权归作者所有。请勿转载和采集!