Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #define SIZE 50
  2. #include <ctype.h>
  3. char s[SIZE];
  4. int top = -1;
  5.  
  6. push(char elem) {
  7. s[++top] = elem;
  8. }
  9.  
  10. char pop() {
  11. return (s[top--]);
  12. }
  13.  
  14. int pr(char elem) {
  15. switch (elem) {
  16. case '#':
  17. return 0;
  18. case '(':
  19. return 1;
  20. case '+':
  21. case '-':
  22. return 2;
  23. case '*':
  24. case '/':
  25. return 3;
  26. }
  27. }
  28.  
  29. main() {
  30. char infx[50], pofx[50], ch, elem;
  31. int i = 0, k = 0;
  32. printf("\n\nRead the Infix Expression ? ");
  33. scanf("%s", infx);
  34. push('#');
  35. while ((ch = infx[i++]) != '\0') {
  36. if (ch == '(')
  37. push(ch);
  38. else if (isalnum(ch))
  39. pofx[k++] = ch;
  40. else if (ch == ')') {
  41. while (s[top] != '(')
  42. pofx[k++] = pop();
  43. elem = pop();
  44. } else {
  45. while (pr(s[top]) >= pr(ch))
  46. pofx[k++] = pop();
  47. push(ch);
  48. }
  49. }
  50. while (s[top] != '#')
  51. pofx[k++] = pop();
  52. pofx[k] = '\0';
  53. printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement