Sunday, 2 April 2017

Program Konversi Bilangan Desimal ke Biner Menggunakan Stack di C++

Hay teman - teman mau share coding nie, coding program konversi bilangan desimal ke biner menggunakan stack, langsung aja ya..

 #include <iostream.h>
#include <conio.h>

int MAXSTACK; typedef int itemtype;
typedef struct
{
 itemtype item[300]; int count;
}stack;



void initializestack(stack *s)
{
 s->count = 0;}

int empty(stack *s)
{
 return (s->count == 0);}



int full(stack *s)
{
 return (s->count == MAXSTACK);}

void push(itemtype x, stack *s)
{
 if(full(s))
 cout<<"stack penuh !"<<endl;
 else
 {
  s->item[s->count]=x; ++(s->count);}
}

int pop(stack *s)
{
 if(empty(s))
 cout<<"stack kosong"<<endl; else
 {
  --(s->count);
  return (s->item[s->count]);}
}

main()
{
 int i, n, m, l, z; int input;
 stack tumpukan;

 cout<<"Program Pengkonversi Desimal ke Biner"<<endl<<endl;
 initializestack(&tumpukan);
 cout<<"bilangan desimal = ";
 cin>>input;


 for(z=1,n=input;n>0;n=n/2, z++)
 {
  MAXSTACK=z;
 }
 m=0;
 for(n=input;n>0;n=n/2)
 {
  l=n%2;
  push(l,&tumpukan);
  ++m;
 }

 cout<<"bilangan biner = ";
 for(i=MAXSTACK;i>0;i--)
 {
  cout<<pop(&tumpukan);
 }

 getch();
}

Output Program :


Sekian dulu ya, semoga bermanfaat codingnya...

TERIMA KASIH.

0 komentar:

Post a Comment