#include <conio.h>
#include <cstdlib>
using namespace std;
struct surat
{
int kodepos;
string pengirim, penerima, kota;
};
surat suratku[5];
int a, b, s, total, temp_POS, key, pos;
string kirim, nerima, kot;
void menu()
{
cout<<"\n Pilih yang ingin anda lakukan\n"<<endl;
cout<<" ======================"<<endl;
cout<<"|| [1] Input data ||"<<endl;
cout<<"|| [2] Buble Sort ||"<<endl;
cout<<"|| [3] Exchange Sort ||"<<endl;
cout<<"|| [4] Selection Sort ||"<<endl;
cout<<"|| [5] Insertion Sort ||"<<endl;
cout<<"|| [6] Exit ||"<<endl;
cout<<" ======================"<<endl;
cout<<" ======================>> ";
}
void garis()
{
for(s=0;s<60;s++)
{
cout<<"=";
}
cout<<"\n";
}
void garis_kecil()
{
for(s=0;s<60;s++)
{
cout<<"-";
}
cout<<"\n";
}
int input()
{
cout<<"Berapa banyaknya data pengiriman POS ? "; cin>>total;
system("cls");
cin.ignore();
cout<<"\nInputkan Datanya! \n\n";
for(a=0;a<total;a++)
{
garis();
cout<<"\nInput data POS ke "<<a+1<<"\n\n";
cout<<"Kode POS : ";
cin>>suratku[a].kodepos;
cout<<"Pengirim : ";
cin>>suratku[a].pengirim;
cout<<"Penerima : ";
cin>>suratku[a].penerima;
cout<<"Kota : ";
cin>>suratku[a].kota;
cout<<"\n";
garis();
}
cout<<"\n\nPress enter to continue . . .";
getch();
}
void kodePOS()
{
for(a=0;a<total;a++)
{
cout<<suratku[a].kodepos<<" ";
}
cout<<"\n";
}
void buble()
{
kodePOS();
for(a=0;a<total-1;a++)
{
for(b=0;b<total-1;b++)
{
if(suratku[b].kodepos>suratku[b+1].kodepos)
{
temp_POS=suratku[b+1].kodepos;
suratku[b+1].kodepos=suratku[b].kodepos;
suratku[b].kodepos=temp_POS;
kirim=suratku[b+1].pengirim;
suratku[b+1].pengirim=suratku[b].pengirim;
suratku[b].pengirim=kirim;
nerima=suratku[b+1].penerima;
suratku[b+1].penerima=suratku[b].penerima;
suratku[b].penerima=nerima;
kot=suratku[b+1].kota;
suratku[b+1].kota=suratku[b].kota;
suratku[b].kota=kot;
}
}
for(int a=0;a<total;a++)
{
cout<<suratku[a].kodepos<<" ";
}
cout<<"\n";
}
}
void exchange()
{
kodePOS();
for(a=0;a<total-1;a++)
{
for(b=a+1;b<total;b++)
{
if(suratku[a].kodepos>suratku[b].kodepos)
{
temp_POS=suratku[b].kodepos;
suratku[b].kodepos=suratku[a].kodepos;
suratku[a].kodepos=temp_POS;
kirim=suratku[b].pengirim;
suratku[b].pengirim=suratku[a].pengirim;
suratku[a].pengirim=kirim;
nerima=suratku[b].penerima;
suratku[b].penerima=suratku[a].penerima;
suratku[a].penerima=nerima;
kot=suratku[b].kota;
suratku[b].kota=suratku[a].kota;
suratku[a].kota=kot;
}
}
for(int a=0;a<total;a++)
{
cout<<suratku[a].kodepos<<" ";
}
cout<<"\n";
}
}
void selection()
{
kodePOS();
for(a=0;a<total-1;a++)
{
pos=a;
for(b=a+1;b<total;b++)
{
if(suratku[b].kodepos<suratku[pos].kodepos)
{
pos=b;
}
if(pos!=a)
{
temp_POS=suratku[pos].kodepos;
suratku[pos].kodepos=suratku[a].kodepos;
suratku[a].kodepos=temp_POS;
kirim=suratku[pos].pengirim;
suratku[pos].pengirim=suratku[a].pengirim;
suratku[a].pengirim=kirim;
nerima=suratku[pos].penerima;
suratku[pos].penerima=suratku[a].penerima;
suratku[a].penerima=nerima;
kot=suratku[pos].kota;
suratku[pos].kota=suratku[a].kota;
suratku[a].kota=kot;
}
}
for(int a=0;a<total;a++)
{
cout<<suratku[a].kodepos<<" ";
}
cout<<"\n";
}
}
void insertion()
{
kodePOS();
for(a=0;a<total;a++)
{
key=suratku[a].kodepos;
kirim=suratku[a].pengirim;
nerima=suratku[a].penerima;
kot=suratku[a].kota;
b=a-1;
while(b>=0 && suratku[b].kodepos>key)
{
suratku[b+1].kodepos=suratku[b].kodepos;
suratku[b+1].pengirim=suratku[b].pengirim;
suratku[b+1].penerima=suratku[b].penerima;
suratku[b+1].kota=suratku[b].kota;
b=b-1;
suratku[b+1].kodepos=key;
suratku[b+1].pengirim=kirim;
suratku[b+1].penerima=nerima;
suratku[b+1].kota=kot;
}
for(int a=0;a<total;a++)
{
cout<<suratku[a].kodepos<<" ";
}
cout<<"\n";
}
}
void output()
{
cout<<"\t\t\tData Pengiriman POS \n\n";
garis();
cout<<"\n";
for(a=0;a<total;a++)
{
garis_kecil();
cout<<"\nData POS ke "<<a+1<<"\n";
cout<<"\nKode POS : "<<suratku[a].kodepos;
cout<<"\nPengirim : "<<suratku[a].pengirim;
cout<<"\nPenerima : "<<suratku[a].penerima;
cout<<"\nKota : "<<suratku[a].kota;
cout<<"\n\n";
garis_kecil();
cout<<"\n";
}
garis();
cout<<"\nPress enter to continue . . .\n\n";
getch();
}
void awal()
{
cout<<"\nsebelum di- sorting :\n\n";
output();
cout<<"\nProses pengurutan kode POS : \n\n";
}
void akhir()
{
cout<<"\nHasil sorting :\n\n";
output();
}
int main()
{
int pilih;
balik:
system("Title Program Sorting Data By: Hadi ");
system("color 8");
salah:
menu();
cin>>pilih;
system("cls");
if(pilih==1)
{
system("Title Program Sorting Data (INPUT)");
system("color f");
input(); system("cls"); goto balik;
}
else if(pilih==2)
{
if(total==0)
{
cout<<"Data masih kosong!\n\nInput data terlebih dahulu!\n\npilih 1. Input Data !!\n\n";
goto balik;
}
system("Title Program Sorting Data (Buble Sort)");
system("color e"); cout<<"=> BUble Sort <=\n\n";
awal(); buble(); akhir(); system("cls"); goto balik;
}
else if(pilih==3)
{
if(total==0)
{
cout<<"Data masih kosong!\n\nInput data terlebih dahulu!\n\npilih 1. Input Data !!\n\n";
goto balik;
}
system("Title Program Sorting Data (Exchange Sort)");
system("color a"); cout<<"=> Exchange Sort <=\n\n";
awal(); exchange(); akhir(); system("cls"); goto balik;
}
else if(pilih==4)
{
if(total==0)
{
cout<<"Data masih kosong!\n\nInput data terlebih dahulu!\n\npilih 1. Input Data !!\n\n";
goto balik;
}
system("Title Program Sorting Data (Selection Sort)");
system("color b"); cout<<"=> Selection Sort <=\n\n";
awal(); selection(); akhir(); system("cls"); goto balik;
}
else if(pilih==5)
{
if(total==0)
{
cout<<"Data masih kosong!\n\nInput data terlebih dahulu!\n\npilih 1. Input Data !!\n\n";
goto balik;
}
system("Title Program Sorting Data (Insertion Sort)");
system("color d"); cout<<"=> Insertion Sort <=\n\n";
awal(); insertion(); akhir(); system("cls"); goto balik;
}
else if(pilih==6)
{
system("color 1");
cout<<"\nSekian dari Program Data Pengiriman POS :D"<<endl;
getch();
}
else
{
system("Title Program Data Pengiriman POS (REPEAT)");
system("color c");
cout<<"\n --Kode yang Anda masukkan salah-- "<<endl;
cout<<"\n --Input lagi !-- \n"<<endl;
goto salah;
}
}
0 Comments