-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab03b.c
297 lines (244 loc) · 6.92 KB
/
lab03b.c
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
/*
* File name: lab03b.c
*
* Author: 2013 Fall Semester AED Team
*
* Release date: 2013/10/04
*
* Description: See the handout for Lab 03.
*
* Version: 1.0
*
* Change log: N/A
*
*/
/* Header Inclusions */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "LinkedList.h"
#include "Entry.h"
#define ASCENDENTE 1
#define DESCENDENTE 2
#define ID 1
#define AGE 2
#define HEIGHT 3
#define WEIGHT 4
int comparisonIdAsc(Item item1, Item item2) {
return (getIdEntry(item1) < getIdEntry(item2) ? 1:0);
}
int comparisonAgeAsc(Item item1, Item item2) {
return (getAgeEntry(item1) < getAgeEntry(item2) ? 1:0);
}
int comparisonHeightAsc(Item item1, Item item2) {
return (getHeightEntry(item1) < getHeightEntry(item2) ? 1:0);
}
int comparisonWeightAsc(Item item1, Item item2) {
return (getWeightEntry(item1) < getWeightEntry(item2) ? 1:0);
}
/*
* Function:
* readEntryFile2LinkedList
*
* Description:
* Reads the entries from a file and creates a linked list with
* these entries in the inverse order.
*
* Arguments:
* Pointer to the file stream:
* FILE * fp
*
* Return value:
* Pointer to the first node of the linked list.
*/
LinkedList * readEntryFile2LinkedList(FILE * fp, int parametro)
{
int id;
int age;
int height;
int weight;
int err=0;
LinkedList * lp;
Entry * entry;
/* Initialize linked list */
lp = initLinkedList();
/* Cycle through file rows */
while(fscanf(fp, "%d %d %d %d", &id, &age, &height, &weight) == 4)
{
/* Create new entry */
entry = newEntry(id, age, height, weight);
/* Store entry in the linked list */
if( parametro == ID )
lp = insertSortedLinkedList(lp,(Item) entry, &comparisonIdAsc, &err);
else if( parametro == AGE )
lp = insertSortedLinkedList(lp,(Item) entry, &comparisonAgeAsc, &err);
else if( parametro == HEIGHT )
lp = insertSortedLinkedList(lp,(Item) entry, &comparisonHeightAsc, &err);
else
lp = insertSortedLinkedList(lp,(Item) entry, &comparisonWeightAsc, &err);
if (err==2) {
printf("Erro de memoria");
exit(2);
}
}
/*caso queiramos ordenar a lista toda:*/
/*
if( parametro == ID ){
if( sentido == ASCENDENTE )
lp = insertSortedLinkedList(lp,(Item) entry, &comparisonIdAsc, &err);
else
lp = insertSortedLinkedList(lp,(Item) entry, &comparisonIdDesc, &err);
}
else if( parametro == AGE ){
if( sentido == ASCENDENTE )
lp = insertSortedLinkedList(lp,(Item) entry, &comparisonAgeAsc, &err);
else
lp = insertSortedLinkedList(lp,(Item) entry, &comparisonAgeDesc, &err);
}
else if( parametro == HEIGHT ){
if( sentido == ASCENDENTE )
lp = insertSortedLinkedList(lp,(Item) entry, &comparisonHeightAsc, &err);
else
lp = insertSortedLinkedList(lp,(Item) entry, &comparisonHeightDesc, &err);
}
else {
if( sentido == ASCENDENTE )
lp = insertSortedLinkedList(lp,(Item) entry, &comparisonWeightAsc, &err);
else
lp = insertSortedLinkedList(lp,(Item) entry, &comparisonWeightDesc, &err);
}
*/
return lp;
}
/*
* Function:
* writeEntryFile
*
* Description:
* Writes the entries from a linked list to a file.
*
* Arguments:
* Pointer to the first node of the linked list:
* LinkedList * lp
* Pointer to the file stream:
* FILE * fp
*
* Return value:
* None.
*/
void writeEntryFile(LinkedList * lp, FILE *fp)
{
LinkedList * aux;
aux = lp;
while(aux != NULL)
{
printEntry((Entry *) getItemLinkedList(aux), fp);
aux = getNextNodeLinkedList(aux);
}
}
void writeEntryFileRev(LinkedList * lp, FILE * fp);
void writeEntryFileRev(LinkedList * lp, FILE * fp) {
if(lp==NULL) return;
writeEntryFileRev(getNextNodeLinkedList(lp), fp);
printEntry((Entry *) getItemLinkedList(lp), fp);
}
/*
* Function:
* freeEntryItem
*
* Description:
* Frees the memory allocated to an Item of the type Entry
*
* Arguments:
* Pointer to the item
* Item item
*
* Return value:
* None.
*/
void freeEntryItem(Item item)
{
freeEntry((Entry *) item);
}
/*
* Function:
* main
*/
int main(int argc, char *argv[])
{
FILE * fp;
LinkedList * lp;
int sentido = ASCENDENTE;
int parametro = ID;
char extOut[] = ".sort";
char * fileNameIn;
char * fileNameOut;
/* Check number of arguments */
if(argc < 2)
{
printf("Usage: lab03b [filename] [arguments opcions]\n");
exit(1);
}
/* Open input file */
fileNameIn = argv[1];
argv++; /* increments index of argv to point to the
second argument */
while (*(++argv)) {
if (strcmp(*argv, "-id") == 0)
parametro = ID;
else if (strcmp(*argv, "-age") == 0)
parametro = AGE;
else if (strcmp(*argv, "-height") == 0)
parametro = HEIGHT;
else if (strcmp(*argv, "-weight") == 0)
parametro = WEIGHT;
else if (strcmp(*argv, "-a") == 0)
sentido = ASCENDENTE;
else if (strcmp(*argv, "-d") == 0)
sentido = DESCENDENTE;
else {
fprintf(stderr, "Wrong arguments\n");
exit(3);
}
}
fp = fopen(fileNameIn, "r");
if(fp == NULL)
{
printf("Open error of input file.\n");
exit(2);
}
/* Read input file and construct linked list with entries */
lp = readEntryFile2LinkedList(fp, parametro);
/* Close input file */
fclose(fp);
/* Determine output filename */
fileNameOut = (char *) malloc(sizeof(char) *
(strlen(fileNameIn) +
strlen(extOut) + 1));
if(fileNameOut == NULL)
{
printf("Memory allocation error for fileNameOut.\n");
exit(1);
}
strcpy(fileNameOut, fileNameIn);
strcat(fileNameOut, extOut);
/* Open output file */
fp = fopen(fileNameOut, "w");
if(fp == NULL)
{
printf("Open error of input file.\n");
exit(2);
}
/* Write entries to output file */
if (sentido==ASCENDENTE)
writeEntryFile(lp, fp);
else if (sentido==DESCENDENTE)
writeEntryFileRev(lp, fp);
/* Free space allocated to fileNameOut */
free(fileNameOut);
/* Close output file */
fclose(fp);
/* Free linked list with entries */
freeLinkedList(lp, freeEntryItem);
return 0;
}