设有一元多项式Am(x) 和Bn(x)。 Am(x) = A0+A1x1+A2x2+A3x3+… +Amxm Bn(x) = B0+B1x1+B2x2+B3x3+… +Bnxn 试求M(x)= Am(x)+Bn(x)、N(x)= Am(x)-Bn(x)。利用C语言实现 (1)要求结果M(x)中无重复阶项和无零系数项。 (2)要求输出结果的升幂和降幂两种排列情况。 (3)设计实现菜单方式的交互界面,界面友好,可反复操作。
时间: 2023-08-11 11:03:09 浏览: 128
以下是实现此功能的C语言代码,其中使用了结构体来表示多项式:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_DEGREE 100
typedef struct {
int degree; // 多项式最高阶次数
float coefficient[MAX_DEGREE + 1]; // 多项式系数
} Polynomial;
// 多项式相加
Polynomial add(Polynomial a, Polynomial b) {
Polynomial result;
int i;
for (i = 0; i <= MAX_DEGREE; i++) {
result.coefficient[i] = 0;
}
result.degree = a.degree > b.degree ? a.degree : b.degree;
for (i = 0; i <= result.degree; i++) {
result.coefficient[i] = a.coefficient[i] + b.coefficient[i];
}
return result;
}
// 多项式相减
Polynomial subtract(Polynomial a, Polynomial b) {
Polynomial result;
int i;
for (i = 0; i <= MAX_DEGREE; i++) {
result.coefficient[i] = 0;
}
result.degree = a.degree > b.degree ? a.degree : b.degree;
for (i = 0; i <= result.degree; i++) {
result.coefficient[i] = a.coefficient[i] - b.coefficient[i];
}
return result;
}
// 输出多项式
void print(Polynomial p) {
int i;
printf("Degree: %d\n", p.degree);
printf("Coefficients: ");
for (i = 0; i <= p.degree; i++) {
printf("%g ", p.coefficient[i]);
}
printf("\n");
}
// 多项式去重和去零
Polynomial simplify(Polynomial p) {
int i, j;
Polynomial result;
result.degree = p.degree;
for (i = 0; i <= p.degree; i++) {
result.coefficient[i] = p.coefficient[i];
}
// 去重
for (i = 0; i <= result.degree; i++) {
for (j = i + 1; j <= result.degree; j++) {
if (i != j && result.coefficient[i] != 0 && result.coefficient[i] == result.coefficient[j]) {
result.coefficient[j] = 0;
}
}
}
// 去零
for (i = 0; i <= result.degree; i++) {
if (result.coefficient[i] == 0) {
result.degree--;
for (j = i; j <= result.degree; j++) {
result.coefficient[j] = result.coefficient[j + 1];
}
}
}
return result;
}
int main() {
Polynomial a, b, m, n;
int choice;
do {
printf("Menu:\n");
printf("1. Input polynomials\n");
printf("2. Add polynomials\n");
printf("3. Subtract polynomials\n");
printf("4. Print polynomials (ascending order)\n");
printf("5. Print polynomials (descending order)\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// 输入多项式
int i;
printf("Enter the degree of polynomial A (<= %d): ", MAX_DEGREE);
scanf("%d", &a.degree);
printf("Enter the coefficients of polynomial A:\n");
for (i = 0; i <= a.degree; i++) {
scanf("%f", &a.coefficient[i]);
}
printf("Enter the degree of polynomial B (<= %d): ", MAX_DEGREE);
scanf("%d", &b.degree);
printf("Enter the coefficients of polynomial B:\n");
for (i = 0; i <= b.degree; i++) {
scanf("%f", &b.coefficient[i]);
}
break;
case 2:
// 多项式相加
m = simplify(add(a, b));
printf("M(x) = A(x) + B(x):\n");
print(m);
break;
case 3:
// 多项式相减
n = simplify(subtract(a, b));
printf("N(x) = A(x) - B(x):\n");
print(n);
break;
case 4:
// 升幂排序输出
printf("Polynomial A:\n");
print(a);
printf("Polynomial B:\n");
print(b);
printf("Polynomial M:\n");
print(m);
printf("Polynomial N:\n");
print(n);
break;
case 5:
// 降幂排序输出
printf("Polynomial A:\n");
int j;
printf("Degree: %d\n", a.degree);
printf("Coefficients: ");
for (j = a.degree; j >= 0; j--) {
printf("%g ", a.coefficient[j]);
}
printf("\n");
printf("Polynomial B:\n");
printf("Degree: %d\n", b.degree);
printf("Coefficients: ");
for (j = b.degree; j >= 0; j--) {
printf("%g ", b.coefficient[j]);
}
printf("\n");
printf("Polynomial M:\n");
printf("Degree: %d\n", m.degree);
printf("Coefficients: ");
for (j = m.degree; j >= 0; j--) {
printf("%g ", m.coefficient[j]);
}
printf("\n");
printf("Polynomial N:\n");
printf("Degree: %d\n", n.degree);
printf("Coefficients: ");
for (j = n.degree; j >= 0; j--) {
printf("%g ", n.coefficient[j]);
}
printf("\n");
break;
case 0:
// 退出程序
break;
default:
printf("Invalid choice!\n");
}
printf("\n");
} while (choice != 0);
return 0;
}
```
在程序中,首先定义了一个结构体 `Polynomial` 表示多项式,包括最高阶次数和系数数组。然后分别实现了多项式相加、多项式相减、输出多项式、多项式去重和去零等函数。在主函数中,使用菜单方式的交互界面,根据用户输入的选择进行相应的操作。其中,升幂排序输出和降幂排序输出使用不同的方式实现。
阅读全文