设计程序计算任意两个最高阶不超过10的一元多项式的和,约定各项系数均为整数。要求:(1)从键盘输入多项式各项,并输出计算结果。(2)分别用顺序表和链表为存储结构进行实现。
时间: 2023-03-21 12:04:59 浏览: 319
这是一道计算两个一元多项式和的程序设计题目,其中多项式的最高阶不超过10,系数均为整数。要求从键盘输入多项式各项,并输出计算结果。同时,要求使用顺序表和链表两种存储结构进行实现。
具体实现方法可以分为以下几步:
1. 从键盘输入两个多项式的各项系数,存储到顺序表或链表中。
2. 对两个多项式进行相加,得到结果多项式。
3. 输出结果多项式的各项系数。
对于顺序表的实现,可以定义一个数组来存储多项式的各项系数,数组下标表示多项式的指数。具体实现方法可以参考以下代码:
```c
#include <stdio.h>
#define MAX_DEGREE 10
typedef struct {
int coef[MAX_DEGREE + 1]; // 存储多项式各项系数的数组
int degree; // 多项式的最高阶
} Poly;
// 从键盘输入多项式各项系数
void input_poly(Poly *p) {
printf("请输入多项式的最高阶:");
scanf("%d", &p->degree);
printf("请输入多项式各项系数:");
for (int i = 0; i <= p->degree; i++) {
scanf("%d", &p->coef[i]);
}
}
// 计算两个多项式的和
Poly add_poly(Poly p1, Poly p2) {
Poly result;
int i;
for (i = 0; i <= p1.degree && i <= p2.degree; i++) {
result.coef[i] = p1.coef[i] + p2.coef[i];
}
while (i <= p1.degree) {
result.coef[i] = p1.coef[i];
i++;
}
while (i <= p2.degree) {
result.coef[i] = p2.coef[i];
i++;
}
result.degree = i - 1;
return result;
}
// 输出多项式各项系数
void print_poly(Poly p) {
printf("多项式各项系数为:");
for (int i = 0; i <= p.degree; i++) {
printf("%d ", p.coef[i]);
}
printf("\n");
}
int main() {
Poly p1, p2, result;
input_poly(&p1);
input_poly(&p2);
result = add_poly(p1, p2);
print_poly(result);
return 0;
}
```
对于链表的实现,可以定义一个结构体来表示多项式的每一项,包括系数和指数。具体实现方法可以参考以下代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct PolyNode *PtrToPolyNode;
struct PolyNode {
int coef; // 系数
int expon; // 指数
PtrToPolyNode next; // 指向下一项的指针
};
typedef PtrToPolyNode Polynomial;
// 从键盘输入多项式各项系数
Polynomial input_poly() {
Polynomial p, rear, t;
int c, e;
p = (Polynomial) malloc(sizeof(struct PolyNode));
p->next = NULL;
rear = p;
printf("请输入多项式各项系数(以指数为-1结束):");
while (1) {
scanf("%d", &c);
scanf("%d", &e);
if (e == -1) {
break;
}
t = (Polynomial) malloc(sizeof(struct PolyNode));
t->coef = c;
t->expon = e;
t->next = NULL;
rear->next = t;
rear = t;
}
return p;
}
// 计算两个多项式的和
Polynomial add_poly(Polynomial p1, Polynomial p2) {
Polynomial front, rear, t;
front = (Polynomial) malloc(sizeof(struct PolyNode));
rear = front;
while (p1 && p2) {
if (p1->expon == p2->expon) {
int sum = p1->coef + p2->coef;
if (sum != 0) {
t = (Polynomial) malloc(sizeof(struct PolyNode));
t->coef = sum;
t->expon = p1->expon;
t->next = NULL;
rear->next = t;
rear = t;
}
p1 = p1->next;
p2 = p2->next;
} else if (p1->expon > p2->expon) {
t = (Polynomial) malloc(sizeof(struct PolyNode));
t->coef = p1->coef;
t->expon = p1->expon;
t->next = NULL;
rear->next = t;
rear = t;
p1 = p1->next;
} else {
t = (Polynomial) malloc(sizeof(struct PolyNode));
t->coef = p2->coef;
t->expon = p2->expon;
t->next = NULL;
rear->next = t;
rear = t;
p2 = p2->next;
}
}
while (p1) {
t = (Polynomial) malloc(sizeof(struct PolyNode));
t->coef = p1->coef;
t->expon = p1->expon;
t->next = NULL;
rear->next = t;
rear = t;
p1 = p1->next;
}
while (p2) {
t = (Polynomial) malloc(sizeof(struct PolyNode));
t->coef = p2->coef;
t->expon = p2->expon;
t->next = NULL;
rear->next = t;
rear = t;
p2 = p2->next;
}
rear->next = NULL;
return front;
}
// 输出多项式各项系数
void print_poly(Polynomial p) {
printf("多项式各项系数为:");
while (p->next) {
printf("%d ", p->next->coef);
p = p->next;
}
printf("\n");
}
int main() {
Polynomial p1, p2, result;
p1 = input_poly();
p2 = input_poly();
result = add_poly(p1->next, p2->next);
print_poly(result);
return 0;
}
```
阅读全文