sandeshMC

Untitled

Aug 2nd, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.41 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define MAX 20
  4. struct queue {
  5. char stack[MAX];
  6. int top;
  7. }q;
  8.  
  9. char pop();
  10. void push(char item);
  11. int prcd(char symbol)
  12. {
  13. switch(symbol)
  14. {
  15. case '+':
  16. case '-':return 2;
  17. break;
  18. case '*':
  19. case '/':return 4;
  20. break;
  21. case '^':
  22. return 6;
  23. break;
  24. case '(':
  25. case ')':
  26. case '#':return 1;
  27. break;
  28. }
  29. }
  30. int isoperator(char symbol)
  31. {
  32. switch(symbol)
  33. {
  34. case '+':
  35. case '-':
  36. case '*':
  37. case '/':
  38. case '^':
  39. case '(':
  40. case ')':return 1;
  41. break;
  42. default:return 0;
  43. }
  44. }
  45. void convertip(char infix[],char postfix[])
  46. {
  47. int i,symbol,j=0;
  48. q.stack[q.top=q.top+1]='#';
  49. for(i=0;i<strlen(infix);i++)
  50. {
  51. symbol=infix[i];
  52. if(isoperator(symbol)==0)
  53. {
  54. postfix[j]=symbol;
  55. j++;
  56. }
  57. else{
  58. if(symbol=='(')push(symbol);
  59. else if(symbol==')')
  60. {
  61. while(q.stack[q.top]!='(')
  62. {
  63. postfix[j]=pop();
  64. j++;
  65. }
  66. pop();
  67. }
  68. else{
  69. if(prcd(symbol)>prcd(q.stack[q.top]))
  70. push(symbol);
  71. else{
  72. while(prcd(symbol)<=prcd(q.stack[q.top]))
  73. {
  74. postfix[j]=pop();
  75. j++;
  76. }
  77. push(symbol);
  78. }
  79. }
  80. }
  81. }
  82. while(q.stack[q.top]!='#')
  83. {
  84. postfix[j]=pop();
  85. j++;
  86. }
  87. postfix[j]='\0';
  88. }
  89. void main()
  90. {
  91. q.top=-1;
  92. char infix[20],postfix[20];
  93. printf("Enter the valid infix string:\n");
  94. gets(infix);
  95. convertip(infix,postfix);
  96. printf("The corresponding postfix string is:\n");
  97. puts(postfix);
  98. }
  99. void push(char item)
  100. {
  101. q.top++;
  102. q.stack[q.top]=item;
  103. }
  104. char pop()
  105. {
  106. char a;
  107. a=q.stack[q.top];
  108. q.top--;
  109. return a;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment