Rabu, 16 Maret 2011
coding for transformation 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(' TRANSFORMASI BENTUK INFIX MENJADI POSTFIX ');
writeln('@@@@@@@@@@@@@@@@@@@@@@@');
writeln;
repeat
write('Input bentuk infix (tanpa spasi): ');
readln(Infix); writeln;
write('tampilan postfixnya : ');
KONVERSI_CETAK (Infix);
writeln; writeln;
write('test lagi? Y(a)/T(idak): ');
readln(lagi);
writeln
until not (lagi in ['Y', 'y'])
end.
thanks for pembaca....
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar