2013-05-03 121 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;