用C语言写三元三次多项式的存储和加法
时间: 2023-06-01 10:01:44 浏览: 117
C 代码 计算值、导数或积分 埃尔米特三次多项式,或操作插值 由分段的埃尔米特三次多项式组成.rar
5星 · 资源好评率100%
#include <stdio.h>
#include <stdlib.h>
typedef struct {
float coef;
int x1;
int x2;
int x3;
} Term;
typedef struct {
Term *terms;
int num_terms;
} Polynomial;
void init_poly(Polynomial *poly, int num_terms) {
poly->terms = (Term *)malloc(sizeof(Term) * num_terms);
poly->num_terms = num_terms;
}
void read_poly(Polynomial *poly) {
int i;
for (i = 0; i < poly->num_terms; i++) {
printf("Enter coefficient, x1, x2, and x3 for term %d: ", i + 1);
scanf("%f %d %d %d", &poly->terms[i].coef, &poly->terms[i].x1, &poly->terms[i].x2, &poly->terms[i].x3);
}
}
void print_poly(Polynomial *poly) {
int i;
for (i = 0; i < poly->num_terms; i++) {
printf("%.2fx^%d*y^%d*z^%d", poly->terms[i].coef, poly->terms[i].x1, poly->terms[i].x2, poly->terms[i].x3);
if (i < poly->num_terms - 1) {
printf(" + ");
}
}
printf("\n");
}
Polynomial add_poly(Polynomial *poly1, Polynomial *poly2) {
int i, j, k;
Polynomial result;
init_poly(&result, poly1->num_terms + poly2->num_terms);
i = j = k = 0;
while (i < poly1->num_terms && j < poly2->num_terms) {
if (poly1->terms[i].x1 == poly2->terms[j].x1 && poly1->terms[i].x2 == poly2->terms[j].x2 && poly1->terms[i].x3 == poly2->terms[j].x3) {
result.terms[k].coef = poly1->terms[i].coef + poly2->terms[j].coef;
result.terms[k].x1 = poly1->terms[i].x1;
result.terms[k].x2 = poly1->terms[i].x2;
result.terms[k].x3 = poly1->terms[i].x3;
i++;
j++;
k++;
} else if (poly1->terms[i].x1 > poly2->terms[j].x1 || (poly1->terms[i].x1 == poly2->terms[j].x1 && poly1->terms[i].x2 > poly2->terms[j].x2) || (poly1->terms[i].x1 == poly2->terms[j].x1 && poly1->terms[i].x2 == poly2->terms[j].x2 && poly1->terms[i].x3 > poly2->terms[j].x3)) {
result.terms[k].coef = poly1->terms[i].coef;
result.terms[k].x1 = poly1->terms[i].x1;
result.terms[k].x2 = poly1->terms[i].x2;
result.terms[k].x3 = poly1->terms[i].x3;
i++;
k++;
} else {
result.terms[k].coef = poly2->terms[j].coef;
result.terms[k].x1 = poly2->terms[j].x1;
result.terms[k].x2 = poly2->terms[j].x2;
result.terms[k].x3 = poly2->terms[j].x3;
j++;
k++;
}
}
while (i < poly1->num_terms) {
result.terms[k].coef = poly1->terms[i].coef;
result.terms[k].x1 = poly1->terms[i].x1;
result.terms[k].x2 = poly1->terms[i].x2;
result.terms[k].x3 = poly1->terms[i].x3;
i++;
k++;
}
while (j < poly2->num_terms) {
result.terms[k].coef = poly2->terms[j].coef;
result.terms[k].x1 = poly2->terms[j].x1;
result.terms[k].x2 = poly2->terms[j].x2;
result.terms[k].x3 = poly2->terms[j].x3;
j++;
k++;
}
result.num_terms = k;
return result;
}
int main() {
Polynomial poly1, poly2, result;
int num_terms;
printf("Enter the number of terms in the first polynomial: ");
scanf("%d", &num_terms);
init_poly(&poly1, num_terms);
read_poly(&poly1);
printf("Enter the number of terms in the second polynomial: ");
scanf("%d", &num_terms);
init_poly(&poly2, num_terms);
read_poly(&poly2);
result = add_poly(&poly1, &poly2);
printf("The first polynomial is: ");
print_poly(&poly1);
printf("The second polynomial is: ");
print_poly(&poly2);
printf("The sum of the polynomials is: ");
print_poly(&result);
free(poly1.terms);
free(poly2.terms);
free(result.terms);
return 0;
}
阅读全文