从控制台分别输入两个关于x的多项式,其中各项系数均为整数,输入时乘号不可忽略,乘方以“^”表示,x的指数按降序排列。各操作数、运算符之间用空格分隔。输出两个多项式相乘的结果,其中x的指数按降序排列。要求:使用队列的顺序存储实现,用c语言,输出代码
时间: 2023-09-16 08:05:14 浏览: 78
### 回答1:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int coef; // 系数
int expn; // 指数
} Term;
typedef struct {
Term data[MAX_SIZE];
int length;
} Poly;
void init(Poly *p);
void read_poly(Poly *p);
void print_poly(Poly *p);
void merge_poly(Poly *pa, Poly *pb, Poly *pc);
void add_poly(Poly *pa, Poly *pb, Poly *pc);
void sub_poly(Poly *pa, Poly *pb, Poly *pc);
void mul_poly(Poly *pa, Poly *pb, Poly *pc);
int main() {
Poly pa, pb, pc;
init(&pa);
read_poly(&pa);
init(&pb);
read_poly(&pb);
init(&pc);
mul_poly(&pa, &pb, &pc);
print_poly(&pc);
return 0;
}
void init(Poly *p) {
p->length = 0;
}
void read_poly(Poly *p) {
char c;
int coef = 0, expn = 0, flag = 0; // flag 用于判断读入的是系数还是指数
while ((c = getchar()) != '\n') {
if (c == 'x' || c == 'X') { // 读入 x
flag = 1;
} else if (c == '^') { // 读入指数
flag = 0;
} else if (c == '+' || c == '-') { // 读入系数
if (coef != 0 || expn != 0) { // 当前项读入完成
p->data[p->length].coef = coef;
p->data[p->length].expn = expn;
coef = 0;
expn = 0;
p->length++;
}
if (c == '-') { // 系数为负
coef = -1;
} else { // 系数为正
coef = 1;
}
} else { // 数字
if (flag == 0) { // 读入的是指数
expn = expn * 10 + c - '0';
} else { // 读入的是系数
coef = coef * 10 + c - '0';
}
}
}
p->data[p->length].coef = coef;
p->data[p->length].expn = expn;
p->length++;
}
void print_poly(Poly *p) {
int i;
for (i = 0; i < p->length; i++) {
if (i > 0 && p->data[i].coef > 0) {
printf("+"); // 正系数前面需要加上加号
}
printf("%d", p->data[i].coef);
if (p->data[i].expn > 0) {
printf("x^%d", p->data[i].expn);
} else if (p->data[i].expn == 0) {
printf("%d", p->data[i].coef);
}
}
printf("\n");
}
void merge_poly(Poly *pa, Poly *pb, Poly *pc) {
int i = 0, j = 0, k = 0;
while (i < pa->length && j < pb->length) {
if (pa->data[i].expn > pb->data[j].expn) {
pc->data[k++] = pa->data[i++];
} else if (pa->data[i].expn == pb->data[j].expn) {
pc->data[k].expn = pa->data[i].expn;
pc->data[k++].coef = pa->data[i++].coef + pb->data[j++].coef;
} else {
pc->data[k++] = pb->data[j++];
}
}
while (i < pa->length) {
pc->data[k++] = pa->data[i++];
}
while (j < pb->length) {
pc->data[k++] = pb->data[j++];
}
pc->length = k;
}
void add_poly(Poly *pa, Poly *pb, Poly *pc) {
int i = 0, j = 0, k = 0;
while (i < pa->length && j < pb->length) {
if (pa->data[i].expn > pb->data[j].expn) {
pc->data[k++] = pa->data[i++];
} else if (pa->data[i].expn == pb->data[j].expn) {
if (pa->data[i].coef + pb->data[j].coef != 0) {
pc->data[k].expn = pa->data[i].expn;
pc->data[k++].coef = pa->data[i++].coef + pb->data[j++].coef;
} else {
i++;
j++;
}
} else {
pc->data[k++] = pb->data[j++];
}
}
while (i < pa->length) {
pc->data[k++] = pa->data[i++];
}
while (j < pb->length) {
pc->data[k++] = pb->data[j++];
}
pc->length = k;
}
void sub_poly(Poly *pa, Poly *pb, Poly *pc) {
int i = 0, j = 0, k = 0;
while (i < pa->length && j < pb->length) {
if (pa->data[i].expn > pb->data[j].expn) {
pc->data[k++] = pa->data[i++];
} else if (pa->data[i].expn == pb->data[j].expn) {
if (pa->data[i].coef - pb->data[j].coef != 0) {
pc->data[k].expn = pa->data[i].expn;
pc->data[k++].coef = pa->data[i++].coef - pb->data[j++].coef;
} else {
i++;
j++;
}
} else {
pc->data[k++] = pb->data[j++];
}
}
while (i < pa->length) {
pc->data[k++] = pa->data[i++];
}
while (j < pb->length) {
pc->data[k].coef = -pb->data[j].coef;
pc->data[k++].expn = pb->data[j++].expn;
}
pc->length = k;
}
void mul_poly(Poly *pa, Poly *pb, Poly *pc) {
int i, j;
Poly tmp;
init(&tmp);
for (i = 0; i < pa->length; i++) {
for (j = 0; j < pb->length; j++) {
tmp.data[j].coef = pa->data[i].coef * pb->data[j].coef;
tmp.data[j].expn = pa->data[i].expn + pb->data[j].expn;
}
tmp.length = pb->length;
if (i == 0) {
for (j = 0; j < pb->length; j++) {
pc->data[j].coef = tmp.data[j].coef;
pc->data[j].expn = tmp.data[j].expn;
}
pc->length = pb->length;
} else {
Poly t;
init(&t);
merge_poly(&tmp, pc, &t);
*pc = t;
}
}
}
### 回答2:
以下是一个使用队列的顺序存储实现计算两个多项式相乘的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int coefficient; // 系数
int exponent; // 指数
} Term;
typedef struct {
Term terms[MAX_SIZE];
int rear;
} Queue;
void initQueue(Queue* queue) {
queue->rear = -1;
}
void enqueue(Queue* queue, Term term) {
if (queue->rear == MAX_SIZE - 1) {
printf("Queue is full.\n");
return;
}
queue->rear++;
queue->terms[queue->rear] = term;
}
Term dequeue(Queue* queue) {
if (queue->rear == -1) {
printf("Queue is empty.\n");
exit(1);
}
Term term = queue->terms[0];
for (int i = 0; i < queue->rear; i++) {
queue->terms[i] = queue->terms[i + 1];
}
queue->rear--;
return term;
}
void multiplyPolynomials(Queue* polynomial1, Queue* polynomial2, Queue* result) {
for (int i = 0; i <= polynomial1->rear; i++) {
for (int j = 0; j <= polynomial2->rear; j++) {
Term term;
term.coefficient = polynomial1->terms[i].coefficient * polynomial2->terms[j].coefficient;
term.exponent = polynomial1->terms[i].exponent + polynomial2->terms[j].exponent;
enqueue(result, term);
}
}
}
int main() {
Queue polynomial1, polynomial2, result;
initQueue(&polynomial1);
initQueue(&polynomial2);
initQueue(&result);
printf("请输入第一个多项式: ");
int coefficient, exponent;
char operator;
while (scanf("%d %c %d", &coefficient, &operator, &exponent) != EOF) {
Term term;
term.coefficient = coefficient;
term.exponent = exponent;
enqueue(&polynomial1, term);
}
printf("请输入第二个多项式: ");
while (scanf("%d %c %d", &coefficient, &operator, &exponent) != EOF) {
Term term;
term.coefficient = coefficient;
term.exponent = exponent;
enqueue(&polynomial2, term);
}
multiplyPolynomials(&polynomial1, &polynomial2, &result);
printf("两个多项式相乘的结果为: ");
while (result.rear != -1) {
Term term = dequeue(&result);
printf("%dX^%d ", term.coefficient, term.exponent);
}
return 0;
}
```
你可以先输入第一个多项式的各项系数和指数,以及乘号和空格分隔,然后按下回车键,并输入第二个多项式的各项系数和指数,同样以乘号和空格分隔,再按下回车键,即可输出两个多项式相乘的结果。
### 回答3:
下面是用队列的顺序存储实现的,C语言代码:
```
#include <stdio.h>
#define MAX_SIZE 100
// 队列结构体
typedef struct {
int data[MAX_SIZE]; // 存储数据的数组
int front; // 队头指针
int rear; // 队尾指针
} Queue;
// 初始化队列
void initQueue(Queue *q) {
q->front = q->rear = -1;
}
// 判断队列是否为空
int isEmpty(Queue *q) {
return q->front == q->rear;
}
// 入队
void enqueue(Queue *q, int item) {
q->rear++;
q->data[q->rear] = item;
}
// 出队
int dequeue(Queue *q) {
q->front++;
return q->data[q->front];
}
// 计算两个多项式相乘的结果
void multiplyPolynomials(Queue *p1, Queue *p2, Queue *result) {
int size1 = p1->rear - p1->front; // 多项式p1的项数
int size2 = p2->rear - p2->front; // 多项式p2的项数
int i, j;
// 初始化结果队列
initQueue(result);
// 多项式相乘
for (i = 0; i <= size1; i++) {
for (j = 0; j <= size2; j++) {
int coef = p1->data[i] * p2->data[j]; // 乘积的系数
int exp = size1 - i + size2 - j; // 乘积的指数
result->data[exp] += coef; // 将同一指数的项相加
}
}
}
int main() {
Queue p1, p2, result;
int i, coef, exp;
// 初始化多项式p1
initQueue(&p1);
printf("请输入第一个多项式的系数和指数,用空格分隔(输入0 0表示输入结束):\n");
while (1) {
scanf("%d %d", &coef, &exp);
if (coef == 0 && exp == 0) {
break;
}
enqueue(&p1, coef);
}
// 初始化多项式p2
initQueue(&p2);
printf("请输入第二个多项式的系数和指数,用空格分隔(输入0 0表示输入结束):\n");
while (1) {
scanf("%d %d", &coef, &exp);
if (coef == 0 && exp == 0) {
break;
}
enqueue(&p2, coef);
}
// 计算两个多项式相乘的结果
multiplyPolynomials(&p1, &p2, &result);
// 输出结果
for (i = result.rear; i >= result.front + 1; i--) {
printf("%dx^%d + ", result.data[i], i);
}
printf("%d\n", result.data[result.front]);
return 0;
}
```
使用以上代码,你可以按照以下方式输入多项式的系数和指数:
- 请先输入第一个多项式的系数和指数,用空格分隔,每个项占一行,输入结束以0 0表示;
- 然后请输入第二个多项式的系数和指数,用空格分隔,每个项占一行,输入结束以0 0表示;
程序会计算两个多项式相乘的结果,并将结果输出。
阅读全文