Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int priority(char op)
  5. {
  6. switch (op)
  7. {
  8. case '+':
  9. case '-':
  10. return 1;
  11. case '*':
  12. case '/':
  13. return 2;
  14. default:
  15. return 0;
  16. }
  17. };
  18.  
  19. void inToPost(char* input, char* post)
  20. {
  21. char stack[100] = { NULL };
  22. int i, j, top;
  23. for (i = 0, j = 0, top = 0; input[i] != '\0'; i++)
  24. {
  25. switch (input[i])
  26. {
  27. case '(':
  28. stack[++top] = input[i];
  29. break;
  30. case '+':
  31. case '-':
  32. case '*':
  33. case '/':
  34. while (priority(stack[top]) >= priority(input[i]))
  35. post[j++] = stack[top--];
  36. stack[++top] = input[i];
  37. break;
  38. case')':
  39. while (stack[top] != '(')
  40. post[j++] = stack[top--];
  41. top--;
  42. break;
  43. default:
  44. post[j++] = input[i];
  45. }
  46. }
  47. while (top > 0)
  48. post[j++] = stack[top--];
  49. };
  50.  
  51. int main()
  52. {
  53. char post[100] = { NULL };
  54. char input[100] = { NULL };
  55.  
  56. while (1)
  57. {
  58. scanf("%s", input);
  59.  
  60. inToPost(input, post);
  61.  
  62. for (int i = 0; post[i] != '\0'; i++)
  63. cout << post[i];
  64. cout << endl;
  65. }
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement