Advertisement
YauhenMardan

Untitled

Oct 31st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. #include <stack>
  9.  
  10. const int MAX=50;
  11. /////////////////////////////////
  12. void addSpace(char *ch,int &i)
  13. {
  14.     ch[i]='_';
  15.     i++;
  16. }
  17. /////////////////////////////////
  18. void addChar(char *ch,int &i, char tp)
  19. {
  20.     ch[i]=tp;
  21.     i++;
  22. }
  23. /////////////////////////////////
  24. bool isOperator(char op1) // calculate
  25. {
  26.     if (op1=='+' || op1=='-' || op1=='*' || op1=='/' || op1=='^')
  27.         return true;
  28.     return false;
  29. }
  30. /////////////////////////////////
  31. bool compPrior(char op1, char op2) // compare
  32. {
  33.     const int Op=6;
  34.     char opers[Op]={'+', '-', '*', '/', '(', '^'};
  35.     int priors[Op]={ 1 ,  1 ,  2 ,  2 ,  0 ,  3 };
  36.     int op1p=0, op2p=0, i;
  37.     for (i=0; i<Op; i++)
  38.     {
  39.         if(op1==opers[i])
  40.             op1p=i;
  41.         if(op2==opers[i])
  42.             op2p=i;
  43.     }
  44.     if(priors[op1p]<=priors[op2p])
  45.         return 1;
  46.     return 0;
  47. }
  48. /////////////////////////////////
  49. void convertPolish(char *ex, char *pl)
  50. {
  51.     //STACK//
  52.    
  53.     stack <char> stk;
  54.    
  55.     //CONVERTING//
  56.     unsigned long int ln=strlen(ex);
  57.     int k=0; //counter for pl
  58.     int i=0; //counter for ex
  59.     while (ln!=i)
  60.     {
  61.         if (isdigit(ex[i]))
  62.         {
  63.             addSpace(pl, k);
  64.             while(isdigit(ex[i]))
  65.             {
  66.                 addChar(pl, k, ex[i]);
  67.                 i++;
  68.             }
  69.         }
  70.         else if (ex[i]=='(') //   (
  71.         {
  72.             stk.push(ex[i]);
  73.             i++;
  74.         }
  75.         else if (ex[i]==')') //   )
  76.         {
  77.             char elem;
  78.             do
  79.             {
  80.                 elem=stk.top();
  81.                 stk.pop();
  82.                 if (elem!='(')
  83.                 {
  84.                     addChar(pl, k, elem);
  85.                 }
  86.             } while (elem!='(');
  87.             i++;
  88.         }
  89.         else if (isOperator(ex[i]))  // operation
  90.         {
  91.             char temp=ex[i];
  92.             char a;
  93.             if(!stk.empty())
  94.             {
  95.                 a=stk.top();
  96.                 while (compPrior(temp, a) && !stk.empty())
  97.                 {
  98.                     char elem=stk.top();
  99.                     if(!stk.empty())
  100.                         stk.pop();
  101.                     addSpace(pl, k);
  102.                     addChar(pl, k, elem);
  103.                     if(!stk.empty())
  104.                         a=stk.top();
  105.                 }
  106.             }
  107.             stk.push(temp);
  108.             i++;
  109.         }
  110.     }
  111.     while (!stk.empty())
  112.     {
  113.         char elem=stk.top();
  114.         stk.pop();
  115.         addSpace(pl, k);
  116.         addChar(pl, k, elem);
  117.     }
  118. }
  119. /////////////////////////////////
  120. int calculatePolish(char *pl)
  121. {
  122.     //STACK//
  123.    
  124.     stack<int> stk;
  125.    
  126.     //CALCULATING//
  127.     unsigned long int pllen=strlen(pl);
  128.     int i=0; // counter
  129.     while (i<=pllen) //
  130.     {
  131.         if (stk.size()==1 && i>=pllen-1)
  132.             return stk.top();
  133.         else if(pl[i]=='_')
  134.             i++;
  135.         else if (isdigit(pl[i]))
  136.         {
  137.             char tempnum [pllen];
  138.             int v=0; // counter
  139.             while(isdigit(pl[i]))
  140.             {
  141.                 tempnum[v]=pl[i];
  142.                 v++;
  143.                 i++;
  144.             }
  145.             stk.push(atoi(tempnum));
  146.             v=0;
  147.         }
  148.         else if (isOperator(pl[i]))
  149.         {
  150.             if(pl[i]=='+')
  151.             {
  152.                 int a= stk.top();
  153.                 stk.pop();
  154.                 a+=stk.top();
  155.                 stk.pop();
  156.                 i+=2;
  157.                 stk.push(a);
  158.             }
  159.             else if(pl[i]=='-')
  160.             {
  161.                 int a= stk.top();
  162.                 stk.pop();
  163.                 a=stk.top()-a;
  164.                 stk.pop();
  165.                 i+=2;
  166.                 stk.push(a);
  167.             }
  168.             else if(pl[i]=='*')
  169.             {
  170.                 int a= stk.top();
  171.                 stk.pop();
  172.                 a*=stk.top();
  173.                 stk.pop();
  174.                 i+=2;
  175.                 stk.push(a);
  176.             }
  177.             else if(pl[i]=='/')
  178.             {
  179.                 int a= stk.top();
  180.                 stk.pop();
  181.                 a=stk.top()/a;
  182.                 stk.pop();
  183.                 i+=2;
  184.                 stk.push(a);
  185.             }
  186.             else if(pl[i]=='^')
  187.             {
  188.                 int a= stk.top();
  189.                 stk.pop();
  190.                 a=pow(stk.top(), a);
  191.                 stk.pop();
  192.                 i+=2;
  193.                 stk.push(a);
  194.             }
  195.         }
  196.        
  197.        
  198.     }
  199.     return stk.top();
  200. }
  201. /////////////////////////////////
  202. bool checkBrckts(char *ex)
  203. {
  204.     //STACK//
  205.    
  206.     stack<char> stk;
  207.    
  208.     //CHECKING//
  209.     unsigned long int ln=strlen(ex);
  210.     for (int i=0; i<ln ; i++)
  211.     {
  212.         if (ex[i]=='(')
  213.             stk.push(ex[i]);
  214.         if (ex[i]==')')
  215.         {
  216.             char tempchar = stk.top();
  217.             stk.pop();
  218.             if (tempchar!='(')
  219.                 return false;
  220.         }
  221.     }
  222.     if (stk.empty())
  223.         return true;
  224.     else
  225.         return false;
  226. }
  227. /////////////////////////////////
  228. bool checkChar(char *ex)
  229. {
  230.     //CHECKING//
  231.     unsigned long int ln=strlen(ex);
  232.     for (int i=0; i<ln ; i++)
  233.     {
  234.         if (isOperator(ex[i]) || isdigit(ex[i]) || ex[i]=='(' || ex[i]==')')
  235.             i++;
  236.         else
  237.             return false;
  238.     }
  239.     return true;
  240. }
  241. /////////////////////////////////
  242. int main()
  243. {
  244.     //EXPRESSION//
  245.    
  246.     char expr [MAX];
  247.     cout<<"Input your expression: ";
  248.     cin.getline(expr, MAX);
  249.    
  250.     //EXPRESSION CONTROL//
  251.    
  252.     if(!checkChar(expr))
  253.     {
  254.         cout<<"Check the correctness of your expression"<<endl;;
  255.         return 0;
  256.     }
  257.    
  258.     if(!checkBrckts(expr))
  259.     {
  260.         cout<<"Check brackets int your expression"<<endl;;
  261.         return 0;
  262.     }
  263.    
  264.     //INITIALIZATION//
  265.    
  266.     char *polish = new char [MAX];
  267.    
  268.     //CONVERTING//
  269.    
  270.     convertPolish(expr, polish);
  271.     cout<<"Your expression in polish notation: "<<polish<<endl;
  272.    
  273.     //CALCULATING//
  274.    
  275.     cout<<"Calculating your expression..."<<endl;
  276.     cout<<"Your result is: "<<calculatePolish(polish)<<endl;
  277.    
  278.     //DElETING//
  279.     delete [] polish;
  280.    
  281.     return 0;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement