-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment11.cpp
143 lines (131 loc) · 3.73 KB
/
Assignment11.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
/*
Assignment 11
Name : Swaraj Sachin Gosavi
Roll no : 21327
Batch : F3 [SE 3]
Write C++ program using STL for sorting and searching user defined records such as Item records
(Item code, name, cost, quantity etc) using vector container.
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
class item{
public:
string name;
int cost, code, quantity;
};
item insert(){
item temp;
cout << "\nEnter the product details" << endl;
cout << "Item code : ";
cin >> temp.code;
cout << "Item name : ";
cin >> temp.name;
cout << "Item cost (Rupees) : ";
cin >> temp.cost;
cout << "Quantity : ";
cin >> temp.quantity;
return temp;
}
bool cname(const item& lhs, const item& rhs) {
return lhs.name < rhs.name;
}
bool ccost(const item& lhs, const item& rhs) {
return lhs.cost < rhs.cost;
}
bool ccode(const item& lhs, const item& rhs) {
return lhs.code < rhs.code;
}
bool cquant(const item& lhs, const item& rhs) {
return lhs.quantity < rhs.quantity;
}
void searching(vector<item> a){
char ch;
vector<item> :: iterator p;
cout << "\n\nEnter the parameter to search" << endl;
cout << "1. Item Name\n2. Item code" << endl;
cout << "Choice : ";
cin >> ch;
string name;
int code;
switch(ch){
case '1':
cout << "Name : ";
cin >> name;
for(p = a.begin(); p!=a.end(); p++){
if(p->name == name) {
cout << "Found";
return;
}
}
cout << "404 Not Found";
break;
case '2':
cout << "Code : ";
cin >> code;
for(p = a.begin(); p!=a.end(); p++){
if(p->code == code) {
cout << "Found";
return;
}
}
cout << "404 Not Found";
break;
}
}
int main(){
vector<item> i;
bool f=1;
char ch;
vector<item> :: iterator p;
while(f){
cout << "\n\nEnter your choice" << endl;
cout << "1. Add records\n2. Sort\n3. Search\n4. Display\n5. Exit" << endl;
cout << "Choice : " ;
cin >> ch;
switch(ch){
case '1':
i.push_back(insert());
break;
case '2':
cout << "\n\nEnter the parameter to sort" << endl;
cout << "1. Item Name\n2. Item cost\n3. Item code\n4. Quantity" << endl;
cout << "Choice : ";
cin >> ch;
switch(ch){
case '1':
sort(i.begin(), i.end(), &cname);
break;
case '2':
sort(i.begin(), i.end(), &ccost);
break;
case '3':
sort(i.begin(), i.end(), &ccode);
break;
case '4':
sort(i.begin(), i.end(), &cquant);
break;
}
for(p = i.begin(); p != i.end(); p++){
cout << p->code << " " << p->name << setw(10) << p->cost << setw(10) << p->quantity << endl;
}
break;
case '3':
searching(i);
break;
case '4':
for(p = i.begin(); p != i.end(); p++){
cout << p->code << " " << p->name << setw(10) << p->cost << setw(10) << p->quantity << endl;
}
break;
case '5':
f = 0;
break;
default:
cout << "Enter a valid choice " << endl;
}
}
return 0;
}