program infixtopostfix

|

program infixtopostfix;
uses crt;
var
masuk:string[50];
ifa,ye:array[1..30]of string;
t,u,i,j,i4,top,co,n,ptr:integer;
token:string;
a1,stacke,tu:char;
stack:array[1..10]of char;
procedure push(oprtr:char);
begin
ptr:=ptr+1;
stack[ptr]:=oprtr;
end;
function pop:char;
begin
if ptr=0 then
begin
pop:=' ';
end
else
begin
pop:=stack[ptr];
ptr:=ptr-1;
end;
end;
begin
clrscr;
write ('Masukkan ekspresi Infix: ');
read (masuk);
u:=length(masuk);
j:=0;i:=1;
while (i<=u) do
begin
a1:=masuk[i];
val (a1,i4,co);
top:=0;
if co=0 then
begin
while (co=0) and (i<=u) do
begin
top:=top*10+i4;
i:=i+1;
a1:=masuk[i];
val(a1,i4,co);
end;
j:=j+1;
str(top,ifa[j]);
end;
if i<=u then
begin
j:=j+1;
ifa[j]:=masuk[i];
i:=i+1;
end;
end;
n:=j;

ptr:=0;
i:=1; j:=0;
while(i<=(u)) do
begin
token:=ifa[i];
val (token,i4,co);
if co = 0 then
begin
j:=j+1;
ye[j]:=token;
end
else
begin
tu:=token[1];
case tu of
'(','^':push(tu);
')':begin
stacke:= pop;
while (stacke <>' ') do
begin
j:=j+1;
ye[j]:=stacke;
stacke:=pop
end;
end;
'+','-' : begin
stacke:=pop;
while (stacke <> '(') and (stacke <>' ') do
begin
j:=j+1;
ye[j]:=stacke;
stacke:=pop;
end;
if stacke='('then
push ('(');
push(tu);
end;
'*','/','^' : begin
stacke:=pop;
while (stacke='*') or (stacke='/')
or (stacke='^') do
begin
j:=j+1;
ye[j]:=stacke;
stacke:=pop;
end;
if stacke <>' ' then push (stacke);
push(tu);
end;
end;
end;
i:=i+1;
end;
stacke:=pop;
while(stacke <>' ') do
begin
j:=j+1;
ye[j]:=stacke;
stacke:=pop;
end;
writeln ('Notasi Postfix: ');
n:=j;
for i:=1 to n do
write (ye[i],' ');
readln;
repeat until keypressed;
end.

0 komentar:

Posting Komentar