-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkarin.c
More file actions
331 lines (290 loc) · 8.1 KB
/
karin.c
File metadata and controls
331 lines (290 loc) · 8.1 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
const int catMax=51;
const int cityMax=51;
const int strMax=101;
const int desMax=201;
typedef struct Property{
char category[51];
char city[51];
char street[101];
int area;
int price;
char description[201];
struct Property *next;
} PROPERTY;
//pomocna funkcia na kontrolu obsahu prveho zaznamu
int headCheck(PROPERTY **first){
if (*first == NULL){
return 1;
}
return 0;
}
void loadNode(PROPERTY current){
fgets(current->category, catMax, file);
fgets(current->city, cityMax, file);
fgets(current->street, strMax, file);
fscanf(file,"%d %d ", ¤t->area, ¤t->price);
fgets(current->description, desMax, file);
}
//funkcia na vytvorenie spajaneho zoznamu struktur zo suboru
//vstupuje do nej smernik na prvy zaznam a pocet zaznamov
int createListFromFile(PROPERTY **first, int val){
*first = malloc(sizeof(PROPERTY));
int count = 0;
char input;
FILE *file;
val = 0;
if ((file = fopen("reality.txt", "r")) == NULL) {
printf("Zaznamy neboli nacitane");
}
else{
while ((input = fgetc(file))!= EOF) {
if (input == '&' || input == '$') {
val++;
}
}
rewind(file);
PROPERTY *current = NULL, *last = NULL;
while ((fscanf(file, "%c ", &input)) != EOF) {
count++;
current = malloc(sizeof(PROPERTY));
loadNode(current);
if (count != 1) {
last->next = current;
current->next = NULL;
last = current;
}
else{
*first = last = current;
}
}
printf("Nacitalo sa %d zaznamov\n", val);
}
return val;
}
//funkcia na vypis jedneho zaznamu
void printNode(PROPERTY current){
printf("kategoria ponuky: %s
miesto ponuky: %s
ulica: %s
rozloha v m2: %d\n
cena: %d\n
popis: %s",
current->category,
current->city,
current->street,
current->area,
current->price,
current->description);
}
//funkcia na vypis celeho spajaneho zoznamu struktur
int printList(PROPERTY *first, int val) {
headCheck(&first);
PROPERTY *current = first;
int i;
//cyklus urceny na prechod celym zoznamom
for (i = 1; i <= val; i++) {
printf("%d. \n", i);
printNode(current);
if(current->next == NULL){
break;
}
current = current->next;
}
return 0;
}
//funkcia, ktora sluzi na pridanie zaznamu na poziciu position zadanu pouzivatelom
//zaznam sa prida na position+1 v spajanom zozname struktur
void addProperty(PROPERTY **first, int *val){
int position, i = 1;
scanf(" %d ", &position);
if(*first == NULL) {
*first = malloc(sizeof(PROPERTY));
position = 1;
}
PROPERTY *current = *first;
struct Property *temp = malloc(sizeof(PROPERTY));
loadNode(temp);
//pokial je pozicia jedna, pridavam na zaciatok
if(position == 1) {
PROPERTY *newNode = malloc(sizeof(PROPERTY));
newNode = temp;
newNode->next = *first;
*first = newNode;
}
//pokial je pozicia vacsia ako jedna, prechadzam zoznam
//zaznam pridam na zadanu poziciu
else if(position > 1) {
position = 0;
for(i = 1; i < position; i++) {
if(current->next == NULL){
break;
}
current = current->next;
}
PROPERTY *newNode = malloc(sizeof(PROPERTY));
newNode = temp;
if(current->next){
newNode->next = current->next;
}
else{
newNode->next = NULL;
}
current->next = newNode;
}
(*val)++;
}
//pomocna funkcia, ktora vsetky velke pismena v zadanom stringu zmeni na male
char *toLower(char *str) {
int i;
char *retStr = strdup(str);
for (i = 0 ; str[i]; ++i){
retStr[i] = tolower(str[i]);
}
return retStr;
}
//funkcia, ktora odstrani zaznamy na zaklade vzadaneho stupu delSubstr
//funkcia porovna zaznam o mieste a pokial ho dana struktura obsahuje, vymaze ho
int deleteProperty(PROPERTY **first, int *val){
PROPERTY *current = *first;
PROPERTY *prev = malloc(sizeof(PROPERTY));
prev = NULL;
int delCounter = 0, changePrev =0;
char *delSubstr = calloc(cityMax, sizeof(char));
scanf(" %[^\n]", delSubstr);
delSubstr = toLower(delSubstr);
while (current) {
changePrev = 1;
if (strstr(toLower(current->city), delSubstr)) {
if (prev && current->next) {
prev->next = current->next;
changePrev = 0;
(*val)--;
delCounter++;
}
else if (!prev && current->next) {
while (strstr(toLower(current->city), delSubstr)) {
if (current->next) {
*first = (*first)->next;
current = *first;
}
else {
free(current);
*first = NULL;
(*val) = 0;
break;
}
}
(*val)--;
delCounter++;
}
else if (!prev && !current->next) {
free(current);
*first = NULL;
(*val) = 0;
delCounter++;
}
else if (prev && !current->next) {
free(current);
current = prev->next = NULL;
(*val)--;
delCounter++;
break;
}
}
if (changePrev) {
prev = current;
current = current->next;
}
else {
current = current->next;
}
}
printf("Vymazalo sa %d zaznamov\n", delCounter);
return *val;
}
//funkcia ktora porovnava hladane mesto a mesta v zozname
//pokial ho zoznam obsahuje, aktualizuje ho
int addPropertyByCity(PROPERTY **first){
headCheck(&first);
char searchCity[51];
int editCount = 0;
fscanf(stdin, "%s ", searchCity);
PROPERTY *new = NULL;
new = malloc(sizeof(PROPERTY));
loadNode(new);
PROPERTY *current = *first;
char *searchCityPointer = toLower(searchCity);
while(1){
char *currentLocation = toLower(current->city);
if (strstr(currentLocation, searchCityPointer)){
strcpy(current->category, new->category);
strcpy(current->city, new->city);
strcpy(current->street, new->street);
current->area = new->area;
current->price = new->price;
strcpy(current->description, new->description);
editCount++;
}
if (current->next == NULL){
break;
}
current = current->next;
}
printf("Aktualizovalo sa %d zaznamov\n", editCount);
return 0;
}
//funkcia ktora vyhladava zaznamy s cenou rovnou alebo mensou ako je hladana
int searchPropertyByPrice(PROPERTY **first){
headCheck(&first);
int searchPrice = 0;
int counter = 0;
scanf("%d", &searchPrice);
PROPERTY *current = NULL;
current = *first;
while (current->next != NULL) {
//porovnava zaznamy
if (current->price <= searchPrice) {
counter++;
printf("%d.\n",counter);
printNode(current);
}
current = current->next;
}
//porovnava posledny zaznam
if (current->price <= searchPrice) {
counter++;
printf("%d.\n",counter);
printNode(current);
}
if (counter == 0) {
printf("V ponuke su len reality s vyssou cenou\n");
}
return 0;
}
int main()
{
char input = 0;
int val = 0;
PROPERTY *first = NULL;
switch(input) {
scanf("%c", &input);
case n: val = createListFromFile(&first, val);
break;
case v: printList(first, val);
break;
case p: addProperty(&first, &val);
break;
case z: deleteProperty(&first, &val);
break;
case h: searchPropertyByPrice(&first);
break;
case a: getc(stdin);
addPropertyByCity(&first);
break;
}
return 0;
}