Minggu, 29 Juni 2014

Contoh Program Queue (antrian bank) C++



Source code:
#include <iostream.h>
#include <conio.h>
#define MAX 6

typedef struct{
int data[MAX];
int head;
int tail;
}
Queue;
Queue antrian;
void Create(){
antrian.head=antrian.tail=-1;
}

int IsEmpty(){
if(antrian.tail==-1)
return 1;
else
return 0;
}

int IsFull(){
if (antrian.tail==MAX-1) return 1;
else return 0;
}

//fungsi memasukkan data

void Enqueue(int data){
if(IsEmpty()==1){
antrian.head=antrian.tail=0;
antrian.data[antrian.tail]=data;

cout << "\t Silahkan Masuk !! \n" ;
cin >> antrian.data[antrian.tail];
void Tampil();
{
if(IsEmpty()==0){
for(int i=antrian.head;i<=antrian.tail;i++){
cout << antrian.data[i];
}
}else
cout<< "data kosong!";
};
}else
if(IsFull()==0){
antrian.tail++;
antrian.data[antrian.tail]=data;
cout << "\t Silahkan Masuk !! \n" << antrian.data[antrian.tail];
}

}

int Dequeue(){
int i;
int e = antrian.data[antrian.head];
for(i=antrian.head;i<=antrian.tail-1;i++){
antrian.data[i]= antrian.data[i+1];
}
antrian.tail--;
return e;
}

void clear(){
antrian.head=antrian.tail=-1;
cout << "Data Clear" ;
}
void Tampil()
{ int jum;
jum=0;

if(IsEmpty()==0){
for(int i=antrian.head;i<=antrian.tail;i++){
jum++;

cout << antrian.data[i]<< "\n";
}
cout << "\njumlah yang mengantri saat ini : "<< jum << "orang";
}
else
cout << "Data Kosong!\n";
}

//sebagai nasabah
void nasabah()
{
int data;
char lagi;
do{
clrscr();
cout << "\t+=================================+\n";
cout << "\t| menu nasabah |\n";
cout << "\t+=================================|\n";
cout << "\t| tekan (a) untuk masukkan data |\n";
cout << "\t| tekan (b) melihat data antrian |\n";
cout << "\t| tekan (x) keluar |\n";
cout << "\t+=================================|\n";
cout << "pilihan : ";
cin >> lagi;
if((lagi=='x') || (lagi=='X'))
{ cout << "terima kasih\n" ;

break;
}

switch(lagi)
{
case 'a':
cout << "masukkan data: " << &data << " ";
Enqueue(data);
break;
case 'b': Tampil();
break;

}getch();
} while(lagi !='x');
getch();
}

//memasukkan password
void password()
{
char nama[20], id[10], password[4];
int i;
{
clrscr();

atas:
clrscr();
cout << "\t\t+===============================================+\n";
cout << "\t\t| S E L A M A T D A T A N G |\n";
cout << "\t\t| jika anda admin dari teller |\n";
cout << "\t\t|Silahkan masukkan User Name & 3 digit Password |\n";
cout << "\t\t+===============================================+\n";
cout << "\t\t UserName : " ; cin>>nama;
cout << "\t\t No. Identitas : "; cin>>id;
cout << "\t\t Password : " ;
for(i=0; i<=2; i++)
{password[i]=getch();
cout << "\bX";
}
cout << "\n\t\t+=============================================+\n";
if (strcmp(password,"aaa")==0)
{
cout << "\n\n\t\t Anda Masuk Sebagai TEllER\n";
cout << "\t\t User :" << nama << "\n";
cout << "\t\t No. Identitas : " << id << "\n";
cout << "\n\n\t\tTekan Sembarang tombol untuk ke menu teller..!";
goto bawah;
}
else
{
cout << "\t\t\tmaaf password salah!!!\n" ;
cout << "\t\tketik sembarang tombol untuk kembali ke menu: ";
goto atas;

}
bawah:
getch();
}
}

//void teller
void teller()
{char menu;
password();
do{
clrscr();
cout << "\t\t+=================================+\n";
cout << "\t\t| menu pilihan untuk TELLER |\n";
cout << "\t\t+=================================+\n";
cout << "\t\t| tekan (a) untuk menghapus data |\n";
cout << "\t\t| tekan (b) melihat data antrian |\n";
cout << "\t\t| tekan (c) untuk mereset |\n";
cout << "\t\t| tekan (x) untuk keluar |\n";
cout << "\t\t+=================================+\n";
cout << "pilihan : " ;cin>> menu;
if((menu=='x') || (menu=='X'))
{ cout << "terima kasih\n";

break;
}

switch(menu)
{
case 'a': cout << "Elemen yang keluar : ";
Dequeue();
break;
case 'b': Tampil();
break;
case 'c': clear();
break;
}getch();
} while(menu !='x');

}

void main(){
char pil;

Create();
do{
clrscr();
//fflush(stdin);
cout << "\t\t+=====================================+\n";
cout << "\t\t| Bank Mandiri |\n";
cout << "\t\t| jalan Menukan |\n";
cout << "\t\t| Yoggyakarta |\n";
cout << "\t\t+=====================================+\n";
cout << "\t\t| tekan (n) untuk Nasabah |\n";
cout << "\t\t| tekan (t) untuk Teller |\n";
cout << "\t\t| tekan (k) keluar dari program |\n";
cout << "\t\t+=====================================+\n";
cout << "pilihan : ";
cin >> pil;
if((pil=='k') || (pil=='k'))
{ cout << "terima kasih";
break;
}

switch(pil)
{
case 'n': nasabah();
break;
case 't': teller();
break;

}

}while(pil!='k');
}

2 komentar:

  1. mengapa cout dan cin pada baris 37 tidak dapat terbaca, dan tertulis, cout tidak dideklaraikan

    BalasHapus
  2. Kenapa pas disalin error, gak bisa dijalankan ya?

    BalasHapus