Guest User

Untitled

a guest
Jun 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. /*Program for Transforming infix expression to postfix expression with Algorithm.
  2. Successfully Tested.*/
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. int stack[20], TOP=-1;
  7. void push(char e)
  8. {
  9. stack[TOP++]=e;
  10. }
  11.  
  12. int pop(){
  13. if(TOP==-1)
  14. return -1;
  15. else
  16. return stack[TOP--];
  17. }
  18.  
  19. int highPrecedence(char operater)
  20. {
  21. if(operater=='^')
  22. return 3;
  23. else if(operater=='*'||operater=='/')
  24. return 2;
  25. else if(operater=='+'||operater=='-')
  26. return 1;
  27. else
  28. return 0;
  29. }
  30.  
  31. //Main fuunction execution starts here.
  32. int main()
  33. {
  34. char expression[20],x,*e;
  35. e=expression;
  36. printf("Enter Expression : ");
  37. scanf("%s",e); printf("%s",e);
  38. /*Step1 : Push left parenthesis '(' on stack[TOP] and append right parenthesis at the end of expression. */
  39. push('(');
  40. strcat(e,")");
  41. /*Step2 : Scan each element of an expression till null character not found. */
  42. while(*e!='\0'){
  43. /*Step3 : If an operand is encountered the print it.*/
  44. if(isalnum(*e))
  45. printf("%c",*e);
  46. if(*e=='(')
  47. push(*e);
  48. if(*e==')'){
  49. while((x=pop())!='(')
  50. printf("%c",x); }
  51. else{
  52. while(highPrecedence(stack[TOP])>=highPrecedence(*e))
  53. printf("%c",pop());
  54. push(*e);
  55. }
  56. e++;
  57. }
  58. }
Add Comment
Please, Sign In to add comment