Vla_DOS

lr Стек

Jun 15th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. struct Stack
  5. {
  6.     string* m_pArr = nullptr;
  7.     unsigned int m_iSize = 0;
  8. };
  9.  
  10. Stack copy_stack(const Stack& otherStack)
  11. {
  12.     Stack newStack;
  13.     newStack.m_pArr = new std::string[otherStack.m_iSize];
  14.     for (size_t i = 0; i < otherStack.m_iSize; i++)
  15.     {
  16.         newStack.m_pArr[i] = otherStack.m_pArr[i];
  17.     }
  18.     newStack.m_iSize = otherStack.m_iSize;
  19.     return newStack;
  20. }
  21.  
  22.  
  23. void push(Stack& otherStack, const std::string& otherValue)
  24. {
  25.     std::string* newArr = new std::string[otherStack.m_iSize + 1];
  26.     for (size_t i = 0; i < otherStack.m_iSize; i++)
  27.     {
  28.         newArr[i] = otherStack.m_pArr[i];
  29.     }
  30.     newArr[otherStack.m_iSize] = otherValue;
  31.     delete[] otherStack.m_pArr;
  32.     otherStack.m_pArr = newArr;
  33.     otherStack.m_iSize++;
  34. }
  35.  
  36. void pop(Stack& otherStack)
  37. {
  38.     std::string* newArr = new std::string[otherStack.m_iSize - 1];
  39.     for (size_t i = 0; i < otherStack.m_iSize - 1; i++)
  40.     {
  41.         newArr[i] = otherStack.m_pArr[i];
  42.     }
  43.     delete[] otherStack.m_pArr;
  44.     otherStack.m_pArr = newArr;
  45.     otherStack.m_iSize--;
  46. }
  47.  
  48. void clear(Stack& otherStack)
  49. {
  50.     delete[] otherStack.m_pArr;
  51.     otherStack.m_pArr = nullptr;
  52.     otherStack.m_iSize = 0;
  53. }
  54.  
  55. std::string& top(const Stack& otherStack)
  56. {
  57.     return *(otherStack.m_pArr + otherStack.m_iSize - 1);
  58. }
  59.  
  60. bool empty(const Stack& otherStack)
  61. {
  62.     return !otherStack.m_iSize;
  63. }
  64.  
  65. void output_stack(const Stack& otherStack)
  66. {
  67.     for (size_t i = 0; i < otherStack.m_iSize; i++)
  68.     {
  69.         cout << otherStack.m_pArr[i] << " ";
  70.     }
  71. }
  72.  
  73. void foo1()
  74. {
  75.     Stack stk1;
  76.     Stack stk2;
  77.  
  78.     int size1;
  79.     int size2;
  80.     cout << "Enter first stack size:  ";
  81.     cin >> size1;
  82.     for (int i = 0; i < size1; i++)
  83.     {
  84.         cout << "Elem " << i + 1 << ": ";
  85.         int tempval;
  86.         cin >> tempval;
  87.         push(stk1, std::to_string(tempval));
  88.     }
  89.     cout << "Enter second stack size: ";
  90.     cin >> size2;
  91.     for (int i = 0; i < size2; i++)
  92.     {
  93.         cout << "Elem " << i + 1 << ": ";
  94.         int tempval;
  95.         cin >> tempval;
  96.         push(stk2, to_string(tempval));
  97.     }
  98.  
  99.     size1 *= (size1 > 0);
  100.     size2 *= (size2 > 0);
  101.     int* resultArr = new int[size1 + size2];
  102.     for (int i = size1 - 1; i >= 0; i--)
  103.     {
  104.         resultArr[i] = stoi(top(stk1));
  105.         pop(stk1);
  106.     }
  107.     for (int i = size1; i < size1 + size2; i++)
  108.     {
  109.         resultArr[i] = stoi(top(stk2));
  110.         pop(stk2);
  111.     }
  112.  
  113.     cout << "Result array: ";
  114.     for (size_t i = 0; i < size1 + size2; i++)
  115.     {
  116.         std::cout << resultArr[i] << " ";
  117.     }
  118.  
  119.     if (resultArr) delete[] resultArr;
  120. }
  121.  
  122.  
  123. int get_priority(char c) {
  124.     int prior = 0;
  125.     switch (c)
  126.     {
  127.     case '(':
  128.         prior = 5;
  129.     case ')':
  130.         prior = 6;
  131.     case '+':
  132.     case '-':
  133.         prior = 1;
  134.         break;
  135.     case '*':
  136.     case '/':
  137.         prior = 2;
  138.         break;
  139.     default:
  140.         break;
  141.     }
  142.     return prior;
  143. }
  144.  
  145. Stack get_polsk(const Stack& q)
  146. {
  147.     Stack stk = copy_stack(q);
  148.     Stack reverseQ;
  149.     while (!empty(stk))
  150.     {
  151.         push(reverseQ, top(stk));
  152.         pop(stk);
  153.     }
  154.     Stack result;
  155.     while (!empty(reverseQ))
  156.     {
  157.         std::string value = top(reverseQ);
  158.         pop(reverseQ);
  159.         if (!isdigit(value.front()))
  160.         {
  161.             if (empty(stk))
  162.             {
  163.                 if (value.front() != '(')
  164.                 {
  165.                     push(stk, value);
  166.                 }
  167.             }
  168.             else if ((get_priority(value.front()) >= get_priority(top(stk).front())))
  169.             {
  170.                 if (value.front() == ')') {
  171.                     while (top(stk).front() != '(' && !empty(stk))
  172.                     {
  173.                         push(result, top(stk));
  174.                         pop(stk);
  175.                     }
  176.                     if (!empty(stk)) pop(stk);
  177.                 }
  178.                 else {
  179.                     if (value.front() != '(')
  180.                     {
  181.                         push(stk, value);
  182.                     }
  183.                 }
  184.             }
  185.             else
  186.             {
  187.                 while (!empty(stk))
  188.                 {
  189.                     push(result, top(stk));
  190.                     pop(stk);
  191.                 }
  192.                 push(stk, value);
  193.             }
  194.         }
  195.         else
  196.         {
  197.             push(result, value);
  198.         }
  199.     }
  200.     while (!empty(stk))
  201.     {
  202.         push(result, top(stk));
  203.         pop(stk);
  204.     }
  205.     return result;
  206. }
  207.  
  208. Stack get_expr()
  209. {
  210.     //(A*B-C)/D-F/E
  211.     float A = 12;
  212.     float B = 3;
  213.     float C = 14;
  214.     float D = 2;
  215.     float E = 9;
  216.     float F = 18;
  217.     cout << "Expression: (A*B-C)/D-F/E" << std::endl;
  218.     cout << "Enter A: ";
  219.     cin >> A;
  220.     cout << "Enter B: ";
  221.     cin >> B;
  222.     cout << "Enter C: ";
  223.     cin >> C;
  224.     cout << "Enter D: ";
  225.     cin >> D;
  226.     cout << "Enter E: ";
  227.     cin >> E;
  228.     cout << "Enter F: ";
  229.     cin >> F;
  230.  
  231.     Stack exprStack;
  232.     push(exprStack, "(");
  233.     push(exprStack, std::to_string(A));
  234.     push(exprStack, "*");
  235.     push(exprStack, std::to_string(B));
  236.     push(exprStack, "-");
  237.     push(exprStack, std::to_string(C));
  238.     push(exprStack, ")");
  239.     push(exprStack, "/");
  240.     push(exprStack, std::to_string(D));
  241.     push(exprStack, "-");
  242.     push(exprStack, std::to_string(F));
  243.     push(exprStack, "/");
  244.     push(exprStack, std::to_string(E));
  245.     //std::string exprStr;
  246.     //exprStr += "(" + std::to_string(A) + "*" + std::to_string(B) + "-" + std::to_string(C) + ")";
  247.     //exprStr += "/" + std::to_string(D) + "-" + std::to_string(F) + "/" + std::to_string(E);
  248.     return exprStack;
  249. }
  250.  
  251. bool isOperator(const char ch)
  252. {
  253.     if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
  254.         return true;
  255.     return false;
  256. }
  257.  
  258. float operation(const float first, const float second, const char op)
  259. {
  260.     if (op == '+')
  261.         return first + second;
  262.     else if (op == '-')
  263.         return first - second;
  264.     else if (op == '*')
  265.         return first * second;
  266.     else if (op == '/')
  267.         return first / second;
  268.     else
  269.         return -1;
  270. }
  271.  
  272. float get_result_by_polsk(const Stack& otherStack)
  273. {
  274.     float result = 0;
  275.     Stack temp_stk = copy_stack(otherStack);
  276.     Stack temp_stk1;
  277.     while (!empty(temp_stk))
  278.     {
  279.         push(temp_stk1, top(temp_stk));
  280.         pop(temp_stk);
  281.     }
  282.     Stack temp_stk2;
  283.     float first;
  284.     float second;
  285.     while (!empty(temp_stk1))
  286.     {
  287.         while (isdigit(top(temp_stk1).front()) && !empty(temp_stk1))
  288.         {
  289.             push(temp_stk2, top(temp_stk1));
  290.             pop(temp_stk1);
  291.         }
  292.         char sign = 0;
  293.         if (!empty(temp_stk1))
  294.         {
  295.             sign = top(temp_stk1).front();
  296.             pop(temp_stk1);
  297.         }
  298.  
  299.         second = stof(top(temp_stk2));
  300.         pop(temp_stk2);
  301.         first = stof(top(temp_stk2));
  302.         pop(temp_stk2);
  303.  
  304.         result = operation(first, second, sign);
  305.         if (result == -1)
  306.         {
  307.             clear(temp_stk1);
  308.             clear(temp_stk2);
  309.             break;
  310.         }
  311.         if (!empty(temp_stk1))
  312.         {
  313.             push(temp_stk1, std::to_string(result));
  314.         }
  315.     }
  316.     return result;
  317. }
  318.  
  319. int main()
  320. {
  321.     foo1();
  322.     cout << endl;
  323.     cout << endl;
  324.     //"(6*7-3)/2-18/9"
  325.     Stack startStk = get_expr();
  326.     cout << "\nStart expression:     ";
  327.     output_stack(startStk);
  328.     cout << endl;
  329.     Stack pstk = get_polsk(startStk);
  330.     cout << "\nPost form expression: ";
  331.     output_stack(pstk);
  332.     cout << "\nResult: " << get_result_by_polsk(pstk) << endl;
  333.  
  334.  
  335.  
  336.     clear(startStk);
  337.     clear(pstk);
  338.     return 0;
  339. }
Advertisement
Add Comment
Please, Sign In to add comment