Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <string.h>
- using namespace std;
- struct data_user{
- char nama[50];
- int umur;
- };
- data_user user[100], temp;
- void input_data(int posisi){
- cin.ignore();
- cout<<"Masukan Nama = ";
- cin.getline(user[posisi].nama, 50);
- cout<<"Masukan Umur = ";
- cin>>user[posisi].umur;
- }
- void sorting_name_asc(int posisi){
- int huruf1, huruf2;
- for(int a=0; a<posisi; a++){
- for(int b=a+1; b<posisi; b++){
- huruf1 = int(user[a].nama[0]);
- huruf2 = int(user[b].nama[0]);
- if(huruf1 < 97)
- huruf1 = huruf1 + 32;
- if(huruf2 < 97)
- huruf2 = huruf2 + 32;
- if(huruf1 > huruf2 ){
- strcpy(temp.nama, user[a].nama);
- temp.umur = user[a].umur;
- strcpy(user[a].nama, user[b].nama);
- user[a].umur = user[b].umur;
- strcpy(user[b].nama, temp.nama);
- user[b].umur = temp.umur;
- }
- else if(huruf1 == huruf2 ){
- int arr = 0;
- while(true){
- huruf1 = int(user[a].nama[arr]);
- huruf2 = int(user[b].nama[arr]);
- if(huruf1 < 97)
- huruf1 = huruf1 + 32;
- if(huruf2 < 97)
- huruf2 = huruf2 + 32;
- if(huruf1 > huruf2){
- strcpy(temp.nama, user[a].nama);
- temp.umur = user[a].umur;
- strcpy(user[a].nama, user[b].nama);
- user[a].umur = user[b].umur;
- strcpy(user[b].nama, temp.nama);
- user[b].umur = temp.umur;
- break;
- }
- if(huruf1 < huruf2)
- break;
- arr++;
- }
- }
- if(b == posisi-1){
- break;
- }
- }
- }
- }
- void sorting_name_desc(int posisi){
- int huruf1, huruf2;
- for(int a=0; a<posisi; a++){
- for(int b=a+1; b<posisi; b++){
- huruf1 = int(user[a].nama[0]);
- huruf2 = int(user[b].nama[0]);
- if(huruf1 < 97)
- huruf1 = huruf1 + 32;
- if(huruf2 < 97)
- huruf2 = huruf2 + 32;
- if(huruf1 < huruf2 ){
- strcpy(temp.nama, user[a].nama);
- temp.umur = user[a].umur;
- strcpy(user[a].nama, user[b].nama);
- user[a].umur = user[b].umur;
- strcpy(user[b].nama, temp.nama);
- user[b].umur = temp.umur;
- }
- else if(huruf1 == huruf2){
- int arr = 0;
- while(true){
- huruf1 = int(user[a].nama[arr]);
- huruf2 = int(user[b].nama[arr]);
- if(huruf1 < 97)
- huruf1 = huruf1 + 32;
- if(huruf2 < 97)
- huruf2 = huruf2 + 32;
- if(huruf1 < huruf2){
- strcpy(temp.nama, user[a].nama);
- temp.umur = user[a].umur;
- strcpy(user[a].nama, user[b].nama);
- user[a].umur = user[b].umur;
- strcpy(user[b].nama, temp.nama);
- user[b].umur = temp.umur;
- break;
- }
- if(huruf1 > huruf2)
- break;
- arr++;
- }
- }
- }
- }
- }
- void search_by_name(int posisi){
- char nama[20];
- bool cek, sub_cek;
- int cari, sub_cari, huruf1, huruf2;
- int ketemu = 0;
- cin.ignore();
- cout<<"Masukan Nama yang ingin Dicari : ";
- cin.getline(nama, 20);
- for(int x = 0; x<posisi; x++){
- cek = true;
- cari = 0;
- while(nama[cari]!='\0'){
- huruf1 = int(user[x].nama[cari]);
- huruf2 = int(nama[cari]);
- if(huruf1 < 97)
- huruf1 = huruf1 + 32;
- if(huruf2 < 97)
- huruf2 = huruf2 + 32;
- if(user[x].nama[cari] != nama[cari] || huruf1 != huruf2)
- cek = false;
- cari++;
- }
- if(!cek){
- bool finded = false;
- sub_cek = false;
- cari = 0;
- sub_cari = 0;
- huruf1 = int(user[x].nama[sub_cari]);
- huruf2 = int(nama[cari]);
- if(huruf1 < 97)
- huruf1 = huruf1 + 32;
- if(huruf2 < 97)
- huruf2 = huruf2 + 32;
- while(user[x].nama[sub_cari]!='\0'){
- huruf1 = int(user[x].nama[sub_cari]);
- huruf2 = int(nama[cari]);
- if(huruf1 < 97)
- huruf1 = huruf1 + 32;
- if(huruf2 < 97)
- huruf2 = huruf2 + 32;
- if(user[x].nama[sub_cari] == nama[cari] || huruf1 == huruf2){
- while(nama[cari]!='\0'){
- huruf1 = int(user[x].nama[sub_cari]);
- huruf2 = int(nama[cari]);
- if(huruf1 < 97)
- huruf1 = huruf1 + 32;
- if(huruf2 < 97)
- huruf2 = huruf2 + 32;
- if(user[x].nama[sub_cari] != nama[cari] || huruf1 != huruf2){
- finded = false;
- break;
- }
- else
- finded = true;
- cari++;
- sub_cari++;
- }
- if(finded){
- sub_cek = true;
- }
- }
- if(!sub_cek)
- sub_cari++;
- else
- break;
- }
- }
- if(cek || sub_cek){
- cout<<"Nama = "<<user[x].nama<<endl;
- cout<<"Umur = "<<user[x].umur<<endl;
- cout<<endl<<endl;
- ketemu++;
- }
- if(ketemu == 0 && x == posisi){
- cout<<"Data Tidak Ditemukan"<<endl;
- }
- }
- }
- void view_data(int posisi){
- for(int b = 0; b<posisi; b++){
- cout<<"Nama = "<<user[b].nama<<endl;
- cout<<"Umur = "<<user[b].umur<<endl;
- }
- }
- int main(){
- int posisi = 0, pil;
- bool ulang;
- do{
- ulang = true;
- cout<<"1. Input The Data "<<endl;
- cout<<"2. Ascending Sorting"<<endl;
- cout<<"3. Descending Sorting"<<endl;
- cout<<"4. View The Data"<<endl;
- cout<<"5. Searching"<<endl;
- cout<<"6. End of Program"<<endl;
- cout<<"Masukan Pilihan Anda = ";
- cin>>pil;
- switch(pil){
- case 1:
- input_data(posisi);
- posisi++;
- break;
- case 2:
- sorting_name_asc(posisi);
- break;
- case 3:
- sorting_name_desc(posisi);
- break;
- case 4:
- view_data(posisi);
- break;
- case 5:
- search_by_name(posisi);
- break;
- case 6:
- ulang = false;
- break;
- default:
- cout<<"Pilihan Tidak ada"<<endl;
- break;
- }
- system("PAUSE");
- system("CLS");
- }while(ulang);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement