Wednesday, October 10, 2007

Jawaban UTS

Beginilah jawaban saya
he,,,,mohon petunjuk dari pembaca, soalnya saya masih pemula dalam soal mogram
//file Header.h
#include
#include
#include
#include
#define MAX_STACK 20
#define MAX_HEIGHT 10
typedef char String[5];
typedef struct
{
float panjang;
float lebar;
float tinggi;
String ID;
}Kotak;

typedef struct
{
int top;
Kotak content[MAX_STACK];
}StackKotak;

void CreateStack(StackKotak *S);
int IsEmpty(StackKotak S);
int IsEmptyKotak(Kotak k);
int IsFull(StackKotak S);
void PUSH(StackKotak *S, Kotak D);
Kotak POP(StackKotak *S);
float getTotalTinggi(StackKotak S);
float getVolume(Kotak D);
void ShowAll(StackKotak S);


//file function.c
#include "Header.h"
void CreateStack(StackKotak *S)
{
(*S).top = -1;
}
int IsEmpty(StackKotak S)
{
return (S).top==-1;
}
int IsEmptyKotak(Kotak k)
{
return k.lebar==0&&k.panjang==0&&k.lebar==0&&strcmpi(k.ID,"")==0;
}
int IsFull(StackKotak S)
{
return getTotalTinggi(S)==10;
}

float getVolume(Kotak D)
{
return (D.panjang*D.lebar*D.tinggi);
}
float getTotalTinggi(StackKotak S)
{
float temp=0.0;
int i;
for (i=0;i<=(S).top;i++) { temp +=(S).content[i].tinggi; } return (temp); } void PUSH(StackKotak *S, Kotak D) { if(IsEmpty(*S)) { (*S).top++; (*S).content[(*S).top] = D; printf("\nOK..."); } else { if ( (!IsFull(*S) ) && (getTotalTinggi(*S) <= (MAX_HEIGHT - D.tinggi)) && (getVolume((*S).content[(*S).top]) > getVolume(D)))
{
(*S).top++;
(*S).content[(*S).top] = D;
printf("\nOK...");
}
else if (getTotalTinggi(*S) > (MAX_HEIGHT - D.tinggi))
printf ("Stack gagal di masukkan\n karena Tinggi pucuk kotak sudah melebihi 10 meter");
else
printf("Stack gagal di masukkan\n karena volumenya terlalu besar");
}
}
Kotak POP(StackKotak *S)
{
Kotak a;
strcpy(a.ID,"");
a.lebar=0;
a.panjang=0;
a.tinggi=0;
if ((*S).top > -1)
{
(*S).top--;
return (*S).content[(*S).top+1];
}
else
{
return a;
}
}

void ShowAll(StackKotak S)
{
if (S.top == -1)
printf("\nStack kosong...");
else
{
int i;
for(i = S.top; i >= 0; i--)
{
printf("\n ID : %s", (S).content[i].ID);
printf("\n Panjang : %.2f",(S).content[i].panjang);
printf("\n Lebar : %.2f", (S).content[i].lebar);
printf("\n Tinggi : %.2f", (S).content[i].tinggi);

printf("\n");
}

}
}

//file function.c
#include "Header.h"

void main()
{
int pilih;
Kotak MyKotak;
StackKotak MyStack;
CreateStack(&MyStack);
do
{
system("cls");
printf("---MENU---");
printf("\n1. Push kotak");
printf("\n2. Pop kotak");
printf("\n3. Show Stack");
printf("\n\n Masukkan Pilihan : ");
pilih = getche();
switch (pilih)
{
case '1' :
{
printf("\nMasukkan ID kotak : ");scanf("%s",(MyKotak.ID));
printf("\nMasukkan Panjang kotak (Meter): ");scanf("%f",&MyKotak.panjang);
printf("\nMasukkan Lebar kotak (Meter): ");scanf("%f",&MyKotak.lebar);
printf("\nMasukkan Tinggi kotak (Meter): ");scanf("%f",&MyKotak.tinggi);
PUSH(&MyStack, MyKotak);
getch();
break;
}
case '2' :
{
MyKotak=POP(&MyStack);
if(!IsEmptyKotak(MyKotak))
{
printf("\nKotak yang di-pop : ");
printf("\n ID : %s", MyKotak.ID);
printf("\n Panjang : %.2f",MyKotak.panjang);
printf("\n Lebar : %.2f", MyKotak.lebar);
printf("\n Tinggi : %.2f", MyKotak.tinggi);
}
else
{
printf("\n Stack kosong...");
}
getch();
break;
}
case '3':
{
ShowAll(MyStack);
getch();
break;
}
}
} while (pilih != 27);
}

No comments: