1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include<iostream>
#include<string.h>
using namespace std;
struct mobile
{
char name[20];
char combany[20];
string number;
} member[2];
enum SortBy { number , Name};
void Entermobile(mobile arr[],int n);
void BubbleSort(mobile arr[],int n, SortBy sortby);
void Printmobile(mobile arr[],int n);
int main()
{
int n=2,x;
Entermobile(member, n);
cout<<"\n\nSortingby Name:\n";
BubbleSort(member,n,Name);
cout<<"\nmobileSorted by Name:\n";
Printmobile(member,n);
cout<<"\n\nSortingby number:\n";
BubbleSort(member,n,number);
cout<<"\nmopileSorted by number:\n";
Printmobile(member,n);
}
void Entermobile(mobile arr[],int n)
{
for(int i=0; i<n; i++)
{
cout <<"\n Entering member "<<i+1<<endl;
cout <<"\n Enter member Name : ";
cin >>arr[i].name;
cout<<"\n Enter member combany : ";
cin>>arr[i].combany;
cout<<"\n Enter member number : ";
cin>>arr[i].number;
}
}
void Printmobile(mobile arr[],int n)
{
for(int i=0; i< n; i++)
cout<<endl<<arr[i].name <<"\t"<< arr[i].combany << "\t" << arr[i].number ;
}
void BubbleSort(mobile arr[],int n, SortBy sortby)
{
mobile temp;
for(int i=1; i< n; i++)
for(int j=0; j< n-i; j++)
{
if (
sortby== Name && strcmp(arr[j].name,arr[j+1].name)>0 ||
sortby== number && arr[j].number>arr[j+1].number )
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
|