source code infix to postfix

program infix_ke_postfix;

uses crt;

const max_elemen = 255;

type S255 = string[max_elemen];
tumpukan = record
rinci : S255;
atas : 0..Max_elemen
end;

var infix : S255;
lagi : char;

function VALENSI (Tanda_Op : char) : integer;

begin
case Tanda_Op of
‘^’ : VALENSI := 3;
‘*’, ‘/’ : VALENSI := 2;
‘+’, ‘-‘ : VALENSI := 1;
‘(‘ : VALENSI := 0
end
end;

procedure PUSH (var T : Tumpukan ; Elemen : char);

begin
T.atas := T.atas + 1;
T.rinci[T.Atas] := Elemen
end;
function POP (var T : Tumpukan) : char;

begin
POP := T.Rinci[T.Atas];
T.Atas := T.Atas – 1
end;

procedure KONVEKSI_CETAK (Infix : S255);

var I : integer;
Operator : set of char;
Temp, Kar : char;
T : Tumpukan;
Test : boolean;

begin

Operator := [‘^’]+[‘*’]+[‘/’]+[‘+’]+[‘-‘];

for I := 1 to length(Infix) do
begin
Kar := Infix[I];

if Kar = ‘(‘ then PUSH(T, Kar)

else if Kar = ‘)’ then
begin
while T.Rici[T.Atas] <> ‘(‘ do
write(POP(T):2);
Temp := POP(T)
end

else if Kar in Operator then
begin
while (T.Atas <> 0) and (VALENSI(Kar) <= VALENSI(T.Rinci[T.Atas])) do
write(POP(T):2);
PUSH(T, Kar)
end

else if Kar <> ‘ ‘ then
write(Kar:2)
end;

if T.Atas <> 0 then

repeat
write(POP(T):2)
until T.Atas = 0;
end;

begin
clrscr;
writeln(‘##################################’);
writeln(‘ MENGUBAH BENTUK INFIX MENJADI POSTFIX ‘);
writeln(‘##################################’);
writeln;
repeat

write(‘Masukkan bentuk infix (tanpa spasi): ‘);
readln(Infix); writeln;
write(‘Bentuk postfix adalah : ‘);

KONVERSI_CETAK (Infix);

writeln; writeln;
write(‘Akan mencoba lagi? Y(a)/T(idak): ‘);
readln(lagi);
writeln
until not (lagi in [‘Y’, ‘y’])
end.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: