StudentSeng

Infix2Postfix

Mar 23rd, 2021
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define SIZE 80 //The limit of expression length
  6.  
  7. typedef struct _stackNode{
  8.   char item;
  9.   struct _stackNode *next;
  10. }StackNode;
  11.  
  12. typedef struct _stack{
  13.   int size;
  14.   StackNode *head;
  15. }Stack;
  16.  
  17. void push(Stack *sPtr, char item);
  18. int pop(Stack *sPtr);
  19. char peek(Stack s);
  20. int isEmptyStack(Stack s);
  21.  
  22. void in2Post(char*, char*);
  23.  
  24. int main()
  25. {
  26.   char infix[SIZE] = {};
  27.   char postfix[SIZE] = {};
  28.  
  29.   printf("Enter an infix expression:\n");
  30.   gets(infix);
  31.  
  32.   in2Post(infix,postfix);
  33.   printf("The postfix expression is \n");
  34.   printf("%s\n",postfix);
  35.   return 0;
  36. }
  37.  
  38. void in2Post(char* infix, char* postfix)
  39. {
  40.   Stack opStack = {};
  41.   char* postfixCurrent = postfix; // where we append to "postfix" (pointer of the first blank index)
  42.   for (char* c = infix; *c != '\0'; c++)
  43.   {
  44.     printf("Current Char: %c\n", *c);
  45.  
  46.     if (*c == '(')  // Open brackets has the "highest precedence" and will always be pushed into opStack.
  47.     {
  48.       push(&opStack, *c);
  49.     }
  50.     // A closed brackets means we have finished with the 'entire expression' within the brackets so we just pop all the operatos into the postfix expression until we reach the opening bracket '(',
  51.     // this is similar to when we finish scanning the whole infix expression, we just pop everything from opStack into the postfix expression (think of the entire infix expression being in 1 large bracket)
  52.     else if (*c == ')')
  53.     {
  54.       while (peek(opStack) != '(')
  55.       {
  56.         *postfixCurrent = peek(opStack);
  57.         pop(&opStack);
  58.         postfixCurrent++;
  59.       }
  60.       pop(&opStack);
  61.     }
  62.     else if (strchr("+-*/", *c)) // For the case of operators
  63.     {
  64.       printf("OPERATOR!!\n");
  65.       // Only push if this operator is higher precedence than the one in stack, or if we reach a '(' (the start of the current bracket we are in) or if the stack is empty
  66.       // Else we keep popping the stack into the postfix expression until one of the above condition is met.
  67.         while (!isEmptyStack(opStack) && peek(opStack) != '(' && (!((strchr("*/", *c)) && strchr("+-", peek(opStack)))))
  68.         {
  69.           printf("SAME OR LOWER PRECEDENCE, POP AND PUSH UNTIL SATISFIED!\n");
  70.           *postfixCurrent = peek(opStack);
  71.           postfixCurrent++; // Increment for 'postfix'
  72.           pop(&opStack);
  73.         }
  74.         printf("NOW HIGHER PRECEDENCE / EMPTY / '(' , PUSH!\n");
  75.         push(&opStack, *c);
  76.     }
  77.     else  // Operands, we just append to postfix expression
  78.     {
  79.       *postfixCurrent = *c;
  80.       postfixCurrent++;
  81.     }
  82.   }
  83.  
  84.   // Pop all remaining operators into postfix expression
  85.   while (!isEmptyStack(opStack))
  86.   {
  87.     *postfixCurrent = peek(opStack);
  88.     pop(&opStack);
  89.     postfixCurrent++;
  90.   }
  91. }
  92.  
  93. void push(Stack *sPtr, char item){
  94.   StackNode *newNode;
  95.   newNode = malloc(sizeof(StackNode));
  96.   newNode->item = item;
  97.   newNode->next = sPtr->head;
  98.   sPtr->head = newNode;
  99.   sPtr->size++;
  100. }
  101.  
  102. int pop(Stack *sPtr){
  103.   if(sPtr == NULL || sPtr->head == NULL){
  104.   return 0;
  105.   }
  106.   else{
  107.    StackNode *temp = sPtr->head;
  108.    sPtr->head = sPtr->head->next;
  109.    free(temp);
  110.    sPtr->size--;
  111.    return 1;
  112.   }
  113. }
  114.  
  115. char peek(Stack s){
  116.   return s.head->item;
  117. }
  118.  
  119. int isEmptyStack(Stack s){
  120.    if(s.size == 0) return 1;
  121.    else return 0;
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment