1
1
#include < cstdio>
2
- #include < cstdlib>
3
- #include < ctime>
4
2
#include < stdexcept>
5
3
#include " array.h"
6
4
@@ -29,6 +27,11 @@ void qsort(Array *arr, size_t start, size_t end){
29
27
qsort (arr, left, end);
30
28
}
31
29
30
+ void qsort (Array *arr)
31
+ {
32
+ qsort (arr, 0 , array_size (arr) - 1 );
33
+ }
34
+
32
35
Array *array_create_and_read (FILE *input)
33
36
{
34
37
int n;
@@ -42,78 +45,55 @@ Array *array_create_and_read(FILE *input)
42
45
if (fscanf (input, " %d" , &x) < 1 ) throw std::invalid_argument (" Failed to read number" );
43
46
array_set (arr, i, x);
44
47
}
48
+
45
49
return arr;
46
50
}
47
51
48
- void task1 (FILE *input )
52
+ void task1 (Array *arr )
49
53
{
50
- size_t n;
51
- Array *arr = NULL ;
52
- if (fscanf (input, " %lu" , &n) < 1 ) throw std::runtime_error (" Failed to read size" );
53
- arr = array_create (n);
54
- for (size_t index {0 }; index < n; ++index ) array_set (arr, index , rand () % 100 );
54
+ size_t size = array_size (arr);
55
55
56
- for (size_t index {0 }; index < array_size (arr) ; ++index ){
56
+ for (size_t index {0 }; index < size ; ++index ){
57
57
if (array_get (arr, index ) % 2 == 0 )
58
58
array_set (arr, index , array_get (arr, index ) * array_get (arr, index ));
59
59
else array_set (arr, index , 2 * array_get (arr, index ));
60
60
}
61
-
62
- array_delete (arr);
63
61
}
64
62
65
- void task2 (FILE *input )
63
+ void task2 (Array *arr )
66
64
{
67
- size_t size_arr;
68
- Array *arr = NULL ;
69
- if (fscanf (input, " %lu" , &size_arr) < 1 ) throw std::runtime_error (" Failed to read size" );
70
- arr = array_create (size_arr);
71
- for (size_t index {0 }; index < size_arr; ++index ) array_set (arr, index , rand () % 100 );
72
- qsort (arr, 0 , size_arr - 1 );
65
+ if (!arr) std::invalid_argument (" Array pointer is null" );
66
+ if (array_size (arr) < 2 ) return ;
67
+ qsort (arr);
68
+
69
+ unsigned cnt = 1 ;
70
+ size_t size_arr = array_size (arr);
71
+ Data item = array_get (arr, 0 );
73
72
74
73
for (size_t index {1 }; index < size_arr; ++index ){
75
- if (index == (size_arr - 1 )){
76
- if (index == 1 ){
77
- if (array_get (arr, 0 ) == array_get (arr, 1 )) printf (" %d " , array_get (arr, 0 ));
78
- continue ;
79
- }
80
- if ((array_get (arr, index - 1 ) == array_get (arr, index )) && (array_get (arr, index - 2 ) != array_get (arr, index )))
81
- printf (" %d " , array_get (arr, index ));
82
- continue ;
83
- };
84
-
85
- if (array_get (arr, index ) != array_get (arr, index + 1 )){
86
- if (index == 1 ) {
87
- if (array_get (arr, 0 ) == array_get (arr, 1 ))
88
- printf (" %d " , array_get (arr, 1 ));
89
- continue ;
90
- }
91
-
92
- if ((array_get (arr, index - 1 ) == array_get (arr, index )) && (array_get (arr, index - 2 ) != array_get (arr, index )))
93
- printf (" %d " , array_get (arr, index ));
74
+ if (array_get (arr, index ) == item) cnt++;
75
+ else {
76
+ if (cnt == 2 ) printf (" %d " , item);
77
+ cnt = 1 ;
78
+ item = array_get (arr, index );
94
79
}
95
80
}
96
81
97
- array_delete (arr );
82
+ if (cnt == 2 ) printf ( " %d " , item );
98
83
printf (" \n " );
99
84
}
100
85
101
86
int main (int argc, char **argv)
102
87
{
103
- /*
104
- Не совсем понял откуда берётся с клавиатуры или из файла. В задание сказано из файла, но если запускать тесты, то будет
105
- вызвано исключение runtime_error (в случае если мы ждем числа с клавиатуры)
106
- */
107
88
Array *arr = NULL ;
108
89
FILE *input = fopen (argv[1 ], " r" );
109
90
arr = array_create_and_read (input);
91
+ task1 (arr);
110
92
array_delete (arr);
111
93
/* Create another array here */
112
94
arr = array_create_and_read (input);
95
+ task2 (arr);
113
96
array_delete (arr);
114
-
115
- task1 (input);
116
- task2 (input);
117
97
118
98
fclose (input);
119
99
return 0 ;
0 commit comments