C++完成多项式的加减法

该博客详细介绍了如何使用C++编程实现多项式的输入和多项式之间的加减操作,重点在于递减指数的多项式处理。
摘要由CSDN通过智能技术生成

完成了多项式输入,以及多个多项式的相加减。

多项式指数默认递减

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值