-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1DZ_NumberOrderer.c
More file actions
99 lines (79 loc) · 3.07 KB
/
1DZ_NumberOrderer.c
File metadata and controls
99 lines (79 loc) · 3.07 KB
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
#include <stdlib.h>
#include <stdio.h>
const int STRING_SIZE = 255;
int* open_file_read_numbers(char* _name)
{
FILE *_h;
_h = fopen(_name, "rw+");
if(!_h)
{
printf("Couldn't open %s!", _name);
return NULL;
}
int counter = 0;
char* _buf = (char*)malloc(sizeof(char));
while(fgets(_buf, STRING_SIZE, _h)) counter++; //ñ÷èòàåì êîë-âî ÷èñåë â ôàéëå
int* _returnVector = (int*)malloc(sizeof(int) * counter + 1);
realloc(_buf, sizeof(int));
rewind(_h);
for(int i = 1; i < counter+1; i++)
{
fgets(_buf, 255, _h);
*(_returnVector + i) = atoi(_buf);
}
*_returnVector = counter;
free(_buf);
fclose(_h);
return _returnVector;
}
void sort_numbers(int numberOfNumbers, int* _massiv)
{
_massiv++;
for(int i = 0; i < numberOfNumbers; i++)
{
for(int j = i; j < numberOfNumbers; j++)
{
if(*(_massiv + j) == 0)
{
int buf = *(_massiv + i);
*(_massiv + i) = *(_massiv + j);
*(_massiv + j) = buf;
}
}
}
for(int i = 0; i < numberOfNumbers; i++)
{
if(*(_massiv + i) == 0) continue;
for(int j = i; j < numberOfNumbers; j++)
{
if(*(_massiv + j) < 0)
{
int buf = *(_massiv + i);
*(_massiv + i) = *(_massiv + j);
*(_massiv + j) = buf;
}
}
}
}
int main(int argc, char *argv[])
{
if(argv[1] == NULL)
{
printf("Please provide a file");
return 0;
}
int* _numbers = open_file_read_numbers(argv[1]); //ïåðâîå ÷èñëî â ïîëó÷åííîì ìàññèâå - êîëè÷åñòâî ÷èñåë â ìàññèâå
if(_numbers == NULL)
{
printf("Please provide a text file filled with a number on each line.");
}
sort_numbers(*_numbers, _numbers);
FILE *_h;
_h = fopen(argv[1], "w+");
for(int i = 1; i < *_numbers + 1; i++)
{
// printf("%d \n", *(_numbers + i));
fprintf(_h, "%d \n", *(_numbers + i));
}
fclose(_h);
}