Anda di halaman 1dari 8

# Procedure Inisialisasi(I/O temp:Stack, I/O top:integer) {I. S. : Memberi harga 1 ke var. i, lalu push '(' ke temp. } {F. S.

## : implementasi Stack siap digunakan} Algoritma: i 1 temp[1] '(' top i Endprocedure

Procedure InputInfix(input temp:Stack, I/O X:Stack, output j:integer) {I. S. : User memasukkan notasi infix} {F. S. : Menghasilkan notasi infiks} Algoritma: output(' Masukkan Notasi Infix : ') input(infix) j 1 for i 1 to length(infix) do notasi infix[i] if (((notasi = '+')or(notasi = '-')or(notasi = '*')or(notasi = '/'))and(temp[top]= '^')) then X[j] temp[top] top top-1 j j+1 Endif if (((notasi = '+')or(notasi = '-'))and((temp[top] = '*')or(temp[top] = '/')))

then X[j] temp[top] top top-1 j j+1 Endif if ((notasi = '+')or(notasi = '-')or(notasi = '*')or(notasi = '/')or(notasi = '^')or(notasi = '(')) then top top +1 temp[top] notasi else if(notasi<>')') then X[j] notasi j j+1 Endif if (notasi = ')') then while (temp[top]<>'(') do X[j] temp[top] top top-1 j j+1 Endwhile top top-1 Endif Endfor

## While(temp[top]<>'(') do X[j] temp[top] top top-1 j j+1 Endwhile Endprocedure

Procedure Konversi(input X:Stack, input j:integer) {I. S. : Notasi infix telah terdefinisi} {F. S. : Menghasilkan notasi postfix} Algoritma: i 1 output(' Notasi Postfix repeat output (X[i]) i i+1 until(i=J) Endprocedure : ')

Procedure InputOperand(I/O X:Stack, input j:integer) {I. S. : Operand telah terdefinisi, User memasukkan nilai operand} {F. S. : Menghasilkan nilai operand} Algoritma: i 1 while (i<>j) do notasi X[i]

if (notasi <> '+')and(notasi <> '-')and(notasi <> '*')and(notasi <> '/')and(notasi <> '^') then output (' Masukkan Nilai ',X[i],' input(notasi2) X[i] notasi2 Endif i i+1 Endwhile Endprocedure : ')

Procedure Hitung(input X:Stack, input j:integer) {I. S. : Nilai operand telah terdefinisi} {F. S. : Menghasilkan hasil perhitungan berdasarkan operator yang diinput User} Algoritma: i 1 k 1 l 1 notasi ')' X[j] notasi while(X[i]<>')') do if ((X[i] <> '+')and(X[i] <> '-')and(X[i] <> '*')and(X[i] <> '/')and(X[i] <> '^')) then bantu X[i] if(bantu='1') then operand[k] 1 else

if(bantu='2') then operand[k] 2 else if(bantu='3') then operand[k] 3 else if(bantu='4') then operand[k] 4 else if(bantu='5') then operand[k] 5 else if(bantu='6') then operand[k] 6 else if(bantu='7') then operand[k] 7 else if(bantu='8') then operand[k] 8 else if(bantu='9') then operand[k] 9 else operand[k] 0 Endif

Endif Endif Endif Endif Endif Endif Endif Endif Endif Endwhile k k+1 i i+1 if ((X[i] = '+')or(X[i] = '-')or(X[i] = '*')or(X[i] = '/')or(X[i] = '^')) then var1 operand[k-1] k k-1 var2 operand[k-1] k k-1 if(X[i] = '+') then hasil var2+var1 else if(X[i] = '-') then hasil var2-var1 else if(X[i] = '*') then

hasil var2*var1 else if(X[i] = '/') then hasil trunc(var2) div trunc(var1) else hasil var1 for l 1 to trunc(var2) do hasil hasil * var1 Endfor Endif Endif Endif Endif Endif operand[k] hasil i i+1 k k+1 k k-1 output(' Hasil Hitung : ',operand[k])

{Algoritma Utama} KonversiKeInfix Const: maks = 25 Type: Stack = array [1..maks] of char

nilai = array [1..maks] of real Kamus: temp,X operand top i,j,k,l infix notasi,notasi2 bantu hasil var1,var2 : Stack : nilai : integer : integer : string : char : char : real : real

Procedure Inisialisasi(I/O temp:Stack, I/O top:integer) Procedure InputInfix(input temp:Stack, I/O X:Stack, output j:integer) Procedure Konversi(input X:Stack, input j:integer) Procedure InputOperand(I/O X:Stack, input j:integer) Procedure Hitung(input X:Stack, input j:integer) Algoritma: Inisialisasi(temp,top) InputInfix(temp,X,j) Konversi(X,j) InputOperand(X,j) Hitung(X,j)