Contoh Program Supermarket C++
#include <iostream.h>#include <conio.h>
#include <stdio.h>
garis()
{
cout<<"==============================================================================================="<<endl;
}
struct nilai
{
char nama[40];
char barang[20];
char swap[40];
char swap1[40];
int jumlah, harga, total, kode;
}
data[30];
int u=1;
int i=0;
int simpan, pilih;
void insert();
void view();
void sort();
void search();
void delet();
void main()
{
do
{
clrscr();
cout<<"Menu"<<endl;
cout<<"1. Insert Data (PUSH/ENQUEUE)"<<endl;
cout<<"2. View Data"<<endl;
cout<<"3. Sorting"<<endl;
cout<<"4. Searching"<<endl;
cout<<"5. Delete"<<endl;
cout<<"6. Exit"<<endl<<endl;
cout<<"Masukkan Pilihan Anda : ";
cin>>pilih;
switch(pilih)
{
case 1:
{
insert();
break;
}
case 2:
{
view();
break;
}
case 3:
{
sort();
break;
}
case 4:
{
search();
getch();
}
case 5:
{
delet();
break;
}
case 9:
{
u=2;
}
}
} while(u==1);
}
void insert()
{
clrscr();
cout<<"Masukkan Kode Transaksi \t : ";
cin>>data[i].kode;
cout<<"Masukkan Nama Pelanggan \t : ";
gets(data[i].nama);
cout<<"Masukkan Barang Belian \t\t : ";
gets(data[i].barang);
cout<<"Harga Barang \t\t\t : ";
cin>>data[i].harga;
cout<<"Banyak Barang \t\t\t : ";
cin>>data[i].jumlah;
data[i].total=(data[i].harga*data[i].jumlah);
cout<<"Total Harga \t\t\t : "<<data[i].total<<endl;
i++;
getch();
}
void view()
{
clrscr();
if(i==0)
{
cout<<"Data kosong";
}
else
{
garis();
cout<<"| No | Kode\t| Nama\t\t| Barang\t| Jumlah\t| Harga\t\t| Total\t\t|"<<endl;
garis();
for(int j=0; j<i; j++)
{
cout<<"| "<<j+1<<" | "<<data[j].kode<<"\t| "<<data[j].nama<<"\t| "<<data[j].barang<<"\t| "<<data[j].jumlah<<"\t\t| "<<data[j].harga<<"\t\t| "<<data[j].total<<"\t\t|"<<endl;
}
}
getch();
}
void sort()
{
clrscr();
cout<<"1. Bubble Sort"<<endl;
cout<<"2. Exchange Sort"<<endl;
cout<<"3. Selection Sort"<<endl;
cout<<"4. Insertion Sort"<<endl;
cout<<"Masukkan Pilihan Anda : ";
cin>>pilih;
switch(pilih)
{
case 1:
{
for(int k=1;k<i;k++)
{
for(int l=i-1;l>=k;l--)
{
if(data[l].total<data[l-1].total) //tukar(l,l-1);
{
//ganti nama
strcpy(data[l].swap,data[l-1].nama);
strcpy(data[l-1].nama,data[l].nama);
strcpy(data[l].nama,data[l].swap);
//ganti barang
strcpy(data[l].swap,data[l-1].barang);
strcpy(data[l-1].barang,data[l].barang);
strcpy(data[l].barang,data[l].swap);
float t;
t = data[l].total;
data[l].total = data[l-1].total;
data[l-1].total = t;
t = data[l].harga;
data[l].harga = data[l-1].harga;
data[l-1].harga = t;
t = data[l].jumlah;
data[l].jumlah = data[l-1].jumlah;
data[l-1].jumlah = t;
t = data[l].kode;
data[l].kode = data[l-1].kode;
data[l-1].kode = t;
}
}
}
getch();
break;
}
case 2:
{
for (int k=0; k<i-1; k++)
{
for(int l = (k+1); l<i; l++)
{
if (data[k].total > data[l].total) //tukar(k,l);
{
strcpy(data[k].swap,data[l].nama);
strcpy(data[l].nama,data[k].nama);
strcpy(data[k].nama,data[k].swap);
//ganti barang
strcpy(data[k].swap,data[l].barang);
strcpy(data[l].barang,data[k].barang);
strcpy(data[k].barang,data[k].swap);
float t;
t = data[k].total;
data[k].total = data[l].total;
data[l].total = t;
t = data[k].harga;
data[k].harga = data[l].harga;
data[l].harga = t;
t = data[k].jumlah;
data[k].jumlah = data[l].jumlah;
data[l].jumlah = t;
t = data[k].kode;
data[k].kode = data[l].kode;
data[l].kode = t;
}
}
}
}
case 3:
{
int pos,k,l;
for(k=0;k<i-1;k++)
{
pos = k;
for(l = k+1;l<i;l++)
{
if(data[l].total < data[pos].total)
{
pos=l;
}
}
if(data[pos].total != data[k].total)
{
//ganti nama
strcpy(data[k].swap,data[pos].nama);
strcpy(data[pos].nama,data[k].nama);
strcpy(data[k].nama,data[k].swap);
//ganti barang
strcpy(data[k].swap,data[pos].barang);
strcpy(data[pos].barang,data[k].barang);
strcpy(data[k].barang,data[k].swap);
float t;
t = data[k].total;
data[k].total = data[pos].total;
data[pos].total = t;
t = data[k].harga;
data[k].harga = data[pos].harga;
data[pos].harga = t;
t = data[k].jumlah;
data[k].jumlah = data[pos].jumlah;
data[pos].jumlah = t;
t = data[k].kode;
data[k].kode = data[pos].kode;
data[pos].kode = t;
}
}
getch();
break;
}
case 4:
{
int temp, temp1, temp2, temp3,k,l;
for(k=1;k<i;k++)
{
temp=data[k].kode;
temp1=data[k].total;
temp2=data[k].harga;
temp3=data[k].jumlah;
strcpy(data[k].swap,data[k].nama);
strcpy(data[k].swap1,data[k].barang);
cout<<data[k].swap;
l = k-1;
while((data[l].total>temp1) && l>=0)
{
data[l+1].kode=data[l].kode;
data[l+1].total=data[l].total;
data[l+1].harga=data[l].harga;
data[l+1].jumlah=data[l].jumlah;
strcpy(data[l+1].nama,data[l].nama);
strcpy(data[l+1].barang,data[l].barang);
l--;
}
data[l+1].kode=temp;
data[l+1].total=temp1;
data[l+1].harga=temp2;
data[l+1].jumlah=temp3;
strcpy(data[l+1].nama,data[k].swap);
strcpy(data[l+1].barang,data[k].swap1);
}
getch();
break;
}
}
}
void delet()
{
clrscr();
cout<<"1. POP"<<endl;
cout<<"2. DEQUEUE"<<endl;
cout<<"3. Default"<<endl;
cout<<"Masukkan Pilihan Anda : ";
cin>>pilih;
switch(pilih)
{
case 1:
{
i--;
break;
}
case 2:
{
int pos,k;
for(k=0;k<=i;k++)
{
data[k].kode = data[k+1].kode;
data[k].jumlah = data[k+1].jumlah;
data[k].total = data[k+1].total;
data[k].harga = data[k+1].harga;
strcpy(data[k].nama,data[k+1].nama);
strcpy(data[k].barang,data[k+1].barang);
}
i--;
break;
}
case 3:
{
clrscr();
int pil;
cout<<"Masukkan Index yang akan dihapus : ";
cin>>pil;
int pos,k,l;
for(k=pil;k<=i;k++)
{
data[k].kode = data[k+1].kode;
data[k].jumlah = data[k+1].jumlah;
data[k].total = data[k+1].total;
data[k].harga = data[k+1].harga;
strcpy(data[k].nama,data[k+1].nama);
strcpy(data[k].barang,data[k+1].barang);
}
i--;
getch();
break;
}
}
}
void search()
{
clrscr();
cout<<"1. Sequence Search"<<endl;
cout<<"2. Binary Search"<<endl;
cout<<"Masukkan Pilihan Anda : ";
cin>>pilih;
switch(pilih)
{
case 1:
{
int cari;
clrscr();
cout<<" Input Kode yang Dicari : ";
cin>>cari;
garis();
cout<<"| No | Kode\t| Nama\t\t| Barang\t| Jumlah\t| Harga\t\t| Total\t\t|"<<endl;
garis();
for(int j=0; j<i; j++)
{
if(data[j].kode==cari)
{
cout<<"| "<<j+1<<" | "<<data[j].kode<<"\t| "<<data[j].nama<<"\t| "<<data[j].barang<<"\t| "<<data[j].jumlah<<"\t\t| "<<data[j].harga<<"\t\t| "<<data[j].total<<"\t\t|"<<endl;
}
}
getch();
break;
}
case 2:
{
int cari;
clrscr();
cout<<" Input Kode yang Dicari : ";
cin>>cari;
int kiri=0;
int kanan=i-1;
int tengah=(kanan-kiri)/2;
for(int k=1;k<i;k++)
{
for(int l=i-1;l>=k;l--)
{
if(data[l].total<data[l-1].total) //tukar(l,l-1);
{
//ganti nama
strcpy(data[l].swap,data[l-1].nama);
strcpy(data[l-1].nama,data[l].nama);
strcpy(data[l].nama,data[l].swap);
//ganti barang
strcpy(data[l].swap,data[l-1].barang);
strcpy(data[l-1].barang,data[l].barang);
strcpy(data[l].barang,data[l].swap);
float t;
t = data[l].total;
data[l].total = data[l-1].total;
data[l-1].total = t;
t = data[l].harga;
data[l].harga = data[l-1].harga;
data[l-1].harga = t;
t = data[l].jumlah;
data[l].jumlah = data[l-1].jumlah;
data[l-1].jumlah = t;
t = data[l].kode;
data[l].kode = data[l-1].kode;
data[l-1].kode = t;
}
}
}
garis();
cout<<"| No | Kode\t| Nama\t\t| Barang\t| Jumlah\t| Harga\t\t| Total\t\t|"<<endl;
garis();
if(cari<data[tengah].kode)
{
for(int j=tengah; j>0; j--)
{
if(data[j].kode==cari)
{
cout<<"| "<<j+1<<" | "<<data[j].kode<<"\t| "<<data[j].nama<<"\t| "<<data[j].barang<<"\t| "<<data[j].jumlah<<"\t\t| "<<data[j].harga<<"\t\t| "<<data[j].total<<"\t\t|"<<endl;
}
}
}
if(cari>data[tengah].kode)
{
for(int j=tengah; j<i; j++)
{
if(data[j].kode==cari)
{
cout<<"| "<<j+1<<" | "<<data[j].kode<<"\t| "<<data[j].nama<<"\t| "<<data[j].barang<<"\t| "<<data[j].jumlah<<"\t\t| "<<data[j].harga<<"\t\t| "<<data[j].total<<"\t\t|"<<endl;
}
}
}
else
{
clrscr();
cout<<"Data tidak ditemukan";
getch();
}
break;
}
}
}
0 komentar:
Posting Komentar