-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcPointersMAlloc.cpp
167 lines (150 loc) · 5.31 KB
/
cPointersMAlloc.cpp
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
//Pawel Adamczuk
//This exercise is juggling with C-type memory allocation and pointer dereferencing while modifying character strings.
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
//WCZYTANIE
int dl2;
int dl;
char temp5;
char temp6;
int temp7;
int temp8;
char * tempStr;
int* strLen;
int n;
cin >> n;
int temp;
int temp2;
char temp3;
int m;
int b;
char oper;
strLen = (int*) malloc ( n * sizeof (int));
char** arr;
arr = (char**) malloc ( n * sizeof (char*));
for ( int i = 0; i < n; i++ )
{
cin >> *(strLen + i);
*(arr + i) = (char*) malloc ( sizeof (char) * (*(strLen + i)));
temp = 0;
while (temp < *(strLen + i))
{
cin >> temp2;
cin >> temp3;
for (int j = 0; j < temp2; j++)
{
*(*(arr + i) + temp + j) = temp3;
}
temp += temp2;
}
}
//KONIEC WCZYTYWANIA
cin >> oper;
while (1)
{
switch (oper)
{
case 'W':
for ( int l = 0; l < n; l++ )
{
temp7 = 0;
temp6 = *(*(arr + l) + temp7);
while (temp7 < *(strLen + l))
{
temp8 = 0;
temp5 = temp6;
temp6 = *(*(arr + l) + temp7); // a a a a b b b
while ( temp6 == temp5 )
{
if ( temp8 + temp7 < *(strLen + l) )
{
temp8++;
temp6 = *(*(arr + l) + temp7 + temp8);
}
else
temp6 = 0;
}
cout << temp8 << ' ' << temp5;
temp7 += temp8;
cout << ' ';
if (temp7 >= *(strLen + l))
cout << endl;
}
}
break;
case 'U':
cin >> m;
free ( *(arr + m) );
for ( int i = 0; i < n - m - 1; i++ )
{
*(arr + m + i) = *(arr + m + i + 1);
*( strLen + m + i ) = *( strLen + m + i + 1 );
}
n = n - 1;
break;
case 'D':
cin >> m;
cin >> b;
* (arr + b) = (char*) realloc ( *(arr + b), sizeof (char) * (*(strLen + b) + *(strLen + m)));
for ( int i = *(strLen + b); i < (*(strLen + b) + *(strLen + m)); i++)
{
*(*(arr + b) + i) = *(*(arr + m) + i - *(strLen + b));
}
*(strLen + b) += *(strLen + m);
break;
case 'R':
cin >> m;
dl = *(strLen + m);
tempStr = (char*) malloc ( sizeof (char) * dl);
for ( int i = 0; i < dl; i++)
*(tempStr + i) = *( *( arr + m ) + dl - 1 - i);
for ( int i = 0; i < dl; i++)
*( *( arr + m ) + i) = *(tempStr + i );
free ( tempStr );
break;
case 'X':
cin >> m;
if ( *(strLen + m) == 1)
{
free ( *(arr + m) );
for ( int i = 0; i < n - m - 1; i++ )
{
*(arr + m + i) = *(arr + m + i + 1);
*( strLen + m + i ) = *( strLen + m + i + 1 );
}
n = n - 1;
}
else
{
tempStr = (char*) malloc ( ( *(strLen + m) / 2 ) * sizeof (char));
b = 0;
for (int i = 0; i < *(strLen + m); i++)
{
if (i % 2 != 0)
{
*(tempStr + b) = *(*(arr + m) + i);
b += 1;
}
}
free ( *(arr + m) );
*(arr + m) = tempStr;
*(strLen + m) = (*(strLen + m) / 2);
}
break;
case 'K':
break;
default:
break;
}
if (oper == 'K')
break;
cin >> oper;
}
for ( int i = 0; i < n; i++ )
free ( *(arr + i) );
free (arr);
free (strLen);
}