Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #include <iostream>
  2. #include "string"
  3. #include "conio.h"
  4. #include <cstdlib>
  5. #include <algorithm>
  6. #include <bits/stdc++.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. using namespace std;
  10. //template <class variable>
  11. struct Node {
  12. int data;
  13. Node *next;
  14. Node(){
  15. data=0;
  16. next=NULL;
  17. }
  18. Node(int x){
  19. data=x;
  20. next=NULL;
  21. }
  22. };
  23. //template <class variable>
  24. class Stack{
  25. Node *top=NULL;
  26. public:
  27. bool IsEmpty(){
  28. if(top==NULL)
  29. return true;
  30. else
  31. return false;
  32. }
  33. /*int isempty(){
  34. return top==0;
  35. }*/
  36. void Push(int x){
  37. Node *n=new Node (x);
  38. //n->data=x;
  39. if(IsEmpty())
  40. top=n;
  41. else{
  42. n->next=top;
  43. top=n;
  44. }
  45. }
  46. int Pop(){
  47. if(IsEmpty()==false){
  48. Node *t=top;
  49. int x=top->data;
  50. top=top->next;
  51. t->next=NULL;
  52. delete t;
  53. return x;
  54. }
  55. }
  56. void display(){
  57. if(IsEmpty()==false){
  58. Node *t=top;
  59. while(t->next!=NULL){
  60. cout<<t->data<<endl;
  61. t=t->next;
  62. }
  63. }
  64. }
  65. int Top(){
  66. if(!IsEmpty())
  67. return top->data;
  68. else
  69. exit(EXIT_FAILURE);
  70.  
  71. }
  72. void Clear(){
  73. top=NULL;
  74. }
  75. };
  76.  
  77. //Function to return precedence of operators
  78. int prec(char c)
  79. {
  80. if(c == '*' || c == '/')
  81. return 2;
  82. else if(c == '+' || c == '-')
  83. return 1;
  84. else
  85. return -1;
  86. }
  87.  
  88. // The main function to convert infix expression
  89. //to postfix expression
  90. void infixToPostfix(string s)
  91. {
  92. Stack<char> st;
  93. st.Push('N');
  94. int l = s.length();
  95. string ns;
  96. for(int i = 0; i < l; i++)
  97. {
  98. // If the scanned character is an operand, add it to output string.
  99. if((s[i] >= 'a' && s[i] <= 'z')||(s[i] >= 'A' && s[i] <= 'Z'))
  100. ns+=s[i];
  101.  
  102. // If the scanned character is an ‘(‘, push it to the stack.
  103. else if(s[i] == '(')
  104.  
  105. st.Push('(');
  106.  
  107. // If the scanned character is an ‘)’, pop and to output string from the stack
  108. // until an ‘(‘ is encountered.
  109. else if(s[i] == ')')
  110. {
  111. while(st.Top() != 'N' && st.Top() != '(')
  112. {
  113. char c = st.Top();
  114. st.Pop();
  115. ns += c;
  116. }
  117. if(st.Top() == '(')
  118. {
  119. char c = st.Top();
  120. st.Pop();
  121. }
  122. }
  123.  
  124. //If an operator is scanned
  125. else{
  126. while(st.Top() != 'N' && prec(s[i]) <= prec(st.Top()))
  127. {
  128. char c = st.Top();
  129. st.Pop();
  130. ns += c;
  131. }
  132. st.Push(s[i]);
  133. }
  134.  
  135. }
  136. //Pop all the remaining elements from the stack
  137. while(st.Top() != 'N')
  138. {
  139. char c = st.Top();
  140. st.Pop();
  141. ns += c;
  142. }
  143.  
  144. cout << ns << endl;
  145.  
  146. }
  147.  
  148. //Driver program to test above functions
  149. int main()
  150. {
  151. string exp = "a+b*(c^d-e)^(f+g*h)-i";
  152. infixToPostfix(exp);
  153. return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement