Advertisement
Toliak

__code__

Oct 10th, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int calc(int*, int*, string);
  9. int reform(stack <string>*, string);
  10.  
  11. double rebild(string s1, bool f1)
  12. {
  13.     int i = 0;
  14.     double result = 0;
  15.  
  16.     if (f1 == true)
  17.     {
  18.         while (s1[i] != '.')   // 2141.1468 leng=9,
  19.         {
  20.             i++;
  21.         }
  22.         i--; //3
  23.         int buf = i;
  24.         for (int ind = 0; ind <= i; ind++)
  25.         {
  26.             switch (s1[ind])
  27.             {
  28.             case '1':
  29.                 result = result + 1 * pow(10, i);
  30.                 break;
  31.             case '2':
  32.                 result = result + 2 * pow(10, i);
  33.                 break;
  34.             case '3':
  35.                 result = result + 3 * pow(10, i);
  36.                 break;
  37.             case '4':
  38.                 result = result + 4 * pow(10, i);
  39.                 break;
  40.             case '5':
  41.                 result = result + 5 * pow(10, i);
  42.                 break;
  43.             case '6':
  44.                 result = result + 6 * pow(10, i);
  45.                 break;
  46.             case '7':
  47.                 result = result + 7 * pow(10, i);
  48.                 break;
  49.             case '8':
  50.                 result = result + 8 * pow(10, i);
  51.                 break;
  52.             case '9':
  53.                 result = result + 9 * pow(10, i);
  54.                 break;
  55.             case '0':
  56.                 result = result + 0 * pow(10, i);
  57.                 break;
  58.             }
  59.             i--;
  60.         }
  61.         i = 0;
  62.         for (int ind = buf + 2; ind <= s1.length() - 1; ind++)
  63.         {
  64.             switch (s1[ind])
  65.             {
  66.             case '1':
  67.                 result = result + 1 * pow(10, -(i + 1));
  68.                 break;
  69.             case '2':
  70.                 result = result + 2 * pow(10, -(i + 1));
  71.                 break;
  72.             case '3':
  73.                 result = result + 3 * pow(10, -(i + 1));
  74.                 break;
  75.             case '4':
  76.                 result = result + 4 * pow(10, -(i + 1));
  77.                 break;
  78.             case '5':
  79.                 result = result + 5 * pow(10, -(i + 1));
  80.                 break;
  81.             case '6':
  82.                 result = result + 6 * pow(10, -(i + 1));
  83.                 break;
  84.             case '7':
  85.                 result = result + 7 * pow(10, -(i + 1));
  86.                 break;
  87.             case '8':
  88.                 result = result + 8 * pow(10, -(i + 1));
  89.                 break;
  90.             case '9':
  91.                 result = result + 9 * pow(10, -(i + 1));
  92.                 break;
  93.             case '0':
  94.                 result = result + 0 * pow(10, -(i + 1));
  95.                 break;
  96.             }
  97.             i++;
  98.         }
  99.     }
  100.     else
  101.     {
  102.         int ind = 0;
  103.         for (i = s1.length() - 1; i >= 0; i--)   // 16724
  104.         {
  105.             switch (s1[i])
  106.             {
  107.             case '1':
  108.                 result = result + 1 * pow(10, ind);
  109.                 break;
  110.             case '2':
  111.                 result = result + 2 * pow(10, ind);
  112.                 break;
  113.             case '3':
  114.                 result = result + 3 * pow(10, ind);
  115.                 break;
  116.             case '4':
  117.                 result = result + 4 * pow(10, ind);
  118.                 break;
  119.             case '5':
  120.                 result = result + 5 * pow(10, ind);
  121.                 break;
  122.             case '6':
  123.                 result = result + 6 * pow(10, ind);
  124.                 break;
  125.             case '7':
  126.                 result = result + 7 * pow(10, ind);
  127.                 break;
  128.             case '8':
  129.                 result = result + 8 * pow(10, ind);
  130.                 break;
  131.             case '9':
  132.                 result = result + 9 * pow(10, ind);
  133.                 break;
  134.             case '0':
  135.                 result = result + 0 * pow(10, ind);
  136.                 break;
  137.             }
  138.             ind++;
  139.         }
  140.     }
  141.     return result;
  142. }
  143.  
  144.  
  145. int main()
  146. {
  147.     std::string expr;
  148.     std::cout << "Enter the mathematical expression" << std::endl;
  149.     std::getline(std::cin, expr);
  150.     std::cout << expr << std::endl;
  151.     int AmountOp = 0, AmountBack = 0, AmountOut = 0;
  152.     calc(&AmountOp, &AmountBack, expr);
  153.  
  154.  
  155.     int d = expr.length() - AmountBack;
  156.  
  157.  
  158.  
  159.     //  Operations[AmountOp + 1] = '\0';
  160.  
  161.     //Out[d + 1] = 0;
  162.  
  163.  
  164.  
  165.     cout << endl;
  166.     stack <string> out;
  167.     reform(&out, expr);
  168.     while (!out.empty())
  169.     {
  170.         cout << out.top() << ' ';
  171.         out.pop();
  172.     }
  173.     int a;
  174.     cin >> a;
  175.     system("pause");
  176.     return 0;
  177. }
  178.  
  179.  
  180. int calc(int *n, int* back, string str)
  181. {
  182.     for (int i = 0; i < str.length(); i++)
  183.     {
  184.         if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
  185.             (*n)++;
  186.         if (str[i] == ' ')
  187.             (*back)++;
  188.     }
  189.     return 0;
  190. }
  191.  
  192.  
  193. int reform(stack<string>* out, string str)
  194. {
  195.     //int i = 0, j = -1;
  196.     //cout << endl;
  197.  
  198.     stack <char> op;
  199.     for (int k = 0; k < str.length();)
  200.     {
  201.         bool flNum = false;
  202.         string chislo = "";
  203.         while (str[k] >= '0' && str[k] <= 9/*str[k] == '1' || str[k] == '2' || str[k] == '3' || str[k] == '4' || str[k] == '5' || str[k] == '6' || str[k] == '7' || str[k] == '8' || str[k] == '9' || str[k] == '0' || str[k] == '.'*/)
  204.  
  205.         {
  206.             chislo += str[k];
  207.             flNum = true;
  208.             k++;
  209.         }
  210.         if (flNum)
  211.         {
  212.             out->emplace(chislo);
  213.             continue;
  214.         }
  215.  
  216.         if (str[k] == '*' || str[k] == '/')
  217.         {
  218.             while (op.top() == '*' || op.top() == '/') {
  219.                 out->emplace(to_string(op.top()));
  220.                 op.pop();
  221.             }
  222.             op.emplace(str[k]);
  223.             k++;
  224.             continue;
  225.         }
  226.  
  227.         if (str[k] == '+' || str[k] == '-')
  228.         {
  229.             while (op.top() == '*' || op.top() == '/' || op.top() == '-' || op.top() == '+')
  230.             {
  231.                 out->emplace(to_string(op.top()));
  232.                 op.pop();
  233.             }
  234.             op.emplace(str[k]);
  235.             k++;
  236.             continue;
  237.         }
  238.  
  239.  
  240.         if (str[k] == ' ')
  241.         {
  242.             k++;
  243.             continue;
  244.         }
  245.  
  246.         if (str[k] == '(')
  247.         {
  248.             op.emplace(str[k]);
  249.             k++;
  250.             continue;
  251.         }
  252.         if (str[k] == ')')
  253.         {
  254.             while (op.top() != '(')
  255.             {
  256.                 out->emplace(to_string(op.top()));
  257.                 op.pop();
  258.             }
  259.             op.pop();
  260.             k++;
  261.             continue;
  262.         }
  263.     }
  264.     while (op.empty() == false)
  265.     {
  266.         out->emplace(to_string(op.top()));
  267.         op.pop();
  268.     }
  269.  
  270.     return 0;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement