C++中缀到后缀

发布于 2024-09-26 12:25:31 字数 2524 浏览 0 评论 0原文

编译时出现以下错误:

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

I get the following error when I compile:

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

Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余厌 2024-10-03 12:25:31

看来您的代码只是缺少一些右大括号。就这样。

例如,看看这个:

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

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

else 语句之前的结束 } 在哪里。只需检查您的代码并修复这些愚蠢的错误即可。

如果你让间距和缩进更整齐一些,那么摆脱这些东西就会容易得多

It appears that your code is just missing some closing braces. That's all.

For example, look at this:

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

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

Where is the closing } before the else statement. Just go through your code and fix these silly mistakes.

It would be a lot easier to rid yourself of these things if you made your spacing and indentation a little neater.

秋凉 2024-10-03 12:25:31

“else 之前的预期主表达式”诊断是编译器作者报告您粗鲁地强加给他一个“else”的方式,而没有前面的“if”或(相当于同一件事)“else if”。亚历山大·拉弗蒂(Alexander Rafferty)正确地指出,这是因为代码......

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

当也许你的意思是......

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

尽管也许你不小心删除了一大堆东西,并且很幸运删除导致了在无法解析的代码中,所以你会意识到出了问题。

The "expected primary expression before else" diagnostic is the compiler-author's way of reporting you rudely foisted on him an "else" without either a preceding "if" or (what amounts to the same thing) "else if". Alexander Rafferty correctly points out that this is because the code has ...

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

... when perhaps what you meant was ...

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

... though maybe you deleted a whole bunch of stuff by accident, and were lucky enough that the deletion resulted in unparseable code, so you will realize that something is wrong.

预谋 2024-10-03 12:25:31

看看这些行:

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

else

Look at these lines:

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

else
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文