Advertisement
vencinachev

Hw2-V2

Mar 14th, 2021
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int result, cntDigit = 0, num = 0;
  10.     vector<int> numbers;
  11.     vector<char> operations;
  12.     string expr;
  13.  
  14.     cout << "Enter expression: ";
  15.     getline(cin, expr);
  16.  
  17.     for (int i = expr.length() - 2; i >= 0; i--)
  18.     {
  19.         if (isdigit(expr[i]))
  20.         {
  21.             num += (int)(expr[i] - '0') * pow(10, cntDigit);
  22.             cntDigit++;
  23.         }
  24.         else
  25.         {
  26.             operations.insert(operations.begin(), expr[i]);
  27.             numbers.insert(numbers.begin(), num);
  28.             cntDigit = 0;
  29.             num = 0;
  30.         }
  31.     }
  32.     numbers.insert(numbers.begin(), num);
  33.  
  34.     result = numbers.at(0);
  35.     for (int i = 0; i < operations.size(); i++)
  36.     {
  37.         switch (operations.at(i))
  38.         {
  39.             case '+': result += numbers.at(i+1); break;
  40.             case '-': result -= numbers.at(i+1); break;
  41.             case '*': result *= numbers.at(i+1); break;
  42.             case '/': result /= numbers.at(i+1); break;
  43.             case '%': result %= numbers.at(i+1); break;
  44.         }
  45.     }
  46.     cout << expr << result << endl;
  47.     return 0;
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement