C ++中缀到postfix

时间:2010-10-03 00:14:25

标签: c++ compiler-errors

编译时出现以下错误:

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;
} 

3 个答案:

答案 0 :(得分:4)

您的代码似乎缺少一些结束括号。就是这样。

例如,看看这个:

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

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

else语句之前的结束}。只需查看代码并修复这些愚蠢的错误。

如果你的间距和缩进稍微整齐一点,那么很多就更容易摆脱这些事情。

答案 1 :(得分:1)

“在其他之前预期的主要表达”诊断是编译器 - 作者报告你粗暴地强加给他一个“其他”而没有前面的“if”或(相同的东西)“else if”的方式。 Alexander Rafferty正确地指出这是因为代码有......

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

......或许你的意思是......

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

...虽然也许你偶然删除了一大堆东西,并且很幸运,删除导致了无法解析的代码,所以你会发现有些东西是错的。

答案 2 :(得分:1)

看看以下几行:

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

else