2010-10-03 134 views
2

我收到以下錯誤,當我編譯:C++中綴到後綴

convert.cpp: In function 'std::string convert()': 
convert.cpp:62: error: expected primary-expression before 'else' 
convert.cpp:62: error: expected `;' before 'else' 
convert.cpp:108: error: expected `}' at end of input 
convert.cpp:108: error: expected `}' at end of input 
convert.cpp:108: error: expected `}' at end of input 
convert.cpp:108: error: expected `}' at end of input 
convert.cpp:108: error: expected `}' at end of input 

代碼:

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

string infix; // infix expression string 
string operand; 
string operate; 
stack <string> mystack; // this stack is used to push the operator 
string postfix; // postfix string where the operand is appended 

//.................................................................... 
// this function read the infix expression from user 
string input() 
{ 
cout<<" Enter the damn infix expression: "<<endl; 
getline(cin, infix); 
return infix; 
} 
//...................................................................... 
// this function checks for operator precedence in the stack 
int precedence(string e) 
{ 
int f; 
if(e == "*" || e== "/" || e =="%") 
f = 2; 
else 
{ 
if(e == "+" || e == "-") 
f = 1; 
} 

if(e=="."){ 
f=0; 
} 

return f; 

} 




//.................................................................... 
// This function converts infix to postfix 
string convert() 
{ 
for(int i=0; i<infix.length(); i++) 
{ 

    switch(infix[i]){ 

    // operate case start 
    case '+': case'-': case'*': case'/': case'^': case '(': case ')': case'.': 

    operate=infix[i]; 
    { 
    if(mystack.empty() || precedence(operate)>= precedence(mystack.top())) 
    { 
    mystack.push(operate);   

    else 
    { 
    while(precedence(operate)<= precedence(mystack.top())) 
    { 
    postfix.append(mystack.top()); 
    mystack.pop(); 
    } 

    mystack.push(operate); 
    } 

    } 
    }//operate case closed 

    default:  //when operand string char is parsed 
    { 
         operand=infix[i]; 
         postfix.append(operand); 
         break; 

    } // default case closed 

    }//switch closed 

} 

while(!mystack.empty()) 
{ 
postfix.append(mystack.top()) 
mystack.pop(); 
} 

return postfix; 
cout<<postfix; 
} 



//................................................................... 
int main() 
{ 

input(); 

convert(); 
cout<<"postfix is "<<postfix<<endl; 
} 

回答

4

看來,你的代碼只是缺少一些結束括號。就這樣。

例如,看看這個:

operate=infix[i]; 
    { 
    if(mystack.empty() || precedence(operate)>= precedence(mystack.top())) 
    { 
    mystack.push(operate);   

    else 
    { 
    while(precedence(operate)<= precedence(mystack.top())) 

哪裏else語句之前關閉}。只需瀏覽你的代碼並修復這些愚蠢的錯誤。

這將是很多更容易擺脫這些東西,如果你讓你的間距和縮進小一點。

+4

此代碼中的另一個問題是接近年底失蹤分號,postfix.append之後(mystack.top()) – jkerian 2010-10-03 00:30:32

1

「預期的主要表達式之前」診斷是編譯器作者的報告方式粗暴地強加給他一個「其他」沒有前面的「如果」或(什麼相同的事情)「否則如果」 。亞歷山大·拉弗蒂正確地指出,這是因爲代碼有...

if (condition) { 
    // ... 
    else { } 

...當或許你的意思是......

if (condition) { 
    // ... 
} 
else { 
} 

...但也許你刪除一個整體一堆意外的東西,並且幸運的是刪除導致了不可解析的代碼,所以你會意識到有什麼不對。

1

看看這些行:

if(mystack.empty() || precedence(operate)>= precedence(mystack.top())) 
{ 
    mystack.push(operate);   

else 
+0

感謝的人。我犯了愚蠢的錯誤。 – udaysubedi 2010-10-03 01:40:04