Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<stack>
- #include <string>
- using namespace std;
- int main() {
- stack<char> operators;
- string input;
- string output = "";
- cout << "Please enter your infix expression without spaces:\n";
- cin >> input;
- for (int i = 0; i < input.length(); i++) {
- switch (input[i]) {
- case '(':
- operators.push(input[i]);
- break;
- case ')':
- while (operators.top() != '(')
- {
- output += operators.top();
- operators.pop();
- }
- operators.pop();
- break;
- case '^':
- while (!operators.empty() && operators.top() == '^')
- {
- output += operators.top();
- operators.pop();
- }
- operators.push(input[i]);
- break;
- case '*':
- case '/':
- while (!operators.empty() && (operators.top() == '^' || operators.top() == '/' || operators.top() == '*'))
- {
- output += operators.top();
- operators.pop();
- }
- operators.push(input[i]);
- break;
- case '+':
- case '-':
- while (!operators.empty() && (operators.top() == '^' || operators.top() == '/' || operators.top() == '*' || operators.top() == '+' || operators.top() == '-'))
- {
- output += operators.top();
- operators.pop();
- }
- operators.push(input[i]);
- break;
- default:
- output += input[i];
- }
- }
- while (!operators.empty()) {
- output += operators.top();
- operators.pop();
- }
- cout << "\nThe converted postfix expression is:\n" << output << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement