Search code examples
cpostfix-notation

How to implement the math pow of my postfix algorithm - C


I have simple algorithm for create postfix in C.

Here is a sample program with my addition to handle pow:

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/

#define SIZE 50            /* Size of Stack */
#include <ctype.h>
char s[SIZE];
int top = -1; /* Global declarations */

push(char elem) { /* Function for PUSH operation */
 s[++top] = elem;
}

char pop() { /* Function for POP operation */
 return (s[top--]);
}

int pr(char elem) { /* Function for precedence */
 switch (elem) {
 case '#':
  return 0;
 case '(':
  return 1;
 case '+':
 case '-':
  return 2;
 case '*':
 case '/':
  return 3;
 case '^':
  return 4;
 }
}

main() { /* Main Program */
 char infx[50], pofx[50], ch, elem;
 int i = 0, k = 0;
 printf("\n\nRead the Infix Expression ? ");
 scanf("%s", infx);
 push('#');
 while ((ch = infx[i++]) != '\0') {
  if (ch == '(')
   push(ch);
  else if (isalnum(ch))
   pofx[k++] = ch;
  else if (ch == ')') {
   while (s[top] != '(')
    pofx[k++] = pop();
   elem = pop(); /* Remove ( */
  } else { /* Operator */
   while (pr(s[top]) >= pr(ch))
    pofx[k++] = pop();
   push(ch);
  }
 }
 while (s[top] != '#') /* Pop from stack till empty */
  pofx[k++] = pop();
 pofx[k] = '\0'; /* Make pofx as valid string */
 printf("\n\nGiven Infix Expn: %s  Postfix Expn: %s\n", infx, pofx);
}

I try to implement math pow, but it doesnt work, can you help me? How can I implement? For example input is: (1-5)^2^3, and output is 15-2^3^ but it is wrong, the correct output is 15-23^^

I think, that error is in method int pr(char elem) , can you help me more?


Solution

  • The problem does not lie in int pr(char elem), as it is a very simple function that only returns the precedence value for an operator. It's more subtle than that:

    Numbers (and actually alphabetic characters as well) get 'pushed' immediately into pofx. Parentheses and operators need to be temporarily saved on a stack of their own: s. Only at the end of a string, or when a higher precedence operator is encountered, the operators currently on their own stack need to get added to pofx:

    ./infix "(1-5)+2*3"
    pushing number '1'
    pushing operator '-' (top is ()
    pushing number '5'
    resolving parentheses
    pushing operator '+' (top is #)
    pushing number '2'
    pushing operator '*' (top is +)
    pushing number '3'
    Postfix Expn: 15-23*+
    

    The original code checks for "higher-or-equal" precedence:

    .. /* Operator */
       while (pr(s[top]) >= pr(ch))
    

    Given the part ..^2^3, the sequence of pushing is

    ^ - operator, #4 -> push on operand stack
    2 - digit -> push immediately
    ^ - operator, #4 -> operand stack is '^' which is *equal*, so move to output stack
        and push next operand
    3 - digit -> push immediately
    .. end of input, move operand stack ('^' only) to end of output
    

    If you change the comparison to this it will work correct:

    while (pr(s[top]) > pr(ch))