文章标题
来源:互联网 发布:守望先锋低配置优化 编辑:程序博客网 时间:2024/05/29 19:54
一元多项式求导求积求和
c++语言代码
#include <iostream>#define OK 0#define ERO -1using namespace std;typedef int Data;struct PolyNode{ double coef; // 系数 int exp; // 指数 PolyNode *next; // 下一项};typedef PolyNode* PolyList;// 初始化int initPolyList(PolyList &poly){ poly = new PolyNode; poly->next = NULL; return OK;}// 销毁int destoryPolyList(PolyList &poly){ if (poly == NULL) return ERO; PolyNode *pA = poly; while (pA != NULL) { poly = poly->next; delete pA; pA = poly; } return OK;}// 在指定节点后面添加新节点int insertAfter(PolyNode *node, double new_coef, int new_exp){ if (node == NULL) return ERO; PolyNode *pNewNode = new PolyNode; pNewNode->coef = new_coef; pNewNode->exp = new_exp; pNewNode->next = node->next; node->next = pNewNode; return OK;}// 在指定节点后面删除节点int deleteAfter(PolyNode *currnode){ if (currnode == NULL || currnode->next == NULL) return ERO; PolyNode *delnode = currnode->next; currnode->next = delnode->next; delete delnode; return OK;}// 打印多项式int printPolyList(PolyList &poly){ if (poly == NULL || poly->next == NULL) { cout << "(null)" << endl; return ERO; } for (PolyNode *node = poly->next; node != NULL; node = node->next) { cout << node->coef << " " << node->exp << " "; } cout << endl; return OK;}// 求导int diffPolyList(PolyList &poly){ if (poly == NULL) return ERO; for (PolyNode *node = poly; node != NULL && node->next != NULL; node = node->next) { if (node->next->exp == 0) { deleteAfter(node); } else { node->next->coef *= node->next->exp; --node->next->exp; } } return OK;}// 求和A += Bint addPolyList(PolyList &polyA, PolyList &polyB){ if (polyA == NULL || polyB == NULL) return ERO; PolyNode *pA = polyA; PolyNode *pB = polyB; while (pA != NULL && pA->next != NULL && pB != NULL && pB->next != NULL) { if (pA->next->exp > pB->next->exp) { pA = pA->next; } else if (pA->next->exp < pB->next->exp) { insertAfter(pA, pB->next->coef, pB->next->exp); pA = pA->next; pB = pB->next; } else { pA->next->coef += pB->next->coef; if (pA->next->coef == 0) { deleteAfter(pA); } else { pA = pA->next; } pB = pB->next; } } while (pB->next != NULL) { insertAfter(pA, pB->next->coef, pB->next->exp); pA = pA->next; pB = pB->next; } return OK;}// 求积int multiPolyList(PolyList &polyA, PolyList &polyB, PolyList &product){ if (polyA == NULL || polyA->next == NULL) return ERO; if (polyB == NULL || polyB->next == NULL) return ERO; if (product == NULL) return ERO; for (PolyNode *pA = polyA->next; pA != NULL; pA = pA->next) { PolyList polyC; initPolyList(polyC); PolyNode *pC = polyC; for (PolyNode *pB = polyB->next; pB != NULL; pB = pB->next) { double new_coef = pA->coef * pB->coef; int new_exp = pA->exp + pB->exp; insertAfter(pC, new_coef, new_exp); pC = pC->next; } addPolyList(product, polyC); destoryPolyList(polyC); } return OK;}// 输入多项式int inputPolyList(PolyList &poly){ PolyNode *insert_pos = poly; int n; cout << "请输入多项式的项数:"; cin >> n; cout << "请输入系数、指数(降序排列,空格隔开):"; for (int i = 0; i < n; ++i) { double coef; int exp; cin >> coef >> exp; insertAfter(insert_pos, coef, exp); insert_pos = insert_pos->next; } return OK;}int showMenu(){ cout << "1.求导" << endl; cout << "2.求和" << endl; cout << "3.求积" << endl; cout << "0.退出" << endl; int choic; cin >> choic; return choic;}int main(){ int choic; while ((choic = showMenu()) != 0) { switch (choic) { case 1: PolyList poly; initPolyList(poly); inputPolyList(poly); diffPolyList(poly); cout << "导多项式为:"; printPolyList(poly); destoryPolyList(poly); break; case 2: PolyList polyA, polyB, polySum; initPolyList(polyA); initPolyList(polyB); initPolyList(polySum); inputPolyList(polyA); inputPolyList(polyB); addPolyList(polySum, polyA); addPolyList(polySum, polyB); cout << "和多项式为:"; printPolyList(polySum); destoryPolyList(polyA); destoryPolyList(polyB); destoryPolyList(polySum); break; case 3: PolyList polyX, polyY, Product; initPolyList(polyX); initPolyList(polyY); initPolyList(Product); inputPolyList(polyX); inputPolyList(polyY); multiPolyList(polyX, polyY, Product); cout << "积多项式为:"; printPolyList(Product); destoryPolyList(polyX); destoryPolyList(polyY); destoryPolyList(Product); break; } } return 0;}
阅读全文
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- leetcode 11. Container With Most Water
- 关于使用proteus进行绘图和仿真
- 第四章类和对象
- PE文件结构
- 大数据正式17
- 文章标题
- redis学习笔记:发布和订阅
- java 抽象类及接口
- What is covariate(协变量)
- Linux 下zip包的压缩与解压
- RabbitMQ的安装与入门
- JavaScript--JS操作属性注意的命名规则
- pandas--DataFrame基本使用
- C语言重要知识点