Advertisement
twist_akid

uva 727(wa)

Sep 30th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<stack>
  5. #include<vector>
  6. using namespace std;
  7.  
  8. int value(char ch)
  9. {
  10.     if(ch=='+')
  11.         return 1;
  12.     else if(ch=='-')
  13.         return 1;
  14.     else if(ch=='*')
  15.         return 2;
  16.     else if(ch=='/')
  17.         return 3;
  18.     else if(ch=='^')
  19.         return 4;
  20.     return 0;
  21. }
  22.  
  23. stack<char>x,y;
  24. char a[10010];
  25. vector<char>v;
  26.  
  27. int main()
  28. {
  29.     int n;
  30.     scanf("%d",&n);
  31.     getchar();
  32.     getchar();
  33.     while(n--)
  34.     {
  35.      while(gets(a))
  36.     {
  37.     int l=strlen(a);
  38.     if(l==0)
  39.         break;
  40.     int i;
  41.     for(i=0;i<l;i++)
  42.     {
  43.         if((a[i]>=48&&a[i]<=57)||(a[i]>=65&&a[i]<=90))
  44.         {
  45.             v.push_back(a[i]);
  46.         }
  47.         else if(a[i]=='(')
  48.             x.push(a[i]);
  49.         else if(a[i]=='+'|| a[i]=='-'|| a[i]=='*'||a[i]=='/'||a[i]=='^')
  50.         {
  51.             if(x.empty())
  52.             x.push(a[i]);
  53.             else
  54.             {
  55.                 char r=x.top();
  56.                 if(value(r)>=value(a[i]))
  57.                 {
  58.                     while(!x.empty())
  59.                     {
  60.                     v.push_back(r);
  61.                     x.pop();
  62.                     if(x.empty())
  63.                         break;
  64.                     else
  65.                         r=x.top();
  66.                     if(value(r)<value(a[i]))
  67.                         break;
  68.                     }
  69.                     x.push(a[i]);
  70.                 }
  71.                 else
  72.                     x.push(a[i]);
  73.             }
  74.         }
  75.         else if(a[i]==')')
  76.         {
  77.             while(!x.empty())
  78.             {
  79.                 char ch=x.top();
  80.                 if(ch=='(')
  81.                 {
  82.                     x.pop();
  83.                     break;
  84.                 }
  85.                 v.push_back(ch);
  86.                 x.pop();
  87.             }
  88.         }
  89.     }
  90. //  printf("\n");
  91.     }
  92.     while(!x.empty())
  93.     {
  94.         char ch1=x.top();
  95.         v.push_back(ch1);
  96.         x.pop();
  97.     }
  98.         for(int i=0;i<v.size();i++)
  99.         printf("%c",v[i]);
  100.         printf("\n");
  101.         v.clear();
  102.     }
  103.     printf("\n");
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement