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
| #include "Header.h"
//1
struct Apteka* Memory_Allocation(struct Apteka* a, int r)
{
a = (struct Apteka*)malloc(r * sizeof(struct Apteka));
if (a == NULL)
{
printf("\n Dinamic memory is not allocated for the array ");
exit(1);
}
else
{
printf("\n allocated memory: %d bytes ", r * sizeof(struct Apteka));
}
return a;
}
//2
void Enter_array_elements(struct Apteka * a, int r)
{
int i = 0;
for (int i = 0; i < r; i++)
{
printf("\n Input Apteka's name: ");
scanf("%s", a[i].name);
printf("\n Input Apteka's adress: ");
scanf("%s", a[i].adress);
printf("\n Input Apteka's telephone: ");
scanf("%d", &a[i].telephone);
printf("\n Input when Apteka open: ");
scanf("%d", &a[i].open);
printf("\n Input when Apteka close: ");
scanf("%d", &a[i].close);
}
}
//3
int Writing_an_array_of_structures_to_a_file(struct Apteka* a, const int r)
{
FILE* outFile;
if ((outFile = fopen("Apteka.txt", "w")) == NULL)
{
printf("Error opening file \n");
return 1;
}
for (int i = 0; i < r; i++)
{
fprintf(outFile, " %s %s %d %d %d \n", a[i].name, a[i].adress, a[i].telephone, a[i].open, a[i].close);
}
fclose(outFile);
return 0;
}
//4
int Reading_an_array_of_structures_from_a_file(struct Apteka* a, const int r)
{
FILE* outFile;
outFile = fopen("Apteka.txt", "r");
for (int i = 0; i < r; i++)
{
fprintf(outFile, "%s %s %d %d %d \n", a[i].name, a[i].adress, a[i].telephone, a[i].open, a[i].close);
}
fclose(outFile);
return 0;
}
//5
void Display_array_elements_on_the_screen(struct Apteka * a, int r)
{
int i=0;
printf("\n Apteka info \n");
printf("\n----------------------------------------------------------------------------------\n");
printf("\n| # | Name | Adress | Telephone | Open | Close | \n");
printf("\n----------------------------------------------------------------------------------\n");
for (int i = 0; i < r; i++)
{
printf("| %4d | %-15s | %-13s | %11d | %10d | %10d | \n" , i+1, a[i].name, a[i].adress, a[i].telephone, a[i].open, a[i].close);
printf("\n----------------------------------------------------------------------------------\n");
}
printf("\n----------------------------------------------------------------------------------\n");
}
//6
void Sort_array_elements(struct Apteka * a, const int r)
{
int i, j;
Apteka* a = new Apteka * [r];
Apteka* tmp;
for (int i = 0; i < r; i++)
{
}
for (int j = 0; j < r; j++)
{
for (int i = 0; i < r - j - 1; i++)
{
if (a[i].open > a[i + 1].open)
{
tmp = a[i];
a[i] = a[i + 1];
a[i + 1] = tmp;
}
}
}
printf("\n------------------\n");
printf("Array was successfuly sorted!");
printf("\n------------------\n");
}
//12//
void Delete_memory_of_array(struct Apteka* a)
{
if (a!=NULL)
{
free(a);
a = NULL;
printf("\n %p Memory freed", a);
}
return 0;
} |