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