Program Stack Part 5 (Program Stack Konversi Infix ke Postfix)

40 : begin
clrscr;
 gotoxy(20,4);writeln(' Program Konversi Infix Ke Postfix');
 gotoxy(20,5);writeln('***********************************');
writeln;
 gotoxy(15,8);write('Ketik Notasi Infix : ');
  read(okunan);
  uzunluk:=length(okunan);
  okunan[0]:=chr(uzunluk);
  j:=0;i:=1;
  while(i<=uzunluk) do
   begin
     a1:=okunan[i];
     val(a1,i4,code);
     top:=0;
     if code=0 then
      begin
        while (code=0) and (i<=uzunluk)do
          begin
            top:=top*10+i4;
            i:=i+1;
            a1:=okunan[i];
            val(a1,i4,code);
          end;
        j:=j+1;
        str(top,ifade[j]);
      end;
     if i<=uzunluk then
      begin
        j:=j+1;
        ifade[j]:=okunan[i];
        i:=i+1;
      end;
   end;
   n:=j;
{
Conversion to Postfix
}
  ptr:=0;
  i:=1; j:=0;
  while(i<=(uzunluk)) do
   begin
     token:=ifade[i];
     val(token,i4,code);
     if code = 0 then
      begin
        j:=j+1;
        yeni[j]:=token;
      end
     else
      begin
        tut:=token[1];
        case tut of
           '(','^': push2(tut);
           ')': begin
                  stack_elemani:=pop2;
                  while stack_elemani <> '(' do
                    begin
                      j:=j+1;
                      yeni[j]:=stack_elemani;
                      stack_elemani:=pop2;
                    end;
                end;
           '+','-': begin
                      stack_elemani:=pop2;
                      while (stack_elemani <> '(') and
                             (stack_elemani <> ' ') do
                        begin
                          j:=j+1;
                          yeni[j]:=stack_elemani;
                          stack_elemani:=pop2;
                        end;
                        if stack_elemani ='(' then
                           push2('(');
                        push2(tut);
                    end;
           '*','/','^':begin
                         stack_elemani:=pop2;
                         while(stack_elemani='*') or (stack_elemani='/')
                               or (stack_elemani='^')  do
                           begin
                             j:=j+1;
                             yeni[j]:=stack_elemani;
                             stack_elemani:=pop2;
                           end;
                         if stack_elemani <> ' ' then
                           push2(stack_elemani);
                         push2(tut);
                       end;
           end;{ case }
     end;{ else }
    i:=i+1;
   end; { while }
  stack_elemani:=pop2;
  while(stack_elemani <> ' ') do
    begin
       j:=j+1;
       yeni[j]:=stack_elemani;
       stack_elemani:=pop2;
    end;

gotoxy(20,12);writeln('- Setelah operasi push dan pop -');
  writeln;
gotoxy(15,16);write('Notasi Postfix    : ');
  n:=j;
  for i:=1 to n do
     write(yeni[i],' ');
  readln;
{
 Calculating the result using Postfix
}
  i:=1;
  ptr:=0;
  while(i<=n) do
    begin
      token:=yeni[i];
      val(token,i4,code);
      while(code=0) do
        begin
          ptr:=ptr+1;
          sayi_stack[ptr]:=i4;
          i:=i+1;
          token:=yeni[i];
          val(token,i4,code);
        end;
      tut:=token[1];
      sayi1:=sayi_stack[ptr];
      ptr:=ptr-1;
      sayi2:=sayi_stack[ptr];
      case tut of
        '+':sayi_stack[ptr]:=sayi1+sayi2;

        '-': sayi_stack[ptr]:=sayi2-sayi1;

        '*': sayi_stack[ptr]:=sayi2*sayi1;

        '/': sayi_stack[ptr]:=sayi2 div sayi1;

        '^':begin
              carp:=1;
              for ii:=1 to sayi1 do
                carp:=carp*sayi2;
              sayi_stack[ptr]:=carp;
            end;
        end; { case }
      i:=i+1;
    end;
gotoxy(15,18);  write('Hasil Perhitungan : ');write(sayi_stack[1]);

gotoxy(15,21);writeln('Ingin Kembali Ke Menu?');
gotoxy(15,23);write('Tekan y Untuk Ya atau Enter Untuk Tidak');
gotoxy(1,25);write('Masukkan pilihan anda : ');
read(pil2);
case  pil2 of
'y': goto awal;
else goto akhir
end;
end;

akhir : begin
clrscr;
gotoxy(20,10);write('Terima Kasih Sudah Menggunakan Program Ini :) ');
end;
readln;
end.

Itulah source code program yang saya jabarkan mulai dari deklarasi variabel sampai codingan pengaplikasian program stack. Silakan dicoba ya :) Sampai bertemu lagi di postingan selanjutnya.

0 komentar:

Tutorial Matematika

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | GreenGeeks Review