【c++数据结构】多项式类及相加相乘

#include<iostream>
using namespace std;
struct Term {
	float coef;  
	int exp;
	Term* link;
	Term(float c, int e, Term* next = NULL) {
		coef = c, exp = e, link = next;
	}
	Term* InsertAfter(float c, int e);
	friend ostream& operator<<(ostream&, const Term&);
};

class Polynomial {
private:
	Term* first;
	friend ostream& operator<<(ostream &, const Polynomial&);
	friend istream& operator>>(istream&, const Polynomial&);
	friend Polynomial operator+(Polynomial&, Polynomial&);
	friend Polynomial operator*(Polynomial&, Polynomial&);

public:
	Polynomial()              { first = new Term(0, -1); }
	Polynomial(Polynomial& P);
	Term* getHead()const      { return first; }
	int maxOrder();
};

Term* Term::InsertAfter(float c, int e) {
	link = new Term(c, e, link);
	return link;
}

Polynomial::Polynomial(Polynomial& P) {
	first = new Term(0, -1);
	Term* destptr = first, * scrptr = P.getHead()->link;
	while (scrptr != NULL) {
		destptr->InsertAfter(scrptr->coef, scrptr->exp);
		scrptr = scrptr->link;
		destptr = destptr->link;
	}
}

istream& operator>>(istream& in, Polynomial& P) {
	Term* rear = P.getHead(); float c; int e;
	while (1) {
		in >> c >> e;
		if (e < 0)break;
		rear=rear->InsertAfter(c, e);

	}
	return in;
}

ostream& operator<<(ostream&out , const Term& x) {
	if (x.coef == 0.0)return out;
	out << x.coef;
	switch(x.exp) {
		case 0:break;
		case 1:out << "X"; break;
		default:out << "X^" << x.exp; break;
	}
	return out;
}

ostream& operator<<(ostream& out, const Polynomial& P) {
	bool ishead = true;
	Term*currt=P.getHead()->link;
	while (currt != NULL) {
		if (ishead == false && currt->coef > 0.0)out << "+";
		ishead = false;
		out << *currt;
		currt = currt->link;
	}
	return out;
}

int Polynomial::maxOrder() {
	Term* current = first;
	while (current->link != NULL) {
		current = current->link;
	}
	return current->exp;
}

Polynomial operator+(Polynomial& A,Polynomial& B){
	Polynomial C;
	Term* Acurrt = A.getHead()->link, * Bcurrt =B.getHead()->link;
	Term* Ccurrt = C.getHead();
	while (Acurrt != NULL && Bcurrt!= NULL ) {
		float tempcoef; int tempexp;
		if (Acurrt->exp == Bcurrt->exp) {
			tempcoef = Acurrt->coef + Bcurrt->coef;
			tempexp = Acurrt->exp;
			Acurrt = Acurrt->link;
			Bcurrt = Bcurrt->link;
		}
		else if (Acurrt->exp > Bcurrt->exp) {
			tempcoef = Bcurrt->coef;
			tempexp = Bcurrt->exp;
			Bcurrt = Bcurrt->link;
		}
		else {
			tempcoef = Bcurrt->coef;
			tempexp = Bcurrt->exp;
			Acurrt = Acurrt->link;
		}
		if (tempcoef != 0) Ccurrt->InsertAfter(tempcoef, tempexp);
		Ccurrt = Ccurrt->link;
	}
	while(Acurrt!=NULL){
		Ccurrt->InsertAfter(Acurrt->coef,Acurrt->exp);
		Ccurrt = Ccurrt->link;
		Acurrt = Acurrt->link;

	}
	while (Bcurrt != NULL) {
		Ccurrt->InsertAfter(Bcurrt->coef, Bcurrt->exp);
		Ccurrt = Ccurrt->link;
		Bcurrt = Bcurrt->link;
	}
	return C;
}


Polynomial operator*(Polynomial& A, Polynomial& B) {
	Term* pa, * pb, * pc; int AL, BL, i, k, maxExp;
	Polynomial C;
	pc = C.getHead();
	AL = A.maxOrder(); BL = B.maxOrder();
	if (AL != -1 || BL != -1) {
		maxExp = AL + BL;
		float* result = new float[maxExp + 1];
		for (i = 0; i <= maxExp; i++)result[i] = 0.0;
		pa = A.getHead()->link;
		while (pa != NULL) {
			pb = B.getHead()->link;
			while (pb != NULL) {
				k = pa->exp + pb->exp;
				result[k] = result[k] + pa->coef * pb->coef;
				pb = pb->link;
			}
			pa = pa->link;
		}
		for (int i = 0; i <= maxExp; i++)
			if (abs(result[i]) > 0.001)
				pc = pc->InsertAfter(result[i], i);
		delete[]result;
	}
	pc->link = NULL;
	return C;
}


//void test02() {
//	Polynomial P1,P2,P3,P4;
//	P1.getHead()->InsertAfter(6, 0)->InsertAfter(1,2)->InsertAfter(1, 8);
//	cout <<"P1="<< P1 << endl;
//	P2.getHead()->InsertAfter(2, 0)->InsertAfter(3, 2)->InsertAfter(1, 7);
//	cout << "P2="<< P2<<endl;
//	P3 = P1+P2;
//	cout << "P3=P1+P2=" <<P3<<endl;
//	P4 = P1 * P2;
//	cout << "P4=P1*P2=" << P4 << endl;
//}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值