Guest User

Untitled

a guest
Feb 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. // Stack type
  6. struct Stack
  7. {
  8. int top;
  9. unsigned capacity;
  10. int* array;
  11. };
  12.  
  13. // Stack Operations
  14. struct Stack* createStack( unsigned capacity )
  15. {
  16. struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
  17.  
  18. if (!stack)
  19. return NULL;
  20.  
  21. stack->top = -1;
  22. stack->capacity = capacity;
  23.  
  24. stack->array = (int*) malloc(stack->capacity * sizeof(int));
  25.  
  26. if (!stack->array)
  27. return NULL;
  28. return stack;
  29. }
  30. int isEmpty(struct Stack* stack)
  31. {
  32. return stack->top == -1 ;
  33. }
  34. char peek(struct Stack* stack)
  35. {
  36. return stack->array[stack->top];
  37. }
  38. char pop(struct Stack* stack)
  39. {
  40. if (!isEmpty(stack))
  41. return stack->array[stack->top--] ;
  42. return '$';
  43. }
  44. void push(struct Stack* stack, char op)
  45. {
  46. stack->array[++stack->top] = op;
  47. }
  48.  
  49.  
  50. // A utility function to check if the given character is operand
  51. int isOperand(char ch)
  52. {
  53. return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
  54. }
  55.  
  56. // A utility function to return precedence of a given operator
  57. // Higher returned value means higher precedence
  58. int Prec(char ch)
  59. {
  60. switch (ch)
  61. {
  62. case '+':
  63. case '-':
  64. return 1;
  65.  
  66. case '*':
  67. case '/':
  68. return 2;
  69.  
  70. case '^':
  71. return 3;
  72. }
  73. return -1;
  74. }
  75.  
  76.  
  77. // The main function that converts given infix expression
  78. // to postfix expression.
  79. int infixToPostfix(char* exp)
  80. {
  81. int i, k;
  82.  
  83. // Create a stack of capacity equal to expression size
  84. struct Stack* stack = createStack(strlen(exp));
  85. if(!stack) // See if stack was created successfully
  86. return -1 ;
  87.  
  88. for (i = 0, k = -1; exp[i]; ++i)
  89. {
  90. // If the scanned character is an operand, add it to output.
  91. if (isOperand(exp[i]))
  92. exp[++k] = exp[i];
  93.  
  94. // If the scanned character is an ‘(‘, push it to the stack.
  95. else if (exp[i] == '(')
  96. push(stack, exp[i]);
  97.  
  98. // If the scanned character is an ‘)’, pop and output from the stack
  99. // until an ‘(‘ is encountered.
  100. else if (exp[i] == ')')
  101. {
  102. while (!isEmpty(stack) && peek(stack) != '(')
  103. exp[++k] = pop(stack);
  104. if (!isEmpty(stack) && peek(stack) != '(')
  105. return -1; // invalid expression
  106. else
  107. pop(stack);
  108. }
  109. else // an operator is encountered
  110. {
  111. while (!isEmpty(stack) && Prec(exp[i]) <= Prec(peek(stack)))
  112. exp[++k] = pop(stack);
  113. push(stack, exp[i]);
  114. }
  115.  
  116. }
  117.  
  118. // pop all the operators from the stack
  119. while (!isEmpty(stack))
  120. exp[++k] = pop(stack );
  121.  
  122. exp[++k] = '\0';
  123. printf( "%s", exp );
  124. return 0;
  125. }
  126.  
  127. // Driver program to test above functions
  128. int main()
  129. {
  130. char exp[20000];
  131. scanf("%s",exp);
  132. infixToPostfix(exp);
  133. return 0;
  134. }
Add Comment
Please, Sign In to add comment