Konversi Infix to Postfix Pascal (Lazarus)

Berikut Koding pemrograman Konversi Infix to Postfix Pascal (tapi menggunakan Software Lazarus). Semoga bermanfaat kawan.

Program LAZARUS_KONVERSI_INFIX_KE_POSTFIX;
const Max_Elemen = 255;

type isi = string[Max_Elemen];
stack = record
hasl : isi;
top : 0..Max_Elemen
end;
var Infix : isi; {* notasi infix *}
Lagi : char;
t : stack;

function ALIANSI (macam_op : char) : integer;

begin
case macam_op of
'^' : ALIANSI := 3; {* pangkat *}
'*', '/' : ALIANSI := 2; {* kali atau bagi *}
'+', '-' : ALIANSI := 1; {* plus atau minus *}
'(' : ALIANSI := 0 {* kurung buka *}
end
end;

procedure PUSH (var t : stack; Elemen : char);

begin
t.top := t.top + 1;
t.hasl[t.top] := Elemen
end;

function POP (var t : stack) : char;

begin
POP := t.hasl[t.top];
t.top := t.top - 1
end;

procedure KONVERSI_CETAK (Infix : isi);

var
i : integer;
temp, Kar : char;
opr : set of char;

begin
opr := ['^']+['*']+['/']+['+']+['-'];
for i := 1 to length(Infix) do
begin
Kar := Infix[i]; {* Kurung buka. Push ke dalam tumpukan *}
if Kar = '(' then PUSH(t, Kar)
else if Kar = ')' then
begin
while T.hasl[t.top] <> '(' do
write(POP(T):2);
temp := POP(T)
end

else if Kar in opr then
begin
while (t.top <> 0) and (ALIANSI(Kar) <= ALIANSI(T.hasl[t.top])) do
write(POP(T):2);
PUSH(T, Kar)
end

else if Kar <> ' ' then
write(Kar:2)
end;
if t.top <> 0 then
repeat
write(POP(T):2)
until t.top = 0;
end;

begin
writeln(' =======================================');
writeln('| MENGUBAH NOTASI INFIX MENJADI POSTFIX |');
writeln(' =======================================');
writeln;

repeat
write('Masukkan ungkapan Infix : ');
readln(Infix); writeln;
write('Hasil Ungkapan postfix : ');

KONVERSI_CETAK(Infix);

writeln; writeln;
write('Akan mencoba lagi? Y/N : ');
readln(Lagi);
writeln
until not (Lagi in ['Y', 'y'])
end.


Update 20-12-2012 : Script opr := ['$']+['*']+['/']+['+']+['-']; salah dan diganti opr := ['^']+['*']+['/']+['+']+['-'];

Terima kasih.
Sumber : http://aumar.blogdetik.com/
thumbnail
Judul: Konversi Infix to Postfix Pascal (Lazarus)
Rating: 100% based on 99998 ratings. 5 user reviews.
Ditulis Oleh

Artikel Terkait Pemrograman :

2 comments:

 
Copyright © 2013. About - Sitemap - Contact - Privacy
Template Seo Elite oleh Bamz