{ Program Name : Konversi.Pas
Authors : Oke Hendradhy
Version : 1.0
}
Uses crt;
const
Noelstack = 80;
Makschar = 80;
Type
Eon = char;
stack = Record
Top : array[1...Noelstack] of Eon;
Noel : 0... Noelstack;
End;
Tipeindex = 0... makschar;
Typeeks = array[1... Noelstack] of char
Var
lagi : char;
{Bentuk-bentuk operasi stack}
Function isempty(Var s : stack):Boolean;
Begin
IsEmpty : = s. noel = 0
End;
Procedure create (var s. stack);
Begin
S. Noel : = 0;
End;
procedure buatkosong (Var s. stack);
Begin
S. Noel : = 0;
End ;
Procedure stack Error (tingkat Error: integer);
Begin
Case tingkat error of
1 : Writeln (isi stack sudah terlalu penuh);
2 : Writeln (isi stack kosong);
End
End;
Procedure push( var s : stack; tipebaru : Eon);
Begin
If s. Noel = Noel stack then
stack Error(1)
Else
Begin
s.Noel : = s. Noel+1 ;
s.Top[s.Noel] : = tipebaru
End
End;
Procedure pop(var s: stack ;
var nilaistack : Eon);
Begin
If s. Noel = 0 then
Stack error(2)
Else
Begin
Nilaistack : = s.top[s.Noel];
S.Noel : = s. Noel - 1;
End
End;
{proses konversi suatu ekspresi}
Function puncak stack(s : stack) : Eon;
Begin
Puncak stack : = s.top[s.noel]
End;
Function isidlmstack (operator: char); integer;
Begin
case operator of
'(' : isidlmstack : = 0;
'+','-' : isidlmstack : = 2;
'*',','/' : isidlmstack : = 4;
End
End;
Function stackyangdibaca (operator : char): integer
Begin
Case operator of
')' : stackyangdibaca : =0;
'+','-' : stackyangdibaca : = 1;
'*','/' : stackyangdibaca : = 3;
'(' : stackyangdibaca : = 5;
End
End;
Procedure simpanchar(ch:char; Var ekspost : Tipeks;Var indekspost ; Tipeindex);
Begin
Indekspost : = indeks post+1;
ekspost [indekspost]: = ch;
End;
Procedure konversi infixkePostfix (eks dlm : Tipe eks;
pjg dlm : Tipe index;
var ekspost: Tipe Eks;
var panjang post : Tipe indeks);
Var
opstack : stack ;
indeksdlm, indeks post: Tipe index;
chdlm, operator, simpan : char ;
Begin
create (Opstack);
Push (Opstack, '(');
eksdlm[pjg dlm +1] := ')';
Indeks post: = 0;
For indeksdlm : = 1 to pjgdlm +1 Do
Begin
chdlm: = Eks Dlm [indeks dlm];
if ch dlm in ['A'....'Z'] then
simpan char (ch Dlm, Ekspost,indekspost)
Else
Begin
While isidlmstack(puncak stack (opstack))> stack yang dibaca (ch dlm) Do
begin
pop(opstack,operator);
simpanchar(operator,ekspost,indekspost)
end;
if chdlm = ')' then
pop(opstack,simpan)
else
push(opstack,simpan)
end
panjang post := indekspost
end;
panjang post := indekspost
end;
produce konversi ekspresi;
var
indeks panjangdlm, panjang post :tipe index;
eksdlm,ekspot :tipe eks
begin
lagi :='y';
While (upcase(lagi) ='x' do
Begin
clsscr;
panjangdlm := 0
while not eoln do
begin
panjangdlm:=panjangdlm +1;
read(eksdlm[panjangdlm])
end;
readln ;
write('ekspresi infix:');
for index := 1 to panjangdlm do
write(eksdlm[index]);
writeln;
konversiinfixkePostfix(eksdlm, panjangdlm, ekspost, panjang post);
write ('dalam bentuk Postfix')
for indeks := 1 to panjangpost do
write (ekspost [indeks]);
writeln;
writeln;
write ('ada data lagi<y/t>.'); readln (lagi)
end;
End;
begin
konversi ekspresi;
end
Tidak ada komentar:
Posting Komentar