ASSIGNMENT 6
Name : Sagar Patil
Class: SE(B)
Roll no. : 25
Subject: OOP
INPUT:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm> #include <vector> using
namespace std; typedef struct rec { char
name[20]; char BirthDt[20]; char
phone[11]; } node; node temp; vector<node>
rec; vector<node>::iterator ptr; bool
compare(const node &r1, const node &r2)
{ return strcmp(r1.name, r2.name) <
0;
} void Create()
int n;
cout << "\nHow many elements do you want to insert? ";
cin >> n;
cout << "\nEnter the elements in the database: " << endl;
for (int i = 0; i < n; i++) { cout << "\nName: "; cin >>
temp.name; cout << "\nBirth Date (dd-mm-yy): "; cin
>> temp.BirthDt; cout << "\nPhone: "; cin >>
temp.phone; rec.push_back(temp);
} } void Display() { cout << "\n\tThe contents
of the database are: "; cout << "\
n------------------------------------------"; cout <<
"\nName Birth Date Phone ";
cout << "\n------------------------------------------";
for (ptr = rec.begin(); ptr != rec.end(); ptr++)
{ cout << "\n " <<
(*ptr).name; cout << " " <<
(*ptr).BirthDt; cout << " " <<
(*ptr).phone;
} } void
Searching()
{ char key[20]; int flag = 0; cout << "\nEnter
the name you want to search: "; cin >> key;
for (ptr = rec.begin(); ptr != rec.end(); ptr++)
{
if (strcmp((*ptr).name, key) == 0)
{ flag =
1; break;
} } if (flag == 1) { cout << "\nThe desired element is
present in the database.";
} else { cout << "\nThe desired element is not present in the
database.";
} } void Sorting() { sort(rec.begin(),
rec.end(), compare); cout << "\n\nRecord
is sorted !!!" << endl;
} int main() { char ans = 'y'; int choice; cout
<< "\nProgram for searching and sorting"; do {
cout << "\nMain Menu "; cout << "\n1. Create
a database."; cout << "\n2. Display a
database.";
cout << "\n3. Search for a particular element.";
cout << "\n4. Sort the database (based on name)";
cout << "\nEnter your choice: "; cin >> choice;
switch (choice)
{ case 1:
Create();
break; case
2:
Display();
break; case
3:
Searching();
break; case
4:
Sorting(); Display(); break; default: cout << "\nInvalid
choice! Please select again.";
cout << "\nDo you want to go back to Main Menu? (y/n): ";
cin >> ans;
} while (ans == 'y' || ans == 'Y');
return 0;
}
OUTPUT:
/tmp/mKrt5bKlhC.o
Program for searching and sorting
Main Menu
1. Create a database.
2. Display a database.
3. Search for a particular element.
4. Sort the database (based on name)
Enter your choice: 1
How many elements do you want to insert? 3
Enter the elements in the database:
Name: Amey
Birth Date (dd-mm-yy): 29/01/2005
Phone: 9766034913
Name: Rushikesh
Birth Date (dd-mm-yy): 20/04/2005
Phone: 9322724979
Name: Mahesh
Birth Date (dd-mm-yy): 04/03/2005
Phone: 9667844670
Do you want to go back to Main Menu? (y/n): y
Main Menu
1. Create a database.
2. Display a database.
3. Search for a particular element.
4. Sort the database (based on name)
Enter your choice: 2
The contents of the database are:
------------------------------------------
Name Birth Date Phone
------------------------------------------
Amey 29/01/2005 9766034913
Rushikesh 20/04/2005 9322724979
Mahesh 04/03/2005 9667844670
Do you want to go back to Main Menu? (y/n): y
Main Menu
1. Create a database.
2. Display a database.
3. Search for a particular element.
4. Sort the database (based on name)
Enter your choice: 3
Enter the name you want to search: Rushikesh
The desired element is present in the database.
Do you want to go back to Main Menu? (y/n): y
Main Menu
1. Create a database.
2. Display a database.
3. Search for a particular element.
4. Sort the database (based on name)
Enter your choice: 4
Record is sorted !!!
The contents of the database are:
------------------------------------------
Name Birth Date Phone
------------------------------------------
Amey 29/01/2005 9766034913
Mahesh 04/03/2005 9667844670
Rushikesh 20/04/2005 9322724979
Do you want to go back to Main Menu? (y/n): n
=== Code Execution Successful ===