C 语言实现一元稀疏多项式 a=3x^14+2x^8+1 与 b=8x^14–3x^10+10x^6 的表示及基本操作
以下是使用 C 语言实现一元稀疏多项式的表示及基本操作的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 多项式的项 typedef struct PolyTerm { int coef; // 系数 int exp; // 指数 struct PolyTerm* next; } PolyTerm; // 创建新的项 PolyTerm* createTerm(int coef, int exp) { PolyTerm* newTerm = (PolyTerm*)malloc(sizeof(PolyTerm)); if (newTerm == NULL) { printf("内存分配失败\n"); return NULL; } newTerm->coef = coef; newTerm->exp = exp; newTerm->next = NULL; return newTerm; } // 销毁多项式 void destroyPoly(PolyTerm* head) { PolyTerm* temp; while (head!= NULL) { temp = head; head = head->next; free(temp); } } // 输出多项式 void printPoly(PolyTerm* head) { PolyTerm* current = head; while (current!= NULL) { printf("%dx^%d ", current->coef, current->exp); if (current->next!= NULL) { printf("+ "); } current = current->next; } printf("\n"); } // 加法操作 PolyTerm* addPoly(PolyTerm* a, PolyTerm* b) { PolyTerm* result = NULL; PolyTerm* tail = NULL; while (a!= NULL && b!= NULL) { PolyTerm* newTerm = createTerm(0, 0); if (a->exp > b->exp) { newTerm->coef = a->coef; newTerm->exp = a->exp; a = a->next; } else if (a->exp < b->exp) { newTerm->coef = b->coef; newTerm->exp = b->exp; b = b->next; } else { newTerm->coef = a->coef + b->coef; newTerm->exp = a->exp; a = a->next; b = b->next; } if (newTerm->coef!= 0) { if (result == NULL) { result = newTerm; tail = newTerm; } else { tail->next = newTerm; tail = newTerm; } } else { free(newTerm); } } while (a!= NULL) { PolyTerm* newTerm = createTerm(a->coef, a->exp); if (result == NULL) { result = newTerm; tail = newTerm; } else { tail->next = newTerm; tail = newTerm; } a = a->next; } while (b!= NULL) { PolyTerm* newTerm = createTerm(b->coef, b->exp); if (result == NULL) { result = newTerm; tail = newTerm; } else { tail->next = newTerm; tail = newTerm; } b = b->next; } return result; } // 减法操作 PolyTerm* subtractPoly(PolyTerm* a, PolyTerm* b) { PolyTerm* result = NULL; PolyTerm* tail = NULL; while (a!= NULL && b!= NULL) { PolyTerm* newTerm = createTerm(0, 0); if (a->exp > b->exp) { newTerm->coef = a->coef; newTerm->exp = a->exp; a = a->next; } else if (a->exp < b->exp) { newTerm->coef = -b->coef; newTerm->exp = b->exp; b = b->next; } else { newTerm->coef = a->coef - b->coef; newTerm->exp = a->exp; a = a->next; b = b->next; } if (newTerm->coef!= 0) { if (result == NULL) { result = newTerm; tail = newTerm; } else { tail->next = newTerm; tail = newTerm; } } else { free(newTerm); } } while (a!= NULL) { PolyTerm* newTerm = createTerm(a->coef, a->exp); if (result == NULL) { result = newTerm; tail = newTerm; } else { tail->next = newTerm; tail = newTerm; } a = a->next; } while (b!= NULL) { PolyTerm* newTerm = createTerm(-b->coef, b->exp); if (result == NULL) { result = newTerm; tail = newTerm; } else { tail->next = newTerm; tail = newTerm; } b = b->next; } return result; } int main() { // 构建多项式 a PolyTerm* a = createTerm(3, 14); PolyTerm* term2 = createTerm(2, 8); PolyTerm* term3 = createTerm(1, 0); a->next = term2; term2->next = term3; // 构建多项式 b PolyTerm* b = createTerm(8, 14); PolyTerm* term4 = createTerm(-3, 10); PolyTerm* term5 = createTerm(10, 6); b->next = term4; term4->next = term5; printf("多项式 a: "); printPoly(a); printf("多项式 b: "); printPoly(b); PolyTerm* sum = addPoly(a, b); printf("相加结果: "); printPoly(sum); PolyTerm* diff = subtractPoly(a, b); printf("相减结果: "); printPoly(diff); // 销毁多项式 destroyPoly(a); destroyPoly(b); destroyPoly(sum); destroyPoly(diff); return 0; } ``` 这段代码实现了一元稀疏多项式的表示和基本操作,包括创建、销毁、输出、加法和减法。在`main`函数中,构建了两个示例多项式`a`和`b`,并进行了加法和减法操作,最后销毁了使用的内存。 ######[AI写代码神器 | 1615点数解答 | 2024-10-18 17:19:15]
- C 语言实现一元稀疏多项式 a=3x^14+2x^8+1 与 b=8x^14–3x^10+10x^6 的表示及基本操作(字节豆包 | 1615点数解答 | 2024-10-18 17:19:15)238
- Python实现Q老师成绩管理任务:修改与查看学生成绩(字节豆包 | 191点数解答 | 2025-12-20 20:55:41)70
- Python 代码助力 Q 老师高效管理班级学生成绩:修改与查看一键搞定(字节豆包 | 326点数解答 | 2025-12-27 20:48:12)70
- Octave 4:绘制球面、柱面与极坐标曲线,计算多项式商余及多阶拟合 cos(x) (字节豆包 | 500点数解答 | 2025-03-20 19:37:31)152
- C++实现:画家在洪水中安全回家的最短时间求解(百度文心 | 1384点数解答 | 2025-03-16 17:33:49)398
- Octave实战:多项式求值、矩阵运算及多项式和差积计算代码揭秘(字节豆包 | 450点数解答 | 2025-03-20 19:10:01)221
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)343
- 破解密码锁:Python 实现最少操作次数计算,复杂度低超高效!(字节豆包 | 377点数解答 | 2025-11-08 22:11:16)114
- 破解密码锁:计算最少操作次数打开由n个圆环组成的密码锁(字节豆包 | 163点数解答 | 2025-11-15 21:22:40)112
- Python 实现:轻松将十进制数转换为 2 - 36 进制数(字节豆包 | 509点数解答 | 2026-01-02 19:40:44)48
- Python实现:轻松计算n阶数字矩阵主对角线之和(字节豆包 | 358点数解答 | 2025-12-08 12:25:06)74
- Python 实现:计算数字矩阵左上角数字之和(附代码及详解)(字节豆包 | 333点数解答 | 2025-12-08 17:52:36)80