Ifix到postfix转换 [英] Ifix to postfix conversion

查看:76
本文介绍了Ifix到postfix转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好guys.i已经制作了一个程序将ifix转换为postfix,但是编译器没有提供我想要的输出。请帮我看看我的代码中是否有任何错误感谢



我尝试了什么:



hello guys.i had made a program to convert ifix into postfix but the compiler is not giving the output which i wanted.kindly help me out and see if there is any mistake in my code thanks

What I have tried:

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
void disp(char postfix[],int size)
{
    for (int i=0;i<size;i++)
    {
        cout<<postfix[i];
    }
}
int priority(char);
int main()
{
    char weight;
    char c;
    int tcount=0;  //counter loop to count the  number of brackets//
    int ecount=0;   //counter loop to count the  number of brackets//
    int size=8;
 char equ[8];
 stack<char> s;
 char postfix[8];
 cin.getline(equ,size);
  for(int i=0;i<size;i++ )
    {
        if(equ[i]=='(')
        {
            tcount++;
        }
        if(equ[i]==')')
        {
            ecount++;
        }
    }
    if(tcount==ecount)
    {
        cout<<"equation is valid"<<endl;
    }
    else
        cout<<"equation is not valid"<<endl;
 for(int i=0;i<size;i++)
 {
      c=equ[i];
      if(c!='+' || c!='-' || c!='*' || c!='/')
      {
          postfix[i]=c;
      }
     else if(c=='(')
    s.push(c);

     else if(c==')')
     {
        while( !s.empty() && s.top()!= '(' )
        {
         postfix[i]=s.top();
         s.pop();
     }}

     if(c=='+' || c=='-' || c=='*' || c=='/')
        {
            weight=priority(c);
           if(weight<=priority(s.top()))
         {
                postfix[i]=s.top();
                s.pop();
                s.push(c);
          }
          else
                s.push(c);

while (!s.empty()) {
		postfix[i] = s.top();
		s.pop();}
}
}

disp(postfix,size);
    return 0;
}

int priority(char s)
{
    if(s=='+' || s=='-')
        return 0;
    if(s=='*' || s=='/')
        return 1;
}

推荐答案

只需几点:

Just a couple of points:
Quote:

if(c == e)

{

cout<<等式有效< < endl;

}

if(c==e)
{
cout<<"equation is valid"<<endl;
}

此处您的代码使用未初始化变量 c



here your code is using the uninitialised variable c.

Quote:

if(c!='+ '|| c!=' - '|| c!='*'|| c!='/')

if(c!='+' || c!='-' || c!='*' || c!='/')

这是错误的:这样的不等式应该是AND,而不是ORed,否则if块始终执行。



可能还有其他错误,但您用于变量的短名称短名称会阻碍进一步的分析。





[更新]

你没有修复信号错误!



无论如何,通常这种转换是通过基于语法的递归调用来执行的,如同sho wn在以下代码中:



This is wrong: such inequalities should be ANDed not ORed, otherwise the if block is always executed.

Possibly there are other mistakes, but the infortunate short names you used for the variables discourages further analysis.


[update]
You didn't fixed the signaled bugs!

Anyway, usually such transformations are performed with recursive calls, based on a grammar, as shown in the following code:

#include <iostream>
using namespace std;

/* grammar
  exp  := term + exp |
          term - exp |
          term

  term := fact * term |
          fact / term |
          fact

  fact :=  '(' exp ')' |
          digit
*/

class Expression
{
  const string ifx;
  size_t index;
  string pfx;

  void exp();
  void term();
  void fact();
  char next(){ return ifx[index]; }
  void consume()
  {
    if ( index < ifx.size()-1)
      ++index;
  }
  void emit(char c)
  {
    pfx += ' ';
    pfx += c;
  }

public:
  Expression(string infix_text) : ifx(infix_text){}

  string infix(){return ifx;}
  string postfix()
  {
    if ( pfx == "")
    {
      index = 0;
      exp();
    }
    return pfx;
  }
};

int main()
{
  // some test cases...
  Expression te[3]={{"1+2"}, {"2*4/5+3*(4+2-7)+8"}, {"2*(3+(4-2))"}};
  for (size_t n=0; n<3; ++n)
  {
    cout << te[n].infix()  << " -> " << te[n].postfix() << endl;
  }
}

void Expression::exp()
{
  term();
  char c = next();
  if ( c == '+' || c == '-')
  {
    consume();
    exp();
    emit(c);
  }
}

void Expression::term()
{
  fact();
  char c = next();
  if ( c == '*' || c == '/')
  {
    consume();
    term();
    emit(c);
  }
}

void Expression:: fact()
{
  char c = next();
  if ( c == '(')
  {
    consume();
    exp();
    if ( next() != ')')
      throw invalid_argument("no closing brace");
    consume();
  }
  else
  {
    if ( c < '0' || c>'9' )
      throw invalid_argument("digit expected");
    consume();
    emit(c);
  }
}





[/ update]


这是调试代码的任务。写一些简单的测试数据并检查结果。比写更复杂的测试数据。



我看到你的代码没有正确缩进,因此一些大括号可能不正确。总是在else代码中使用大括号。你的函数disp()是空的。



在关键代码中输出一些输出来查找错误。



快乐虫狩猎; - )
That is your task to debug your code. Write some simple test data and check the results. Than write more complex test data.

I see that your code is not correctly indented and so some braces may not be correct. Alway use braces in the else code. Your function disp() is empty.

Make some output in the critical code pathes to find the bugs.

Happy bug hunting ;-)


这篇关于Ifix到postfix转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆