include iostream using namespace std union cost int integer double _do

 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
#include <iostream>
using namespace std;
union cost {
int integer;
double _double;
void init() {
cin >> _double;
if ((int) _double == _double) {
integer = (int) _double;
}
}
double get() {
if (_double != NULL){
return _double;
}
return integer;
}
void unset() {
integer = NULL;
_double = NULL;
}
};
struct taxi {
string number;
cost price;
string type;
};
int main() {
cout << "Enter number of taxi array: ";
size_t number;
cin >> number;
taxi *_taxi = new taxi[number];
string _number;
for (int i = 0; i < number; i++) {
cout << "Enter number of the taxi[" << i << "]:" << endl;
cin >> _taxi[i].number;
cout << "Enter cost of the taxi[" << i << "]:" << endl;
_taxi[i].price.init();
cout << "Enter type of the taxi[" << i << "]:" << endl;
cin >> _taxi[i].type;
}
for (int i = 0; i < number; i++) {
cout << "Taxi[" << i << "] name: " << _taxi[i].number << endl;
cout << "Taxi[" << i << "] cost: " << _taxi[i].price.get() << endl;
cout << "Taxi[" << i << "] type: " << _taxi[i].type << endl;
}
cout << "Enter the number of the taxi structure you want to delete." << endl;
cin >> _number;
for (int i = 0; i < number; i++) {
if (_number == _taxi[i].number) {
cout << "Taxi with number " << _taxi[i].number << " has been removed." << endl;
_taxi[i].number = "";
_taxi[i].price.unset();
_taxi[i].type = "";;
int position = i;
swap(_taxi[position], _taxi[number - 1]);
number--;
i--;
}
}
for (int i = 0; i < number; i++) {
cout << "Taxi[" << i << "] name: " << _taxi[i].number << endl;
cout << "Taxi[" << i << "] cost: " << _taxi[i].price.get() << endl;
cout << "Taxi[" << i << "] type: " << _taxi[i].type << endl;
}
delete[] _taxi;
return 0;
}