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
| #include <vector>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
enum class options {brand,year,price};
struct Car
{
string brand;
string colour;
int year;
int price;
bool operator<(Car& c)const //For sort from <algorithm>
{
return year < c.year;
}
};
void Print(vector<Car>&, options opt);
istream& operator>>(istream&, Car&); //INPUT
ostream& operator<<(ostream&, Car); //OUTPUT
istream& operator>>(istream& is, Car& c)
{
cout << "Brand: ";
cin >> c.brand;
cout << "Colour: ";
cin >> c.colour;
cout << "Year: ";
cin >> c.year;
cout << "Price: ";
cin >> c.price;
cout << endl;
return is;
}
ostream& operator<<(ostream& os, Car c)
{
os << c.brand << ", " << c.colour << ", " << c.year << ", " << c.price << "$.";
return os;
}
void Print(vector<Car>& vec, options opt)
{
int from, to, year;
string brand;
switch (opt)
{
case options::brand:
cout << "Brand: ";
cin >> brand;
break;
case options::year:
cout << "Year: ";
cin >> year;
break;
case options::price:
cout << "From: ";
cin >> from;
cout << "To: ";
cin >> to;
break;
}
for (Car c : vec)
{
switch (opt)
{
case options::brand:
if (c.brand == brand) cout << c<<endl;
break;
case options::year:
if (c.year == year) cout << c<<endl;
break;
case options::price:
if (c.price >= from && c.price <= to) cout << c<<endl;
break;
}
}
}
int main()
{
int n;
vector<Car> vec;
cout << "Please Enter a number of cars: ";
cin >> n;
cout << endl;
for (int i = 0; i < n;i++)
{
cout << "Car #" << i+1 << endl;
Car c;
cin >> c;
vec.push_back(c);
}
sort(vec.begin(), vec.end());
Print(vec, options::brand);
Print(vec, options::year);
Print(vec, options::price);
system("pause");
} |