kali ini masbro rezky akan memposting contoh program sequential search yg saya pelajari dan compile melalui devC++...
//sequential search
#include<iostream>
#include <conio.h>
#include<math.h>
#include <windows.h> //standard library buat tipe data HANDLE, fungsi GetStdHandle dan prosedur SetConsoleTextAtrribute
void setcolor(unsigned short color)
{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,color);
}
using namespace std;
int a;
int cari,i;
int cari1;
int data[10] = {101, 102, 201, 301, 302, 401, 500, 602, 699, 777};
int barang[10] = {7, 3, 5, 12, 8, 21, 7, 3, 17, 4};
int main()
{
int kode;
menu:
system("cls");
setcolor(2);
cout << "\nTUGAS PROGRAM PEMANGGILAN ARRAY-SEARCH "<<endl<<endl;
setcolor(7);
cout << "\n Nama : REZKI SURYANA ";
cout << "\n Nim : 2012141855 ";
setcolor(6);
cout << "\n ========================================== ";
setcolor(5);
cout << "\nDaftar Menu : "<<endl;
cout << "\n ========================================== ";
cout << "\n1. Stok Barang";
cout << "\n2. Kode Barang";
cout << "\n3. Exit"<<endl<<endl;
setcolor(6);
cout << "\n ========================================== ";
setcolor(6);
cout << " \nMasukkan pilihan Anda [1..3]"<<endl ;
cin >> kode;
switch (kode)
{
case 1 :
setcolor(6);
cout << "\nStok Barang ";
cout << "\n======================================== "<<endl<<endl;
char pil;
ulang:
system("cls");
cout <<"\nMasukkan Stok barang yang ingin Anda cari = "<<endl;
cin>>cari1;
barang[10] = cari1;
a=0;
while(barang[a] != cari1)
a++;if(a<10)
cout<<"\nStok Barang yang Anda masukkan ada\n";
if(barang[0]== cari1 ){
cout<<"\n Kode Barang = 101 \n";
cout<<"\n Nama Barang = Razer Headset \n";
cout<<"\n Stok Barang = 7 \n";
cout<<"\n Lokasi Barang = T4 \n";
}
else if(barang[1]== cari1 ){
cout<<"\n Kode Barang = 102 \n";
cout<<"\n Nama Barang = DAS Keyboard \n";
cout<<"\n Stok Barang = 3 \n";
cout<<"\n Lokasi Barang = j8 \n";
}
else if(barang[2]== cari1 ){
cout<<"\n Kode Barang = 201 \n";
cout<<"\n Nama Barang = Shenneiser Earphone \n";
cout<<"\n Stok Barang = 5 \n";
cout<<"\n Lokasi Barang = w1 \n";
}
else if(barang[3]== cari1 ){
cout<<"\n Kode Barang = 301 \n";
cout<<"\n Nama Barang = Kingston RAM \n";
cout<<"\n Stok Barang = 12 \n";
cout<<"\n Lokasi Barang = D3 \n";
}
else if(barang[4]== cari1 ){
cout<<"\n Kode Barang = 302 \n";
cout<<"\n Nama Barang = LG Monitor \n";
cout<<"\n Stok Barang = 8 \n";
cout<<"\n Lokasi Barang = G5 \n";
}
else if(barang[5]== cari1 ){
cout<<"\n Kode Barang = 401 \n";
cout<<"\n Nama Barang = Samsung Tab \n";
cout<<"\n Stok Barang = 21 \n";
cout<<"\n Lokasi Barang = X2 \n";
}
else if(barang[6]== cari1 ){
cout<<"\n Kode Barang = 500 \n";
cout<<"\n Nama Barang = Kindle Fire \n";
cout<<"\n Stok Barang = 7 \n";
cout<<"\n Lokasi Barang = U3 \n"; }
else if(barang[7]== cari1 ){
cout<<"\n Kode Barang = 602 \n";
cout<<"\n Nama Barang = The New Ipad \n";
cout<<"\n Stok Barang = 3 \n";
cout<<"\n Lokasi Barang = P7 \n";
} else if(barang[8]== cari1 ){
cout<<"\n Kode Barang = 699 \n";
cout<<"\n Nama Barang = Kindle PaperWhite \n";
cout<<"\n Stok Barang = 17 \n";
cout<<"\n Lokasi Barang = h6 \n";
} else if(barang[9]== cari1 ){
cout<<"\n Kode Barang = 777 \n";
cout<<"\n Nama Barang = Cisco Router \n";
cout<<"\n Stok Barang = 4 \n";
cout<<"\n Lokasi Barang = y2 \n"; }
else
cout<<"\n kode barang yang anda masukkan tidak ada\n"; setcolor(4);
cout<<"\nApakah anda ingin mencari kembali (Y/N) ? "<<endl;
pil=getche();
cout<<endl;
if(pil=='y' || pil =='Y')
goto ulang;
else
goto menu;
break;
case 2:
setcolor(2);
cout << "\nKode Barang ";
cout << "\n======================================== "<<endl<<endl;
char pil1;
ulang1:
system("cls");
cout <<"\nMasukkan kode barang yang ingin Anda cari = "<<endl;
cin>>cari;
data[10] = cari;
i=0;
while(data[i] != cari)
i++;if(i<10)
cout<<"\nKode Barang yang Anda masukkan ada\n"; if(data[0]== cari ){
cout<<"\n Kode Barang = 101 \n";
cout<<"\n Nama Barang = Razer Headset \n";
cout<<"\nStok Barang = 7 \n";
cout<<"\n Lokasi Barang = T4 \n";
} else if(data[1]== cari ){
cout<<"\n Kode Barang = 102 \n";
cout<<"\n Nama Barang = DAS Keyboard \n";
cout<<"\n Stok Barang = 3 \n";
cout<<"\n Lokasi Barang = j8 \n";
}
else if(data[2]== cari ){
cout<<"\n Kode Barang = 201 \n";
cout<<"\n Nama Barang = Shenneiser Earphone \n";
cout<<"\n Stok Barang = 5 \n";
cout<<"\n Lokasi Barang = w1 \n";
} else if(data[3]== cari ){
cout<<"\n Kode Barang = 301 \n";
cout<<"\n Nama Barang = Kingston RAM \n";
cout<<"\n Stok Barang = 12 \n";
cout<<"\n Lokasi Barang = D3 \n";
} else if(data[4]== cari ){
cout<<"\n Kode Barang = 302 \n";
cout<<"\n Nama Barang = LG Monitor \n";
cout<<"\n Stok Barang = 8 \n";
cout<<"\n Lokasi Barang = G5 \n";
} else if(data[5]== cari ){
cout<<"\n Kode Barang = 401 \n";
cout<<"\n Nama Barang = Samsung Tab \n";
cout<<"\n Stok Barang = 21 \n";
cout<<"\n Lokasi Barang = X2 \n";
} else if(data[6]== cari ){
cout<<"\n Kode Barang = 500 \n";
cout<<"\n Nama Barang = Kindle Fire \n";
cout<<"\n Stok Barang = 7 \n";
cout<<"\n Lokasi Barang = U3 \n";
} else if(data[7]== cari ){
cout<<"\n Kode Barang = 602 \n";
cout<<"\n Nama Barang = The New Ipad \n";
cout<<"\n Stok Barang = 3 \n";
cout<<"\n Lokasi Barang = P7 \n";
} else if(data[8]== cari ){
cout<<"\n Kode Barang = 699 \n";
cout<<"\n Nama Barang = Kindle PaperWhite \n";
cout<<"\n Stok Barang = 17 \n";
cout<<"\n Lokasi Barang = h6 \n";
} else if(data[9]== cari ){
cout<<"\n Kode Barang = 777 \n";
cout<<"\n Nama Barang = Cisco Router \n";
cout<<"\n Stok Barang = 4 \n";
cout<<"\n Lokasi Barang = y2 \n";
}
else
cout<<"\nkode barang yang anda masukkan tidak ada\n";
cout<<"\nApakah anda ingin mencari kembali (Y/N) ? "<<endl;
pil1=getche();
cout<<endl;
if(pil1=='y' || pil1 =='Y')
goto ulang1;
else
goto menu;
break;
case 3:
setcolor(9);
char pil2;
cout<<"\n Exit";
cout<<""<<endl;
cout<<"\nAnda Yakin ingin keluar ? (Yes/no)";
pil2= getche();
cout<<endl;
if(pil2=='n' || pil2=='N')
goto menu;
break;
default :
setcolor(9);
char pil3;
cout << "\n Anda salah memasukkan kode !! " ;
cout<<"\nAnda ingin mengulang memasukkan pilihan (Y/N) ? "<<endl;
pil3=getche();
cout<<endl;
if(pil3=='y' || pil3 =='Y')
goto menu;
break ;
}
getch();
system("pause");
return 0;
}
-SEMOGA BERMANFAAT(http://masbronotes.blogspot.com)
No comments:
Post a Comment