missy 0 Newbie Poster

how do u rewrite the infix to postfix conversion using class. i have my code from struct stack type.

#include <iostream>
#include <cstring>
using namespace std;
const int SIZE = 20;

private:          int top;
              char num [SIZE];
   
public:     void stack_init (stack_type&);
              void push (stack_type& , char);
              void pop (stack_type&, char& );
              int is_empty ( stack_type&);
              int is_full (stack_type&);
}

int main ()
{
        char infix [20], postfix [20]="", ch;
        int size, i, j=-1;
        stack_type stack;
        stack_init (stack);
        cout << "Enter an infix string with no embedded blanks";
        cin >> infix;
        size = strlen(infix);
        for ( i = 0;  i < size ;  i++ )
        {
                if (infix [i] == '+' || infix [i] == '-' ||
                        infix [i] == '*' || infix [i] == '/' )
                // if current char is an operator *************
                        if ( infix [i] == '+' || infix [i] == '-')
                        {  if (!is_empty(stack))
                                {pop (stack, ch);
                                 while (ch=='+'|| ch=='-'||ch=='*'||ch=='/')
                                 { j++;
                                   postfix [j] = ch; 
                                   ch = ' ';  // blank out ch for later testing
                                   if (!is_empty(stack))
                                           pop (stack, ch);
                                 }
                                 if (ch != ' ')
                                         push (stack, ch );
                                 push (stack,infix[i]);
                                }
                           else // if stack is empty
                                push (stack, infix[i]);
                        }
                        else  // if current operand is a high prec operand
                        {  if (!is_empty(stack))
                                {pop (stack, ch);
                                 while (ch=='*'||ch=='/')
                                 { j++;
                                   postfix [j] = ch; 
                                   ch = ' ';  // blank out ch for later testing
                                   if (!is_empty(stack))
                                           pop (stack, ch);
                                 }
                                 if (ch != ' ')
                                         push (stack, ch );
                                 push (stack,infix[i]);
                                }
                           else // if stack is empty
                                push (stack, infix[i]);
                        }

                else // current char is operand or parenthesis
                        if (infix[i] == '(')
                                push (stack, infix[i]);
                        else
                                if (infix[i] == ')')
                                {pop (stack,ch);
                                 while (ch != '(')
                                 { j++;
                                   postfix[j] = ch;
                                   pop (stack,ch);
                                 }
                                }
                                 else   // current is an operand
                                 {j++;
                                  postfix[j] = infix[i];
                                 }
        }  // end of for i loop
while (!is_empty(stack))
{j++;
 pop (stack,ch);
 postfix[j] = ch;
}
cout << postfix << endl;
return 0;
}



void stack_init (stack_type& s )
        {  s.top = -1;
        }
void push (stack_type& s, char item)
        {
          if (s.top+1 < SIZE)
                {  s.top ++;
                        s.num [s.top] = item;
                }
        }
void pop (stack_type & s , char& item )
        {  if (s.top > -1 )
                {  item = s.num[s.top];
                        s.top --;
                }
        }
int is_empty ( stack_type& s )
        {  if (s.top > -1 )
                 return 0;
                else
                 return 1;
        }
int is_full (stack_type& s )
        {  if (s.top == SIZE-1)
                 return 1;
                else
                 return 0;
        }

<< moderator edit: added [code][/code] tags >>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.