Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. int convertToPostfix( char *pszInfix, Out out )
  2. {
  3.     Stack stack = newStack();
  4.     Element element;
  5.     Element popped;
  6.     int bLeft = TRUE;
  7.     pszInfix = getToken( pszInfix, element.szToken , MAX_TOKEN );
  8.     while( pszInfix != NULL )
  9.     {
  10.         categorize( &element );
  11.         switch( element.iCategory )
  12.         {
  13.             //case for '(' character
  14.             case 1:
  15.                     push( stack, element );
  16.                    
  17.  
  18.             //case for ')' character
  19.             case 2:
  20.                     while( isEmpty( stack ) == FALSE )
  21.                     {
  22.                         popped = pop( stack );
  23.  
  24.                         if( strcmp( popped.szToken, "(" ) == TRUE )
  25.                         {
  26.                             bLeft == TRUE;
  27.                             break;
  28.                         }
  29.  
  30.                         addOut( out, popped );
  31.  
  32.                     }
  33.  
  34.                     if( bLeft == FALSE )
  35.                         errExit( "Missing '('" );
  36.  
  37.             //case for '=, NEVER, ONLY, AND, OR'
  38.             case 3:
  39.                     while( isEmpty( stack ) == FALSE )
  40.                     {
  41.                         popped = topElement( stack );
  42.                         if( element.iPrecedence > popped.iPrecedence )
  43.                             break;
  44.                         addOut( out, pop( stack ) );
  45.                     }
  46.  
  47.                     push( stack, element );
  48.  
  49.             //case for operands
  50.             case 4:
  51.                     addOut( out, element );
  52.         }
  53.  
  54.         pszInfix = getToken( pszInfix, element.szToken , MAX_TOKEN );
  55.  
  56.     }
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement