Program Pascal Mengkonversi Bilangan Desimal menjadi Bilangan Biner Menggunakan Stack
Source Code
Program
Convert_Decimal_To_Biner;
Uses
crt;
Const N
= 100;
Type
Stack = record
Isi : Array[1..N]of integer;
Top : integer;
end;
Procedure
CreateStack(Var S:Stack);
begin
S.Top := 0;
end;
Function
IsFull(S:Stack):boolean;
begin
IsFull := (S.Top = N);
end;
Function
IsEmpty(S:Stack):boolean;
begin
IsEmpty := (S.Top = 0);
end;
Procedure
Push(X:integer; var S:Stack);
begin
If Not IsFull(S) then
begin
S.Top := S.Top + 1;
S.Isi[S.Top] := X;
end;
end;
Procedure
Pop(var X:integer; var S:Stack);
begin
IF NOt IsEmpty(S) then
begin
X := S.Isi[S.Top];
S.Top := S.Top - 1;
end;
end;
Procedure
Convert(var S:Stack; X:integer);
var Sisa : integer;
begin
Repeat
Sisa := X mod 2;
X := X div 2;
Push(Sisa,S);
Until X = 0 ;
end;
Var
S : Stack;
X,i,Sisa: integer;
Begin
writeln('+---------------------------------+');
Writeln('|Program Konversi Desimal ke
Biner|');
writeln('+---------------------------------+');
Writeln;
Write('Masukkan Bilangan Desimal : ');
readln(X);
writeln;
write('Hasil Konversi ke Bilangan Biner ',
X ,' : ');
CreateStack(S);
Convert(S,X);
for i:=1 to S.Top do
begin
Pop(Sisa,S);
write(Sisa);
end;
End.
0 Comments