Senin, 29 April 2013
Contoh Program Stack
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
main()
{
int plh;
int top=-1, i;
int nim[20], kls[20]; // variabel strack
char nm[20][20]; // variabel strack
do{
clrscr();
cout<<"===================================\n";
cout<<" MENU PILIHAN\n";
cout<<"===================================\n";
cout<<"1.PUSH\n";
cout<<"2.POP\n";
cout<<"3.ISFULL\n";
cout<<"4.ISEMPTY\n";
cout<<"5.CLEAR\n";
cout<<"6.VIEW\n";
cout<<"7.EXIT\n";
cout<<"===================================\n";
cout<<"Masukan pilihan : "; cin>>plh;
switch(plh)
{
case 1: //menu push
if(top==9)
{
clrscr();
cout<<"maaf stack telah penuh";
}
else
{
cout<<"\nMasukan Nama : "; gets(nm[top+1]);
cout<<"Masukan NIM : "; cin>>nim[top+1];
cout<<"Masukan Kelas : "; cin>>kls[top+1];
top++;
}
break;
case 2: //menu pop
if(top<0)
{
clrscr();
cout<<"maaf stack masih kosong";
}
else
{
cout<<"\nData yang dihapus adalah :\n\n";
cout<<"Nama : "<<nm[top]<<endl;
cout<<"NIM : "<<nim[top]<<endl;
cout<<"Kelas : "<<kls[top]<<endl;
top--;
}
getch();
break;
case 3: //menu isfull
if(top==9)
cout<<"Sekarang strack yang ada telah penuh";
else
cout<<"Sekarang strack yang ada masih belum penuh";
getch();
break;
case 4: //menu isempty
if(top==-1)
cout<<"Sekarang strack kosong";
else
cout<<"Sekarang strack tidak kosong";
getch();
break;
case 5: //menu clear
top=-1;
cout<<"strack telah dihapus semua";
getch();
break;
case 6: //menu view
cout<<"NO NAMA NIM KELAS\n";
for(i=top; i>=0; i--)
{
cout<<setiosflags(ios::left)<<setw(5)<<i;
cout<<setiosflags(ios::left)<<setw(8)<<nm[i];
cout<<setiosflags(ios::left)<<setw(8)<<nim[i];
cout<<setiosflags(ios::left)<<setw(10)<<kls[i]<<endl;
}
getch();
break;
case 7: //menu exit
cout<<"\n\nEXIT.........";
getch();
break;
}
}while(plh!=7);
}
Kamis, 25 April 2013
Contoh Program Queue Sederhana
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
#define max 6
typedef struct
{
int head;
int tail;
}mmmm;
mmmm ue;
typedef struct
{
int umr[max];
int tgi[max];
int brt[max];
char *nm[max];
} qqqq;
qqqq qu;
void creat()
{
ue.head=ue.tail=-1;
}
empty()
{
if(ue.tail==-1)
return 1;
else
return 0;
}
full()
{
if(ue.tail==max-1)
return 1;
else
return 0;
}
void en(char *n, int a, int b, int c)
{
if(empty()==1)
{
ue.head=ue.tail=0;
qu.nm[ue.tail]=n;
qu.brt[ue.tail]=a;
qu.umr[ue.tail]=b;
qu.tgi[ue.tail]=c;
}
else if(full()==0)
{
ue.tail++;
qu.nm[ue.tail]=n;
qu.brt[ue.tail]=a;
qu.umr[ue.tail]=b;
qu.tgi[ue.tail]=c;
}
else if(full()==1)
{
cout<<"QUEUE PENUH";
}
}
de()
{
if(empty()==0)
{
int i;
char *nn=qu.nm[ue.tail];
int aa=qu.brt[ue.tail];
int bb=qu.umr[ue.tail];
int cc=qu.tgi[ue.tail];
for(i=ue.head; 1<=ue.tail-1; i++)
{
qu.nm[i]=qu.nm[i+1];
qu.brt[i]=qu.brt[i+1];
qu.umr[i]=qu.umr[i+1];
qu.tgi[i]=qu.tgi[i+1];
}
ue.tail--;
cout<<"DATA YANG DIHAPUS ADALAH : \n";
cout<<"NAMA : "<<nn<<endl;
cout<<"BERAT : "<<aa<<endl;
cout<<"UMUR : "<<bb<<endl;
cout<<"TINGGI : "<<cc<<endl;
}
else if(empty()==1)
{ cout<<"DATA EROR......QUEUE KOSONG"; }
return 1;
}
void clear()
{
ue.head=ue.tail=-1;
cout<<"QUEUE CLEAR";
}
void view()
{
if(empty()==0)
{
cout<<"NO NAMA BERAT UMUR TINGGI\n";
for(int i=ue.head; i<=ue.tail; i++)
{
cout<<setiosflags(ios::left)<<setw(3)<<i;
cout<<setiosflags(ios::left)<<setw(7)<<qu.nm[i];
cout<<setiosflags(ios::left)<<setw(8)<<qu.brt[i];
cout<<setiosflags(ios::left)<<setw(7)<<qu.umr[i];
cout<<setiosflags(ios::left)<<setw(9)<<qu.tgi[i]<<endl;
}
}
else
{ cout<<"NO NAMA BERAT UMUR TINGGI\n"; }
}
main()
{
int menu;
int umr, tgi, brt;
char nm[20];
creat();
do{
clrscr();
cout<<"===========================\n";
cout<<" MENU PROGRAM QUEUE\n";
cout<<"===========================\n";
cout<<"1.ENQUEUE\n";
cout<<"2.DEQUEUE\n";
cout<<"3.CLEAR\n";
cout<<"4.VIEW\n";
cout<<"5.EXIT\n";
cout<<"===========================\n";
cout<<"PILIH MENU : "; cin>>menu;
switch(menu)
{
case 1:
cout<<"MASUKAN NAMA : "; gets(nm);
cout<<"MASUKAN BERAT : "; cin>>brt;
cout<<"MASUKAN UMUR : "; cin>>umr;
cout<<"MASUKAN TINGGI : "; cin>>tgi;
en(nm, brt, umr, tgi);
break;
case 2:
de();
break;
case 3:
clear();
break;
case 4:
view();
break;
case 5:
cout<<"exiiitttt......";
break;
}
getch();
}while(menu!=5);
}
Langganan:
Postingan (Atom)