c语言编写数据结构的一元多项式计算器,输入多项式,序列按指数升序排列,实现两个多项式的相加相减相乘,并输出相加相减相乘结果
时间: 2023-10-30 18:23:20 浏览: 133
好的,我可以帮你解答这个问题。首先,我们需要定义多项式的结构体,包含系数和指数两个成员变量:
```c
typedef struct PolyNode {
float coef; // 系数
int expn; // 指数
struct PolyNode *next;
} PolyNode, *Polynomial;
```
接着,我们可以实现多项式的输入函数,按照指数升序排列:
```c
Polynomial CreatePoly() {
Polynomial head = (Polynomial)malloc(sizeof(PolyNode));
head->next = NULL;
Polynomial p = head;
int n;
printf("请输入多项式项数:");
scanf("%d", &n);
printf("请按指数升序输入各项系数和指数,如:a*x^b\n");
for (int i = 0; i < n; i++) {
Polynomial node = (Polynomial)malloc(sizeof(PolyNode));
scanf("%f*x^%d", &node->coef, &node->expn);
while (p->next && p->next->expn <= node->expn) {
if (p->next->expn == node->expn) {
p->next->coef += node->coef;
free(node);
node = NULL;
break;
}
p = p->next;
}
if (node) {
node->next = p->next;
p->next = node;
p = head;
}
}
return head;
}
```
接下来,我们可以实现多项式的加减乘运算,具体实现可以参考下面的代码:
```c
Polynomial Add(Polynomial P1, Polynomial P2) {
Polynomial head = (Polynomial)malloc(sizeof(PolyNode));
head->next = NULL;
Polynomial p = head;
Polynomial p1 = P1->next, p2 = P2->next;
while (p1 && p2) {
if (p1->expn < p2->expn) {
p->next = p1;
p1 = p1->next;
} else if (p1->expn > p2->expn) {
p->next = p2;
p2 = p2->next;
} else {
float sum = p1->coef + p2->coef;
if (sum != 0) {
Polynomial node = (Polynomial)malloc(sizeof(PolyNode));
node->coef = sum;
node->expn = p1->expn;
p->next = node;
p = p->next;
}
p1 = p1->next;
p2 = p2->next;
}
}
p->next = p1 ? p1 : p2;
return head;
}
Polynomial Subtract(Polynomial P1, Polynomial P2) {
Polynomial head = (Polynomial)malloc(sizeof(PolyNode));
head->next = NULL;
Polynomial p = head;
Polynomial p1 = P1->next, p2 = P2->next;
while (p1 && p2) {
if (p1->expn < p2->expn) {
p->next = p1;
p1 = p1->next;
} else if (p1->expn > p2->expn) {
Polynomial node = (Polynomial)malloc(sizeof(PolyNode));
node->coef = -p2->coef;
node->expn = p2->expn;
p->next = node;
p = p->next;
p2 = p2->next;
} else {
float diff = p1->coef - p2->coef;
if (diff != 0) {
Polynomial node = (Polynomial)malloc(sizeof(PolyNode));
node->coef = diff;
node->expn = p1->expn;
p->next = node;
p = p->next;
}
p1 = p1->next;
p2 = p2->next;
}
}
while (p1) {
p->next = p1;
p1 = p1->next;
p = p->next;
}
while (p2) {
Polynomial node = (Polynomial)malloc(sizeof(PolyNode));
node->coef = -p2->coef;
node->expn = p2->expn;
p->next = node;
p = p->next;
p2 = p2->next;
}
return head;
}
Polynomial Multiply(Polynomial P1, Polynomial P2) {
Polynomial head = (Polynomial)malloc(sizeof(PolyNode));
head->next = NULL;
Polynomial p = head;
for (Polynomial p1 = P1->next; p1; p1 = p1->next) {
for (Polynomial p2 = P2->next; p2; p2 = p2->next) {
int expn = p1->expn + p2->expn;
float coef = p1->coef * p2->coef;
Polynomial node = (Polynomial)malloc(sizeof(PolyNode));
node->coef = coef;
node->expn = expn;
while (p->next && p->next->expn <= expn) {
if (p->next->expn == expn) {
p->next->coef += coef;
free(node);
node = NULL;
break;
}
p = p->next;
}
if (node) {
node->next = p->next;
p->next = node;
p = head;
}
}
}
return head;
}
```
最后,我们可以在主函数中调用上述函数实现多项式的输入和运算,并输出结果:
```c
int main() {
Polynomial P1, P2, P;
printf("请输入第一个多项式:\n");
P1 = CreatePoly();
printf("请输入第二个多项式:\n");
P2 = CreatePoly();
P = Add(P1, P2);
printf("相加结果为:\n");
PrintPoly(P);
P = Subtract(P1, P2);
printf("相减结果为:\n");
PrintPoly(P);
P = Multiply(P1, P2);
printf("相乘结果为:\n");
PrintPoly(P);
return 0;
}
```
其中,PrintPoly函数可以用于输出多项式:
```c
void PrintPoly(Polynomial P) {
if (!P->next) {
printf("0\n");
return;
}
for (Polynomial p = P->next; p; p = p->next) {
if (p->coef > 0 && p != P->next) {
printf("+");
}
if (p->expn == 0) {
printf("%.2f", p->coef);
} else if (p->expn == 1) {
printf("%.2f*x", p->coef);
} else {
printf("%.2f*x^%d", p->coef, p->expn);
}
}
printf("\n");
}
```
这样,我们就完成了一个简单的多项式计算器。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)