class多项式(链表实现)
生活随笔
收集整理的這篇文章主要介紹了
class多项式(链表实现)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
直接上代碼是不是不好?
如果是大佬就直接跳過(guò)這個(gè)階段。
鏈表實(shí)現(xiàn)的多項(xiàng)式鏈表
以每一個(gè)節(jié)點(diǎn)代表多項(xiàng)式的一個(gè)項(xiàng),常數(shù)項(xiàng)可以理解為次數(shù)為0的項(xiàng)
再用指針將項(xiàng)與項(xiàng)之間聯(lián)系起來(lái)??梢哉f(shuō)是數(shù)據(jù)結(jié)構(gòu)聯(lián)系必做的東西!
代碼390行,慢慢啃吧,有什么不理解,就在評(píng)論區(qū)問(wèn),我還是蠻活躍的。
#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;//判斷終止的條件就是在這個(gè)鏈表走到了終點(diǎn),NULLint size;//記錄節(jié)點(diǎn)數(shù),用于判斷定位刪除和添加是否合理 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();//將原數(shù)據(jù)排序 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;}//經(jīng)過(guò)移動(dòng),使得將新節(jié)點(diǎn)放到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;}//經(jīng)過(guò)移動(dòng),使得要?jiǎng)h除的點(diǎn)就是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)//如果兩者在地址上等價(jià) 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;}}//去除將多項(xiàng)式0轉(zhuǎn)換成數(shù)值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,就是排序,合并重復(fù),還有就是 去掉可以去掉的0項(xiàng) 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,就是排序,合并重復(fù),還有就是 去掉可以去掉的0項(xiàng) 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;}
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的class多项式(链表实现)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: QT,C++多项式计算器—version
- 下一篇: 讲讲排序(C++描述)