Program Pengubah Notasi Infix Menjadi Notasi Postfix


program infix_postfix;

uses crt;

const maxelemen = 255;

type

read = string[maxelemen];

stack = record

isi: read;

atas: integer;

end;

var

notasiinfix: read;

lagi: char;

(*/fungsi untuk menentukan derajat operator/*)

function derajat(operator: char): integer;

begin

case operator of

‘^’ : derajat := 3;

‘*’,’/’ : derajat := 2;

‘+’,’-‘ : derajat := 1;

‘(‘ : derajat := 0;

end;

end;

(*/prosedur untuk memasukkan elemen ke dalam stack/*)

procedure push(var t:stack; elemen:char);

begin

if t.atas = maxelemen then

writeln(‘Stack Penuh’)

else begin

t.atas := t.atas + 1;

t.isi[t.atas] := elemen;

end;

end;

(*/fungsi untuk mengeluarkan elemen dari dalam stack/*)

function pop(var t: stack): char;

begin

if t.atas = 0 then

writeln(‘Stack Kosong’)

else begin

pop := t.isi[t.atas];

t.atas := t.atas – 1;

end;

end;

(*/prosedur untuk mengkonversikan notasi infix menjadi notasi postfix/*)

procedure konversiinfix(infix: read);

var

i : integer;

t : stack;

operatorsign: set of char;

kar, temp : char;

test : boolean;

begin

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

for i := 1 to length(infix) do

begin

kar := infix[i];

if kar = ‘(‘ then

push(t,kar)

else if kar = ‘)’ then

begin

while t.isi[t.atas] <> ‘(‘ do

write(pop(t):2);

temp := pop(t);

end

else if kar in operatorsign then

begin

while (t.atas <> 0) and (derajat(kar) <= derajat(t.isi[t.atas])) do

begin

write(pop(t):2);

end;

end;

push(t,kar);

end else

if kar <> ‘ ‘ then write(kar:2);

end;

if t.atas <> 0 then

begin

repeat

write(pop(t):2);

until t.atas = 0;

end;

end;

(*/program utama/*)

begin

repeat

clrscr;

writeln(‘  Mengubah Notasi Infix menjadi Postfix ‘);

writeln(‘  Dengan memanfaatkan Struktur Stack ‘);

writeln;

write(‘ Masukkan notasi Infix: ‘); readln(notasiinfix);

writeln;

write(‘ Hasil notasi Postfix: ‘);

konversiinfix(notasiinfix);

writeln; writeln;

write(‘ Coba Lagi (Y/T): ‘); readln(lagi);

until lagi in [‘T’,’t’,’N’,’n’];

end.

About these ads

~ by sirhexa on December 11, 2009.

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

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: