-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.func.cpp
167 lines (145 loc) · 3.05 KB
/
6.func.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
#include "tasks.h"
void push_back(double* array, size_t& size, size_t& capacity, const double& element) {
resize(array, size, capacity, size + 1);
array[size] = element;
size++;
}
void pop_back(double* array, size_t& size) {
array[size-1] = NULL;
bool flag = true;
int i = size-1;
while (flag) {
if (array[i] == NULL) {
size--;
i--;
}
else {
flag = false;
}
}
}
void insert(double* array, size_t& size, size_t& capacity, size_t& ind, const double& element) {
if (ind <= size) {
resize(array, size, capacity, size+1);
size++;
}
else {
resize(array, size, capacity, ind);
for (int i = size; i <= ind; i++) {
array[i] = NULL;
size++;
}
}
double a = element;
double temp = array[ind];
array[ind] = a;
for (int i = ind+1; i < size+1; i++) {
a = temp;
temp = array[i];
array[i] = a;
}
}
double at(double* array, size_t ind) {
return array[ind];
}
void resize(double* array, size_t& size, size_t& capacity, size_t new_size) {
for (int p = size; p <= new_size; p++) {
capacity++;
array = (double*)malloc(capacity * sizeof(double));
}
}
void sort(double* array, size_t size)//ñîðòèðîâêà
{
double k;
for (int i = 0; i < size-1; i++)
{
for (int j = i; j < size-1; j++) {
if (array[j] > array[j + 1])
{
k = at(array, j);
array[j] = array[j + 1];
array[j + 1] = k;
}
}
}
}
void destruct(double* array) {
free(array);
}
// ----- ÑÒÝÊ -----
void AddMemmory(int* Arr, int* N)
{
int* Buffer = (int*)malloc(*N * sizeof(int));
for (int i = 0; i < *N; i++)
{
Buffer[i] = Arr[i];
}
Arr = (int*)malloc(2 * *N * sizeof(int));
for (int i = 0; i < 2 * *N; i++)
{
Arr[i] = Buffer[i];
}
(*N) *= 2;
free(Buffer);
printf("\nstack size enlarged to ");
printf("%d\b", *N);
}
void printarr(int* Arr, int N)
{
for (int i = 0; i < N; i++)
{
printf("%d\n", Arr[N - i - 1]);
}
}
void printall(int* Arr, int N, int index)
{
printf("\n\nStack:\n");
printarr(Arr, index);
printf("filled: %d/%d\n\n", index, N);
}
void push(int x, int* Arr, int* N, int* index)
{
if (*index == *N) {
AddMemmory(Arr, N);
}
Arr[*index] = x;
(*index)++;
}
int pop(int* Arr, int* N, int* index)
{
(*index)--;
return Arr[*index];
}
// ----- ÄÂÓÌÅÐÍÛÉ ÌÀÑÑÈÂ (äëÿ 6.15) -----
void print(double* array, size_t size, size_t& columns) {
for (int i = 0; i < size; i++) {
cout << at(array, i) << " ";
if (i % columns == columns - 1) {
cout << endl;
}
}
}
void add_row(double* array, size_t& size, size_t columns, size_t& rows, size_t capacity) {
int a, ind;
cout << "Ââåäèòå èíäåêñ ðÿäà: ";
cin >> ind;
cout << "Ââåäèòå íîâûé ðÿä: ";
for (int i = 0; i < columns; i++) {
cin >> a;
size_t index = (ind - 1) * columns + i;
insert(array, size, capacity, index, a);
}
rows++;
}
void add_column(double* array, size_t& size, size_t& columns, size_t rows, size_t capacity) {
int a, ind;
cout << "Ââåäèòå èíäåêñ ñòîëáöà: ";
cin >> ind;
cout << "Ââåäèòå íîâûé ñòîëáåö: ";
for (int i = 0; i < rows; i++) {
cin >> a;
size_t index = (columns + 1) * i + ind - 1;
insert(array, size, capacity, index, a);
}
columns++;
}