kursach08

  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
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
class VirtualArray
{
////////////////////////////////////////////////////////////////////////////////
protected:
VirtualArray * next;
int value;
int position;
////////////////////////////////////////////////////////////////////////////////
public:
VirtualArray()
{
next = this;
value = 0;
position = 0;
}
VirtualArray(int v1, int p1)
{
next = this;
value = v1;
position = p1;
}
////////////////////////////////////////////////////////////////////////////
VirtualArray * Add(VirtualArray * p1)
{
p1->next = next;
next = p1;
return p1;
}
void RemoveNext()
{
VirtualArray * rem = next;
next = next->next;
delete rem;
}
void Set(int pos, int val)
{
VirtualArray * curr = this;
do
{
if (curr->position == pos)
{
curr->value = val;
return;
}
curr = curr->next;
}
while (curr != this);
Add(new VirtualArray(val, pos));
}
int & operator[](int pos)
{
VirtualArray * curr = this, * prev = NULL;
do
{
if (curr->position == pos)
return curr->value;
prev = curr;
curr = curr->next;
if ((curr->value == 0) && (curr != this))
{
prev->RemoveNext();
curr = prev->next;
}
}
while (curr != this);
return Add(new VirtualArray(0, pos))->value;
}
void Clear()
{
VirtualArray * curr = next, * prev = NULL;
while (curr != this)
{
prev = curr;
curr = curr->next;
delete prev;
}
value = 0;
next = this;
}
int GetLenght()
{
VirtualArray * curr = next;
int len = 1;
while (curr != this)
{
len++;
curr = curr->next;
}
return len;
}
void Shift(int p1, int m1 = 0)
{
VirtualArray * curr = this;
int len = GetLenght();
if (m1 && (m1 < len))
m1 = len + 1;
do
{
curr->position = (m1 ? (curr->position + p1) % m1 : (curr->position + p1));
curr = curr->next;
}
while (curr != this);
}
void Print()
{
VirtualArray * curr = next;
cout << "[" << value;
while (curr != this)
{
cout << ", " << curr->value;
curr = curr->next;
}
cout << "]" << endl;
}
};
////////////////////////////////////////////////////////////////////////////////
int main()
{
VirtualArray va1;
for (int i=0; i<20; i++)
{
va1[i] = (i*9) % 10;
}
for (int i=0; i<20; i++)
cout << "a[" << i << "] = " << va1[i] << endl;
cout << "va1 = ";
va1.Print();
cout << "Shift on 1 element toward:" << endl;
va1.Shift(1, 10);
for (int i=0; i<20; i++)
cout << "a[" << i << "] = " << va1[i] << endl;
va1.Print();
cout << "Count of elements = " << va1.GetLenght();
getchar();
return 0;
}