1

所以前半部分工作得很好,在将方程转换为后缀表示法方面做得很好,但在那之后,命令提示符就在那里闪烁。我想转换为后缀,然后解方程。我能得到任何关于我做错了什么的帮助吗?谢谢!

#include <iostream>
#include <stack>
#include <string>
#include <sstream>
using namespace std;

int priority(string item) //prioritizing the operators
{
    if(item == "(" || item == ")")
    {
        return 0;
    }
    else if(item == "+" || item == "-")
    {
        return 1;
    }
    else
    {
        return 2;
    }
}
int main()
{
    stack <string> myStack; // initializing the stack.
    stack <int> myNewStack; //used for part 2
    char line[256];
    char line2[256];
    cin.getline( line, 256); // this and the proceeding line get the input.
    string exp = line;
    string item;
    string postItem; //used for part 2
    string postFix; //used for part 2

    istringstream iss(exp);

    iss >> item;

    while(iss)
    {
        if(item != "+" && item != "-" && item != "/" && item != "*" && item != "(" && item != ")") //If the char is a number
        {
            cout << item;
            postFix = postFix + item;
        }
        else if(myStack.size() == 0) // If the stack is empty
        {
            myStack.push(item);
        }
        else if( item == "+" || item == "-" || item == "/" || item == "*") //If the char is an operator
        {
            if(priority(myStack.top()) < priority(item)) // the item on the stack is greater priority than the array item
            {
                myStack.push(item);
            }
            else
            {
                while(myStack.size() > 0 && priority(myStack.top()) >= priority(item)) //while the stack contains something, and the item on
                {
                    cout << myStack.top();
                    postFix = postFix + myStack.top();
                    myStack.pop();
                }
                myStack.push(item);
            }
        }
        else if(item == "(") // left peren
        {
            myStack.push(item);
        }
        else if(item == ")") // right peren
        {
            while(myStack.top() != "(")
            {
                cout << myStack.top();
                postFix = postFix + myStack.top();
                myStack.pop();

            }
            myStack.pop();
        }
        iss >> item;
    }
    while (myStack.size() > 0 ) //When nothing is left to evaluate
    {
        cout << myStack.top();
        postFix = postFix + myStack.top();
        myStack.pop();
    }

    //PART 2

    int x1;
    int x2;
    int x3;

    istringstream iss2(postFix);

    iss2 >> postItem;

    while(iss2)
    {
        if(postItem != "+" && postItem != "-" && postItem != "/" && postItem != "*") //if its a number
        {
            int n;
            n = atoi(postItem.c_str());
            myNewStack.push(n);
        }
        else if( postItem == "+" || postItem == "-" || postItem == "/" || postItem == "*") //if its an operator
        {
            if(postItem == "+")
            {
                x1 = myNewStack.top();
                myNewStack.pop();
                x2 = myNewStack.top();
                myNewStack.pop();
                x3 = x1 + x2;
                myNewStack.push(x3);
            }
            else if(postItem == "-")
            {
                x1 = myNewStack.top();
                myNewStack.pop();
                x2 = myNewStack.top();
                myNewStack.pop();
                x3 = x1 - x2;
                myNewStack.push(x3);
            }
            else if(postItem == "/")
            {
                x1 = myNewStack.top();
                myNewStack.pop();
                x2 = myNewStack.top();
                myNewStack.pop();
                x3 = x1 * x2;
                myNewStack.push(x3);
            }
            else if(postItem == "*")
            {
                x1 = myNewStack.top();
                myNewStack.pop();
                x2 = myNewStack.top();
                myNewStack.pop();
                x3 = x1 / x2;
                myNewStack.push(x3);
            }

            iss2 >> postItem;
        }
    }
    cout << "The conversion into infix notation is" + myNewStack.top() << endl;
}
4

0 回答 0