#include<iostream>
#include<string>
using namespace std;
class stack
{
    string item;
    int top;
    public:
        stack()
        {
            top=-1;
        }
        void push(char ch)
        {
            top++;
            item[top]=ch;
        }
        char pop()
        {        
            char ele;
            if(isempty())
            {
                printf("\n stack overflow!!");
                return '@';
            }
            ele=item[top];
            top--;
            return ele;
        }
        char peep()
        {
            return(item[top]);
        }
        int isempty()
        {
            if(top==-1)
            return 1;
            return 0;
        }
        int priority(char ch)
        {        
            if(ch==')')
                return 1;
            else if(ch=='+' || ch=='-')
                return 2;
            else if(ch=='*' || ch=='/')
                return 3;
            else 
                return 4;
        }
};

int main()
{
    string infix,prefix;
    char ch,ch2,token,temp;
    int i,j=0,len;
    stack st;
    cout<<"enter the valid infix expression: ";
    cin>>infix;
    len=infix.size();
    for(i=len-1;i>=0;i--)
    {
        token=infix[i];
        if(token==')')
            st.push(token);
        else if(token=='(')
        {
            ch=st.pop();
            while(ch!=')')
            {
                prefix.push_back(ch);
                ch=st.pop();
            }
        }
        else if(token=='+' || token=='-' || token=='*' || token=='/' || token=='^')
        {
            ch2=st.peep();
            while(!st.isempty() && st.priority(token)<st.priority(ch2))
            {
                ch=st.pop();
                prefix.push_back(ch);
                ch2=st.peep();
            }
            st.push(token);
        }
        else
            st.push(token);
    }
    while(!st.isempty())
    {
        ch=st.pop();
        prefix.push_back(ch);
    }

    cout<<"\n The Corresponding Prefix Expression is:";
       for(j = prefix.size() - 1;j>=0;j--)
       {
           cout<<prefix[j];
    }
     return 0;
}

// I am trying to change this to C program languge if possiable .

What is stopping you? Also, why do this since C++ compilers are good to go on almost every OS.

Since auto resizing arrays are not there in c, not classes, you might want to use a linked list or realloc() a char[] to size.

You need to track both the number of cells used (top for a stack) and the capacity (max cells allocated) so you know when to expand the container (stack). Evict your methods from the class, removing class:: and call it a struct.

Member Avatar for kevenm
/* 20191226 Keven Miller convert C++ to C                                */
/* A quick convert to C. Using MAX length definitions for string types.  */
/* Plus some debug fprintfs to help debug.                               */
/* Note: sizeof() returns an unsigned int type, be careful with compares */
/*   such that an int -1 is the Largest unsigned int (not less than 0)   */
/* Note: when changing cout/cin << data, be mindfull of newlines '\n'    */
/* Note: I didn't check boundary conditions!                             */

#include <stdio.h>
#include <string.h>

#define MAX_LINE        256
#define MAX_STK         512

int debug = 0;

typedef struct stack_s {
  int top;
  char item [MAX_STK];
} stack_t;

void stack ( stack_t *stk )
{
  memset ( stk, 0, sizeof(stack_t));
  stk->top = -1;
}

void push ( stack_t *stk, char ch )
{
  if (debug)
    fprintf (stderr, "[push] [%c] top %d\n", ch, stk->top );

  if ( stk->top < (int)(sizeof(stk->item) -1) )
  {
    stk->top++;
    stk->item [stk->top] = ch;
  }
  else
    printf ("** push failed [%c] max stack %d reached\n",
      ch, sizeof(stk->item));
}

int pop ( stack_t *stk )
{
  char ele;

  if ( isempty ( stk ))
  {
    printf (" stack underflow!!\n");
    return '@';
  }
  ele = stk->item [stk->top];
  stk->top--;

  if (debug)
    fprintf (stderr, "[pop] [%c] top %d\n", ele, stk->top );

  return ele;
}

int peep ( stack_t *stk )
{
  if (debug)
    fprintf (stderr, "[peep] [%c] top %d\n", stk->item[stk->top], stk->top );

  return ( stk->item [stk->top] );
}

int isempty ( stack_t *stk )
{
  if ( stk->top == -1 )
    return 1;
  return 0;
}

int priority ( stack_t *stk, char ch )
{
  if ( ch == ')' )
    return 1;
  else if ( ch == '+' || ch == '-' )
    return 2;
  else if ( ch == '*' || ch == '/' )
    return 3;
  else 
    return 4;
}

void push_back ( int ch, char* str, int max )
{
  int len;
  len = strlen (str);
  if ( len < (max -1) )
    str [len] = ch;
  else
    printf ("** push_back failed [%c] max length %d reached\n",
      ch, max);
}

int main ( int argc, char *argv[] )
{
  char infix [MAX_LINE];
  char prefix [MAX_LINE];
  char ch, ch2, token, temp;
  int  i, j=0, len;
  stack_t _st, *st = &_st;

/* Initialize stack */
  stack ( st );
/* zero out prefix for use of push_back routine */
  memset (prefix, 0, sizeof(prefix));

  printf ("Enter the valid infix expression: ");
  fflush (stdout);
  if (!fgets ( infix, sizeof(infix), stdin))
    return 1;
  len = strlen (infix);
  if ( infix [len-1] == '\n')
    infix [--len] = 0;
  else
  {
    printf ("** Input length > allowed maximum of %d\n",
      sizeof(infix)-1 );
    return 2;
  }

  for ( i = len-1; i >= 0; i-- )
  {
    token = infix [i];
    if ( token == ')' )
      push ( st, token );
    else if ( token == '(' )
    {
      ch = pop ( st );
      while ( ch != ')' )
      {
        push_back ( ch, prefix, sizeof(prefix) );
        ch = pop ( st );
      }
    }
    else if ( token == '+' || token == '-' ||
              token == '*' || token == '/' || token == '^' )
    {
      ch2 = peep ( st );
      while (!isempty ( st ) && priority ( st, token ) < priority ( st, ch2 ))
      {
        ch = pop ( st );
        push_back ( ch, prefix, sizeof(prefix) );
        ch2 = peep ( st );
      }
      push ( st, token );
    }
    else
      push ( st, token );
  }

  while (!isempty ( st ))
  {
    ch = pop ( st );
    push_back ( ch, prefix, sizeof(prefix) );
  }

  printf ("The Corresponding Prefix Expression is: ");
  for ( j = strlen (prefix) - 1; j >= 0; j-- )
  {
    printf ("%c", prefix [j] );
  }
  printf ("\n");
  return 0;
}
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.