Advertisement
Guest User

Untitled

a guest
Apr 24th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6. stack<char>digit_stack;
  7. stack<char>operator_stack;
  8.  
  9. int convert(char karakter)
  10. {
  11.     if(karakter=='*' || karakter=='/') return 2;
  12.     else return 1;
  13. }
  14.  
  15. bool bitnost(char top_stack, char onaj_koji_nabacujemo)
  16. {
  17.     //TRUE za top_stack bitnije;;;
  18.     int vrijednost_topa=convert(top_stack);
  19.     int vrijednost_onoga=convert(onaj_koji_nabacujemo);
  20.  
  21.     if(vrijednost_topa>vrijednost_onoga) return true;
  22.     else return false;
  23. }
  24.  
  25. int main()
  26. {
  27.     string infix_string;
  28.     string postfix_string;
  29.     cin>>infix_string;
  30.  
  31.     for(int i=0; i<infix_string.length(); i++)
  32.     {
  33.         if(isdigit(infix_string[i]))
  34.         {
  35.             digit_stack.push(infix_string[i]);
  36.             postfix_string+=infix_string[i];
  37.         }
  38.         else
  39.         {
  40.             if(operator_stack.empty()) {operator_stack.push(infix_string[i]);}
  41.  
  42.             if(!operator_stack.empty())
  43.             {
  44.                 char top_stack=operator_stack.top();
  45.                 //TRUE za top_stack bitnije
  46.                 if(!bitnost(top_stack,infix_string[i])){
  47.                     operator_stack.push(infix_string[i]);}
  48.  
  49.                 while(bitnost(top_stack,infix_string[i]))
  50.                 {
  51.                     operator_stack.pop();
  52.                     postfix_string+=top_stack;
  53.                     top_stack=operator_stack.top();
  54.                 }
  55.             }
  56.         }
  57.     }
  58.  
  59.     while(operator_stack.empty())
  60.     {
  61.         postfix_string+=operator_stack.top();
  62.         operator_stack.pop();
  63.     }
  64.  
  65.     cout<<postfix_string<<endl;
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement