Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <queue>
- using namespace std;
- int evaluate(queue<char> expr);
- int main()
- {
- queue<char> expression;
- string input;
- cout << "Enter expression: ";
- cin >> input;
- for (int i = 0; i < input.size(); i++)
- {
- expression.push(input[i]);
- }
- cout << "Result = " << evaluate(expression) << endl;
- return 0;
- }
- int evaluate(queue<char> expr)
- {
- stack<int> nums;
- stack<char> funcs;
- while (!expr.empty())
- {
- char sym = expr.front();
- expr.pop();
- if (sym == 'f' || sym == 'g')
- {
- funcs.push(sym);
- }
- else if (sym >= '0' && sym <= '9')
- {
- nums.push(sym - '0');
- }
- else if (sym == ')')
- {
- int result = 0;
- int x = nums.top();
- nums.pop();
- if (funcs.top() == 'f')
- {
- result = (x*x + 3*x + 7) % 10;
- }
- else if (funcs.top() == 'g')
- {
- result = (3*x*x + 2*x + 4) % 10;
- }
- funcs.pop();
- nums.push(result);
- }
- }
- return nums.top();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement