完成了多项式输入,以及多个多项式的相加减。
多项式指数默认递减
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
typedef struct tagNode
{
int coef;//系数
int exp;//指数
}Node;
queue<Node> readPoly(){
queue<Node> poly;
int tempExp;
int tempCoef;
Node node;
cin>>tempCoef>>tempExp;
while (!(tempExp==0 && tempCoef==0))
{
node.coef = tempCoef;
node.exp = tempExp;
poly.push(node);
cin>>tempCoef>>tempExp;
}
return poly;
}
void writePoly(queue<Node> poly)
{
Node tempNode;
while(!poly.empty())
{
tempNode = poly.front();
poly.pop();
cout<<tempNode.coef<<"X^"<<tempNode.exp<<(poly.empty()?"":"+");
}
cout<<endl;
}
//多项式加法
queue<Node> addPoly(queue<Node> poly1,queue<Node> poly2){
queue<Node> resultPoly;
Node node1;
Node node2;
while (!poly1.empty()&&!poly2.empty())
{
node1 = poly1.front();
node2 = poly2.front();
Node resultNode;
if(node1.exp == node2.exp)//指数相同,系数相加
{
resultNode.exp = node1.exp;
resultNode.coef = node1.coef + node2.coef;
resultPoly.push(resultNode);
poly1.pop();
poly2.pop();
continue;
}
else if(node1.exp > node2.exp)
{
resultPoly.push(node1);
poly1.pop();
}
else if(node1.exp < node2.exp)
{
resultPoly.push(node2);
poly2.pop();
}
}
while (!poly1.empty())//此时poly1不为空的话,节点依次进入队列
{
node1 = poly1.front();
resultPoly.push(node1);
poly1.pop();
}
while (!poly2.empty())
{
node2 = poly2.front();
resultPoly.push(node2);
poly2.pop();
}
return resultPoly;
}
//多项式减法
queue<Node> subPoly(queue<Node> poly1,queue<Node> poly2){
queue<Node> resultPoly;
Node node1;
Node node2;
while (!poly1.empty()&&!poly2.empty())
{
node1 = poly1.front();
node2 = poly2.front();
Node resultNode;
if(node1.exp == node2.exp)//指数相同,系数相减
{
resultNode.exp = node1.exp;
resultNode.coef = node1.coef - node2.coef;
if (resultNode.coef!=0)//系数相减后不等于0时,push到结果多项式
{
resultPoly.push(resultNode);
}
poly1.pop();
poly2.pop();
continue;
}
else if(node1.exp > node2.exp)
{
resultPoly.push(node1);
poly1.pop();
}
else if(node1.exp < node2.exp)
{
resultPoly.push(node2);
poly2.pop();
}
}
while (!poly1.empty())
{
node1 = poly1.front();
resultPoly.push(node1);
poly1.pop();
}
while (!poly2.empty())
{
node2 = poly2.front();
resultPoly.push(node2);
poly2.pop();
}
return resultPoly;
}
queue<Node> calculate(stack<queue<Node>> polystack, stack<char> op){
queue<Node> resultPoly = polystack.top();//将结果初始化为栈顶
polystack.pop();//出栈
while (!polystack.empty())
{
queue<Node> temppoly = polystack.top();
polystack.pop();
char oper = op.top();
op.pop();
if (oper=='+')
{
resultPoly = addPoly(temppoly,resultPoly);
}
else if(oper=='-')
{
if (op.empty())//前一个操作符为空
{
resultPoly = subPoly(temppoly,resultPoly);
}
else if(op.top()=='+')//前一个操作符为+
{
resultPoly = subPoly(temppoly,resultPoly);
}
else if (op.top()=='-')//前一个操作符为-
{
resultPoly = addPoly(temppoly,resultPoly);
}
}
}
return resultPoly;
}
int main(){
queue<Node> resultPoly;
stack<queue<Node>> polystack;//多项式栈
stack<char> op;//符号栈
char oper ='!';
while (oper!='=')//输入多项式和操作符
{
queue<Node> poly = readPoly();
polystack.push(poly);
cin>>oper;
if (oper!='=')//不等于=时,op入符号栈
{
op.push(oper);
}
}
resultPoly = calculate(polystack,op);
writePoly(resultPoly);
return 0;
}