Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- using namespace std;
- bool isOperation(char sym)
- {
- return sym == '+' || sym == '-' || sym == '*' || sym == '/' || sym == '%';
- }
- int main()
- {
- int result, cntDigit = 0, num = 0;
- vector<int> numbers;
- vector<char> operations;
- string expr;
- cout << "Enter expression: ";
- getline(cin, expr);
- for (int i = expr.length() - 1; i >= 0; i--)
- {
- if (isdigit(expr[i]))
- {
- num += (int)(expr[i] - '0') * pow(10, cntDigit);
- cntDigit++;
- }
- else if (isOperation(expr[i]))
- {
- operations.insert(operations.begin(), expr[i]);
- numbers.insert(numbers.begin(), num);
- cntDigit = 0;
- num = 0;
- }
- }
- numbers.insert(numbers.begin(), num);
- result = numbers.at(0);
- for (int i = 0; i < operations.size(); i++)
- {
- switch (operations.at(i))
- {
- case '+': result += numbers.at(i+1); break;
- case '-': result -= numbers.at(i+1); break;
- case '*': result *= numbers.at(i+1); break;
- case '/': result /= numbers.at(i+1); break;
- case '%': result %= numbers.at(i+1); break;
- }
- }
- cout << expr << result << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment