Advertisement
Kaidul

727 Equation

Jun 8th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | None | 0 0
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cctype>
  4. #include <cmath>
  5. #include <complex>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <ctime>
  10. #include <deque>
  11. #include <fstream>
  12. #include <iostream>
  13. #include <list>
  14. #include <climits>
  15. #include <map>
  16. #include <memory>
  17. #include <queue>
  18. #include <set>
  19. #include <sstream>
  20. #include <stack>
  21. #include <string>
  22. #include <utility>
  23. #include <vector>
  24. #include <iomanip>
  25. using namespace std;
  26. /*** typedef ***/
  27. #define MEMSET_INF 127
  28. #define MEMSET_HALF_INF 63
  29. #define stream istringstream
  30. #define rep(i,n) for(__typeof(n) i=0; i<(n); i++)
  31. #define repl(i,n) for(__typeof(n) i=1; i<=(n); i++)
  32. #define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
  33. #define INF (1<<30)
  34. #define PI acos(-1.0)
  35. #define pb push_back
  36. #define ppb pop_back
  37. #define all(x) x.begin(),x.end()
  38. #define mem(x,y) memset(x,y,sizeof(x))
  39. #define memsp(x) mem(x,MEMSET_INF)
  40. #define memdp(x) mem(x,-1)
  41. #define memca(x) mem(x,0)
  42. #define eps 1e-9
  43. #define pii pair<int,int>
  44. #define pmp make_pair
  45. #define ft first
  46. #define sd second
  47. #define vi vector<int>
  48. #define vpii vector<pii>
  49. #define si set<int>
  50. #define msi map<string , int >
  51. #define mis map<int , string >
  52. typedef long long i64;
  53. typedef unsigned long long ui64;
  54. /** function **/
  55. #define SDi(x) sf("%d",&x)
  56. #define SDl(x) sf("%lld",&x)
  57. #define SDs(x) sf("%s",x)
  58. #define SD2(x,y) sf("%d%d",&x,&y)
  59. #define SD3(x,y,z) sf("%d%d%d",&x,&y,&z)
  60. #define pf printf
  61. #define print(x) pf("%d ", x)
  62. #define println(x) pf("%d\n", x)
  63. #define sf scanf
  64. #define READ(f) freopen(f, "r", stdin)
  65. #define WRITE(f) freopen(f, "w", stdout)
  66.  
  67. /** My algorithm:
  68.  * Define a stack
  69.  * Go through each character in the string
  70.  * If it is between 0 to 9, append it to output string.
  71.  * If it is left brace push to stack
  72.  * If it is operator *+-/ then
  73.          * If the stack is empty push it to the stack
  74.          * If the stack is not empty then start a loop:
  75.                              * If the top of the stack has higher precedence
  76.                                  * Then pop and append to output string
  77.                              * Else break
  78.                      * Push to the stack
  79.  
  80. * If it is right brace then
  81.             * While stack not empty and top not equal to left brace
  82.                * Pop from stack and append to output string
  83.                * Finally pop out the left brace.
  84.  
  85. * If there is any input in the stack pop and append to the output string
  86. **/
  87.  
  88. stack <char> Stack;
  89. vector <char> output;
  90. queue <char> input;
  91.  
  92. bool isOperator(char c) {
  93.  
  94.     if(c == '*' || c == '/' || c == '+' || c == '-' )
  95.         return true;
  96.  
  97.     return false;
  98. }
  99.  
  100. bool hasHigherPrecedence(char top, char c) {
  101.  
  102.     if((top == '*' || top == '/') && (c == '-' || c == '+'))
  103.         return true;
  104.  
  105.     if((top == '+' || top == '-') && (c == '+' || c == '-'))
  106.         return true;
  107.  
  108.     if((top == '*' || top == '/') && (c == '/' || c == '*'))
  109.         return true;
  110.  
  111.     return false;
  112. }
  113.  
  114. void infixToPostfix() {
  115.  
  116.     Stack = stack <char> ();
  117.     output.clear();
  118.  
  119.     while(!input.empty()) {
  120.  
  121.         char front = input.front();
  122.         input.pop();
  123.  
  124.         if(isdigit(front))
  125.             output.pb(front);
  126.  
  127.         else if(front == '(')
  128.            Stack.push(front);
  129.  
  130.         else if(isOperator(front)) {
  131.             while(!Stack.empty()) {
  132.                 char top = Stack.top();
  133.                 if(top != '(' && hasHigherPrecedence(top, front))
  134.                     output.pb(top), Stack.pop();
  135.                 else break;
  136.             }
  137.             Stack.push(front);
  138.         }
  139.  
  140.         else if(front == ')') {
  141.             while(!Stack.empty()) {
  142.                 char top = Stack.top();
  143.                 Stack.pop();
  144.                 if(top == '(')
  145.                     break;
  146.                 output.pb(top);
  147.             }
  148.         } else {
  149.             //nothing
  150.         }
  151.     }
  152.  
  153.     while(!Stack.empty()) {
  154.         char top = Stack.top();
  155.         output.pb(top), Stack.pop();
  156.     }
  157. }
  158.  
  159. int main() {
  160.     #ifndef ONLINE_JUDGE
  161.         READ("input.txt");
  162.     #endif
  163.     int tcase;
  164.     SDi(tcase);
  165.     bool blank = true;
  166.     char ch[2];
  167.     getchar(), getchar();
  168.     while(tcase--) {
  169.         while(gets(ch) && strlen(ch)) {
  170.             input.push(ch[0]);
  171.         }
  172.  
  173.         infixToPostfix();
  174.         rep(i, output.size()) pf("%c", output[i]);
  175.         pf("\n");
  176.         if(!tcase) blank = false;
  177.         if(blank) pf("\n");
  178.     }
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement