-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main_Program
168 lines (142 loc) · 2.68 KB
/
Main_Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//Oleh :
//Andhika Kurniawan - 1817051027
//Ario Rinaldi - 1817051040
//Kurnia Agung Firdaus - 1817051056
//Muhammad Alamsyah - 1817051051
#include <iostream>
#include <cstring>
using namespace std;
#define MAX_STACK_SIZE 20
class stack {
char data[MAX_STACK_SIZE];
char top;
public:
stack() {
top = -1;
}
void push(char input){
if (top == MAX_STACK_SIZE-1) {
cout << "Melebihi Batas Stack" << endl;
return;
}
data[++top] = input;
}
void pop() {
if (top == -1) {
return;
}
top--;
}
int Top() {
return data[top];
}
bool IsEmpty() {
if (top == -1) {
return true;
}
else {
return false;
}
}
void print() {
int i;
cout << "Stack : ";
for (i = 0; i <= top; i++){
cout << data[i] << " ";
}
cout << endl;
}
};
int prec(char op) {
if(op == '*' || op == '/'){
return 2;
} else if (op == '+' || op == '-'){
return 1;
} else {
return -1;
}
}
void display () {
stack s;
string Q, P;
int jumlah_Q, hasil;
cout << " Program Konversi Infix ke Postfix" << endl << endl << endl;
cout << "Masukkan ekspresi Infix ";
getline(cin, Q);
jumlah_Q = Q.length();
for (int i=0 ;i < jumlah_Q; i++){
if (Q[i] >= '0' && Q[i] <= '9'){
P+= Q[i];
}
else if (Q[i] == '('){
s.push(Q[i]);
}
else if (Q[i] == ')'){
while (s.IsEmpty() != true && s.Top() != '('){
char tmp = s.Top();
s.pop();
P+= tmp;
}
if (s.Top() == '('){
s.pop();
}
}
if (Q[i] == '+' || Q[i] == '-' || Q[i] == '*' || Q[i] == '/'){
if (s.IsEmpty() == true || s.Top() == '('){
s.push(Q[i]);
}
else{
while (s.IsEmpty() != true && s.Top() != '(' && prec(Q[i]) <= prec(s.Top())){
char tmp=s.Top();
s.pop();
P+= tmp;
}
s.push(Q[i]);
}
}
}
while(s.IsEmpty() != true){
char tmp=s.Top();
s.pop();
P+= tmp;
}
for(int i = 0 ;i < jumlah_Q; i++){
if (P[i] >= '0' && P[i] <= '9'){
s.push(P[i]);
}
if ( P[i]=='+' || P[i]=='-' || P[i]=='*' || P[i]=='/' ){
int A = s.Top() - '0';
s.pop();
int B = s.Top() - '0';
s.pop();
if (P[i]=='+') {
hasil = B + A;
char tmphasil = hasil + '0';
s.push(tmphasil);
}
else if (P[i]=='-') {
hasil = B - A;
char tmphasil = hasil + '0';
s.push(tmphasil);
}
else if (P[i] =='*') {
hasil = B * A;
char tmphasil = hasil + '0';
s.push(tmphasil);
}
else if (P[i]=='/') {
hasil = B / A;
char tmphasil = hasil + '0';
s.push(tmphasil);
}
}
}
char charhasil = s.Top();
int hasilakhir = charhasil - '0';
cout << "Postfix : " << P << endl << endl;
cout << "Hasil Akhir = " << hasilakhir << endl;
}
int main(){
display ();
return 0;
}