C++ 多项式相加相减相乘 面向对象类实现

#include<iostream>
#include<iomanip>
using namespace std;
class LinkNode {
public:
	int zhishu;
	double xishu;
	LinkNode* next;
	LinkNode() {
		this->zhishu = 0;
		this->xishu = 0;
		this->next = NULL;
	};
	//LinkNode(int zhishu, double xishu) {
	//	this->zhishu = zhishu;
	//	this->xishu = xishu;
	//	this->next = NULL;
	//}
	LinkNode(double xishu, int zhishu) {
		this->zhishu = zhishu;
		this->xishu = xishu;
		this->next = NULL;
	}
	~LinkNode() {};
};
class LinkList {
public:
	LinkNode* node;
	LinkList() {
		node = new LinkNode();
		node->next = NULL;
	}
	LinkList(bool) {
		node = new LinkNode();
		node->next = NULL;
		char douhao;
		while (true) {
			cin >> node->xishu >> douhao >> node->zhishu;
			if (node->xishu == 0 && node->zhishu == 0)
				break;
			this->AddNode(node->xishu, node->zhishu);
		}
	}
	int AddNode(double xishu,int zhishu) {
		LinkNode* nptr = new LinkNode(xishu,zhishu);
		LinkNode* nodert = node;
		while (nodert && nodert->next && nodert->next->zhishu > nptr->zhishu) {
			nodert = nodert->next;
		}
		if (nptr && nodert && nodert->next && nptr->zhishu == nodert->next->zhishu) {
			nodert->next->xishu += nptr->xishu;
			if (nodert->next->xishu == 0) {
				nodert->next = nodert->next->next;
			}
			delete nptr;
		}
		else {
			if (nptr && nodert)
				nptr->next = nodert->next;
			if (nodert)
				nodert->next = nptr;
		}
		return 0;
	}
	int TraceRt() {
		LinkNode* nodeptr = node;
		while (nodeptr->next) {
			cout << nodeptr->next->xishu << " " << nodeptr->next->zhishu << endl;
			nodeptr = nodeptr->next;
		}
		return 0;
	}
	int ShowFx() {
		LinkNode* nodeptr = node;
		if (nodeptr && nodeptr->next && nodeptr->next->xishu < 0)
			cout << "-";
		while (nodeptr&&nodeptr->next) {
			if (nodeptr->next->xishu == 0) {
				continue;
			}
			else if (nodeptr->next->zhishu == 0) {
				/*cout << setprecision(2) << nodeptr->next->xishu;*/
				printf("%.1f", fabs(nodeptr->next->xishu));
			}
			else if (fabs(nodeptr->next->xishu) == 1 || nodeptr->next->zhishu == 1) {
				if (fabs(nodeptr->next->xishu) == 1 && nodeptr->next->zhishu == 1) {
					cout << "X";
				}
				else if (fabs(nodeptr->next->xishu) == 1)
					cout << "X^" << nodeptr->next->zhishu ;
				else {
					printf("%.1f", fabs(nodeptr->next->xishu));
					cout << "X";
				}
			}
			else {
				printf("%.1f", fabs(nodeptr->next->xishu));
				cout << "X^" << nodeptr->next->zhishu;

			}
			if (nodeptr->next->next) {
				if (nodeptr->next->next->xishu > 0)
					cout << "+";
				else
					cout << "-";
			}
			nodeptr = nodeptr->next;
		}
        cout << endl;
		return 0;
	}
	LinkList* AddList(LinkList* other) {
		LinkList* LHead = new LinkList();
		LinkNode* LAptr = this->node;
		LinkNode* LBptr = other->node;
		while (LAptr && LAptr->next) {
			LHead->AddNode(LAptr->next->xishu, LAptr->next->zhishu);
			LAptr = LAptr->next;
		}
		while (LBptr && LBptr->next) {
			LHead->AddNode(LBptr->next->xishu, LBptr->next->zhishu);
			LBptr = LBptr->next;
		}
		return LHead;
	}
	LinkList* SubList(LinkList* other) {
		LinkList* LHead = new LinkList();
		LinkNode* LAptr = this->node;
		LinkNode* LBptr = other->node;
		while (LAptr && LAptr->next) {
			LHead->AddNode(LAptr->next->xishu, LAptr->next->zhishu);
			LAptr = LAptr->next;
		}
		while (LBptr && LBptr->next) {
			LHead->AddNode(-LBptr->next->xishu, LBptr->next->zhishu);
			LBptr = LBptr->next;
		}
		return LHead;
	}
	LinkList* MulList(LinkList* other) {
		LinkNode* LAptr = this->node;
		LinkNode* LBptr = other->node;
		LinkList* LHead = new LinkList();
		while (LAptr && LAptr->next) {
			LBptr = other->node;
			while (LBptr && LBptr->next) {
				LHead->AddNode(LAptr->next->xishu * LBptr->next->xishu, LAptr->next->zhishu + LBptr->next->zhishu);
				LBptr = LBptr->next;
			}
			LAptr = LAptr->next;
		}
		return LHead;
	}
	double ShowValue(double x) {
		double end = 0;
		LinkNode* nodert = node;
		while (nodert && nodert->next) {
			end += nodert->next->xishu * pow(x, nodert->next->zhishu);
			nodert = nodert->next;
		}
		return end;
	}
};
int main(void) {
	LinkList* LA = new LinkList(true);
	/*LA->AddNode(3, 2);
	LA->AddNode(1, 1);
	LA->AddNode(1, 0);*/

	LinkList* LB = new LinkList(true);
	/*LB->AddNode(-2, 2);
	LB->AddNode(-1, 1);
	LB->AddNode(-1, 0);*/
	double test;
	cin >> test;
	LinkList* LC = LA->AddList(LB);
	LinkList* LD = LA->SubList(LB);
	LinkList* LE = LA->MulList(LB);

	cout << "f(x)=";
	LA->ShowFx();
	cout << "g(x)=";
	LB->ShowFx();

	cout << "f(x)+g(x)=";
	LC->ShowFx();
	cout << "f(x)-g(x)=";
	LD->ShowFx();
	cout << "f(x)*g(x)=";
	LE->ShowFx();

	printf("f(%.1f)=%.1f", test, LA->ShowValue(test));
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

alasnot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值