· 6 years ago · Nov 15, 2019, 10:44 AM
1#include "pch.h"
2#include <cstdio>
3#include <cstdlib>
4#include <ctime>
5#include <cmath>
6#include <conio.h>
7
8using namespace std;
9
10typedef double **table;
11
12int makeTable(table &tab, int rows, int cols);
13/*
14Создает динамический массив указанных размеров
15Входные параметры: rows - количество строчек (положительное число)
16 cols - количество столбцов (положительное число)
17Выходные параметры: tab - ссылка на переменную типа table, являющуюся указателем на указатель(2D массивом)
18Возвращает 0 если все хорошо.
19Возвращает 1 если размеры (cols / rows) указаны некорректно.
20*/
21
22void fillTable(table &tab, int rows, int cols, int isRand);
23/*
24Заполняет динамический массив указанных размеров
25Получаемые параметры как у makeTable, + входной isRand
26 isRand должен быть равен или 0, или 1 для того чтобы программа сделала хоть что-то.
27 если isRand == 0, то программа просит пользователя ввести значение в каждую ячейку вручную.
28 если isRand == 1, то программа заполняет массив случайными величинами.
29*/
30
31void outputTable(table &tab, int rows, int cols);
32/*
33Выводит массив на экран
34Получаемые параметры как у makeTable.
35*/
36
37void delTable(table &tab, int &rows, int &cols);
38/*
39Освобождает память из под динамического массива.
40Входных параметров нет.
41Выходные параметры: tab - ссылка на переменную типа table, являющуюся указателем на указатель(2D массивом)
42 rows - ссылка на количество строк в этом массиве, при выполнении программы зануляется
43 cols - ссылка на количество столбцов в этом массиве, при выполнении программы зануляется
44*/
45
46void computeTable(table &tab, int rows, int cols, double &result);
47/*
48Вычисляет наименьшую сумму абсолютных величин среди строк, в которых содержится больше всего нулей
49Входные параметры: rows - количество строчек (положительное число)
50 cols - количество столбцов (положительное число)
51Выходные параметры: tab - ссылка на переменную типа table, являющуюся указателем на указатель(2D массивом)
52 result - ссылка на переменную, изменяется программой. Результат
53*/
54
55//void testProg();
56
57
58int main()
59{
60 //testProg();
61
62 table tab;
63 int rows = 0;
64 int cols = 0;
65 char c = 'f';
66 bool isFilled = false;
67 bool exists = false;
68 double result = 0;
69
70 while (c != '7')
71 {
72 if (c == '1')
73 {
74 if (exists)
75 {
76 delTable(tab, rows, cols);
77 isFilled = false;
78 }
79 while (rows <= 0) {
80 printf("Input positive number of rows\n");
81 scanf_s("%d", &rows);
82 }
83 while (cols <= 0) {
84 printf("Input positive number of cols\n");
85 scanf_s("%d", &cols);
86 }
87 makeTable(tab, rows, cols);
88 exists = true;
89 }
90 else if (c == '2')
91 {
92 isFilled = true;
93 fillTable(tab, rows, cols, 0);
94 }
95 else if (c == '3')
96 {
97 isFilled = true;
98 fillTable(tab, rows, cols, 1);
99 }
100 else if (c == '4')
101 {
102 outputTable(tab, rows, cols);
103 }
104 else if (c == '5')
105 {
106 exists = false;
107 isFilled = false;
108 delTable(tab, rows, cols);
109 }
110 else if (c == '6')
111 {
112 computeTable(tab, rows, cols, result);
113 printf("result = %.4lf\n", result);
114 }
115 c = 'f';
116
117 if (!exists)
118 {
119 printf("Table does not exist.\n"\
120 "1. to create new table.\n"\
121 "2. to exit.\n");
122 while (c != '1' && c != '2')
123 c = _getch();
124 if (c == '2')
125 c = '7';
126 }
127 else
128 {
129 if (!isFilled)
130 {
131 printf("Seems that your table is not filled.\n"\
132 "1. to fill it manually.\n"\
133 "2. to fill it with random values.\n"\
134 "3. to exit.\n");
135 while (c != '1' && c != '2' && c != '3')
136 c = _getch();
137 if (c == '1')
138 c = '2';
139 else if (c == '2')
140 c = '3';
141 else if (c == '3')
142 c = '7';
143 }
144 else
145 {
146 printf("Table exists and filled.\n"\
147 "1. to create a new table.\n"\
148 "2. to refill table manually.\n"\
149 "3. to refill table with random values.\n"\
150 "4. to output table.\n"\
151 "5. to delete table.\n"\
152 "6. to compute table.\n"\
153 "7. to exit\n");
154 while (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7')
155 c = _getch();
156 }
157 }
158 //system("cls");
159 }
160
161 if (exists)
162 delTable(tab, rows, cols);
163
164 return 0;
165}
166
167int makeTable(table &tab, int rows, int cols)
168{
169 if (rows > 0 && cols > 0)
170 {
171 tab = new double*[rows];
172 for (int i = 0; i < rows; i++)
173 tab[i] = new double[cols];
174 return 0;
175 }
176 else
177 return 1;
178}
179
180void fillTable(table &tab, int rows, int cols, int isRand)
181{
182 double d;
183 double val;
184 int chnc;
185 int sgn;
186 srand(time(0));
187
188 for (int i = 0; i < rows; i++)
189 {
190 for (int j = 0; j < cols; j++)
191 {
192 if (isRand == 1)
193 {
194 d = rand();
195 tab[i][j] = rand();
196 sgn = rand() % 2;
197 chnc = rand() % 20;
198 if (chnc > 0)
199 tab[i][j] += d / RAND_MAX;
200 if (sgn == 1)
201 tab[i][j] *= -1;
202 }
203 else
204 {
205 printf("Table[%d][%d] = ", i, j);
206 scanf_s("%lf", &val);
207 tab[i][j] = val;
208 }
209 }
210 }
211 return;
212}
213
214void outputTable(table &tab, int rows, int cols)
215{
216 for (int i = 0; i < rows; i++)
217 {
218 for (int j = 0; j < cols; j++)
219 {
220 printf("%10.3lf ", tab[i][j]);
221 }
222 putchar('\n');
223 }
224 return;
225}
226
227void delTable(table &tab, int &rows, int &cols)
228{
229 for (int i = 0; i < rows; i++)
230 delete[] tab[i];
231 delete[] tab;
232 rows = 0;
233 cols = 0;
234 return;
235}
236
237void computeTable(table &tab, int rows, int cols, double &result)
238{
239 int maxzir = -1; // int maxzir = 0; // maxzir (max zeros in row)
240 double minsum = 0; // minsum
241 int zir;
242 double sum;
243 double eps = 1.0e-18;
244
245 //for (int j = 0; j < cols; j++)
246 // minsum += abs(tab[0][j]);
247
248 for (int i = 0; i < rows; i++)
249 {
250 zir = 0;
251 sum = 0;
252
253 for (int j = 0; j < cols; j++)
254 {
255 if (abs(tab[i][j]) < eps)
256 zir += 1;
257 else
258 sum += abs(tab[i][j]);
259 }
260
261 if (zir >= maxzir)
262 {
263 if (zir > maxzir)
264 {
265 maxzir = zir;
266 minsum = sum;
267 }
268 else
269 {
270 if (sum < minsum || minsum == -1)
271 minsum = sum;
272 }
273 }
274 }
275 result = minsum;
276
277 return;
278}
279
280/*
281void testProg()
282{
283 table tab;
284 int rows = 0;
285 int cols = 0;
286 char c = 'f';
287 bool isFilled = false;
288 bool exists = false;
289 double result = 0;
290
291 while (c != '7')
292 {
293 if (c == '1')
294 {
295 c = 'f';
296 if (exists)
297 {
298 delTable(tab, rows, cols);
299 isFilled = false;
300 }
301 while (rows <= 0) {
302 printf("Input positive number of rows\n");
303 scanf_s("%d", &rows);
304 }
305 while (cols <= 0) {
306 printf("Input positive number of cols\n");
307 scanf_s("%d", &cols);
308 }
309 makeTable(tab, rows, cols);
310 exists = true;
311 }
312 else if (c == '2')
313 {
314 c = 'f';
315 isFilled = true;
316 fillTable(tab, rows, cols, 0);
317 }
318 else if (c == '3')
319 {
320 c = 'f';
321 isFilled = true;
322 fillTable(tab, rows, cols, 1);
323 }
324 else if (c == '4')
325 {
326 c = 'f';
327 outputTable(tab, rows, cols);
328 }
329 else if (c == '5')
330 {
331 c = 'f';
332 exists = false;
333 isFilled = false;
334 delTable(tab, rows, cols);
335 }
336 else if (c == '6')
337 {
338 c = 'f';
339 computeTable(tab, rows, cols, result);
340 printf("result = %.4lf\n", result);
341 }
342
343 if (!exists)
344 {
345 printf("Table does not exist.\n"\
346 "1. to create new table.\n"\
347 "2. to exit.\n");
348 while (c != '1' && c != '2')
349 c = _getch();
350 if (c == '2')
351 c = '7';
352 }
353 else
354 {
355 if (!isFilled)
356 {
357 printf("Seems that your table is not filled.\n"\
358 "1. to fill it manually.\n"\
359 "2. to fill it with random values.\n"\
360 "3. to exit.\n");
361 while (c != '1' && c != '2' && c != '3')
362 c = _getch();
363 if (c == '1')
364 c = '2';
365 else if (c == '2')
366 c = '3';
367 else if (c == '3')
368 c = '7';
369 }
370 else
371 {
372 printf("Table exists and filled.\n"
373 "1. to create a new table.\n"\
374 "2. to refill table manually.\n"\
375 "3. to refill table with random values.\n"\
376 "4. to output table.\n"\
377 "5. to delete table.\n"\
378 "6. to compute table.\n"\
379 "7. to exit\n");
380 while (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7')
381 c = _getch();
382 }
383 }
384 system("cls");
385 }
386
387 if (exists)
388 delTable(tab, rows, cols);
389
390 return;
391}
392*/