program infiks;
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_off : char) : integer;
begin
case tanda_op of
'^' : valensi := 3;
'*', '/' : valensi := 2;
'+', '-' : valensi := 1;
'(' : valensi := 0
end
end;
procedure push (var s : tumpukan; elemen : char);
begin
s.atas := s.atas +1;
s.rinci[s.atas] := elemen
end;
function pop (var s : tumpukan) : char;
begin
pop := s.rinci[s.atas];
s.atas := s.atas - 1
end;
procedure konversi_cetak (infix : s255);
var i : integer;
operator : set of char;
temp, kar : char;
s : tumpukan;
test : boolean;
begin
operator := ['^']+['*']+['/']+['+']+['-'];
for i := 1 to length (infix) do
begin
kar := infix[i];
if kar = '(' then push (s, kar)
else if kar = ')' then
begin
while s.rinci[s.atas] <> '(' do
write (pop(s) : 2);
temp := pop(s)
end
else if kar in operator then
begin
while (s.atas <> 0 ) and (valensi(kar)
<= valensi (s.rinci[s.atas])) do write (pop(t) : 2); push (s, kar) end else if kar <> ' ' then
write (kar : 2)
end;
if s.atas <> 0 then
repeat
write (pop(s):2)
until s.atas = 0
end;
begin
clrscr;
writeln ('Infix to Postfix');
writeln ('use Stack');
writeln ('-------------------------------------');
writeln ;
repeat
write ('Masukkan Infix : ');
readln (infix); writeln;
write ('Ungkapan Postfix : ');
konversi_cetak (infix);
writeln; writeln;
write ('Coba lagi ? y(a) / t(idak) : ');
readln (lagi);
writeln
until not (lagi in ['Y', 'y'])
end.
0 comments:
Post a Comment