blob: 6fbb5f65a8ede8fba021d9cc420f7e3f34f5efc7 (
plain) (
blame)
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
|
struct Receipt {
private:
float* prices;
unsigned allocated;
unsigned size;
void resize() {
allocated *= 2;
float* morePrices = new float[allocated];
for (int i = 0; i < size; i++) {
morePrices[i] = prices[i];
}
delete[] prices;
prices = morePrices;
}
void free() {
delete[] prices;
}
void copyFrom(const Receipt& other) {
this->allocated = other.allocated;
this->size = other.size;
this->prices = new float[allocated];
for (int i = 0; i < size; i++) {
this->prices[i] = other.prices[i];
}
}
public:
Receipt() {
allocated = 2;
size = 0;
prices = new float[allocated];
}
~Receipt() {
free();
}
Receipt(const Receipt& other) {
copyFrom(other);
}
Receipt& operator=(const Receipt& other) {
if (this != &other) {
free();
copyFrom(other);
}
return *this;
}
void AddItemPrice(float price) {
if (price < 0.0) return;
if (this->allocated == this->size) {
resize();
}
this->prices[size++] = price;
}
void RemoveItemPrice(unsigned index) {
if (index >= size) return;
size--;
for (int i = index; i < size; i++) {
this->prices[i] = this->prices[i+1];
}
}
};
|