2010-10-03 82 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