多项式及其计算

来源:互联网 发布:国产电视机 知乎 编辑:程序博客网 时间:2024/05/21 21:38

昨天加上今天 想把之前学过的链表进行复习一遍 就花了一个下午加一个晚上的时间,把多项式及其计算做一下,然而让人遗憾的是 我一直想用string 来创建一个多项式 结果发现不行 ==

Polynomial.h

#include<iostream>using namespace std;struct Term    //多项式的节点定义{double coef;    //系数int exp;//指数Term* link;Term(double a = 0, int b = 0, Term *next = NULL) : coef(a), exp(b), link(next) {}Term *InsertAfter(double, int);friend ostream& operator<<(ostream &, const Term &);};class Polynomial{public:Polynomial() { head = new Term(); }Polynomial(Polynomial &);~Polynomial();Polynomial & operator = (const Polynomial &);void PolyClear();int  PolyLength() const;bool PolyEmpty() const;bool AddTerm(double const, int const);void Simplify();void PolySort();int maxOrder();Term * getHead()const { return head; }friend ostream& operator<<(ostream &, const Polynomial &);friend ostream& operator>>(istream &, string &);friend Polynomial& operator+(Polynomial &,  Polynomial &);friend Polynomial& operator*(Polynomial &,  Polynomial &);private:Term *head;};


Polynomial.cpp

#include"Polynomial.h"Term *Term::InsertAfter(double a, int b){link = new Term(a, b, link);return link;}ostream& operator<<(ostream & out, const Term & T){if (0 == T.coef)return out;out << T.coef;switch(T.exp){case 0:break;case 1:out << "X"; break;default: out << "X^" << T.exp; break;}return out;}Polynomial::Polynomial(Polynomial &T){head = new Term();Term* destptr = head;Term* srcptr = T.getHead()->link;while (srcptr != NULL){destptr->InsertAfter(srcptr->coef, srcptr->exp);srcptr = srcptr->link;destptr = destptr->link;}}Polynomial & Polynomial::operator = (const Polynomial & T){if (&T == this)return *this;else{this->~Polynomial();head = new Term();Term* destptr = head;Term* srcptr = T.getHead()->link;while (srcptr != NULL){destptr->InsertAfter(srcptr->coef, srcptr->exp);srcptr = srcptr->link;destptr = destptr->link;}}return *this;}Polynomial::~Polynomial(){Term* frist = head;while (frist != NULL){frist = frist->link;delete[]head;head = frist;}frist = NULL;head = NULL;}void Polynomial::PolyClear(){Term* frist = head->link;Term* next = NULL;while (frist != NULL){next = frist;frist = frist->link;delete[]next;}frist = NULL;next = NULL;}int  Polynomial::PolyLength() const{int length = 0;Term* temp = head->link;while (temp != NULL){if (0 != temp->coef)length++;temp = temp->link;}return length;}bool Polynomial::PolyEmpty() const{if (head == NULL)return true;Term *temp = head->link;while (temp != NULL){if (temp->coef != 0)return false;temp = temp->link;}return true;}bool Polynomial::AddTerm(double const coef, int const exp){Term *term = new Term(coef, exp);if (term == NULL)return false;Term *temp = head;while (temp->link != NULL) { temp = temp->link; }temp->link = term;return true;}void Polynomial::Simplify(){//分为两步 第一步删除0项 第二步合并。Term *pre = head;Term *p = pre->link;while (p != NULL){if (0 == p->coef){pre->link = p->link;delete []p;p = NULL;}pre = pre->link;if (pre == NULL)break;p = pre->link;}//合并pre = head;p =head->link;Term* next = NULL;while (pre != NULL){p = pre->link;next = pre;while (p != NULL){if (pre->exp == p->exp){pre->coef += p->coef;next->link = p->link;delete[]p;}next = next->link;if (next == NULL)break;p = next->link;}pre = pre->link;}pre = NULL;next = NULL;p = NULL;}void Polynomial::PolySort(){Term* pre = head;Term* p = head->link;Term* frist= head;double temp1;int temp2;while (frist != NULL){for (pre = frist; pre->link != NULL; pre = pre->link){p = pre->link;if (pre->exp > p->exp){temp2 = pre->exp;pre->exp = p->exp;p->exp = temp2;temp1 = pre->coef;pre->coef = p->coef;p->coef = temp1;}}frist = frist->link;}}ostream& operator<<(ostream &out, const Polynomial &T){Term* p = T.head->link;while (p != NULL){if (p->link == NULL){ out << *p;return out;}if (p->link->coef > 0)out << *p << "+";elseout << *p;p = p->link;}return out;}int Polynomial::maxOrder(){PolySort();Term *p = head;while (p->link != NULL)p = p->link;return p->exp;}Polynomial& operator+(Polynomial &T, Polynomial &R){T.PolySort();R.PolySort();double temp;Term *t = T.head;Term *r = R.head;Polynomial *N = new Polynomial;while (t->link != NULL || r->link != NULL){if (t->exp == r->exp){temp = t->coef + r->coef;if (temp != 0)(*N).AddTerm(temp, t->exp);t = t->link;r = r->link;}else if(t->exp < r->exp){ (*N).AddTerm(t->coef, t->exp);t = t->link;}else{ (*N).AddTerm(r->coef, r->exp);r = r->link;}}return *N;}Polynomial& operator*(Polynomial &T, Polynomial &R){int length = T.maxOrder() + R.maxOrder()+1;double *temp = new double[length];for (int i = 0; i < length; i++)temp[i] = 0;Term *t = T.head;Term *r = R.head;Polynomial *N = new Polynomial;while (t != NULL){r = R.head;while (r != NULL){temp[(t->exp) + (r->exp)] += (t->coef) * (r->coef);r = r->link;}t = t->link;}for (int i = 0; i < length; i++){if (temp[i] != 0){(*N).AddTerm(temp[i], i);}}delete []temp;temp = NULL;return *N;}
Main.cpp
#include"Polynomial.h"void main(){Polynomial T;T.AddTerm(1, 2);T.AddTerm(-2, 4);T.AddTerm(3, 4);T.AddTerm(-7, 1);T.AddTerm(-5, 2);Polynomial Y;Y.AddTerm(1, 2);Y.AddTerm(4, 3);Y.AddTerm(5, 1);Y.AddTerm(6, 7);cout << T << endl;cout << Y << endl;cout << T + Y << endl;cout << T * Y << endl;system("pause");}

由于时间关系 就直接贴上代码了 要是小伙伴们有啥看不懂的 欢迎评价 我会回复具体思想的。