You are on page 1of 7

TUGAS STRUKTUR DATA

STACK

Disusun untuk Memenuhi Matakuliah Struktur Data


Dibimbing oleh Bapak Wahyu Sakti Gunawan Irianto

Oleh :
Muhammad Ghifariansyah
NIM 220533611230
S1 PTI ’22 OFF B

UNIVERSITAS NEGERI MALANG


FAKULTAS TEKNIK
JURUSAN TEKNIK ELEKTRO
PRODI S1 PENDIDIKAN TEKNIK INFORMATIKA
Nama program : infix to
prefix Bahasa pemrograman :
C++ Compiler : Dev C++
studio

#include<iostream>
#include<stack>
#include<locale> //for function isalnum()
#include<algorithm>
using namespace std;
int preced(char ch) {
if(ch == '+' || ch == '-') {
return 1; //Precedence of + or - is 1
}else if(ch == '*' || ch == '/') {
return 2; //Precedence of * or / is 2
}else if(ch == '^') {
return 3; //Precedence of ^ is 3
}else {
return 0;
}
}
string inToPost(string infix) {
stack<char> stk;
stk.push('#'); //add some extra character to avoid underflow
string postfix = ""; //initially the postfix string is empty
string::iterator it;
for(it = infix.begin(); it!=infix.end(); it++) {
if(isalnum(char(*it)))
postfix += *it; //add to postfix when character is letter or
number
else if(*it == '(')
stk.push('(');
else if(*it == '^')
stk.push('^');
else if(*it == ')') {
while(stk.top() != '#' && stk.top() != '(') {
postfix += stk.top(); //store and pop until ( has found
stk.pop();
}
stk.pop(); //remove the '(' from stack
}else {
if(preced(*it) > preced(stk.top()))
stk.push(*it); //push if precedence is high
else {
while(stk.top() != '#' && preced(*it) <= preced(stk.top())) {
postfix += stk.top(); //store and pop until higher
precedence is found
stk.pop();
}
stk.push(*it);
}
}
}
while(stk.top() != '#') {
postfix += stk.top(); //store and pop until stack is not empty
stk.pop();
}
return postfix;
}
string inToPre(string infix) {
string prefix;
reverse(infix.begin(), infix.end()); //reverse the infix expression
string::iterator it;
for(it = infix.begin(); it != infix.end(); it++) { //reverse the
parenthesis after reverse
if(*it == '(')
*it = ')';
else if(*it == ')')
*it = '(';
}
prefix = inToPost(infix); //convert new reversed infix
to postfix form.
reverse(prefix.begin(), prefix.end()); //again reverse the result to
get final prefix form
return prefix;
}
int main() {
string infix ;
cout <<"masukkan nilai infix= ";
cin >> infix;
cout << "Prefix Form Is: " << inToPre(infix) << endl;
}

OUTPUT
Nama program : prefix to
postfix

Bahasa pemrograman : C++


Compiler : Dev C++ studio

#include <iostream>
#include <stack>
using namespace std;
bool isOperator(char x) {
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
}
return false;
}
string convertToPostfix(string prefix) {
stack<string> expression;
int length = prefix.size();
for (int i = length - 1; i >= 0; i--) {
if (isOperator(prefix[i])) {
string op1 = expression.top();
expression.pop();
string op2 = expression.top();
expression.pop();
string temp = op1 + op2 + prefix[i];
expression.push(temp);
}
else
expression.push(string(1, prefix[i]));
}
return expression.top();
}
int main() {
string prefix = "*-AB/+CD*XY";
cout<<"Prefix expression : "<<prefix<<endl;
cout<<"Postfix expression : "<<convertToPostfix(prefix);
return 0;
}
OUTPUT
Nama program : Postfix to infix
Bahasa pemrograman : C++
Compiler : Dev C++ studio

#include <bits/stdc++.h>
using namespace std;
bool isOperand(char ch){
return (ch>='a' && ch<='z') || (ch>='A' &&
ch <='Z');
}
string postfixToInfix(string postfix) {
stack<string> st;
int len = postfix.size();
for (int i = 0; i < len; i++) {
if(isOperand(postfix[i])){

st.push(string(1,postfix[i]));

}
else{
string operand1=st.top();
st.pop();

string operand2=st.top();
st.pop();

st.push("(" + operand2 +
string(1,postfix[i]) + operand1 +
")");
}
}
return st.top();
}
int main() {

string postfix="abc/-ad/e-*";
string infix=postfixToInfix(postfix);
cout<<"Postfix expression :
"<<postfix<<endl;
cout<<"Infix expression : "<<infix<<endl;

return 0;
return 0;
}

OUTPUT

You might also like