不记得是从哪里转来的了,我修改了一下,加了多项式相减(Polynomial::poly_dec),计算结果(Polynomial::evaluate)和解析多项式字符串(Polynomial::parse)。
term.h
#include <iostream> using namespace std; class Polynominal; class Term { public: Term(int c, int e); Term(int c, int e, Term *next); Term *insert(int c, int e); public: int coef; int exp; Term *link; friend ostream &operator<<(ostream &out,const Term &a); friend class Polynominal; }; Term::Term(int c,int e) : coef(c), exp(e) { link=0; } Term::Term(int c, int e, Term* next) : coef(c), exp(e) { link=next; } Term* Term::insert(int c, int e)//创建新项 { link = new Term(c, e, link); return link; } ostream& operator<<(ostream &out, const Term &a) { if(a.coef == 0) return out; if(a.coef == -1) out<<'-'; else if(a.coef != 1) out<<a.coef; switch(a.exp) { case 0: break; case 1: out<<"x"; break; default: out<<"x^"<<a.exp; break } return out; }
#include "Term.h" #include <cmath> #include <string> #include <vector> using namespace std; class Polynomial { public: Polynomial();//构造函数 ~Polynomial();//析构函数 void add_terms(int c, int e);//直接增加多项式项 void output(ostream &out) const;//将多项式中的各项送输出流 void poly_inc(Polynomial &r);//多项式相加 void poly_dec(Polynomial &r);//多项式相减 void poly_mul(Polynomial &r);//多项式相乘 void copy(Polynomial& r);//保存中间多项式 bool parse(const string &str);//解析多项式 double evaluate(double x);//计算多项式结果 private: //循环链表 Term *list; //输出重载 friend ostream &operator<<(ostream &out, const Polynomial &r); //输入重载,使用parse friend istream &operator>>(istream &in, Polynomial &r); //加号重载 friend Polynomial &operator+(Polynomial &a, Polynomial &b); //减号重载 friend Polynomial &operator-(Polynomial &a, Polynomial &b); //乘号重载 friend Polynomial &operator*(Polynomial &a, Polynomial &b); }; Polynomial::Polynomial() { list = new Term(0, -1); list->link = list; } Polynomial::~Polynomial() { Term *p = list->link; while(p != list) { list->link = p->link; delete p; p = list->link; } delete list; } bool is_digit(char ch) { return '0' <= ch && ch <= '9'; } bool is_unknown(char ch) { return ch == 'x' || ch == 'X'; } bool is_operator(char ch) { return ch == '-' || ch == '*'; } bool Polynomial::parse(const string &str) { vector<int> ceof, exp; int i; int tmp_a = 0; int tmp_e = 0; int negative_number = 1; // 系数的正负号 bool is_X_front = true; // 判断是系数还是指数,X前面的是系数,X后面是指数 bool is_start = true; for (i = 0; i <= str.length(); i++) { char c; if(i == str.length()) c = '\0'; else c = str[i]; if (is_unknown(c)) { if (tmp_a == 0) tmp_a = 1; ceof.push_back(negative_number * tmp_a); is_X_front = false; } else if (c == '^'){} else if (is_operator(c) || c == '\0') { if (i != 0) { if(tmp_e==INF) exp.push_back(1); else exp.push_back(tmp_e); } if(is_X_front && !is_start) ceof.push_back(tmp_a); is_X_front = true; is_start = false; tmp_a = 0; tmp_e = 0; if (c == '+') negative_number = 1; else negative_number = -1; } else if (is_digit(c)) { if (is_X_front) tmp_a = 10 * tmp_a + (c - '0'); else { if(tmp_e == INF) tmp_e=0; tmp_e = 10 * tmp_e + (c - '0'); } } else return false; } for(int i = 0; i < ceof.size(); i++) add_terms(ceof[i], exp[i]); return true; } void Polynomial::add_terms(int c, int e) { Term *q = list; q = q->insert(c, e); } void Polynomial::output(ostream &out) const { int first = 1; for(Term *p = list->link; p != list; p = p->link) { if(!first && p->coef > 0) out<<"+"; first = 0; out<<*p; } out<<endl; } double Polynomial::evaluate(double x) { double res; Term *p = this->list->link; while(p->exp >= 0) { res += pow((float)p->coef, (float)p->exp); p = p->link; } return res; } void Polynomial::poly_inc(Polynomial &r) { Term*p, *q1 = list, *q; p = r.list->link; q = q1->link; while(p->exp >= 0) { if(p->exp < q->exp) { q1 = q; q = q->link; } if(p->exp==q->exp) { q->coef = p->coef + q->coef; if(q->coef == 0) { q1->link = q->link; delete q; q = q1->link; } else { q1 = q; q = q->link; } } else q1 = q1->insert(p->coef, p->exp); p = p->link; } } void Polynomial::poly_dec(Polynomial &r) { Polynomial p = Polynomial(r); Term *t = p.list->link; while(t->exp >= 0) { t->coef = -t->coef; t = t->link; } poly_inc(p); } void Polynomial::poly_mul(Polynomial &r) { Polynomial a;//过渡 Polynomial b;//保存中间结果 Term *q = list->link; Term *p = r.list->link; Term *q1; for(p = r.list->link; p->exp >= 0; p = p->link) { q1 = a.list->link; for(q = list->link; q->exp >= 0; q = q->link) { q1 = q1->insert(q->coef * p->coef, q->exp + p->exp); } b.poly_inc(a); q1 = a.list->link; while(q1 != a.list) { a.list->link = q1->link; delete q1; q1 = a.list->link; } } this->copy(b); } ostream& operator<<(ostream &out,const Polynomial &r) { r.output(out); return out; } istream& operator>>(istream &in,Polynomial &r) { string str; in>>str; r.parse(str); return in; } Polynomial& operator+(Polynomial &a,Polynomial &b) { a.poly_inc(b); return a; } Polynomial& operator-(Polynomial &a,Polynomial &b) { a.poly_dec(b); return a; } Polynomial &operator *(Polynomial &a, Polynomial &b) { a.poly_mul(b); return a; } void Polynomial::copy(Polynomial &r) { Term *p; Term *q = list->link; while(q != list) { list->link=q->link; delete q; q=list->link; } for(q = list->link, p = r.list->link; p->exp >= 0; p = p->link) { q = q->insert(p->coef, p->exp); } }