Advertisement
YauhenMardan

Lab7Zad3

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