soal:
1. Merubah notasi infix menjadi postfix
jawab:
uses crt;
const max_elemen = u19;
type u19 = string [max_elemen];
tumpukan = record
rinci : u19;
atas : 0..max_elemen
end;
var infix : u19;
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 konversi_cetak (infix : z100);
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.rinci[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 ('Mengubah Notasi Infix Menjadi Postfix');
writeln ('Dengan Memanfaatkan Struktur Tumpukan');
writeln ('-------------------------------------');
writeln ;
repeat
write ('Masukkan Ungkapan Infix : ');
readln (infix); writeln;
write ('Ungkapan Postfix : ');
konversi_cetak (infix);
writeln; writeln;
write ('Akan Mencoba Lagi ? y(a) / t(idak) : ');
readln (lagi);
writeln
until not (lagi in ['Y', 'y'])
end.
const max_elemen = u19;
type u19 = string [max_elemen];
tumpukan = record
rinci : u19;
atas : 0..max_elemen
end;
var infix : u19;
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 konversi_cetak (infix : z100);
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.rinci[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 ('Mengubah Notasi Infix Menjadi Postfix');
writeln ('Dengan Memanfaatkan Struktur Tumpukan');
writeln ('-------------------------------------');
writeln ;
repeat
write ('Masukkan Ungkapan Infix : ');
readln (infix); writeln;
write ('Ungkapan Postfix : ');
konversi_cetak (infix);
writeln; writeln;
write ('Akan Mencoba Lagi ? y(a) / t(idak) : ');
readln (lagi);
writeln
until not (lagi in ['Y', 'y'])
end.
hmmmmmm
BalasHapus