This repository has been archived by the owner on May 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Livro.c
306 lines (295 loc) · 9.22 KB
/
Livro.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
298
299
300
301
302
303
304
305
306
/*
TRABALHO 1 -- SSC0502 -- ALGORITMOS E ESTRUTURAS DE DADOS
PROFESSOR: Bruno Roberto Nepomuceno Matheus
ALUNOS:
Bruno Arantes de Achilles Mello 9866490
Laura Pereira de Gouveia 9880200
*/
#include "Livro.h"
Livro* novoLivro(char* titulo, char* autor, char* editora, uint64 isbn, int ano, int edicao, int copias, int* erro){
Livro* nLi = getLivro();
if(nLi == NULL){
*erro = 1;
return NULL;
}
strcpy(nLi->titulo, titulo);
strcpy(nLi->autor, autor);
strcpy(nLi->editora, editora);
nLi->isbn=isbn;
nLi->edicao=edicao;
nLi->ano=ano;
nLi->copias=copias;
nLi->disponiveis=copias;
initLista(&(nLi->fila));
*erro=0;
return nLi;
}
Livro* findLivro_N(Lista* livros, uint64 isbn, int* erro){
Livro* saida;
if(livros->tamanho == 0){
*erro = 1;
return NULL;
}
Node* no = getNode();
no->valor = livros->inicio->valor;
no->prox = livros->inicio->prox;
uint32 i;
for(i=0;i<livros->tamanho;i++){
if(((Livro*)no->valor)->isbn == isbn){
*erro = 0;
saida = no->valor;
freeNode(no);
return saida;
}
no->valor=no->prox->valor;
no->prox=no->prox->prox;
}
freeNode(no);
*erro = 1;
return NULL;
}
void printLivroInfo(Livro* livro){
if(livro == NULL){
printf("Erro na impressao do livro.\n");
return;
}
printf("\nTitulo: %s%da edicao, %d\n", livro->titulo, livro->edicao, livro->ano);
printf("Autor: %s", livro->autor);
printf("ISBN: %llu\n", livro->isbn);
printf("Disponiveis: %d/%d\n\n", livro->disponiveis, livro->copias);
}
int getAlunoPosition(Livro* livro, Aluno* aluno, int* erro){
if(livro->fila.tamanho == 0){
*erro=1;
return 0;
}
int i;
for(i=0;i<livro->fila.tamanho;i++){
if((Aluno*)atLista(&(livro->fila), i, erro) == aluno){
*erro=0;
return i+1;
}
}
*erro=1;
return 0;
}
int cadastrarLivro(Lista* livros){
int erro, edicao, ano, copias;
Livro* nLi;
uint64 isbn;
char titulo[100], autor[100], editora[100];
printf("Digite o titulo do livro: ");
fflush(stdin);
fgets(titulo, 100, stdin);
printf("Digite o nome do autor: ");
fflush(stdin);
fgets(autor, 100, stdin);
printf("Digite o nome da editora: ");
fflush(stdin);
fgets(editora, 100, stdin);
printf("Digite o ISBN do livro: ");
fflush(stdin);
scanf("%llu", &isbn);
printf("Digite o ano do livro: ");
fflush(stdin);
scanf("%d", &ano);
printf("Digite a edicao do livro: ");
fflush(stdin);
scanf("%d", &edicao);
printf("Digite o numero de copias do livro: ");
fflush(stdin);
scanf("%d", &copias);
nLi = novoLivro(titulo, autor, editora, isbn, ano, edicao, copias, &erro);
if(livros == NULL || erro){
return 1;
}
printLivroInfo(nLi);
return insereInicio(livros, nLi);
}
void findLivro_ST(Lista* livros, char* titulo, Lista* alunos){
Lista matches;
Livro* livro;
initLista(&matches);
int i, j, erro, checker;
for(i=0;i<(livros->tamanho);i++){
livro = atLista(livros, i, &erro);
checker=1;
for(j=0; (j<strlen(titulo) && j<strlen(livro->titulo)); j++){
if(tolower(titulo[j]) != livro->titulo[j] && toupper(titulo[j]) != livro->titulo[j]){
checker=0;
}
}
if(checker){
insereInicio(&matches, atLista(livros, i, &erro));
}
}
for(i=0;i<matches.tamanho;i++){
printf("\n%d)\n", i+1);
printLivroInfo(atLista(&matches, i, &erro));
}
do{
fflush(stdin);
scanf("%d", &i);
if(i<1 || i>matches.tamanho){
printf("Por favor digite um valor entre os mostrados\n");
}
}while(i<1 || i>matches.tamanho);
operarLivro(livros, atLista(&matches, i-1, &erro), alunos);
limpaLista(&matches);
}
void findLivro_SA(Lista* livros, char* autor, Lista* alunos){
Lista matches;
Livro* livro;
initLista(&matches);
int i, j, erro, checker, escolha;
for(i=0;i<(livros->tamanho);i++){
livro = atLista(livros, i, &erro);
checker=1;
for(j=0; (j<strlen(autor) && j<strlen(livro->autor)); j++){
if(tolower(autor[j]) != livro->autor[j] && toupper(autor[j]) != livro->autor[j]){
checker=0;
}
}
if(checker){
insereInicio(&matches, atLista(livros, i, &erro));
}
}
printf("Escolha entre as opcoes encontradas: \n");
for(i=0;i<matches.tamanho;i++){
printf("\n%d)\n", i+1);
printLivroInfo(atLista(&matches, i, &erro));
}
do{
fflush(stdin);
scanf("%d", &escolha);
if(escolha<1 || escolha>matches.tamanho){
printf("Por favor digite um valor entre os mostrados\n");
}
}while(escolha<1 || escolha>matches.tamanho);
operarLivro(livros, atLista(&matches, escolha-1, &erro), alunos);
limpaLista(&matches);
}
void operarLivro(Lista* livros, Livro* livro, Lista* alunos){
uint32 nusp;
char* msg;
Aluno* emprestador;
int escolha, erro;
char texto[100];
printLivroInfo(livro);
printf("Escolha a operacao que deseja realizar:\n");
printf("1) Alterar titulo.\n2) Alterar autor.\n3) Alterar editora.\n4) Alterar isbn.\n5) Alterar edicao.\n6) Alterar ano.\n7) Alterar copias.\n8) Emprestar para aluno.\n9) Devolver livro.\n10) Remover livro.\n11) Nada\n");
do{
fflush(stdin);
scanf("%d", &escolha);
if(escolha<1 || escolha>11){
printf("Por favor digite um valor entre os mostrados\n");
}
}while(escolha<1 || escolha>11);
switch(escolha){
case 1: //TROCA DE TITULO
printf("Digite o novo titulo: ");
fflush(stdin);
fgets(texto, 100, stdin);
strcpy(livro->titulo, texto);
break;
case 2: //TROCA DE AUTOR
printf("Digite o novo autor: ");
fflush(stdin);
fgets(texto, 100, stdin);
strcpy(livro->autor, texto);
break;
case 3: //TROCA DE EDITORA
printf("Digite a nova editora: ");
fflush(stdin);
fgets(texto, 100, stdin);
strcpy(livro->editora, texto);
break;
case 4: //TROCA DE ISBN
printf("Digite o novo ISBN: ");
fflush(stdin);
scanf("%llu", &livro->isbn);
break;
case 5: //TROCA DE EDICAO
printf("Digite a nova edicao: ");
fflush(stdin);
scanf("%d", &livro->edicao);
break;
case 6: //TROCA DE ANO
printf("Digite o novo ano: ");
fflush(stdin);
scanf("%d", &livro->ano);
break;
case 7: //TROCA DO NUMERO DE COPIAS
fflush(stdin);
printf("Digite a diferenca no numero de copias: ");
scanf("%d", &escolha);
if(escolha+livro->disponiveis < 0){
printf("Nao ha essa quantidade de livros disponiveis para ser removida.\n");
}else{
livro->copias+=escolha;
livro->disponiveis+=escolha;
}
break;
case 8: //EMPRESTAR LIVRO
printf("Digite o numero USP do aluno: ");
scanf("%u", &nusp);
emprestador = findAluno_N(alunos, nusp, &erro);
if(erro){
printf("Aluno nao encontrado.\n");
}else{
msg = getMensagem();
if(livro->disponiveis == 0){
printf("Nao ha livros disponiveis, aluno colocado na fila de espera.\n");
entraLista(&livro->fila, emprestador);
insereInicio(&emprestador->livros, livro);
strcpy(msg, "Colocado na fila para o livro ");
strcat(msg, livro->titulo);
pushLista(&emprestador->mensagensPilha, msg);
} else{
livro->disponiveis--;
strcpy(msg, "Livro retirado: ");
strcat(msg, livro->titulo);
pushLista(&emprestador->mensagensPilha, msg);
printf("Livro retirado.\n");
}
}
break;
case 9: //DEVOLVER LIVRO
if(livro->disponiveis == livro->copias){
printf("Nao ha livros para se devolver.\n");
break;
}else{
if(livro->fila.tamanho != 0){
msg = getMensagem();
emprestador = saiLista(&livro->fila, &erro);
strcpy(msg, "Livro disponivel para retirada: ");
strcat(msg, livro->titulo);
pushLista(&emprestador->mensagensPilha, msg);
printf("Livro encaminhado para proximo aluno.\n");
}else{
livro->disponiveis++;
}
}
break;
case 10: //REMOVER LIVRO
removerLivro(livros, livro);
printf("Livro removido.\n");
break;
case 11: //CANCELAR
break;
default:
break;
}
}
void removerLivro(Lista* livros, Livro* livro){
int i, erro;
for(i=0; i<livro->fila.tamanho; i++){
removerLivroAluno(livro, atLista(&livro->fila, i, &erro));
}
for(i=0; i<livros->tamanho; i++){
if(atLista(livros, i, &erro) == livro){
getLista(livros, i, &erro);
}
}
freeLivro(livro);
}