Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <deque>
- using namespace std;
- deque<double> operands;
- deque<char> operators;
- void filterInput(char input[])
- {
- char ops[5] = { '+','-','*','/','=' };
- for (char item : ops)
- {
- if (input[0] == item)
- {
- operators.push_back(input[0]);
- return;
- }
- }
- operands.push_back(atof(input));
- }
- double calculate()
- {
- double result;
- while (operators.size())
- {
- switch (operators.front())
- {
- case '+':
- result = operands[0] + operands[1];
- break;
- case '-':
- result = operands[0] - operands[1];
- break;
- case '*':
- result = operands[0] * operands[1];
- break;
- case '/':
- result = operands[0] / operands[1];
- break;
- default:
- operators.pop_front();
- break;
- }
- //pop the 1st and 2nd elements from the que
- operands.pop_front();
- operands.pop_front();
- //push in the result from the front
- operands.push_front(result);
- //pop the operator
- operators.pop_front();
- }
- return operands[0];
- }
- int main()
- {
- char buffer[50];
- memset(buffer, 0, sizeof(buffer));
- while (!cin.eof()) //while CTRL+Z is not pressed
- {
- cin >> buffer;
- filterInput(buffer);
- }
- operands.pop_back(); // ^Z would get pushed as 0 value, so we pop it out when we exit the loop
- cout << "Result: " << calculate() << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement