直接上代码是不是不好?

如果是大佬就直接跳过这个阶段。

链表实现的多项式链表

以每一个节点代表多项式的一个项,常数项可以理解为次数为0的项

再用指针将项与项之间联系起来。可以说是数据结构联系必做的东西!

代码390行,慢慢啃吧,有什么不理解,就在评论区问,我还是蛮活跃的。

#include <iostream>
#include <cmath>
#include <sstream>
#include <stack>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
#define EPS 1e-6struct Node{int exponent;double ratio;Node *next;Node (double r = 0,int e = 0,Node*n = NULL):exponent(e),ratio(r),next(n){};bool operator > (const Node& n) {return exponent > n.exponent; }bool operator < (const Node& n) {return exponent < n.exponent; }bool operator == (const Node& n) {return exponent == n.exponent;}bool operator >= (const Node& n) {return exponent >= n.exponent;}bool operator <= (const Node& n) {return exponent <= n.exponent;}
}; class polynome{Node *first;//判断终止的条件就是在这个链表走到了终点,NULLint size;//记录节点数,用于判断定位删除和添加是否合理
public:polynome();polynome(const polynome & pl);~polynome();polynome& operator =(const polynome & pl);void insert(double r,int e,int pos);void remove(int pos);void clear();int length()const;void sort();//将原数据排序 friend istream& operator >> (istream & cin, polynome & p);friend ostream& operator << (ostream & cout, const polynome & p);//输入输出 //calculate polynome operator + ( polynome & p );polynome operator - ( polynome & p );polynome operator * ( polynome & p );//辅助*polynome help_for_multiply (double r,int e);double getNum(double x);polynome dericative();
}; polynome::polynome():first(NULL),size(0){};
polynome::~polynome() {for(Node *p; p = first; delete p)//思路,如果头指针不是空,就把头指针清掉 first = first -> next;
}
void polynome::insert(double r,int e,int pos) {if(pos > size|| pos < 0)return;if(pos == 0){Node* n = new Node(r,e,first);first = n;++size;return ;} Node *current = first;Node *prev = NULL;int p = 0;for (int i = 0; i < pos;++i){prev = current;current = current->next;}//经过移动,使得将新节点放到prev和current之间 Node *n = new Node(r,e,current);prev->next = n;++size;
}void polynome::remove(int pos) {if (pos < 0|| pos >= size)return;if (pos == 0){Node *p = first;first = first->next;--size;delete p;return;}Node* current = first;Node* prev = NULL;for (int i=0;i < pos;++i){prev = current;current = current->next;}//经过移动,使得要删除的点就是current prev -> next = current ->next;delete current; --size;
}int polynome::length()const {return size;
}void polynome::clear() {for(Node *p; p = first; delete p)//思路,如果头指针不是空,就把头指针清掉 first = first -> next; size = 0;
}polynome::polynome(const polynome & pl) {//copy constructorif(pl.length() <= 0){first = 0;size = 0;return;}*this = pl;
}polynome& polynome::operator = (const polynome & pl) {if (this == &pl)//如果两者在地址上等价 return *this;if(pl.length() <= 0){clear();return *this;}first = new Node(pl.first -> ratio, pl.first -> exponent);Node *current = first;Node *current_pl = pl.first -> next;while (current_pl != NULL){current -> next = new Node(current_pl -> ratio,current_pl -> exponent);current = current -> next;current_pl = current_pl -> next;}size = pl.size;return *this;
}istream& operator >> (istream & cin, polynome & p) {int n,e;double r;p.clear();cin >> n;for (int i = 0; i < n; ++i) {cin >> r>>e;p.insert(r,e,0);}p.sort();return cin;
}ostream& operator << (ostream & cout, const polynome & p) {if (p.length() <= 0)return cout;Node *current = p.first;bool first_time = true;for (int i = 0; i < p.length() ; ++i) {if (current -> ratio == 0) {if (current -> exponent == 0 && p.length() == 1) {cout << current -> ratio;first_time = false;} else {cout << "the exponent =  "<<current->exponent<<endl;cout << "the ratio =  "<<current->ratio<<endl; } current = current -> next;continue;}if (current -> exponent == 0) {if (first_time || current -> ratio < 0) {cout <<current -> ratio;first_time =  false;} else{cout <<"+"<< current -> ratio; } } else if (current -> exponent == 1) {if (first_time || current -> ratio < 0) {if (current -> ratio != 1)cout << current -> ratio << "x";else cout << "x";first_time =  false;} else {cout << "+";if (current -> ratio != 1)cout << current -> ratio << "x";else cout << "x";}} else {if (first_time || current -> ratio < 0){if (current -> ratio != 1)cout << current -> ratio << "x^"<<current->exponent;else cout << "x^"<<current->exponent;first_time =  false;}else{cout << "+";if (current -> ratio != 1)cout << current -> ratio << "x^"<<current->exponent;else cout << "x^"<<current->exponent;}}current = current -> next;}return cout;
} void polynome::sort() { for (Node *p = first; p ; p = p -> next){for (Node* q = p -> next; q; q = q->next) {if (*p > *q) {int temp_exponent = p->exponent;p->exponent = q->exponent;q->exponent = temp_exponent;double temp_ratio = p->ratio;p->ratio = q->ratio;q->ratio = temp_ratio; }}}//完成排序 stack<int>stack_exponent;Node *s,*prev;//用于删除 for (Node *p = first; p ; ) {if (stack_exponent.empty() || stack_exponent.top() != p->exponent){stack_exponent.push(p->exponent);prev = p;p = p -> next; }else {prev->ratio += p->ratio;s = p;prev->next = p->next;p = p->next;delete s;size --;}} //完成去重//完成去不合理的0 prev = first;if (first != NULL){for (Node *p = first -> next; p ; ) {if ( abs(p -> ratio) < EPS ) {prev -> next = p -> next;s = p;p = p -> next;delete s; --size;}else {prev = p;p = p -> next;}}if (size > 1 && abs(first -> ratio) < EPS ) {s = first;first = first -> next;delete s; --size;}}//去除将多项式0转换成数值0if(size == 1 && first -> ratio == 0) {first->exponent = 0;} else if (size == 0) {first = new Node(0,0,0);size ++;}
}polynome polynome::operator + ( polynome & p ) {polynome new_polynome;sort();p.sort();//整理p和this,就是排序,合并重复,还有就是 去掉可以去掉的0项 Node *it_p = first, *it_q = p.first;while (it_p != NULL && it_q != NULL) {if ( it_p -> exponent == it_q -> exponent ) {new_polynome.insert(it_p ->ratio + it_q->ratio, it_p->exponent,0);it_p = it_p -> next;it_q = it_q -> next;} else if (it_p ->exponent < it_q -> exponent ){new_polynome.insert(it_p -> ratio, it_p -> exponent,0);it_p = it_p -> next; } else if (it_p -> exponent > it_q -> exponent) {new_polynome.insert(it_q -> ratio, it_q -> exponent,0);it_q = it_q -> next;}}while (it_p != NULL) {new_polynome.insert(it_p -> ratio, it_p -> exponent,0);it_p = it_p -> next; }while (it_q != NULL) {new_polynome.insert(it_q -> ratio, it_q -> exponent,0);it_q = it_q -> next;}new_polynome.sort();return new_polynome;
}polynome polynome::operator - ( polynome & p ) {polynome new_polynome;sort();p.sort();//整理p和this,就是排序,合并重复,还有就是 去掉可以去掉的0项 Node *it_p = first, *it_q = p.first;while (it_p != NULL && it_q != NULL) {if ( it_p -> exponent == it_q -> exponent ) {new_polynome.insert(it_p ->ratio - it_q->ratio, it_p->exponent,0);it_p = it_p -> next;it_q = it_q -> next;} else if (it_p ->exponent < it_q -> exponent ){new_polynome.insert(it_p -> ratio, it_p -> exponent,0);it_p = it_p -> next; } else if (it_p -> exponent > it_q -> exponent) {new_polynome.insert(-it_q -> ratio, it_q -> exponent,0);it_q = it_q -> next;}}while (it_p != NULL) {new_polynome.insert(it_p -> ratio, it_p -> exponent,0);it_p = it_p -> next; }while (it_q != NULL) {new_polynome.insert(-it_q -> ratio, it_q -> exponent,0);it_q = it_q -> next;}new_polynome.sort();return new_polynome;
}polynome polynome::help_for_multiply (double r,int e) {polynome new_polynome;for (Node *p = first; p ; p = p -> next) {new_polynome.insert( p -> ratio * r, p -> exponent + e, 0);}new_polynome.sort();return new_polynome;
}polynome polynome::operator * ( polynome & p ) {sort();Node *it_p = first;polynome new_polynome,temp;for ( ; it_p ; it_p = it_p -> next) {temp = p.help_for_multiply(it_p -> ratio,it_p -> exponent);new_polynome = new_polynome + temp;}return new_polynome;
}polynome polynome::dericative() {polynome new_polynome;for (Node *p =  first; p ; p = p -> next ) {if (p->exponent == 0){continue;}else {new_polynome.insert(p -> exponent * p -> ratio, p -> exponent - 1, 0);}} new_polynome.sort();return new_polynome;
}double polynome::getNum(double x) {Node *p = first;double sum = 0;while ( p ) {sum +=( pow(x,p->exponent) * p->ratio );p = p -> next;}return sum;
}int main(){polynome p;}

class多项式(链表实现)相关推荐

  1. LeetCode 1634. 求两个多项式链表的和

    文章目录 1. 题目 2. 解题 1. 题目 多项式链表是一种特殊形式的链表,每个节点表示多项式的一项. 每个节点有三个属性: coefficient:该项的系数.项 9x4 的系数是 9 . pow ...

  2. 【数据结构】C++单链表实现多项式加法(直接输入多项式)

    题目描述: 计算两个多项式的和. 输入: 输入有两行,每行为一个每项按照指数从大到小顺序的多项式,多项式的每一项用mx^n表示,其中系数m为非0整数,指数n是非负整数. 输出: 输出两个多项式相加的结 ...

  3. MFC版链表实现稀疏多项式相加减

    链表实现多项式运算(加减)MFC可视化版 题目 设计一个一元稀疏多项式简单计算器. 基本要求 (1)输入并建立两个多项式: (2)多项式a与b相加,建立和多项式c: (3)多项式a与b相减,建立差多项 ...

  4. 多项式加法运算(链表实现)

    文章目录 创建结点类型 打印多项式 尾插 插入排序 多项式相加 代码总览 + 结果展示 创建结点类型 我们用链表存储一个多项式,那么该链表的每一个结点就代表多项式的某一项.所以我们的每一个结点必须包含 ...

  5. 第四周实践项目7 多项式求和

    /* *Copyright (c) 2017,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:项目7- 用单链表存储一元多项式,并实现两个多项式的加法. *作 者 ...

  6. c++用模板实现稀疏多项式_用线性表实现一元多项式及相加运算

    " 本文主要讨论线性表在多项式计算中的应用,讨论内容涉及到一元n次多项式在计算机中的表示,及多项式相加运算." 01 在数学上,一个一元n次多项式可以按照升幂写成 Pn(x)= p ...

  7. 多项式的链式存储方案

    1.题目 设计并建立链式存储分配系统,来表示和操作多项式.使用带头结点的循环链表.多项式的每一项表示为一个结点,结点使用如下结构: coef expon link 为了有效地删除多项式,使用可用表空间 ...

  8. 数据结构上机实践第四周项目7 - 多项式求和

    项目6 - 多项式求和 用单链表存储一元多项式,并实现两个多项式的加法. 提示:  1.存储多项式的数据结构  多项式的通式是pn(x)=anxn+an−1xn−1+...+a1x+a0.n次多项式共 ...

  9. 设计一个一元稀疏多项式简单计算器

    目录 1.题目 2.需求分析 3.程序设计 4.测试结果 5.源码 1.题目 [问题描述] 设计一个一元稀疏多项式简单计算器. [基本要求] (1)输入并建立两个多项式: (2)多项式a与b相加,建立 ...

最新文章

  1. 观百工堰竹筏竞技比赛有感
  2. python运行列表的结果不同_python 3 代码一模一样,出现运行结果不同的情况(只是不以为一样而已)...
  3. mysql符合安可要求吗,安可是什么意思?演唱太过精彩,粉丝要求返场(再唱一个)...
  4. 数据中心细节_当细节很重要时数据不平衡
  5. 对话框找不到WM_ERASEBKGND消息的解决方法与对话框背景图片的载入方法
  6. java.util.Scanner简单应用
  7. 第 4 章(表达式和运算符)(4.1~ 4.7)
  8. cisco packet tracer实验案例-重置路由器ENABLE特权密码
  9. 【vue项目使用echarts实现区域地图绘制,且可点击单独区域】
  10. C# Combobox可输入+自动完成
  11. 初探 amaze-vue( 基于vue.js封装的Amaze UI 组件库)
  12. 【已解决】输入mysqld -install时报错Install/Remove of the Service Denied
  13. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
  14. vivado安装教程
  15. 人机对话系统的对话管理
  16. PMP第六章:项目进度管理
  17. (Note)同比和环比
  18. HDLBits学习笔记——移位寄存器
  19. Windows字符和字符串处理
  20. android 图片画圆,在Android中画圆形图片的几种办法

热门文章

  1. python几种排序_python各种排序算法
  2. Vue实例里this的使用
  3. 让你的插件兼容AMD, CMD ,CommonJS和 原生 JS
  4. SOA与微服务基本原则及对比
  5. BZOJ 2288 贪心 +链表
  6. 记录自定义的代码片段位置
  7. Spring context:component-scan代替context:annotation-config
  8. apache 域名跳转
  9. 跟着老王学python
  10. vsftp 添加虚拟帐号