#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;
}
C++ 多项式相加相减相乘 面向对象类实现
于 2022-10-27 18:10:34 首次发布