Sunday, December 6, 2015

C++ - Konversi Infix Menjadi Postfix Menggunakan Stack

Contoh program C++ mengkonversikan Infix menjadi Postfix menggunakan Stack.

Source Code

#include <iostream>
#include <sstream>
#include <stack>
#include <limits>
#include <string>
using namespace std;

int priority(char a) {
    int temp;
    if (a == '^')
        temp = 1;
    else if (a == '*' || a == '/')
        temp = 2;
    else if (a == '+' || a == '-')
        temp = 3;
    return temp;
}

int main() {
    string infix;
    cout << "Masukan Infix : ";
    getline(cin, infix);

    stack<char> opr_stack;

    stringstream postfix;

    for (unsigned i = 0; i < infix.length(); i++) {
        if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^') {
            while (!opr_stack.empty() && priority(opr_stack.top()) <= priority(infix[i])) {
            postfix << opr_stack.top();
            opr_stack.pop();
            }
        opr_stack.push(infix[i]);
        } else if (infix[i] == '(') {
            opr_stack.push(infix[i]);
        } else if (infix[i] == ')') {
            while (opr_stack.top() != '(') {
            postfix << opr_stack.top();
            opr_stack.pop();
            }
            opr_stack.pop();
        } else {
            postfix << infix[i];
        }
    }

    while (!opr_stack.empty()) {
        postfix << opr_stack.top();
        opr_stack.pop();
    }

    cout << "Postfix : " << postfix.str() << endl;

    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    return 0;
}

(Baca juga : C++ - Konversi Suhu)

Output
C++ Infix to Postfix using Stack
C++ Infix to Postfix using Stack
C++ Infix to Postfix using Stack

Terima kasih
Enjoy your day !


1 komentar so far


EmoticonEmoticon