Repository navigation
Expand file tree
/
Copy pathArrayLibrary.cpp
More file actions
207 lines (153 loc) · 4.66 KB
/
Copy pathArrayLibrary.cpp
File metadata and controls
207 lines (153 loc) · 4.66 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include "ArrayLibrary.h"
using namespace std;
// Array.h // 1.5.3
// constructor
Array::Array(int newSize) {
Size = newSize;
ArrayItems = new int[newSize];
}
// getlength , getSize
int Array::getlength() { return length; }
int Array::getSize() { return Size; }
// fill
void Array::fill(int NumberOfItems) {
if (NumberOfItems == -1) { NumberOfItems = Size; }
if (NumberOfItems < 0 || NumberOfItems > Size) { return; }
for (int item = 0; item < NumberOfItems; item++) {
cin >> ArrayItems[item];
length++;
}
}
// display
void Array::display(int start, int stop, int step) {
{
// Validation
if (step < 0) { cout << "Parameter 'Step' must be greater than 0 ..!?"; return; }
if (start > length) { while (start > length) { start -= length; } }
if (stop > length) { while (stop > length) { stop -= length; } }
if (start < 0) { while (start < 0) { start += length; } }
if (stop < 0) { while (stop < 0) { stop += length; } }
// show all items
if (start == 0 && stop == 0) {
for (int item = start; item < length; item += step) {
cout << ArrayItems[item] << endl;
}
}
// show range of items reversliy .
else if (start > stop && stop > 0) {
for (int item = start; item >= stop; item -= step) {
cout << ArrayItems[item] << endl;
}
}
// show range of items.
else if (start != 0 && stop != 0) {
for (int item = start; item <= stop; item += step) {
cout << ArrayItems[item] << endl;
}
}
// show one item.
else if (start != 0) { cout << ArrayItems[start] << endl; }
else { cout << "Invalid Input\n"; return; }
}
}
// Delete
void Array::Delete(int start, int stop, int step) {
// Validation
if (step < 0) { cout << "Parameter 'Step' must be greater than 0 ..!?"; return; }
if (start > length) { while (start > length) { start -= length; } }
if (stop > length) { while (stop > length) { stop -= length; } }
if (start < 0) { while (start < 0) { start += length; } }
if (stop < 0) { while (stop < 0) { stop += length; } }
// delete all items.
if (start == 0 && stop == 0) {
for (int item = start; item < length; item += step) {
ArrayItems[item] = 0;
}
length = 0;
}
// delete range of items.
else if (start != 0 && stop != 0) {
for (int item = stop; item >= start; item -= step) {
reverseShift(start);
length--;
}
}
// delete one item.
else if (start != 0 && stop == 0) {
reverseShift(start);
length--;
}
else {
cout << "Invalid Input ..!?\n";
}
}
// push
void Array::push(int item) {
if (isFull()) { int size = Size++; enLarge(size); }
ArrayItems[length++] = item;
}
// shift
void Array::shift(int Index) {
if (isFull()) { return; }
for (int item = length; item > Index; item--) {
ArrayItems[item] = ArrayItems[item - 1];
}
length++;
}
// reverseShift
void Array::reverseShift(int Index) {
if (isFull()) { return; }
for (int item = Index; item < length; item++) {
ArrayItems[item] = ArrayItems[item + 1];
}
length--;
}
// islEmpty
bool Array::isEmpty() { return length == 0; }
// isFull
bool Array::isFull() { return length == Size; }
// enlarge
void Array::enLarge(int size) {
int* old = new int[Size];
for (int i = 0; i < Size; i++) {
old[i] = ArrayItems[i];
}
ArrayItems = new int[size];
for (int i = 0; i < size; i++) {
ArrayItems[i] = old[i];
}
delete[] old;
Size = size;
}
// replace
void Array::replace(int item, int index) {
ArrayItems[index] = item;
}
// search
int Array::search(int wantedItem) {
int Index = -1;
for (int item = 0; item < length; item++) {
if (ArrayItems[item] == wantedItem) { Index = item; }
}
return Index;
}
// insert
void Array::insert(int newItem, int wantedIndex) {
int search = Array::search(wantedIndex);
if (wantedIndex > length || wantedIndex < (length * -1)) { return; }
if (wantedIndex < 0) { wantedIndex += length; }
if (search == -1) { return; }
Array::shift(wantedIndex);
ArrayItems[wantedIndex] = newItem;
}
// merge
void Array::merge(Array secondArray) {
int newSize = Size + secondArray.getSize();
int newLength = length + secondArray.getlength();
enLarge(newSize);
for (int item = length; item < newLength; item++) {
ArrayItems[item] = secondArray.ArrayItems[item - length];
}
length = newLength;
}