STL for Sorting and searching with user-defined records
/*
5. Use STL for Sorting and searching with user-defined records such as Person Record
(Name, birth date, telephone no), item record (item code, item name, quantity and cost)
*/
#include<iostream>
#include<process.h>
#include<list>
using namespace std;
typedef struct preson
{
int id;
char name[30];
int cost;
int quantity;
}node;
node p1;
list<node> s1;
list<node>::iterator p;
bool operator <(node &p,node &q)
{
if(p.id<q.id)
return true;
else
return false;
}
void create()
{
int i,n;
cout<<"\n\nHow many records u want to insert= ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n\nEnter code=";
cin>>p1.id;
cout<<"\n\nEnter Name= ";
cin>>p1.name;
cout<<"\n\nEnter cost= ";
cin>>p1.cost;
cout<<"\n\nEnter quantity= ";
cin>>p1.quantity;
s1.push_back(p1);
}
}
void display()
{
cout<<"\nThe Contents of the List = ";
cout<<"\n\nID\tName \tCost\tQuantity\n\n";
for(p=s1.begin();p!=s1.end();p++)
{
cout<<"\n"<<(*p).id;
cout<<"\t"<<(*p).name;
cout<<"\t"<<(*p).cost;
cout<<"\t\t"<<(*p).quantity;
}
}
void search()
{
int temp;
cout<<"\nThe Searching Opeartion = ";
cout<<"\n\nEnter the Product_ID= ";
cin>>temp;
int flag=0;
for(p=s1.begin();p!=s1.end();p++)
{
if((*p).id==temp)
{
cout<<"\nEntered Product is found";
cout<<"\n\nID\tName \tCost\tQuantity\n\n";
cout<<"\n"<<(*p).id;
cout<<"\t"<<(*p).name;
cout<<"\t"<<(*p).cost;
cout<<"\t\t"<<(*p).quantity;
flag=1;
break;
}
}
if(!flag)
cout<<"\nSuch Record is not found. ";
}
void sort()
{
s1.sort();
cout<<"\nRecords sorted successfully. ";
}
void main()
{
int ch;
do{
cout<<"\nPerson Database Program:-";
cout<<"\n1. Create \n2. Display \n3. Search\n4. Sort\n5. Exit.";
cout<<"\nEnter UR choice= ";
cin>>ch;
switch(ch)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
sort();
break;
case 5:
exit(0);
}
}while(ch!=5);
}
OUTPUT:-
Person Database Program:-
1. Create
2. Display
3. Search
4. Sort
5. Exit.
Enter UR choice= 1
How many records u want to insert= 3
Enter code=101
Enter Name= abc1
Enter cost= 100
Enter quantity= 10
Enter code=103
Enter Name= abc3
Enter cost= 225
Enter quantity= 7
Enter code=102
Enter Name= abc2
Enter cost= 150
Enter quantity= 4
Person Database Program:-
1. Create
2. Display
3. Search
4. Sort
5. Exit.
Enter UR choice= 2
The Contents of the List =
ID Name Cost Quantity
101 abc1 100 10
103 abc3 225 7
102 abc2 150 4
Person Database Program:-
1. Create
2. Display
3. Search
4. Sort
5. Exit.
Enter UR choice= 3
The Searching Opeartion =
Enter the Product_ID= 102
Entered product is found
ID Name Cost Quantity
102 abc2 150 4
Person Database Program:-
1. Create
2. Display
3. Search
4. Sort
5. Exit.
Enter UR choice= 4
Records sorted successfully.
Person Database Program:-
1. Create
2. Display
3. Search
4. Sort
5. Exit.
Enter UR choice= 2
The Contents of the List =
ID Name Cost Quantity
101 abc1 100 10
102 abc2 150 4
103 abc3 225 7
Person Database Program:-
1. Create
2. Display
3. Search
4. Sort
5. Exit.
Enter UR choice=5
0 comments:
Post a Comment