include iostream include string using namespace std union weight int d

 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
#include <iostream>
#include <string>
using namespace std;
union weight {
int decim;
double floating;
void Init() {
cout << "Enter weight: ";
cin >> floating;
if ((int) floating == floating) {
decim = (int) floating;
}
}
double show() {
if (floating != NULL) return floating;
return decim;
}
void setNull() {
decim = NULL;
floating = NULL;
}
};
struct foundation {
string name;
string city;
weight w;
};
bool compare(const weight &a, const weight &b) {
if (a.decim == b.decim && a.floating == b.floating) return true;
return false;
}
int main() {
cout << "Number of foundations: ";
int number;
cin >> number;
foundation *founds = new foundation[number];
weight toFind;
for (int i = 0; i < number; i++) {
cout << "Enter name / city" << endl;
cin >> founds[i].name >> founds[i].city;
founds[i].w.Init();
}
for (int i = 0; i < number; i++) {
cout << founds[i].name << " - " << founds[i].city << " - " << founds[i].w.show() << endl;
}
cout << "Enter the weigth of the structure you want to delete." << endl;
toFind.Init();
for (int i = 0; i < number; i++) {
if (compare(toFind, founds[i].w)) {
cout << "Structure with name " << founds[i].name << " has been removed." << endl;
founds[i].w.setNull();
founds[i].name = "";
founds[i].city = "";;
int position = i;
swap(founds[position], founds[number - 1]);
number--;
i--;
}
}
for (int i = 0; i < number; i++) {
cout << founds[i].name << " - " << founds[i].city << " - " << founds[i].w.show() << endl;
}
delete[]founds;
return 0;
}