source kode program sorting C++
#include <iostream>#include <conio.h>
#include <iomanip>
#include <stdlib.h>
using namespace std;
struct datad
{
string nama;
int id;
string jenis;
void input (datad a[],datad b[], int k,int l,int r)
{
if(r==1)
{
for (int i=0; i<k; i++)
{
cin.ignore();
cout<<"Nama = ";getline(cin,a[i].nama);
cout<<"ID = ";cin>>a[i].id;
}
}
else
{
for (int i=0; i<l; i++)
{
cin.ignore();
cout<<"Nama makanan = ";getline(cin,b[i].nama);
cout<<"ID = ";cin>>b[i].id;
cin.ignore();
cout<<"Jenis makanan = ";getline(cin,b[i].jenis);
}
}
}
};
void menu()
{
cout<<"----------------------------------\n";
cout<<" menu \n";
cout<<"----------------------------------\n";
cout<<"1. Input data koki\n";
cout<<"2. Input data makanan\n";
cout<<"3. Output data koki\n";
cout<<"4. Output data makanan\n";
cout<<"5. Sorting data koki menurut id\n";
cout<<"6. Sorting data makanan menurut id\n";
cout<<"7. Edit data koki\n";
cout<<"8. Edit data makanan\n";
cout<<"9. Keluar\n\n";
cout<<"Pilihan = ";
}
void output (datad a[],datad b[], int k,int l,int r)
{
if(r==1)
{
cout<<endl<<setw(20)<<left<<"Nama"<<setw(10)<<left<<"ID"<<endl;
for (int i=0; i<k; i++)
{
cout<<setw(20)<<left<<a[i].nama<<setw(10)<<left<<a[i].id<<a[i].jenis<<endl;
}
}
else
{
cout<<endl<<setw(20)<<left<<"Nama makanan"<<setw(10)<<left<<"ID"<<"Jenis makanan"<<endl;
for (int i=0; i<l; i++)
{
cout<<setw(20)<<left<<b[i].nama<<setw(10)<<left<<b[i].id<<b[i].jenis<<endl;
}
}
}
void sorting (datad a[],datad b[], int k,int l,int r)
{
datad temp;
if(r==1)
{
for(int i=0; i<k; i++)
{
for(int j=0; j<k; j++)
{
if (a[i].id<a[j].id)
{
a[i].nama.swap (a[j].nama);
temp.id=a[i].id;
a[i].id=a[j].id;
//a[j].id=temp.id;
}
}
}
}
else
{
for(int i=0; i<l; i++)
{
for(int j=0; j<l; j++)
{
if (b[i].id<b[j].id)
{
b[i].nama.swap (b[j].nama);
temp.id=b[i].id;
b[i].id=b[j].id;
b[j].id=temp.id;
}
}
}
}
}
void edit (datad a[],datad b[], int k,int f,int r)
{
int asd;
cout<<"Masukkan ID yang ingin diedit = ";cin>>asd;
if(r==1)
{
for(int i=0;i<k;i++)
{
if(asd==a[i].id)
{
int l;
cout<<"\n\n1.Nama\n";
cout<<"2.ID\n\n";
cout<<"pilih = ";cin>>l;
cin.ignore();
system("cls");
if(l==1)
{
cout<<"Nama = ";
getline(cin,a[i].nama);
}
else if(l==2)
{
cout<<"ID = ";
cin>>a[i].id;
}
}
}
}
else
{
for(int i=0;i<f;i++)
{
if(asd==b[i].id)
{
int l;
cout<<"\n\n1.Nama\n";
cout<<"2.ID\n";
cout<<"3.jenis makanan\n\n";
cout<<"pilih = ";cin>>l;
system("cls");
cin.ignore();
if(l==1)
{
cout<<"Nama makanan = ";
getline(cin,b[i].nama);
}
else if(l==2)
{
cout<<"ID = ";
cin>>b[i].id;
}
else if(l==3)
{
cout<<"Jenis makanan = ";
getline(cin,b[i].jenis);
}
}
}
}
}
main()
{
datad dat;
int p,kd=0,ko=0;
datad koki[50];
datad id[50];
l:
system("cls");
menu();
cin>>p;
switch (p)
{
case 1:
{
system("cls");
cout<<"Banyak Data = ";
cin>>kd;
system("cls");
dat.input(koki,id,kd,ko,1);
goto l;
}
case 2:
{
system("cls");
cout<<"Banyak Data = ";
cin>>ko;
system("cls");
dat.input(koki,id,kd,ko,2);
goto l;
}
case 3:
{
output(koki,id,kd,ko,1);
getch();
goto l;
}
case 4:
{
output(koki,id,kd,ko,2);
getch();
goto l;
}
case 5:
{
sorting(koki,id,kd,ko,1);
output(koki,id,kd,ko,1);
cout<<endl<<endl;
getch();
goto l;
}
case 6:
{
sorting(koki,id,kd,ko,2);
output(koki,id,kd,ko,2);
cout<<endl<<endl;
getch();
goto l;
}
case 7:
{
output(koki,id,kd,ko,1);
cout<<endl<<endl;
edit(koki,id,kd,ko,1);
goto l;
}
case 8:
{
output(koki,id,kd,ko,2);
cout<<endl<<endl;
edit(koki,id,kd,ko,2);
goto l;
}
case 9:
{
cout<<"Selesai";
break;
}
}
}
silahkan copy dan pasti kode program tersebut pada compiler anda(ketik ulang biar makin ngerti
) dan jika ada pertanyaan seputar kodingan diatas silahkan bertanya pada kolom komentar.
Tidak ada komentar:
Posting Komentar