C++实现多项式操作及其加减乘除

/*
 * Polynomial.h
 *
 *  Created on: Oct 6, 2015
 *      Author: chris
 */

#ifndef POLYNOMIAL_H_
#define POLYNOMIAL_H_

#include

struct Term{
	double coef;
	int exp;

	Term * next;

	Term():coef(0), exp(0), next(NULL) {}
	Term(double _coef, int _exp = 0):
		coef(_coef), exp(_exp), next(NULL) {}
};


class Polynomial{
	Term poly;

public:
	Polynomial() {}
	Polynomial(const Polynomial & rhs);
	Polynomial & operator = (const Polynomial & rhs);
	~Polynomial();

	void PolyClear();
	int  PolyLength() const;
	bool PolyEmpty() const;

	bool AddTerm(double coef, int exp);
	void Simplify();

	//arithmetic.
	Polynomial & AddPoly(const Polynomial & rhs);
	Polynomial & SubPoly(const Polynomial & rhs);
	Polynomial & MultPoly(const Polynomial & rhs);
	Polynomial & DivPoly(const Polynomial & rhs, Polynomial & rem);
	Polynomial & DivPoly(const Polynomial & rhs);

	void display() const;

	void Swap(Polynomial & rhs);

private:
	void _Copy(const Polynomial & rhs);
};


#endif /* POLYNOMIAL_H_ */




/*
 * Polynomial.cpp
 *
 *  Created on: Oct 6, 2015
 *      Author: chris
 */

#include "Polynomial.h"
#include 
#include 
#include 

using namespace std;

Polynomial::Polynomial(const Polynomial & rhs)
{
	_Copy(rhs);
}

Polynomial & Polynomial::operator = (const Polynomial & rhs)
{
	if(this == &rhs) return *this;

	this->~Polynomial();
	_Copy(rhs);

	return *this;
}

Polynomial::~Polynomial()
{
	PolyClear();
}

void Polynomial::PolyClear()
{
	Term *pH = poly.next, *pT = NULL;
	while(pH) {
		pT = pH;
		pH = pH->next;
		delete pT;
	}
	poly.next = NULL;
}

int Polynomial::PolyLength() const
{
	Term * pT = poly.next;
	int cnt = 0;
	while(pT) {
		++cnt;
		pT = pT->next;
	}
	return cnt;
}

bool Polynomial::PolyEmpty() const
{
	return poly.next == NULL;
}

bool Polynomial::AddTerm(double coef, int exp)
{
	Term *pnew = new Term(coef, exp);
	if(!pnew) return false;

	Term *prior = &poly, *pcur = poly.next;
	while(pcur && exp < pcur->exp) {
		prior = pcur;
		pcur = pcur->next;
	}

	if(!pcur || exp > pcur->exp) {
	    pnew->next = prior->next;
	    prior->next = pnew;
	}else {
		pcur->coef += coef;
		delete pnew;
	}

	return true;
}

void Polynomial::Simplify()
{
	//combine homogeneous terms.
	Term * prior = poly.next;
	while(prior && prior->next) {
		Term * pT = prior->next;
		if(prior->exp == pT->exp) {
			prior->next = pT->next;
			prior->coef += pT->coef;
			delete pT;
		}else {
			prior = pT;
		}
	}
	//delete zeros terms.
	prior = &poly;
	while(prior->next) {
		Term * pT = prior->next;
		if(fabs(pT->coef - 0) < 1e-6 ) {
			prior->next = pT->next;
			delete pT;
		}else
			prior = pT;
	}
}

Polynomial & Polynomial::AddPoly(const Polynomial & rhs)
{
	for(Term * pT = rhs.poly.next; pT; pT = pT->next) {
		AddTerm(pT->coef, pT->exp);
	}
	Simplify();
	return *this;
}

Polynomial & Polynomial::SubPoly(const Polynomial & rhs)
{
	for(Term *pT = rhs.poly.next; pT; pT = pT->next) {
		AddTerm(-pT->coef, pT->exp);
	}
	Simplify();
	return *this;
}

Polynomial & Polynomial::MultPoly(const Polynomial & rhs)
{
	Polynomial prod;
	for(Term* pT1 = poly.next; pT1; pT1 = pT1->next) {
		for(Term* pT2 = rhs.poly.next; pT2; pT2 = pT2->next) {
			prod.AddTerm(pT1->coef * pT2->coef, pT1->exp + pT2->exp);
		}
	}
	Swap(prod);
	Simplify();
	return *this;
}

Polynomial & Polynomial::DivPoly(const Polynomial & rhs, Polynomial & rem)
{
	rem.PolyClear();
	Swap(rem);

	Term *remHead = rem.poly.next, *divHead = rhs.poly.next;
	while(remHead && remHead->exp >= divHead->exp) {
		Term quot(remHead->coef/divHead->coef,
				remHead->exp-divHead->exp);
		AddTerm(quot.coef, quot.exp);

		for(Term * pT = divHead; pT; pT = pT->next) {
			rem.AddTerm(-quot.coef*pT->coef, quot.exp+pT->exp);
		}
		rem.Simplify();
		remHead = rem.poly.next;
	}

	return *this;
}

Polynomial & Polynomial::DivPoly(const Polynomial & rhs)
{
	Polynomial rem;
	return DivPoly(rhs,rem);
}

void Polynomial::display() const
{
	Term * pT = poly.next;
	if(pT == NULL) {
		cout << "0" << endl;
		return;
	}

	bool first = true;
	while(pT) {
		//sign.
		if(pT->coef >= 0) {
			if(!first) {
			    cout << "+";
			}
		}
		else
			cout << "-";

		//coef
		if(fabs(fabs(pT->coef) - 1) > 1e-3)
			cout << fabs(pT->coef);

		//x
		if(pT->exp != 0) {
			cout << "x";
			//exp
			if(pT->exp != 1)
				cout << "^";
			if(pT->exp > 0)
				cout << pT->exp;
			else
				cout << "(" << pT->exp << ")";
		}

		pT = pT->next;
		first = false;
	}
}

void Polynomial::Swap(Polynomial & rhs)
{
	Term * pT = rhs.poly.next;
	rhs.poly.next = poly.next;
	poly.next = pT;
}

//private
void Polynomial::_Copy(const Polynomial & rhs)
{
    Term* pT = rhs.poly.next;
    while(pT) {
    	AddTerm(pT->coef, pT->exp);
    	pT = pT->next;
    }
}




/*
 * Main.cpp
 *
 *  Created on: Oct 6, 2015
 *      Author: chris
 */

#include
#include"Polynomial.h"

using namespace std;

int main(void)
{
	Polynomial poly1, poly2;
	poly1.AddTerm(5, 2);
	poly1.AddTerm(3, 1);
	poly1.AddTerm(7, 0);
	poly1.display();
	cout << endl;

	poly2.AddTerm(4, 2);
	poly2.AddTerm(2, 0);
	poly2.display();
	cout << endl;

	poly1.AddPoly(poly2);
	poly1.display();
	cout << endl;

	poly1.SubPoly(poly2);
	poly1.display();
	cout << endl;

	poly1.MultPoly(poly2);
	poly1.display();
	cout << endl;

	Polynomial rem;
	poly1.DivPoly(poly2, rem);
	poly1.display();
	cout << endl;
	rem.display();
	cout << endl;

	return 0;
}










你可能感兴趣的:(简单数学题,C/C++,数据结构)