YauhenMardan

Lab7Zad2

Nov 5th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7. const int MAX=50;
  8. /////////////////////////////////
  9. /////////////////////////////////
  10. class StackInt
  11. {
  12. public:
  13.     StackInt();
  14.     //~StackChar();
  15.     void push(char a);
  16.     void pop();
  17.     int top();
  18.     bool empty();
  19.     int size(); //
  20. private:
  21.     unsigned int num;
  22.     struct Elem
  23.     {
  24.         int info;
  25.         Elem *ptrprev;
  26.     };
  27.     Elem *ptrtop;
  28. };
  29. StackInt::StackInt():ptrtop(NULL), num(0) //CONSTRUCTOR
  30. {
  31. }
  32. void StackInt::push(char a) //PUSH
  33. {
  34.     Elem *next = new Elem;
  35.     next->info=a;
  36.     next->ptrprev=ptrtop;
  37.     ptrtop=next;
  38.     num++;
  39. }
  40. void StackInt::pop() //POP
  41. {
  42.     ptrtop=ptrtop->ptrprev;
  43.     delete ptrtop;
  44.     num--;
  45. }
  46. int StackInt::top()
  47. {
  48.     return ptrtop->info;
  49. }
  50. int StackInt::size()
  51. {
  52.     return num;
  53. }
  54. bool StackInt::empty()
  55. {
  56.     return ptrtop ? false:
  57.     true;
  58. }
  59. /////////////////////////////////
  60. /////////////////////////////////
  61. class StackChar
  62. {
  63. public:
  64.     StackChar();
  65.     //~StackChar();
  66.     void push(char a);
  67.     void pop();
  68.     char top();
  69.     bool empty();
  70.     int size();
  71. private:
  72.     unsigned int num;
  73.     struct Elem
  74.     {
  75.         char info;
  76.         Elem *ptrprev;
  77.     };
  78.     Elem *ptrtop;
  79. };
  80. StackChar::StackChar():ptrtop(NULL), num(0) //CONSTRUCTOR
  81. {
  82. }
  83. void StackChar::push(char a) //PUSH
  84. {
  85.     Elem *next = new Elem;
  86.     next->info=a;
  87.     next->ptrprev=ptrtop;
  88.     ptrtop=next;
  89.     num++;
  90. }
  91. void StackChar::pop() //POP
  92. {
  93.     ptrtop=ptrtop->ptrprev;
  94.     delete ptrtop;
  95.     num--;
  96. }
  97. char StackChar::top()
  98. {
  99.     return ptrtop->info;
  100. }
  101. int StackChar::size()
  102. {
  103.     return num;
  104. }
  105. bool StackChar::empty()
  106. {
  107.     return ptrtop ? false:
  108.     true;
  109. }
  110. /////////////////////////////////
  111. /////////////////////////////////
  112. void addSpace(char *ch,int &i)
  113. {
  114.     ch[i]='_';
  115.     i++;
  116. }
  117. /////////////////////////////////
  118. void addChar(char *ch,int &i, char tp)
  119. {
  120.     ch[i]=tp;
  121.     i++;
  122. }
  123. /////////////////////////////////
  124. bool isOperator(char op1) // calculate
  125. {
  126.     if (op1=='+' || op1=='-' || op1=='*' || op1=='/' || op1=='^')
  127.         return true;
  128.     return false;
  129. }
  130. /////////////////////////////////
  131. int numErrorChar(char *pl)
  132. {
  133.     unsigned long int pllen=strlen(pl);
  134.     for (int i=0; i<pllen ; i++)
  135.     {
  136.         if (!isOperator(pl[i]) && !isdigit(pl[i]) && !(pl[i]=='_'))
  137.             return i;
  138.     }
  139.     return -1;
  140. }
  141. /////////////////////////////////
  142. bool compPrior(char op1, char op2) // compare
  143. {
  144.     const int Op=6;
  145.     char opers[Op]={'+', '-', '*', '/', '(', '^'};
  146.     int priors[Op]={ 1 ,  1 ,  2 ,  2 ,  0 ,  3 };
  147.     int op1p=0, op2p=0, i;
  148.     for (i=0; i<Op; i++)
  149.     {
  150.         if(op1==opers[i])
  151.             op1p=i;
  152.         if(op2==opers[i])
  153.             op2p=i;
  154.     }
  155.     if(priors[op1p]<=priors[op2p])
  156.         return 1;
  157.     return 0;
  158. }
  159. /////////////////////////////////
  160. void print(char *pl)
  161. {
  162.     int n=numErrorChar(pl);
  163.     for(int i=0 ; i<n ; i++)
  164.         cout<<pl[i];
  165. }
  166. /////////////////////////////////
  167. void convertPolish(char *ex, char *pl)
  168. {
  169.     //STACK//
  170.    
  171.     StackInt  stk;
  172.    
  173.     //CONVERTING//
  174.     unsigned long int ln=strlen(ex);
  175.     int k=0; //counter for pl
  176.     int i=0; //counter for ex
  177.     while (ln!=i)
  178.     {
  179.         if (isdigit(ex[i]))
  180.         {
  181.             addSpace(pl, k);
  182.             while(isdigit(ex[i]))
  183.             {
  184.                 addChar(pl, k, ex[i]);
  185.                 i++;
  186.             }
  187.         }
  188.         else if (ex[i]=='(') //   (
  189.         {
  190.             stk.push(ex[i]);
  191.             i++;
  192.         }
  193.         else if (ex[i]==')') //   )
  194.         {
  195.             char elem;
  196.             do
  197.             {
  198.                 elem=stk.top();
  199.                 stk.pop();
  200.                 if (elem!='(')
  201.                 {
  202.                     addSpace(pl, k); //
  203.                     addChar(pl, k, elem);
  204.                 }
  205.             } while (elem!='(');
  206.             i++;
  207.         }
  208.         else if (isOperator(ex[i]))  // operation
  209.         {
  210.             char temp=ex[i];
  211.             char a;
  212.             if(!stk.empty())
  213.             {
  214.                 a=stk.top();
  215.                 while (compPrior(temp, a) && !stk.empty())
  216.                 {
  217.                     char elem=stk.top();
  218.                     if(!stk.empty())
  219.                         stk.pop();
  220.                     addSpace(pl, k);
  221.                     addChar(pl, k, elem);
  222.                     if(!stk.empty())
  223.                         a=stk.top();
  224.                 }
  225.             }
  226.             stk.push(temp);
  227.             i++;
  228.         }
  229.     }
  230.     while (!stk.empty())
  231.     {
  232.         char elem=stk.top();
  233.         stk.pop();
  234.         addSpace(pl, k);
  235.         addChar(pl, k, elem);
  236.     }
  237. }
  238. /////////////////////////////////
  239.  
  240. /////////////////////////////////
  241.  int calculatePolish(char *pl)
  242.  {
  243.  //STACK//
  244.  
  245.  StackInt stk;
  246.  
  247.  //CALCULATING//
  248.  unsigned long int pllen=strlen(pl);
  249.  int i=0; // counter
  250.  while (i<=pllen) //
  251.  {
  252.  if (stk.size()==1 && i>=pllen-1)
  253.  return stk.top();
  254.  else if(pl[i]=='_')
  255.  {
  256.  i++;
  257.  cout<<"";
  258.  }
  259.  else if (isdigit(pl[i]))
  260.  {
  261.  char tempnum [pllen];
  262.  int v=0; // counter
  263.  while(isdigit(pl[i]))
  264.  {
  265.  tempnum[v]=pl[i];
  266.  v++;
  267.  i++;
  268.  }
  269.  stk.push(atoi(tempnum));
  270.  v=0;
  271.  }
  272.  else if (isOperator(pl[i]) && !stk.empty())
  273.  {
  274.  int a = stk.top();
  275.  stk.pop();
  276.  if(pl[i]=='+')
  277.  {
  278.  a+=stk.top();
  279.  stk.pop();
  280.  i+=2;
  281.  stk.push(a);
  282.  }
  283.  else if(pl[i]=='-')
  284.  {
  285.  a=stk.top()-a;
  286.  stk.pop();
  287.  i+=2;
  288.  stk.push(a);
  289.  }
  290.  else if(pl[i]=='*')
  291.  {
  292.  a*=stk.top();
  293.  stk.pop();
  294.  i+=2;
  295.  stk.push(a);
  296.  }
  297.  else if(pl[i]=='/')
  298.  {
  299.  a=stk.top()/a;
  300.  stk.pop();
  301.  i+=2;
  302.  stk.push(a);
  303.  }
  304.  else if(pl[i]=='^')
  305.  {
  306.  a=pow(stk.top(), a);
  307.  stk.pop();
  308.  i+=2;
  309.  stk.push(a);
  310.  }
  311.  }
  312.  else
  313.  i++;
  314.  }
  315.  return stk.top();
  316.  }
  317.  
  318.  /////////////////////////////////
  319.  bool checkBrckts(char *ex)
  320.  {
  321.  //STACK//
  322.  
  323.  StackInt stk;
  324.  
  325.  //CHECKING//
  326.  unsigned long int ln=strlen(ex);
  327.  for (int i=0; i<ln ; i++)
  328.  {
  329.  if (ex[i]=='(')
  330.  stk.push(ex[i]);
  331.  if (ex[i]==')')
  332.  {
  333.  if (!stk.empty())
  334.  {
  335.  char tempchar = stk.top();
  336.  stk.pop();
  337.  if (tempchar!='(')
  338.  return false;
  339.  }
  340.  else
  341.  return false;
  342.  }
  343.  }
  344.  if (stk.empty())
  345.  return true;
  346.  else
  347.  return false;
  348.  }
  349.  /////////////////////////////////
  350.  bool checkChar(char *ex)
  351.  {
  352.  unsigned long int ln=strlen(ex);
  353.  for (int i=0; i<ln ; i++)
  354.  {
  355.  if (isOperator(ex[i]) || isdigit(ex[i]) || ex[i]=='(' || ex[i]==')')
  356.  i++;
  357.  else
  358.  return false;
  359.  }
  360.  return true;
  361.  }
  362. /////////////////////////////////
  363. int main()
  364. {
  365.     //EXPRESSION//
  366.    
  367.     char expr [MAX];
  368.     cout<<"Input your expression: ";
  369.     cin.getline(expr, MAX);
  370.    
  371.      //EXPRESSION CONTROL//
  372.      
  373.      if(!checkChar(expr))
  374.      {
  375.      cout<<"Check the correctness of your expression"<<endl;;
  376.      return 0;
  377.      }
  378.      
  379.      if(!checkBrckts(expr))
  380.      {
  381.      cout<<"Check brackets int your expression"<<endl;;
  382.      return 0;
  383.      }
  384.  
  385.     //INITIALIZATION//
  386.    
  387.     char polish [MAX];
  388.     //char *polish = new char [MAX];
  389.    
  390.     //CONVERTING//
  391.    
  392.     convertPolish(expr, polish);
  393.    
  394.     //OUTPUTTING POLISH NOTATION//
  395.    
  396.     if(!(numErrorChar(polish)==-1))
  397.     {
  398.         cout<<"Your expression in polish notation: ";
  399.         print(polish);
  400.         cout<<endl;
  401.     }
  402.     else
  403.         cout<<"Your expression in polish notation: "<<polish<<endl;
  404.    
  405.     //CALCULATING//
  406.    
  407.     cout<<"Calculating your expression..."<<endl;
  408.     cout<<"Your result is: "<<calculatePolish(polish)<<endl;
  409.    
  410.     //DElETING//
  411.     //delete [] polish;
  412.    
  413.     return 0;
  414. }
Add Comment
Please, Sign In to add comment