Tuesday, July 3, 2007

SD: Stack Guided 3 Juli 2007

/*karena format HTML, maka semua karakter lebih besar (>) dan lebih kecil (<) diganti dengan karakter ((>)) dan ((<)) sorry (^-^)*/

//file header.h

#include (<)conio.h(>)
#include (<)stdio.h(>)
#include (<)stdlib.h(>)
#include (<)time.h(>)
#define max_stack 5

typedef int infotype;
typedef struct{
infotype top;
int content[max_stack];
}stack;

void init(stack *s);
void push(stack *s, int value);
infotype pop(stack *s);
void show(stack s);
void random(stack *s, int n);


//file definition.c

#include "header.h"

void init(stack *s)
{
(*s).top = -1;
}

void push(stack *s, int value)
{
if((*s).top (<) (max_stack - 1 ))
{
(*s).top++;
(*s).content[(*s).top]=value;
}
else
{
printf("\nStack Penuh...");
getch();
}
}

infotype pop(stack *s)
{
infotype a;
if(((*s).top) (>) (-1))
{
a=(*s).content[(*s).top];
(*s).top--;
return(a);
}
else
{
printf("\nStack Kosong...");
return 0;
}
}

void show(stack s)
{
if(s.top (>) -1)
{
while(s.top != -1)
{
printf("\n %d",s.content[s.top]);
s.top--;
}
}
else
printf("\nStack Kosong...");
}

void random(stack *s, int n)
{
int x,i,temp;
srand((unsigned)time(NULL));
x=rand();
temp=(*s).top+n;
for(i=(*s).top;i(<)temp;i++)
{
if((*s).top (<) (max_stack - 1 ))
{
(*s).top++;
x=rand()%100;
(*s).content[(*s).top]=x;
}
else
printf("Maaf Stack Sudah Penuh");
}
}


//file main.c

#include "header.h"

void main()
{
stack mystack;
char pilih;
infotype newval,a;
int el;
init(&mystack);
do
{
system("cls");
printf("\nMENU");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.Show All");
printf("\n4.Masukkan Bilangan Random");
printf("\n\npilihan : ");
pilih=getch();
switch(pilih)
{
case '1':
printf("\nMasukkan nilai yang akan di push : ");scanf("%d",&newval);
push(&mystack,newval);
break;
case '2':
a=pop(&mystack);
printf("\nIsi stack yang di-POP : %d",a);
getch();
break;
case '3':
printf("\n\n");
show(mystack);
getch();
break;
case '4':
printf("Masukkan banyaknya elemen yang ingin dimasukkan : ");scanf("%d",&el);
random(&mystack,el);
getch();
break;
}
}while(pilih != 27);
}

No comments: