Program untuk mengkonversi bilangan desimal ke biner menggunakan stack.

 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 
 | #include<stdio.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))
 printf("stack penuh !\n");
 else
 {
  s->item[s->count]=x; ++(s->count);
 }
}
int pop(stack *s)
{
 if(empty(s))
 printf("stack kosong\n");
 else
 {
  --(s->count);
  return (s->item[s->count]);
 }
}
main()
{
 int i, n, m, l, z; int input;
 stack tumpukan;
 printf("Program Pengkonversi Desimal ke Biner\n\n");
 initializestack(&tumpukan);
 printf("Masukkan bilangan desimal = ");
 scanf("%d", &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;
 } 
 printf("Masukkan bilangan biner = ");
 for(i=MAXSTACK;i>0;i--)
 {
  printf("%d", pop(&tumpukan));
 }
 getch();
 return 0;
}
 | 
 
 
source : http://noraderysofya.blogspot.com