c++ - 试图编写一个程序来分析数学方程式,转换为后缀表示法,然后再求解吗?

标签 c++

因此,前半部分效果很好,并且很好地完成了将方程式转换为后缀表示法的工作,但此后,命令提示符只是在那里闪烁了。我想转换为后缀,然后求解方程式。我在做错什么可以得到任何帮助吗?谢谢!

#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;
}

最佳答案

转到此链接,单击底部的“与此讲义相关的代码”,它将为您显示一些示例。我不能在这里写代码,时间太长。
http://sci.notbc.org/~weiss/cisc3130/Lectures/04-3-Applications-ExpressionEvaluation.html

关于c++ - 试图编写一个程序来分析数学方程式,转换为后缀表示法,然后再求解吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16342585/

相关文章:

c++ - 如何为MS VC++开发的C++程序分配特定段的代码

c++ - 弱/共享指针,检测何时有用户剩余, boost

c++ - 移植比较 32 位 float 与 64 位 float 的代码,这个值代表什么?

c++ - 为什么锁定会减慢顺序文件解析器的速度?

c++ - 如何使用 COM 从 win32 应用程序处理 Excel 2007 和 Excel 2003(Excel 图表)?

c++ - 如何将文本从 CP437 编码转换为 UTF8 编码?

C++ MFC : Draw a bitmap into a CFrame

c++ - 使用 cURL 在 C++ 中将网页保存到内存

c++ - 如何封装两个流缓冲区

c++ - 如何在 C++ 中初始化静态 vector 数组成员