2013-05-03 63 views
0

所以,我遇到了这个程序的问题。任何人都可以告诉我我在这里做错了什么?该程序应该采取一个简单的中缀记法数学表达式(例如“5 - 2 + 1”),然后将其转换为(例如“5 2 - 1 +”),然后解决它将是4.它转换得很好但一旦进入评估部分,它就不会在命令提示符下显示任何内容。我可以得到一些帮助吗?谢谢!将中缀转换为后缀,然后解决方程

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


int priority(string item) //prioritizing the operators 
{ 
if(item == "(" || item == ")") 
{ 
    return 0; 
} 
else if(item == "+" || item == "-") 
{ 
    return 1; 
} 
else //if(item == "/" || item == "*") <-- guess didnt need to define this one 
{ 
    return 2; 
} 
} 
void welcome()//welcome text 
{ 
    cout << "Welcome to this program!" << endl; 
    cout << "Please enter your equation" << endl; 
} 

int main() 
{ 
    welcome(); // welcome text 

stack <string> myStack; // initializing the stack. 
char line[256]; 
cin.getline(line, 256); // this and the proceeding line get the input. 
string exp = line; 
string item; 
string postFix; 

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 + item; 
       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 + item; 
      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(); 
} 
    cout << endl; 
    //PART 2 

int x1; 
int x2; 
int x3; 

stack<int> thirdStack; 
string exp2 = postFix; 
string item2; 

istringstream iss2(exp2); 

iss2 >> item2; 

while(iss2) 

    if(item2 != "+" && item2 != "-" && item2 != "/" && item2 != "*") //if its a number 
    { 
     int n; 

     n = atoi(item2.c_str()); 
     thirdStack.push(n); 
    } 
    else if(item2 == "+" || item2 == "-" || item2 == "/" || item2 == "*") //if its an operator 
    { 
      x1 = thirdStack.top(); 
      thirdStack.pop(); 
      x2 = thirdStack.top(); 
      thirdStack.pop(); 
     if(item2 == "+") 
     { 

      x3 = x1 + x2; 
      thirdStack.push(x3); 
     } 
     else if(item2 == "-") 
     { 

      x3 = x1 - x2; 
      thirdStack.push(x3); 
     } 
     else if(item2 == "/") 
     { 

      x3 = x1 * x2; 
      thirdStack.push(x3); 
     } 
     else if(item2 == "*") 
     { 
      x3 = x1/x2; 
      thirdStack.push(x3); 
     } 
    } 
} 
cout << "The conversion into infix notation is" + thirdStack.top() << endl; 
} 

回答

1

这段代码有很多问题。

在第1部分中,尽管您的代码似乎写出了正确的后缀转换,但它构建的postFix字符串并不是相同的东西。

例如,在一些地方,你有这样的代码:

cout << myStack.top(); 
postFix = postFix + item; 

你写出myStack.top()但加入item您对Postfix的结果。它应该是这样的:

cout << myStack.top(); 
postFix = postFix + myStack.top(); 

此外,你应该包括您添加到后缀串各项目之间的间隔。所以你的结果应该是5 2 - 1 +而不是52-1+。否则,当试图解释该表达式时,第一项将被解释为52.

然后在第2部分中,您在while循环结束时错过了对iss2 >> item2;的调用。你基本上只是一次又一次地解释第一个项目,所以代码将以无限循环结束。

而在您的计算中,您的操作数顺序不正确。这对于加法和乘法无关紧要,但它可以用于减法和除法。例如,减法计算应该是x3 = x2 - x1;而不是x3 = x1 - x2;

最后,当你流出结果时,你应该有一个流操作符。它应该看起来像这样:

cout << "The conversion into infix notation is" << thirdStack.top() << endl;