-
Notifications
You must be signed in to change notification settings - Fork 12
/
GroupC_Practical6.cpp
149 lines (142 loc) · 2.53 KB
/
GroupC_Practical6.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Write C++ Program using STL for sorting and searching user defined
records such as item records using vector container.
*/
#include <iostream> //standard input output stream header file
#include <algorithm> //The STL algorithms are generic because
they can operate on a variety of data structures
#include <vector> //The header file for the STL vector library is
vector.
using namespace std;
class Item // creating class Item
{
public:
char name[10];
int quantity;
int cost;
int code;
bool operator==(const Item& i1) //Boolean operators allow
you to create more complex conditional statements
{
if(code==i1.code) //operator will return 1 if the
comparison is true, or 0 if the comparison is false
return 1;
return 0;
}
bool operator<(const Item& i1)
{
if(code<i1.code) //operator will return 1 if the
comparison is true, or 0 if the comparison is false
return 1;
return 0;
}
};
vector<Item> o1;
void print(Item &i1);
void display();
void insert();
void search();
void dlt();
bool compare(const Item &i1, const Item &i2)
{
//if (i1.name != i2.name) return i1.cost < i2.cost;
return i1.cost < i2.cost;
}
int main()
{
int ch;
do
{
cout<<"\n* * * * * Menu * * * * *";
cout<<"\n1.Insert";
cout<<"\n2.Display";
cout<<"\n3.Search";
cout<<"\n4.Sort";
cout<<"\n5.Delete";
cout<<"\n6.Exit";
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
sort(o1.begin(),o1.end(),compare);
cout<<"\n\n Sorted on Cost : ";
display();
break;
case 5:
dlt();
break;
case 6:
exit(0);
}
}while(ch!=7);
return 0;
}
void insert()
{
Item i1;
cout<<"\nEnter Item Name : ";
cin>>i1.name;
cout<<"\nEnter Item Quantity : ";
cin>>i1.quantity;
cout<<"\nEnter Item Cost : ";
cin>>i1.cost;
cout<<"\nEnter Item Code : ";
cin>>i1.code;
o1.push_back(i1);
}
void display()
{
for_each(o1.begin(),o1.end(),print);
}
void print(Item &i1)
{
cout<<"\n";
cout<<"\nItem Name : "<<i1.name;
cout<<"\nItem Quantity : "<<i1.quantity;
cout<<"\nItem Cost : "<<i1.cost;
cout<<"\nItem Code : "<<i1.code;
cout<<"\n\n";
}
void search()
{
vector<Item>::iterator p;
Item i1;
cout<<"\nEnter Item Code to search : ";
cin>>i1.code;
p=find(o1.begin(),o1.end(),i1);
if(p==o1.end())
{
cout<<"\nNot found!!!";
}
else
{
cout<<"\nFound!!!";
}
}
void dlt()
{
vector<Item>::iterator p;
Item i1;
cout<<"\nEnter Item Code to delete : ";
cin>>i1.code;
p=find(o1.begin(),o1.end(),i1);
if(p==o1.end())
{
cout<<"\nNot found!!!";
}
else
{
o1.erase(p);
cout<<"\nDeleted!!!";
}
}