-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3DZ_NumbersInReverse.c
More file actions
112 lines (93 loc) · 2.45 KB
/
Copy path3DZ_NumbersInReverse.c
File metadata and controls
112 lines (93 loc) · 2.45 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
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int* _Data;
int maxIndex;
}DynamicMassive;
DynamicMassive* _createDynamicMassive(int wantedSize)
{
DynamicMassive* _returnMassive = malloc(sizeof(DynamicMassive));
if(_returnMassive == NULL)
{
printf("Could not allocate memory to a dynamic massive with size: %d\n", wantedSize);
return NULL;
}
if(wantedSize == 0)
{
_returnMassive->_Data = malloc(sizeof(int));
_returnMassive->maxIndex = -1;
return _returnMassive;
}
else
{
_returnMassive->_Data = malloc(sizeof(int) * wantedSize);
_returnMassive->maxIndex = wantedSize-1;
return _returnMassive;
}
}
void setElement(DynamicMassive* _dynMas, int number, int value)
{
if(_dynMas == NULL) return;
if(number > (_dynMas->maxIndex))
{
_dynMas->_Data = (int*)realloc(_dynMas->_Data, (number + 1) * sizeof(int));
_dynMas->maxIndex = number;
}
_dynMas->_Data[number] = value;
}
int getElement(DynamicMassive* _dynMas, int number)
{
if(number > (_dynMas->maxIndex) || _dynMas == NULL)
{
printf("Could not access A[%d]!\n", number);
return 0;
}
return _dynMas->_Data[number];
}
void freeMassive(DynamicMassive* _dynMas)
{
free(_dynMas->_Data);
free(_dynMas);
}
int main(int argc, char *argv[])
{
FILE* _h1;
if(argv[1] == NULL)
{
printf("Please supply a file!");
return 0;
}
if(argv[2] == NULL)
{
printf("Please supply an output file name!");
return 0;
}
_h1 = fopen(argv[1], "r");
if(_h1 == NULL)
{
printf("Could not open %s!", argv[1]);
return 0;
}
int numbBuf, counter = 0;
DynamicMassive* _dynMas = _createDynamicMassive(0);
if(_dynMas == NULL) return 0;
while(fscanf(_h1, "%d", &numbBuf) != EOF)
{
setElement(_dynMas, counter, numbBuf);
counter++;
}
fclose(_h1);
_h1 = fopen(argv[2], "w");
if(_h1 == NULL)
{
printf("Could not open %s!", argv[2]);
}
for(int i = _dynMas->maxIndex; i >= 0; i--)
{
fprintf(_h1, "%d ", getElement(_dynMas, i));
}
fclose(_h1);
freeMassive(_dynMas);
return 0;
}