利用所学的数据结构的基本知识,设计一个一元稀疏多项式简单计算器。 输入:输入并建立多项式; 输出:输出多项式,输出形式为整数序列要求:1)多项式相加/相减;2)计算多项式在x处的值;可以用带表头结点的单链表存储多项式,多项式的项数存放在头结点中。用C语言数据结构
时间: 2023-12-14 21:37:00 浏览: 132
数据结构(C语言)用单链表存储一元多项式-并实现两个多项式的相加运算.doc
5星 · 资源好评率100%
下面是一个基于带头结点的单链表实现的一元稀疏多项式简单计算器的代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义多项式项的结构体
typedef struct PolyNode {
int coef; // 系数
int expon; // 指数
struct PolyNode *next; // 指向下一项的指针
} PolyNode, *Polynomial;
// 创建一个空的多项式(只有头结点)
Polynomial createPoly() {
Polynomial poly = (Polynomial) malloc(sizeof(PolyNode));
poly->next = NULL;
return poly;
}
// 从标准输入读入多项式
Polynomial readPoly() {
Polynomial poly = createPoly();
int n; // 项数
scanf("%d", &n);
while (n--) {
int coef, expon;
scanf("%d%d", &coef, &expon);
if (coef != 0) { // 忽略系数为0的项
Polynomial p = (Polynomial) malloc(sizeof(PolyNode));
p->coef = coef;
p->expon = expon;
p->next = NULL;
// 将新项插入到多项式中
Polynomial q = poly;
while (q->next && q->next->expon > expon) {
q = q->next;
}
if (q->next && q->next->expon == expon) { // 合并同类项
q->next->coef += coef;
if (q->next->coef == 0) { // 合并后系数为0,删除该项
Polynomial t = q->next;
q->next = t->next;
free(t);
}
} else { // 插入新项
p->next = q->next;
q->next = p;
}
}
}
return poly;
}
// 将多项式输出到标准输出
void printPoly(Polynomial poly) {
if (!poly->next) {
printf("0\n");
} else {
printf("%d ", count(poly)); // 输出项数
Polynomial p = poly->next;
while (p) {
printf("%d %d ", p->coef, p->expon);
p = p->next;
}
printf("\n");
}
}
// 返回多项式的项数
int count(Polynomial poly) {
int count = 0;
Polynomial p = poly->next;
while (p) {
count++;
p = p->next;
}
return count;
}
// 多项式相加
Polynomial addPoly(Polynomial poly1, Polynomial poly2) {
Polynomial poly = createPoly();
Polynomial p = poly1->next, q = poly2->next;
while (p && q) {
if (p->expon > q->expon) {
Polynomial t = (Polynomial) malloc(sizeof(PolyNode));
*t = *p;
t->next = NULL;
p = p->next;
insertPoly(poly, t);
} else if (p->expon < q->expon) {
Polynomial t = (Polynomial) malloc(sizeof(PolyNode));
*t = *q;
t->next = NULL;
q = q->next;
insertPoly(poly, t);
} else {
Polynomial t = (Polynomial) malloc(sizeof(PolyNode));
t->coef = p->coef + q->coef;
t->expon = p->expon;
t->next = NULL;
p = p->next;
q = q->next;
if (t->coef != 0) {
insertPoly(poly, t);
} else {
free(t);
}
}
}
while (p) {
Polynomial t = (Polynomial) malloc(sizeof(PolyNode));
*t = *p;
t->next = NULL;
p = p->next;
insertPoly(poly, t);
}
while (q) {
Polynomial t = (Polynomial) malloc(sizeof(PolyNode));
*t = *q;
t->next = NULL;
q = q->next;
insertPoly(poly, t);
}
return poly;
}
// 在多项式中插入一个项,按指数降序排列
void insertPoly(Polynomial poly, Polynomial node) {
Polynomial p = poly;
while (p->next && p->next->expon > node->expon) {
p = p->next;
}
if (p->next && p->next->expon == node->expon) { // 合并同类项
p->next->coef += node->coef;
if (p->next->coef == 0) { // 合并后系数为0,删除该项
Polynomial t = p->next;
p->next = t->next;
free(t);
} else {
free(node);
}
} else {
node->next = p->next;
p->next = node;
}
}
// 多项式相减
Polynomial subPoly(Polynomial poly1, Polynomial poly2) {
Polynomial poly = createPoly();
Polynomial p = poly1->next, q = poly2->next;
while (p && q) {
if (p->expon > q->expon) {
Polynomial t = (Polynomial) malloc(sizeof(PolyNode));
*t = *p;
t->next = NULL;
p = p->next;
insertPoly(poly, t);
} else if (p->expon < q->expon) {
Polynomial t = (Polynomial) malloc(sizeof(PolyNode));
*t = *q;
t->coef = -t->coef;
t->next = NULL;
q = q->next;
insertPoly(poly, t);
} else {
Polynomial t = (Polynomial) malloc(sizeof(PolyNode));
t->coef = p->coef - q->coef;
t->expon = p->expon;
t->next = NULL;
p = p->next;
q = q->next;
if (t->coef != 0) {
insertPoly(poly, t);
} else {
free(t);
}
}
}
while (p) {
Polynomial t = (Polynomial) malloc(sizeof(PolyNode));
*t = *p;
t->next = NULL;
p = p->next;
insertPoly(poly, t);
}
while (q) {
Polynomial t = (Polynomial) malloc(sizeof(PolyNode));
*t = *q;
t->coef = -t->coef;
t->next = NULL;
q = q->next;
insertPoly(poly, t);
}
return poly;
}
// 计算多项式在x处的值
int calcPoly(Polynomial poly, int x) {
int sum = 0;
Polynomial p = poly->next;
while (p) {
sum += p->coef * pow(x, p->expon);
p = p->next;
}
return sum;
}
int main() {
// 读入第一个多项式
Polynomial poly1 = readPoly();
// 读入第二个多项式
Polynomial poly2 = readPoly();
// 输出第一个多项式
printf("Poly1: ");
printPoly(poly1);
// 输出第二个多项式
printf("Poly2: ");
printPoly(poly2);
// 计算第一个多项式在x=2处的值
int x = 2;
int result = calcPoly(poly1, x);
printf("Poly1(%d) = %d\n", x, result);
// 计算第二个多项式在x=3处的值
x = 3;
result = calcPoly(poly2, x);
printf("Poly2(%d) = %d\n", x, result);
// 计算两个多项式相加的结果
Polynomial sum = addPoly(poly1, poly2);
printf("Poly1 + Poly2: ");
printPoly(sum);
// 计算两个多项式相减的结果
Polynomial diff = subPoly(poly1, poly2);
printf("Poly1 - Poly2: ");
printPoly(diff);
return 0;
}
```
这个计算器支持读入两个多项式,计算多项式在某个点的值,以及将两个多项式相加和相减。其中,多项式以带头结点的单链表形式存储,每个节点表示一个项,包含系数和指数两个成员变量。
阅读全文