-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
271 lines (234 loc) · 8.15 KB
/
main.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
/*
* TITLE: PROGRAMMING II LABS
* SUBTITLE: Practical 2
* AUTHOR 1: Hugo Mato Cancela LOGIN 1: hugo.matoc
* AUTHOR 2: Celia Sousa Boo LOGIN 2: celia.sousa
* GROUP: 1.1
* DATE: 31 / 03 / 22
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#define MAX_BUFFER 255
#include "bid_stack.h"
#include "product_list.h"
tProductCategory strToCategory(char* str) {
if (strcmp(str, "book") == 0){
return book;
}
else return painting;
}
/*{ Objetivo: Cambiar de string a category
Entradas: Un string
Salidas: Category
Precondiciones: Introducir una categoria existente
Postcondiciones: -
}*/
const char* categoryToStr(tProductCategory category){
if (category == book){
return "book";
}
return "painting";
}
/*{ Objetivo: Cambiar de category a string
Entradas: Category
Salidas: Un string
Precondiciones: Introducir una categoría existente
Postcondiciones: -
}*/
void new(char *param1, char *param2, char *param3, char *param4,tList *L){
if(findItem(param1, *L)!=LNULL)
printf("+ Error: New not possible\n");
else{
tItemL I;
float param4_f;
tStack T;
{
//definimos el Item
strcpy(I.productId, param1);
strcpy(I.seller, param2);
//pasamos de char a category
I.productCategory= strToCategory(param3);
//pasamos de *char a float
param4_f = atof(param4);
I.productPrice = param4_f;
I.bidCounter=0;
//Creo una pila vacia
createEmptyStack(&T);
I.bidStack=T;
}
if(insertItem(I, L)!=false)
printf("* New: product %s seller %s category %s price %.2f\n", param1, param2, param3, param4_f);
else
printf("+ Error: New not possible\n");
}
}
/* Objetivo: Añadir un producto nuevo a la lista
Entradas: Identificar de producto, identificador del vendedor, categoria y precio del producto.
Salidas: Identificar de producto, identificador del vendedor, categoria y precio del producto.
Precondiciones: Que no exista un producto con el mismo identificador y que la lista no esté completa.
Postcondiciones: -
*/
void bid(tProductId PI,char UI,tProductPrice PP,tList *L, tStack *S){
//declaracion de variables
tPosL p = findItem(PI, *L);
//el item por el que quiere pujar no existe
if (p == LNULL)
printf("+ Error: Bid not possible");
//el item existe
else {
tItemL I = getItem(p, *L);
tItemS T;
{
//Definimos el Item en el Stack
strcpy(T.bidder, UI);
//Copiamos el precio del Item en el Stack
T.productPrice = PP;
}
//el precio por el que puja es menor o igual al actual o quien puja es el vendedor
if (strcmp(&UI, I.seller) == 0 || PP <= I.productPrice)
printf("+ Error: Bid not possible\n");
else {
//la puja se realiza con exito
I.bidCounter++;
I.productPrice = PP;
updateItem(I,p,L);
push(T, S);
printf("* Bid: product %s seller %s category %s price %.2f bids %d\n", I.productId, I.seller,
categoryToStr(I.productCategory),I.productPrice, I.bidCounter);
}
}
}
/*{ Objetivo: Puja por un determinado producto
Entradas: Identificador del producto, identificador de usuario, precio del producto y una lista
Salidas: Identificador de producto, identificcador del usuario que pide el producto, categoria del producto,
precio actualizado y contador de pujas actualizado
Precondiciones: Tiene que existir el producto por el que quiero pujar y que la puja realizada sea mayor que
la anterior
Postcondiciones: -
}*/
void delete(tProductId PI,tList* L){
if (isEmptyList(*L))
printf("+ Error: Delete not possible");
else{
//Declaracion de variables
tPosL p = findItem(PI, *L);
//Nombre de producto no encontrado
if(p==LNULL)
printf("+ Error: Delete not possible");
//El producto está en la lista
else{
tItemL I = getItem(p, *L);
tStack D = I.bidStack;
printf("* Delete: product %s seller %s category %s price %.2f bids %d",
I.productId, I.seller, categoryToStr(I.productCategory), I.productPrice, I.bidCounter);
do {
pop(&D);
} while (isEmptyStack(D));
deleteAtPosition(p, L);
}
}//else
}//delete
/* Objetivo: Eliminar productos de la lista.
Entradas: Identificador del producto y una lista.
Salidas: La lista sin el producto eliminado.
Precondiciones: Lista inicializada no vacia y que el producto este en la lista
Postcondiciones: -
*/
void award(tProductId PI, tList* L){
if(isEmptyList(*L))
printf("+ Error: Award not possible");
else{
//Declaracion de variables
tPosL p = findItem(PI, *L);
//Nombre de producto no encontrado
if(p==LNULL)
printf("+ Error: Award not possible");
else{
tItemL I = getItem(p, *L);
tStack D = I.bidStack;
tItemS S = peek(D);
printf("* Award: product %s bidder %s category %s price %.2f", I.productId, S.bidder,
categoryToStr(I.productCategory), S.productPrice);
do {
pop(&D);
} while (isEmptyStack(D));
deleteAtPosition(p, L);
}//else
}//else
}
/* Objetivo: Mostrar el ganador de la puja
Entradas: Identificador de producto y una lista
Salidas: Identificador de producto, ganador de la puja, precio final de la puja y categoria del producto
Precondiciones: Lista inicializada no vacia, que el producto este en la lista y que existan pujas para
dicho producto
Postcondiciones: El producto mostrado se eliminara de la lista
*/
void processCommand(char *commandNumber, char command, char *param1, char *param2, char *param3, char *param4, tList *L, tStack *S) {
switch (command) {
case 'N':
printf("********************\n");
printf("%s %c: product %s seller %s category %s price %s", commandNumber, command, param1, param2, param3, param4);
new(param1, param2, param3, param4, L);
break;
case 'S':
break;
case 'B':
printf("********************\n");
printf("%s %c: product %s bidder %s price %s", commandNumber, command, param1, param2, param4);
//pasamos de *char a float
float param4_f;
param4_f = atof(param4);
bid(param1, *param2, param4_f, L, S);
break;
case 'D':
printf("********************\n");
printf("%s %c: product %s", commandNumber, command, param1);
delete(param1, L);
break;
case 'A':
printf("********************\n");
printf("%s %S: producto %s", commandNumber, command, param1);
break;
case 'W':
break;
case 'R':
break;
default:
break;
}
}
void readTasks(char *filename) {
FILE *f = NULL;
char *commandNumber, *command, *param1, *param2, *param3, *param4;
const char delimiters[] = " \n\r";
char buffer[MAX_BUFFER];
f = fopen(filename, "r");
if (f != NULL) {
while (fgets(buffer, MAX_BUFFER, f)) {
commandNumber = strtok(buffer, delimiters);
command = strtok(NULL, delimiters);
param1 = strtok(NULL, delimiters);
param2 = strtok(NULL, delimiters);
param3 = strtok(NULL, delimiters);
param4 = strtok(NULL, delimiters);
processCommand(commandNumber, command[0], param1, param2, param3, param4);
}
fclose(f);
} else {
printf("Cannot open file %s.\n", filename);
}
}
int main(int nargs, char **args) {
char *file_name = "new.txt";
if (nargs > 1) {
file_name = args[1];
} else {
#ifdef INPUT_FILE
file_name = INPUT_FILE;
#endif
}
readTasks(file_name);
return 0;
}