使用C语言编写一个数据结构的一元多项式计算器,多项式序列按指数降序排列,实现两个多项式的相加、相减、相乘,并将结果输出。并输出未知数为1时的值
时间: 2024-03-03 22:52:05 浏览: 134
数据结构实现两个多项式的相加和相减
5星 · 资源好评率100%
好的,下面是使用C语言编写的一元多项式计算器的代码。该计算器使用链表实现多项式的存储和操作,多项式节点包含系数和指数两个成员变量。具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义多项式节点结构体
typedef struct poly_node {
float coef; // 系数
int expn; // 指数
struct poly_node *next; // 指向下一个多项式节点的指针
} PolyNode;
// 函数声明
PolyNode* create_poly();
void add_term(PolyNode *poly, float coef, int expn);
void print_poly(PolyNode *poly);
PolyNode* add_poly(PolyNode *poly1, PolyNode *poly2);
PolyNode* sub_poly(PolyNode *poly1, PolyNode *poly2);
PolyNode* mul_poly(PolyNode *poly1, PolyNode *poly2);
float eval_poly(PolyNode *poly, float x);
// 主函数
int main()
{
// 创建多项式1
PolyNode *poly1 = create_poly();
add_term(poly1, 3.0, 4);
add_term(poly1, 2.0, 2);
add_term(poly1, 1.0, 0);
printf("多项式1:");
print_poly(poly1);
// 创建多项式2
PolyNode *poly2 = create_poly();
add_term(poly2, 4.0, 3);
add_term(poly2, -2.0, 2);
add_term(poly2, 1.0, 0);
printf("多项式2:");
print_poly(poly2);
// 多项式加法
PolyNode *sum = add_poly(poly1, poly2);
printf("多项式加法:");
print_poly(sum);
// 多项式减法
PolyNode *diff = sub_poly(poly1, poly2);
printf("多项式减法:");
print_poly(diff);
// 多项式乘法
PolyNode *prod = mul_poly(poly1, poly2);
printf("多项式乘法:");
print_poly(prod);
// 未知数为1时的值
printf("未知数为1时的值:%.2f\n", eval_poly(prod, 1.0));
return 0;
}
// 创建多项式
PolyNode* create_poly()
{
PolyNode *poly = (PolyNode*)malloc(sizeof(PolyNode));
poly->coef = 0.0;
poly->expn = -1;
poly->next = NULL;
return poly;
}
// 添加一项到多项式中
void add_term(PolyNode *poly, float coef, int expn)
{
PolyNode *p = poly;
while (p->next && p->next->expn > expn) {
p = p->next;
}
if (p->next && p->next->expn == expn) {
p->next->coef += coef;
if (p->next->coef == 0.0) {
PolyNode *tmp = p->next;
p->next = tmp->next;
free(tmp);
}
} else {
PolyNode *new_node = (PolyNode*)malloc(sizeof(PolyNode));
new_node->coef = coef;
new_node->expn = expn;
new_node->next = p->next;
p->next = new_node;
}
}
// 打印多项式
void print_poly(PolyNode *poly)
{
PolyNode *p = poly->next;
if (!p) {
printf("0.0\n");
return;
}
while (p) {
if (p->coef > 0.0 && p != poly->next) {
printf("+");
}
if (p->coef != 1.0 || p->expn == 0) {
printf("%.2f", p->coef);
}
if (p->expn > 1) {
printf("x^%d", p->expn);
} else if (p->expn == 1) {
printf("x");
}
p = p->next;
}
printf("\n");
}
// 多项式加法
PolyNode* add_poly(PolyNode *poly1, PolyNode *poly2)
{
PolyNode *sum = create_poly();
PolyNode *p1 = poly1->next;
PolyNode *p2 = poly2->next;
while (p1 && p2) {
if (p1->expn > p2->expn) {
add_term(sum, p1->coef, p1->expn);
p1 = p1->next;
} else if (p1->expn < p2->expn) {
add_term(sum, p2->coef, p2->expn);
p2 = p2->next;
} else {
add_term(sum, p1->coef + p2->coef, p1->expn);
p1 = p1->next;
p2 = p2->next;
}
}
while (p1) {
add_term(sum, p1->coef, p1->expn);
p1 = p1->next;
}
while (p2) {
add_term(sum, p2->coef, p2->expn);
p2 = p2->next;
}
return sum;
}
// 多项式减法
PolyNode* sub_poly(PolyNode *poly1, PolyNode *poly2)
{
PolyNode *diff = create_poly();
PolyNode *p1 = poly1->next;
PolyNode *p2 = poly2->next;
while (p1 && p2) {
if (p1->expn > p2->expn) {
add_term(diff, p1->coef, p1->expn);
p1 = p1->next;
} else if (p1->expn < p2->expn) {
add_term(diff, -p2->coef, p2->expn);
p2 = p2->next;
} else {
add_term(diff, p1->coef - p2->coef, p1->expn);
p1 = p1->next;
p2 = p2->next;
}
}
while (p1) {
add_term(diff, p1->coef, p1->expn);
p1 = p1->next;
}
while (p2) {
add_term(diff, -p2->coef, p2->expn);
p2 = p2->next;
}
return diff;
}
// 多项式乘法
PolyNode* mul_poly(PolyNode *poly1, PolyNode *poly2)
{
PolyNode *prod = create_poly();
PolyNode *p1 = poly1->next;
while (p1) {
PolyNode *p2 = poly2->next;
while (p2) {
add_term(prod, p1->coef * p2->coef, p1->expn + p2->expn);
p2 = p2->next;
}
p1 = p1->next;
}
return prod;
}
// 计算多项式在x处的值
float eval_poly(PolyNode *poly, float x)
{
float sum = 0.0;
PolyNode *p = poly->next;
while (p) {
sum += p->coef * pow(x, p->expn);
p = p->next;
}
return sum;
}
```
注:这个计算器中的多项式序列按指数降序排列,即从高到低排列。
阅读全文