forked from JhonatanF/Projeto-Biblioteca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acervo.h
404 lines (390 loc) · 14 KB
/
acervo.h
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#ifndef ACERVO_H_INCLUDED
#define ACERVO_H_INCLUDED
#endif // ACERVO_H_INCLUDED
#include <stdio.h>
#include <windows.h>
#include <dos.h>
#include <string.h>
int cod_livro = 100;
//Estrutura para o cadastro de livros
typedef struct livro {
int codigo;
int quantidade;
char nome_livro[50];
char nome_autor[50];
char area[50];
} CAD_LIVROS;
void procurarLivros();
void editarLivros();
void cadastrarLivros();
void excluirLivros();
int contadorCod();
void listarLivros();
void op_acervo () {
int o = 0;
struct livro li;
do {
menuAcervo();
scanf("%d", &o);
system("cls");
switch(o) {
case 1:
cadastrarLivros();
break;
case 2:
excluirLivros();
break;
case 3:
editarLivros();
break;
case 4:
listarLivros();
break;
case 5:
return;
default:
system("cls");
printf("\nOpcao invalida\n");
system("pause");
break;
}
fflush(stdin);
} while(o != 5);
}
int contadorCod() {
int aux = 0;
FILE *arquivo;
arquivo = fopen("cad_livros.txt", "rb");
CAD_LIVROS li;
if(arquivo == NULL) {
system("cls");
printf("\n\n Problemas na abertura do arquivo!\n");
system("pause");
} else
while(fread(&li, sizeof(CAD_LIVROS), 1,arquivo) == 1)
if(&(li.codigo) != NULL)
aux++;
fclose(arquivo);
return aux;
}
int verificarcodigo(int aux){
int a = 0;
FILE *arquivo;
arquivo = fopen("cad_livros.txt", "rb");
CAD_LIVROS li;
verificadorArquivo(arquivo);
while(fread(&li, sizeof(CAD_LIVROS), 1, arquivo) == 1) {
if(aux == li.codigo){
fclose(arquivo);
return 1;
}
}
fclose(arquivo);
}
void cadastrarLivros() {
system("cls");
FILE* arquivo; //FILE (sempre maiuscula), variável do tipo file (arquivo é o nome do ponteiro), nesta linha está sendo criado o ponteiro
CAD_LIVROS livros;
arquivo = fopen ("cad_livros.txt", "ab");// "ab" acrescenta dados binários no fim do arquivo e se o arquivo não existir ele será criado e
//e fopen é responsável fazer com que o ponteiro aponte para um arquivo no programa
verificadorArquivo(arquivo);
char op;
int aux = 0;
aux = contadorCod()+1;
system("cls");
fflush(stdin);
menuCIMA(strlen("Codigo do Livro: ") + 15);
menuOPCAO("Codigo do livro: ", strlen("Codigo do livro: ") + 15);
gotoXY(0, 2);
menuBAIXO(strlen("Codigo do livro: ") + 15);
gotoXY(22, 1);
aux = aux + cod_livro;
if(verificarcodigo(aux) == 1){
aux = aux + 2;
}
printf("%d\n\n", aux);
livros.codigo = aux;
system("pause");
system("cls");
fflush(stdin);
menuCIMA(strlen("Area do Livro: ") + 30);
menuOPCAO("Area do livro: ", strlen("Area do livro: ") + 30);
gotoXY(0, 2);
menuBAIXO(strlen("Area do livro: ") + 30);
gotoXY(17, 1);
scanf("%s", &livros.area);
strupr(livros.area);
system("cls");
fflush(stdin);
menuCIMA(strlen("Digite o Nome do livro: ") + 50);
menuOPCAO("Digite o Nome do livro: ", strlen("Digite o Nome do livro: ") + 50);
gotoXY(0, 2);
menuBAIXO(strlen("Digite o Nome do livro: ") + 50);
gotoXY(26, 1);
gets(livros.nome_livro);
strupr(livros.nome_livro); //Converte o conteúdo digitado em maiúsculo.
fflush(stdin);
system("cls");
menuCIMA(strlen("Digite o Nome do Autor do livro: ") + 30);
menuOPCAO("Digite o Nome do Autor do livro: ", strlen("Digite o Nome do Autor do livro: ") + 30);
gotoXY(0, 2);
menuBAIXO(strlen("Digite o Nome do Autor do livro: ") + 30);
gotoXY(35, 1);
gets(livros.nome_autor);
strupr(livros.nome_autor);//Converte o conteúdo digitado em maiúsculo.
system("cls");
do {
fflush(stdin);
menuCIMA(strlen("Quantidade de Livros: ") + 10);
menuOPCAO("Quantidade de Livros: ", strlen("Quantidade de Livros: ") + 10);
gotoXY(0, 2);
menuBAIXO(strlen("Quantidade de Livros: ") + 10);
gotoXY(24, 1);
scanf("%d", &livros.quantidade);
} while(livros.quantidade <= 0);
fwrite(&livros, sizeof(CAD_LIVROS), 1, arquivo); // o numero 1 representa a quantidade de elementos que desejo gravar na struct
//SIZEOF passa para a função o tamanho em bytes da struct
system("cls");
gotoXY(0, 2);
printf("\n\n Livro gravado com sucesso!\n\n");
gotoXY(22, 1);
system("pause");
fclose(arquivo); //Fecha o arquivo que foi aberto.
system("cls");
}
void editarLivros() {
int op = 0;
menuCIMA(42);
menuOPCAO(" PROCURAR LIVROS PARA EDITAR", 42);
menuBAIXO(42);
do {
printf("\n 1 - Procurar Por Codigo do livro ");
printf("\n 2 - Sair");
printf("\n Opcao: ");
scanf("%d", &op);
setbuf(stdin, NULL);
system("cls");
switch(op) {
case 1:
procurarLivros();
break;
case 2:
return;
break;
default:
system("cls");
printf("\n\n Opcao invalida\n");
system("pause");
break;
}
} while(op != 2);
}
void listarLivros() {
int x = 0;
FILE *arquivo;
arquivo = fopen("cad_livros.txt", "rb");
CAD_LIVROS livros;
menuCIMA(29);
menuOPCAO(" LISTAGEM DE LIVROS", 29);
menuBAIXO(29);
while(fread(&livros, sizeof(CAD_LIVROS), 1,arquivo)==1) {
printf("\n Codigo do Livro: %d\n", livros.codigo);
printf("\n Nome do Livro: %s\n", livros.nome_livro);
printf("\n Nome do Autor do Livro: %s\n", livros.nome_autor);
printf("\n Area que o Livro se encaixa: %s\n", livros.area);
printf("\n Quantidade de Livros no Acervo: %d\n", livros.quantidade);
printf("\n ---------------------------------------------\n");
x++;
}
if(x == 0) {
printf("\n\n LISTA VAZIA!\n\n\n");
}
menuCIMA(31);
menuOPCAO(" FIM DA LISTAGEM DE LIVROS", 31);
menuBAIXO(31);
system("pause");
system("cls");
fclose(arquivo);
}
void procurarLivros() {
CAD_LIVROS livros;
//variavel para contar a posição
int i=0, op=0;
//variavel para salvar o que o usuario digitar e comparar com os que está no arquivo
int auxiliar;
//Criando um arquivo, com um ponteiro do tipo FILE
FILE *arquivo = fopen("cad_livros.txt", "r+b");
if(arquivo == NULL) {
printf("\n Impossivel Abrir o arquivo!\n");
} else {
fflush(stdin);
menuCIMA(57);
menuOPCAO("Digite o Codigo do Livro que Deseja Procurar: ", 57);
menuBAIXO(57);
gotoXY(48, 1);
scanf("%d", &auxiliar);
system("cls");
//lê todos os dados do arquivo até encontrar o final do arquivo (EOF)
while(fread(&livros, sizeof(CAD_LIVROS), 1,arquivo)==1) {
i++;
//comparar a cpf que o usuario digitou com os que ja estão no arquivo
if(auxiliar == livros.codigo) {
menuCIMA(32);
menuOPCAO(" DADOS ATUAIS ", 32);
menuBAIXO(32);
printf("\n Codigo do livro: %d", livros.codigo);
printf("\n Nome do livro: %s", livros.nome_livro);
printf("\n Nome do Autor do Livro: %s", livros.nome_autor );
printf("\n Area de Conhecimento do Livro: %s", livros.area );
printf("\n Quantidade de Livros: %d\n\n", livros.quantidade);
menuCIMA(32);
menuOPCAO("O que voce deseja alterar ?", 32);
menuBAIXO(32);
printf("\n 1 - Nome do Livro");
printf("\n 2 - Nome do Autor do Livro");
printf("\n 3 - Area de Conhecimento do Livro");
printf("\n 4 - Quantidade de Livros");
printf("\n 5 - Sair\n");
menuCIMA(10);
menuOPCAO(" OPCAO: ", 10);
menuBAIXO(10);
gotoXY(10, 20);
scanf("%d", &op);
system("cls");
switch(op) {
case 1:
fflush(stdin);
menuCIMA(strlen("Digite o Novo Nome do Livro: ") + 30);
menuOPCAO("Digite o Novo Nome do Livro: ", strlen("Digite o Novo Nome do Livro: ") + 30);
gotoXY(0, 2);
menuBAIXO(strlen("Digite o Novo Nome do Livro: ") + 30);
gotoXY(strlen("Digite o Novo Nome do Livro: "), 1);
fgets(livros.nome_livro, 50, stdin);
//O novo campo que foi digitado vai receber um \0 para indicar o fim
livros.nome_livro[strlen(livros.nome_livro) - 1]= '\0';
strupr(livros.nome_livro);
break;
case 2:
fflush(stdin);
menuCIMA(strlen("Digite o Novo Nome do Autor do Livro: ") + 30);
menuOPCAO("Digite o Novo Nome do Autor do Livro: ", strlen("Digite o Novo Nome do Autor do Livro: ") + 30);
gotoXY(0, 2);
menuBAIXO(strlen("Digite o Novo Nome do Autor do Livro: ") + 30);
gotoXY(strlen("Digite o Novo Nome do Autor do Livro: "), 1);
fgets(livros.nome_autor, 50, stdin);
//O novo campo que foi digitado vai receber um \0 para indicar o fim
livros.nome_autor[strlen(livros.nome_autor) - 1]= '\0';
strupr(livros.nome_autor);
break;
case 3:
fflush(stdin);
menuCIMA(strlen("Digite a Nova Area de Conhecimento do Livro: ") + 30);
menuOPCAO("Digite a Nova Area de Conhecimento do Livro: ", strlen("Digite a Nova Area de Conhecimento do Livro: ") + 30);
gotoXY(0, 2);
menuBAIXO(strlen("Digite a Nova Area de Conhecimento do Livro: ") + 30);
gotoXY(strlen("Digite a Nova Area de Conhecimento do Livro: "), 1);
fgets(livros.nome_livro, 50, stdin);
//O novo campo que foi digitado vai receber um \0 para indicar o fim
livros.area[strlen(livros.area) - 1]= '\0';
strupr(livros.area);
break;
case 4:
fflush(stdin);
menuCIMA(strlen("Digite a Nova Quantidade de Livros: ") + 20);
menuOPCAO("Digite a Nova Quantidade de Livros: ", strlen("Digite a Nova Quantidade de Livros: ") + 20);
gotoXY(0, 2);
menuBAIXO(strlen("Digite a Nova Quantidade de Livros: ") + 20);
gotoXY(strlen("Digite a Nova Quantidade de Livros: "), 1);
scanf("%d", &livros.quantidade);
break;
case 5:
break;
default:
printf("Opcao Incorreta!\n");
}
fseek(arquivo,(i-1)*sizeof(CAD_LIVROS),SEEK_SET);
if(fwrite(&livros,sizeof(CAD_LIVROS), 1, arquivo) != 1) {
printf("\n Falha ao Alterar o registro!\n");
system("pause");
} else {
printf("\n Registro alterado com sucesso!\n");
}
break;
}
}
}
menuCIMA(32);
menuOPCAO(" FIM DA EDICAO ", 32);
menuBAIXO(32);
system("pause");
system("cls");
fclose(arquivo);
}
void excluirLivros() {
CAD_LIVROS li;
int i = 0;
char auxiliar[12];
//Criando um arquivo, com um ponteiro do tipo FILE
FILE *arquivo = fopen("cad_livros.txt", "r+b");
FILE *arq_auxiliar = fopen("auxiliar.txt", "w+b");
if(arquivo == NULL || arq_auxiliar == NULL) {
printf("\n\n Impossivel Abrir o arquivo!\n");
} else {
fflush(stdin);
menuCIMA(60);
menuOPCAO("Digite o Nome do Livro que Deseja Excluir: ", 60);
menuBAIXO(60);
gotoXY(47, 1);
gets(auxiliar);
strupr(auxiliar);
while(fread(&li, sizeof(CAD_LIVROS), 1,arquivo)==1) {
i++;
if(strcmp(auxiliar, li.nome_livro) == 0) {
fseek(arquivo,(i-1) * sizeof(CAD_LIVROS), SEEK_SET);
} else {
fwrite( &li, sizeof(CAD_LIVROS), 1, arq_auxiliar);
}
}
fclose(arquivo);
fclose(arq_auxiliar);
if(remove("cad_livros.txt")==0) {
menuCIMA(60);
menuOPCAO(" Livro excluido com Sucesso", 60);
menuBAIXO(60);
} else {
menuCIMA(60);
menuOPCAO(" Nao foi possivel excluir o Livro ", 60);
menuBAIXO(60);
printf("\n\n%s\n\n", strerror(errno));
}
rename("auxiliar.txt", "cad_livros.txt");
system("pause");
system("cls");
fclose(arquivo);
fclose(arq_auxiliar);
}
system("pause");
system("cls");
fclose(arquivo);
fclose(arq_auxiliar);
}
int menuAcervo() {
menuCIMA(40);
printf(" %c LIVROS %c\n", 186, 186);
menuOPCAO("", 40);
menuOPCAO(" 1 - Cadastrar livro", 40);
menuOPCAO("", 40);
menuOPCAO(" 2 - Remover livro", 40);
menuOPCAO("", 40);
menuOPCAO(" 3 - Editar livro", 40);
menuOPCAO("", 40);
menuOPCAO(" 4 - Listar livros Cadastrados", 40);
menuOPCAO("", 40);
menuOPCAO(" 5 - Ir para menu anterior", 40);
menuOPCAO("", 40);
menuBAIXO(40);
menuCIMA(10);
menuOPCAO(" OPCAO: ", 10);
menuBAIXO(10);
gotoXY(10, 15);
}