include iostream using namespace std union cost int decim double float

 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
#include <iostream>
using namespace std;
union cost {
int decim;
double floating;
void init() {
cin >> floating;
if ((int) floating == floating) {
decim = (int) floating;
}
}
double get() {
if (floating != NULL) return floating;
return decim;
}
void unset() {
decim = NULL;
floating = NULL;
}
};
struct books {
string name;
cost price;
};
bool compare(const cost &a, const cost &b) {
if (a.decim == b.decim && a.floating == b.floating) return true;
return false;
}
int main() {
cout << "Enter number of books array: ";
size_t number;
cin >> number;
books *_books = new books[number];
cost toFind;
for (int i = 0; i < number; i++) {
cout << "Enter name of the book[" << i << "]:" << endl;
cin >> _books[i].name;
cout << "Enter price of the book[" << i << "]:" << endl;
_books[i].price.init();
}
for (int i = 0; i < number; i++) {
cout << "book[" << i << "] name: " << _books[i].name << endl;
cout << "book[" << i << "] price: " << _books[i].price.get() << endl;
}
cout << "Enter the price of the books structure you want to delete." << endl;
toFind.init();
for (int i = 0; i < number; i++) {
if (compare(toFind, _books[i].price)) {
cout << "book with price " << _books[i].price.get() << " has been removed." << endl;
_books[i].name = "";
_books[i].price.unset();
int position = i;
swap(_books[position], _books[number - 1]);
number--;
i--;
}
}
for (int i = 0; i < number; i++) {
cout << "book[" << i << "] name: " << _books[i].name << endl;
cout << "book[" << i << "] price: " << _books[i].price.get() << endl;
}
delete[] _books;
return 0;
}