-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto.cpp
More file actions
189 lines (177 loc) · 4.19 KB
/
auto.cpp
File metadata and controls
189 lines (177 loc) · 4.19 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <cstdlib>
using namespace std;
const int N=100;
int number_of_vehicles=0;
int last_id = 1;
struct car
{
int id;
string brand;
string model;
int year;
int price;
};
car vehicles[N];
bool filter_array[N];
void create();
void add();
void filter();
void print();
void delete_car();
void reset_filter();
int main ()
{
create();
reset_filter();
char choice;
do{
cout<<"insert a to add a new car;"<<endl;
cout<<"insert d to delete an existing car;"<<endl;
cout<<"insert f to filter the results;"<<endl;
cout<<"insert p to print available cars;"<<endl;
cout<<"insert r to reset filters;"<<endl;
cout<<"make your choice: "<<endl;
cin>>choice;
system("cls");
switch(choice)
{
case 'a':
add();
break;
case 'd':
delete_car();
break;
case 'f':
filter();
break;
case 'p':
print();
break;
case 'r':
reset_filter();
break;
}
}while(true);
}
void create ()
{
for(int i=0; i< 5; i++)
{
vehicles[i].id=last_id;
vehicles[i].brand="Toyota";
vehicles[i].model="Supra";
vehicles[i].year=rand()%47+1978;
vehicles[i].price=rand()%90001+10000;
number_of_vehicles++;
last_id++;
}
}
void add()
{
int year;
vehicles[number_of_vehicles].id=last_id;
vehicles[number_of_vehicles].brand="Toyota";
vehicles[number_of_vehicles].model="Supra";
cout<<"Insert the year: ";
cin>>year;
vehicles[number_of_vehicles].year = year;
cout<<"Insert the price: ";
cin>>vehicles[number_of_vehicles].price;
number_of_vehicles++;
last_id++;
}
void filter ()
{
char choice;
int initial_year, final_year, initial_price, final_price;
cout<<"insert y to filter by year;"<<endl;
cout<<"insert p to filter by price;"<<endl;
cout<<"make your choice: "<<endl;
cin>>choice;
system("cls");
switch(choice)
{
case 'y':
cout<<"Insert the initial year: ";
cin>>initial_year;
cout<<"Insert the final year: ";
cin>>final_year;
for(int i=0; i< number_of_vehicles; i++)
{
if(vehicles[i].year>=initial_year && vehicles[i].year<=final_year && filter_array[i]==true)
{
filter_array[i]=true;
}
else
{
filter_array[i]=false;
}
}
break;
case 'p':
cout<<"Insert the initial price: ";
cin>>initial_price;
cout<<"Insert the final price: ";
cin>>final_price;
for(int i=0; i< number_of_vehicles; i++)
{
if(vehicles[i].price>=initial_price && vehicles[i].price<=final_price && filter_array[i]==true)
{
filter_array[i]=true;
}
else
{
filter_array[i]=false;
}
}
break;
}
}
void reset_filter()
{
for(int i = 0; i < N; i++)
{
filter_array[i] = true;
}
}
void print ()
{
for(int i=0; i< number_of_vehicles; i++)
{
if(filter_array[i]==false)
{
continue;
}
cout<<vehicles[i].id<<") ";
cout<<vehicles[i].brand<<" ";
cout<<vehicles[i].model<<" ";
cout<<vehicles[i].year<<" ";
cout<<vehicles[i].price<<" ";
cout<<endl;
}
}
void delete_car(){
int id;
bool found = false;
cout<<"insert the ID to delete: ";
cin>>id;
for(int i = 0; i<number_of_vehicles; i++){
if(vehicles[i].id == id)
{
found = true;
}
if(/*vehicles[i].id != id &&*/ !found)
{
continue;
}
if(i+1<number_of_vehicles)
{
vehicles[i]=vehicles[i+1];
}
}
if(found)
{
number_of_vehicles--;
}
}