This repository has been archived by the owner on Aug 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1294 lines (1211 loc) · 34 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
// constantes de arquivos
#define IMOVEL_FILE "imovel.bin" // Arquivo contendo os dados da struct House
#define PROPRIETARIO_FILE "proprietario.bin" // Arquivo contendo os dados da struct Owner
#define LOCATARIO_FILE "locatario.bin" // Arquivo contendo os dados da struct locatario
// constantes semanticas
#define SAIR 0
#define CRIAR_PROPRIETARIO 1
#define CRIAR_IMOVEL 2
#define LISTAR_PROPRIETARIO 3
#define LISTAR_IMOVEIS 4
#define ALUGAR_IMOVEL 5
#define LISTAR_LOCATARIOS 6
#define TERMINAR_CONTRATO 7
#define REALIZAR_ALTERACAO 8
#define GERAR_RELATORIO 9
struct addressOwner {
char logradouro[80];
char bairro[20];
char CEP[10];
char cidade[20];
char estado[20];
char fone[15];
char cel[15];
char email[30];
};
struct informacao_casa {
int num_casa;
char status_casa;
};
struct owner {
int reg_prop;
char nome[80];
char CPF[15];
int qntd_de_casas;
struct addressOwner sAddress;
struct informacao_casa sCasa;
};
struct info_loc {
int reg_loc;
};
union data {
char sigla;
struct info_loc loc;
};
struct houseAddress {
char logradouro[80];
char bairro[20];
char CEP[10];
char cidade[20];
};
struct house {
int reg_imov;
struct houseAddress address;
float area;
int quartos;
float valor;
union data status;
};
struct date {
int day;
int month;
int year;
};
struct locatario {
int reg_loc; // gerado automaticamente
char nome[80];
char CPF[15];
struct addressOwner end_loc;
int reg_imov; // registro imóvel – buscar no arq. Imóvel
int dia_venc;
struct date inicio;
struct date termino;
};
// Funcoes de Proprietario
int countOwners(); // Retorna a quantidade de proprietarios no arquivo
struct addressOwner stdWriteAddressOwner(); // Cadastro da struct addressOwner
struct owner stdWriteOwner(); // Cadastro da struct geral owner
struct informacao_casa stdWriteHouseOwner(); // Cadastro da struct informacao_casa
void writeOwner(struct owner _owner); // Escrita dos dados Owner para o arquivo
void stdReadOwner(struct owner _owner); // Mostra dos dados salvos referente ao Owner
void readOwners(); // Leitura dos dados contidos no arquivo referente ao owner
void searchByCPF(char cpf[15]); // Função para busca com filtro referente ao CPF
void stdReadAddressOwner(struct addressOwner _address); // Mostra dos dados da struct addressOwner
void stdReadOwnerHouseAddress(struct informacao_casa _address); // Mostra dos dados da struct informacao_casa
int searchCPF_owner(char cpf[15]);
void altera_sigla_owner(int pos);
// Funcoes de imoveis
void stdReadHouseAddress(struct houseAddress _house); // Mostra dos dados referente a struct houseAddress
void stdReadHouse(struct house _house); // Mostra dos dados referente a struct House
void readHouses(char op); // Função da leitura dos dados adquiridos dentro do arquivo
int countHouses(); // Contador de quantos dados existem dentro do arquivo
struct house stdWriteHouse(); // Cadastro dos dados referente a struct House
struct house searchHouseByRegister(int _register);
void writeHouse(struct house _house); // Escrita dos dados house no arquivo
void readHouseBairro(char bairro[20]);
void readHouseRoom(int quartos);
void readHouseArea(float area);
int searchByRegister(int _register);
struct date inputDate();
struct locatario stdReadLocatario();
void stdWriteLocatario(struct locatario _locatario);
void writeLocatario(struct locatario _locatario);
void readLocatarios();
void alterStatusHouse(int reg);
void endContract(struct locatario _locatario);
struct locatario searchLocatariobyCPF(char cpf[15]);
int cmpDate(struct date _date1, struct date _date2);
void altera_sigla_house(int pos);
// Funcoes do locatario
int altera_locatario(int pos);
int altera_house(int pos);
int altera_owner(int pos);
int searchREG_house(int reg);
int searchCPF_locatario(char cpf[15]);
int countLocatario();
struct locatario search_locatario(char cpf[15]);
int deleta_locatario(struct locatario _locatario);
int compare_date(struct date _date1, struct date _date2);
struct owner searchOwnerByRegister(int _register);
void makeReport(struct date _date);
int main() {
int op, want_rent, reg_num, pos, reg, opcao8, check,dat;
char cpf[15], parametro, opc, escolha;
struct house _house;
struct owner _owner;
struct locatario _locatario;
struct date _date;
do {
fflush(stdin);
system("cls");
showStartMenu();
scanf("%d", &op);
switch (op) {
case 0: {
printf("Fim do programa");
break;
}
case 1: {
system("cls");
_owner = stdWriteOwner();
writeOwner(_owner);
break;
}
case 2: {
system("cls");
if (countOwners() == 0)
{
printf("\nNão existem proprietarios registrados.");
}
else
{
_house = stdWriteHouse();
writeHouse(_house);
}
break;
}
case 3: {
system("cls");
do
{
printf("\nDeseja realizar uma consulta [T]total ou [P]parcial: ");
fflush(stdin);
scanf("%c", &opc);
opc = toupper(opc);
} while (opc != 'T' && opc != 'P');
if (opc == 'T')
{
system("cls");
readOwners();
}
else if (opc == 'P')
{
system("cls");
printf("Informe o CPF: ");
fflush(stdin);
gets(cpf);
searchByCPF(cpf);
system("pause");
}
break;
}
case 4: {
system("cls");
do {
printf("\nDeseja fazer uma consula [T]total ou [P]parcial: ");
fflush(stdin);
scanf("%c", &escolha);
escolha = toupper(escolha);
} while (escolha != 'T' && escolha != 'P');
if (escolha == 'T') {
do {
printf("\nParametro da pesquisa [L]livre ou [A]alugado: ");
fflush(stdin);
scanf("%c", ¶metro);
parametro = toupper(parametro);
} while (parametro != 'L' && parametro != 'A');
fflush(stdin);
readHouses(parametro);
}
else {
system("cls");
float area;
int quartos, opp;
char bairro[20];
do {
system("cls");
printf("\nDeseja fazer consulta com:\n[1] Area util\n[2] Quantidade de quartos\n[3] Bairro\nOpcao: ");
scanf("%d", &opp);
} while (opp < 1 || opp > 3);
system("cls");
switch (opp) {
case 1: {
printf("\nArea util: ");
scanf("%f", &area);
readHouseArea(area);
break;
}
case 2: {
printf("\nQuantidade de quartos: ");
scanf("%d", &quartos);
readHouseRoom(quartos);
break;
}
case 3: {
printf("Bairro: ");
fflush(stdin);
gets(bairro);
readHouseBairro(bairro);
break;
}
default: {
printf("Easteregg, Achievement Unlocked");
break;
}
}
}
break;
}
case 5: {
system("cls");
printf("\n----------Casas Para Aluguel----------\n");
readHouses('L');
printf("\nRegistro do imovel que deseja alugar: ");
scanf("%d", ®_num);
pos = searchByRegister(reg_num);
printf("\nPos: %d", pos);
if (pos < 0)
{
printf("\nRegistro inexistente.");
system("pause");
break;
}
_locatario = stdReadLocatario();
_locatario.reg_imov = pos + 1;
writeLocatario(_locatario);
break;
}
case 6: {
system("cls");
readLocatarios();
break;
}
case 7: {
printf("\nRegistro da casa a alterar: ");
scanf("%d", ®);
alterStatusHouse(reg);
break;
}
case 8: {
printf("\nComece digitando o seu cpf");
scanf("%d", &cpf);
_locatario = search_locatario(cpf);
stdWriteLocatario(_locatario);
printf("\nDigite o ano atual: ");
scanf("%d", &dat);
deleta_locatario(_locatario);
searchCPF_owner(_locatario.CPF);
if (dat > _locatario.termino.year)
{
printf("\nDigite agora o mes atual");
scanf("%d", &dat);
if (dat >_locatario.termino.month)
{
printf("\nDigite agora o dia atual");
scanf("%d", &dat);
if (dat == _locatario.termino.day)
{
}
}
}
break;
}
case 9: {
printf("\nRealizar alteracao em:\n[1]Proprietario\t[2]Imovel\t[3]Locatario");
scanf("%d", &opcao8);
switch (opcao8)
{
case 1: {
printf("Digite o CPF do proprietario");
fflush(stdin);
gets(cpf);
check = searchCPF_owner(cpf);
check = altera_owner(check);
if (check == 1)
{
printf("\n\nLeitura dos dados finalizados.");
readOwners();
}
break;
}
case 2: {
printf("Digite numero de registro do imovel");
fflush(stdin);
scanf("%d", ®);
check = searchREG_house(check);
check = altera_house(check);
if (check == 1)
{
printf("\n\nLeitura dos dados finalizados.");
// readHouses();
}
break;
}
case 3: {
printf("Digite numero de CPF do locatario");
fflush(stdin);
gets(cpf);
check = searchCPF_locatario(cpf);
check = altera_locatario(check);
if (check == 1) {
printf("\n\nLeitura dos dados finalizados.");
readLocatarios();
}
break;
}
}
break;
}
case 10: {
_date = inputDate();
makeReport(_date);
system("pause");
break;
}
default: {
printf("Opcao nao existente");
break;
}
}
} while (op != 0);
system("cls");
}
int getOptions() {
int option;
printf("[0] Sair\n");
printf("[1] Cadastro de proprietario\n");
printf("[2] Cadastrar casa\n");
printf("[3] Consultar proprietarios\n");
printf("[4] Consultar imoveis\n");
printf("[5] Aluguel imovel\n");
printf("[6] Consultar locatarios\n");
printf("[7] Termino do contrato\n");
printf("[8] Alteracao de cadastros\n");
printf("[9] Relatorio\n");
printf("Opcao: ");
scanf("%d", &option);
if (option >= 0 && option <= 9) {
return option;
}
else {
printf("\nOpcao errada!\n");
return getOptions();
}
}
int countOwners() {
long int cont = 0;
FILE *fptr = NULL;
if ((fptr = fopen(PROPRIETARIO_FILE, "rb")) == NULL)
{
return cont; //Retornaria 0 pois o arquivo ainda não existe.
}
else
{
fseek(fptr, 0, 2);
cont = ftell(fptr) / sizeof(struct owner);
fclose(fptr);
return cont; //Retorna quantos dados dessa struct existem no arquivo owner.bin
}
}
struct addressOwner stdWriteAddressOwner() {
struct addressOwner _address;
printf("informe o logradouro: ");
gets(_address.logradouro);
printf("inform o bairro: ");
gets(_address.bairro);
printf("informe o CEP: ");
gets(_address.CEP);
printf("informe a cidade: ");
gets(_address.cidade);
printf("informe o estado: ");
gets(_address.estado);
printf("informe o fone: ");
gets(_address.fone);
printf("informe o cel: ");
gets(_address.cel);
printf("informe o email: ");
gets(_address.email);
return _address; //Retorna a nova struct.
}
void altera_sigla_owner(int pos) {
FILE *fptr = NULL;
struct owner _owner;
if ((fptr = fopen("owner.bin", "rb+")) == NULL)
{
system("cls");
printf("\nERRO AO TENTAR ABRIR O ARQUIVO NA ALTERACAO");
}
else
{
fseek(fptr, pos * sizeof(struct owner), 1);
if(_owner.sCasa.status_casa == 'L'){
_owner.sCasa.status_casa = 'A';
}
else{
_owner.sCasa.status_casa = 'L';
}
fwrite(&_owner, sizeof(struct owner), 1, fptr);
}
}
void altera_sigla_house(int pos) {
FILE *fptr = NULL;
struct house _house;
if ((fptr = fopen("house.bin", "rb+")) == NULL)
{
system("cls");
printf("\nERRO AO TENTAR ABRIR O ARQUIVO NA ALTERACAO");
}
else
{
fseek(fptr, pos * sizeof(struct house), 1);
if(_house.status.sigla == 'L'){
_house.status.sigla = 'A';
}
else{
_house.status.sigla = 'L';
}
fwrite(&_house, sizeof(struct house), 1, fptr);
}
}
int altera_owner(int pos) {
int valida = 0;
struct owner _owner;
FILE *fptr = NULL;
printf("\nRegistro inexistente\n\n");
printf("Inicio da altarecao");
printf("\nEm seguida digite os novos dados a serem alterados");
_owner = stdWriteOwner();
if ((fptr = fopen("owner.bin", "rb+")) == NULL) {
system("cls");
printf("\nERRO AO TENTAR ABRIR O ARQUIVO NA ALTERACAO");
valida = 0;
return valida;
}
else {
fseek(fptr, pos * sizeof(struct owner), 1);
fwrite(&_owner, sizeof(struct owner), 1, fptr);
valida = 1;
}
fclose(fptr);
printf("\nRegistro alterado com sucesso\n\n");
return valida;
} //altera
int altera_house(int pos) {
int valida = 0;
struct house _house;
FILE *fptr = NULL;
printf("\nRegistro inexistente\n\n");
printf("Inicio da altarecao");
printf("\nEm seguida digite os novos dados a serem alterados");
_house = stdWriteHouse();
if ((fptr = fopen("house.bin", "rb+")) == NULL) {
system("cls");
printf("\nERRO AO TENTAR ABRIR O ARQUIVO NA ALTERACAO");
valida = 0;
return valida;
}
else {
fseek(fptr, pos * sizeof(struct house), 1);
fwrite(&_house, sizeof(struct house), 1, fptr);
valida = 1;
}
fclose(fptr);
printf("\nRegistro alterado com sucesso\n\n");
return valida;
} //altera
int altera_locatario(int pos) {
int valida = 0;
struct locatario _locatario;
FILE *fptr = NULL;
printf("\nRegistro inexistente\n\n");
printf("Inicio da altarecao");
printf("\nEm seguida digite os novos dados a serem alterados");
_locatario = stdReadLocatario();
if ((fptr = fopen("locatario.bin", "rb+")) == NULL) {
system("cls");
printf("\nERRO AO TENTAR ABRIR O ARQUIVO NA ALTERACAO");
valida = 0;
return valida;
}
else {
fseek(fptr, pos * sizeof(struct locatario), 1);
fwrite(&_locatario, sizeof(struct locatario), 1, fptr);
valida = 1;
}
fclose(fptr);
printf("\nRegistro alterado com sucesso\n\n");
return valida;
} //altera
int deleta_locatario(struct locatario _locatario) {
int valida = 0;
FILE *fptr = NULL;
printf("\nRegistro inexistente\n\n");
printf("Inicio da altarecao");
_locatario.CPF[0] = '@';
if ((fptr = fopen("locatario.bin", "rb+")) == NULL) {
system("cls");
printf("\nERRO AO TENTAR ABRIR O ARQUIVO NA ALTERACAO");
valida = 0;
return valida;
}
else {
fseek(fptr, _locatario.reg_loc * sizeof(struct locatario), 1);
fwrite(&_locatario, sizeof(struct locatario), 1, fptr);
valida = 1;
}
fclose(fptr);
printf("\nRegistro Deletado com sucesso com sucesso\n\n");
return valida;
} //altera
struct owner stdWriteOwner() {
struct owner _owner;
printf("Informe o nome: ");
fflush(stdin);
gets(_owner.nome);
printf("Informe o CPF: ");
fflush(stdin);
gets(_owner.CPF);
_owner.qntd_de_casas = 0;
_owner.reg_prop = 1 + countOwners(); //retorna quantos dados existem dentro do arquivo + 1 para o registro do proprietario começar em 1.
_owner.sAddress = stdWriteAddressOwner(); //Escrita da struct interna addressOwner e retornando a mesma preenchida.
_owner.sCasa = stdWriteHouseOwner(); //Escrita da struct interna informacao_casa e retornando a mesma preenchida.
return _owner; //Retorna a struct Owner toda preenchida.
}
struct informacao_casa stdWriteHouseOwner() {
struct informacao_casa casa;
printf("\nInforme o numero da casa: ");
fflush(stdin);
scanf("%d", &casa.num_casa);
printf("\nInforme o status da casa: ");
fflush(stdin);
scanf("%c", &casa.status_casa);
return casa; //Retorna a struct prenchida
}
void writeOwner(struct owner _owner) {
FILE *file = fopen(PROPRIETARIO_FILE, "ab");
if (file == NULL)
printf("Erro ao abrir o arquivo!\n");
else
fwrite(&_owner, sizeof(struct owner), 1, file);
fclose(file); //Fechamento do arquivo
}
void stdReadOwner(struct owner _owner) {
printf("\n----------Proprietario----------\n");
printf("Nome: %s\n", _owner.nome);
printf("CPF: %s\n", _owner.CPF);
printf("Registro de proprietario: %d\n", _owner.reg_prop);
stdReadAddressOwner(_owner.sAddress); //Função que mostra os dados da struct interna addressOwner
stdReadOwnerHouseAddress(_owner.sCasa); //Função que mostra os dados da struct interna informacao_casa
}
void readOwners() {
int i;
struct owner _owner;
FILE *file = fopen(PROPRIETARIO_FILE, "rb"); //Abertura do arquivo para a leitura
if (file == NULL) {
printf("Nenhum proprietario registrado.\n");
}
else {
for (i = 0; i < countOwners(); i++) {
fseek(file, i * sizeof(struct owner), SEEK_SET);
fread(&_owner, sizeof(struct owner), 1, file);
if(_owner.CPF != "@")
stdReadOwner(_owner); //Função de leitura da struct adquirida.
}
fclose(file); //Fechamento do arquivo
}
system("pause");
}
void searchByCPF(char cpf[15]) {
int i, j, cmp;
struct owner _owner;
FILE *file = fopen(PROPRIETARIO_FILE, "rb"); //Abertura do arquivo para leitura
if (file == NULL) {
printf("Erro ao abrir o arquivo!\n");
}
else {
for (i = 0; i < countOwners(); i++)
{
fseek(file, i * sizeof(struct owner), SEEK_SET);
fread(&_owner, sizeof(struct owner), 1, file);
cmp = strcmp(_owner.CPF, cpf); //Comparação com o cpf passado com o atual dado pelo read
if (cmp == 0) {
fclose(file);
stdReadOwner(_owner); //Leitura da struct achada
return;
}
}
fclose(file);
}
printf("\nNenhum proprietario encontrado com este cpf\n");
}
int searchCPF_owner(char cpf[15]) { //Função para busca dos dados que se encontram com o CPF requerido
int i, j, cmp;
struct owner _owner;
FILE *file = fopen(PROPRIETARIO_FILE, "rb"); //Abertura do arquivo para leitura
if (file == NULL) {
printf("Erro ao abrir o arquivo!\n");
}
else {
for (i = 0; i < countOwners(); i++)
{
fseek(file, i * sizeof(struct owner), SEEK_SET);
fread(&_owner, sizeof(struct owner), 1, file);
cmp = strcmp(_owner.CPF, cpf); //Comparação com o cpf passado com o atual dado pelo read
if (cmp == 0) { //Parametro que limita a leitura dos dados.
fclose(file);
stdReadOwner(_owner); //Leitura da struct achada
return i;
}
}
fclose(file);
}
return -1;
printf("\nNenhum proprietario encontrado com este cpf\n");
}
int searchREG_house(int reg) { //Função para busca dos dados que se encontram com o reg requerido
int i, j;
struct house _house;
FILE *file = fopen(PROPRIETARIO_FILE, "rb"); //Abertura do arquivo para leitura
if (file == NULL) {
printf("Erro ao abrir o arquivo!\n");
}
else {
for (i = 0; i < countOwners(); i++) {
fseek(file, i * sizeof(struct house), SEEK_SET);
fread(&_house, sizeof(struct house), 1, file);
//Comparação com o reg passado com o atual dado pelo read
if (_house.reg_imov == reg) {
fclose(file);
stdReadHouse(_house); //Leitura da struct achada
return i;
}
}
fclose(file);
}
return -1;
printf("\nNenhum proprietario encontrado com este registro\n");
}
int searchCPF_locatario(char cpf[15]) {
int i, j, cmp;
struct locatario _locatario;
FILE *file = fopen(PROPRIETARIO_FILE, "rb"); //Abertura do arquivo para leitura
if (file == NULL) {
printf("Erro ao abrir o arquivo!\n");
}
else {
for (i = 0; i < countOwners(); i++) {
fseek(file, i * sizeof(struct locatario), SEEK_SET);
fread(&_locatario, sizeof(struct locatario), 1, file);
cmp = strcmp(_locatario.CPF, cpf); //Comparação com o cpf passado com o atual dado pelo read
if (cmp == 0) {
fclose(file);
stdWriteLocatario(_locatario); //Leitura da struct achada
return i;
}
}
fclose(file);
}
return -1;
printf("\nNenhum proprietario encontrado com este cpf\n");
}
struct locatario searchLocatariobyCPF(char cpf[15])
{
int i, j, cmp;
struct locatario _locatario;
FILE *file = fopen(LOCATARIO_FILE, "rb"); //Abertura do arquivo para leitura
if (file == NULL) {
printf("Erro ao abrir o arquivo!\n");
}
else {
for (i = 0; i < countLocatario(); i++) {
fseek(file, i * sizeof(struct locatario), SEEK_SET);
fread(&_locatario, sizeof(struct locatario), 1, file);
cmp = strcmp(_locatario.CPF, cpf); //Comparação com o cpf passado com o atual dado pelo read
if (cmp == 0) { //Parametro que limita a leitura dos dados.
fclose(file);
return _locatario;
}
}
fclose(file);
}
printf("\nNenhum proprietario encontrado com este cpf\n");
}
// ENDERECO DO OWNER
void stdReadAddressOwner(struct addressOwner _address) {
printf("----------Endereco do proprietario----------\n");
printf("Logradouro: %s\n", _address.logradouro);
printf("Bairro: %s\n", _address.bairro);
printf("CEP: %s\n", _address.CEP);
printf("Cidade: %s\n", _address.cidade);
printf("Estado: %s\n", _address.estado);
printf("Fone: %s\n", _address.fone);
printf("Cel: %s\n", _address.cel);
printf("Email: %s\n", _address.email);
}
// CASA DO OWNER
void stdReadOwnerHouseAddress(struct informacao_casa _address) {
printf("----------Endereco da casa----------\n");
printf("Numero da casa: %d\n", _address.num_casa);
printf("Status da casa: %c\n", _address.status_casa);
}
void stdReadHouseAddress(struct houseAddress _house) {
printf("Logradouro da casa: %s\n", _house.logradouro);
printf("Bairro da casa: %s\n", _house.bairro);
printf("CEP da casa: %s\n", _house.CEP);
printf("Cidade da casa: %s\n", _house.cidade);
}
void stdReadHouse(struct house _house) {
printf("\n----------Casa----------\n");
printf("Registro: %d\n", _house.reg_imov);
printf("Quartos: %d\n", _house.quartos);
printf("Area(m2): %.2f\n", _house.area);
printf("Registro da casa: %d\n", _house.reg_imov);
printf("Valor: %.2f\n", _house.valor);
printf("\nStatus da casa: %c\n", _house.status.sigla);
stdReadHouseAddress(_house.address); //Função que fará a leitura da struct interna de House.
}
void readHouses(char op) {
int i;
struct house _house;
FILE *file = fopen(IMOVEL_FILE, "rb"); //Abertura do Arquivo
if (file == NULL) {
printf("Erro ao abrir o arquivo!\n");
}
else {
if (op == 'L') {
for (i = 0; i < countHouses(); i++) {
fseek(file, i * sizeof(struct house), SEEK_SET);
fread(&_house, sizeof(struct house), 1, file);
if (_house.status.sigla == 'L')
stdReadHouse(_house);
}
}
else {
for (i = 0; i < countHouses(); i++)
{
fseek(file, i * sizeof(struct house), SEEK_SET);
fread(&_house, sizeof(struct house), 1, file);
if (_house.status.sigla == 'A') //Parâmetro para exibição de dados contendo sigla == A
stdReadHouse(_house);
}
}
fclose(file); //Fechamento do arquivo
}
system("pause");
}
void readHouseBairro(char bairro[20])
{
int i, j, cont = 0;
struct house _house;
char bairroCasa[20];
for (j = 0; j < strlen(bairro); j++)
bairro[j] = tolower(bairro[j]);
FILE *file = fopen(IMOVEL_FILE, "rb"); //Abertura do Arquivo
if (file == NULL) {
printf("Erro ao abrir o arquivo!\n");
}
else {
for (i = 0; i < countHouses(); i++) {
fseek(file, i * sizeof(struct house), SEEK_SET);
fread(&_house, sizeof(struct house), 1, file);
strcpy(bairroCasa, _house.address.bairro);
for (j = 0; j < strlen(bairroCasa); j++) {
bairroCasa[j] = tolower(bairroCasa[j]);
}
if ((strcmp(bairroCasa, bairro)) == 0) {
stdReadHouse(_house);
cont++;
}
}
}
fclose(file); //Fechamento do arquivo
if (cont == 0)
printf("\nNao ha nenhuma casa no bairro %s\n", bairro);
system("pause");
return;
}
void readHouseRoom(int quartos)
{
int i, j, cont = 0;
struct house _house;
FILE *file = fopen(IMOVEL_FILE, "rb"); //Abertura do Arquivo
if (file == NULL) {
printf("Erro ao abrir o arquivo!\n");
}
else {
for (i = 0; i < countHouses(); i++) {
fseek(file, i * sizeof(struct house), SEEK_SET);
fread(&_house, sizeof(struct house), 1, file);
if (quartos == _house.quartos){
stdReadHouse(_house);
cont++;
}
}
}
fclose(file); //Fechamento do arquivo
if (cont == 0) {
printf("\nNao ha nenhuma casa com %d quartos\n", quartos);
}
system("pause");
return;
}
void readHouseArea(float area)
{
int i, j, cont = 0;
struct house _house;
FILE *file = fopen(IMOVEL_FILE, "rb"); //Abertura do Arquivo
if (file == NULL) {
printf("Erro ao abrir o arquivo!\n");
}
else {
for (i = 0; i < countHouses(); i++) {
fseek(file, i * sizeof(struct house), SEEK_SET);
fread(&_house, sizeof(struct house), 1, file);
if (area == _house.area) {
stdReadHouse(_house);
cont++;
}
}
}
fclose(file); //Fechamento do arquivo
if (cont == 0) {
printf("\nNao ha nenhuma casa com %.2fm2\n", area);
}
system("pause");
return;
}
// WRITE FUNCTIONS
int countHouses() {
long int cont = 0;
FILE *fptr = NULL;
if ((fptr = fopen(IMOVEL_FILE, "rb")) == NULL) { //Abertura do Arquivo
return cont; //Caso ele não ache, o retorno será 0 pois o arquivo não existe ainda;
}
else {
fseek(fptr, 0, 2);
cont = ftell(fptr) / sizeof(struct house); //Retorno da quantidade dos dados
fclose(fptr); //Fechamento do arquivo
return cont; //Retorna a quantidade calculada de dados existentes.
}
}
struct house stdWriteHouse() { //Função de cadastro básico da struct house
struct house _house;
printf("\n----------Dados da casa----------\n");
printf("\nArea em metros quadrados da casa: ");
scanf("%f", &_house.area);
printf("\nQuantos quartos a casa tem: ");
scanf("%d", &_house.quartos);
printf("\nValor da casa: ");
scanf("%f", &_house.valor);
_house.reg_imov = 1 + countHouses();
printf("\n----------Endereco da casa----------\n");
printf("\nLogradouro: ");
fflush(stdin);
gets(_house.address.logradouro);
printf("\nBairro: ");
fflush(stdin);
gets(_house.address.bairro);
printf("\nCEP: ");
fflush(stdin);
gets(_house.address.CEP);
printf("\nCidade: ");
fflush(stdin);
gets(_house.address.cidade);
do
{
printf("\nStatus A para alugado, L para livre: ");
fflush(stdin);
scanf("%c", &_house.status.sigla);
_house.status.sigla = toupper(_house.status.sigla);
} while (_house.status.sigla != 'L' && _house.status.sigla != 'A');
return _house; //Retorna a nova struct
}
void writeHouse(struct house _house) { //Escrita dos dados armazenados na ROM para o arquivo house.bin
FILE *file = fopen(IMOVEL_FILE, "ab");
if (file == NULL) {
printf("\nErro ao abrir o arquivo!");
}
else {
fwrite(&_house, sizeof(struct house), 1, file);
}
fclose(file);
}
int searchByRegister(int _register)
{
struct house _house;
FILE *file = fopen(IMOVEL_FILE, "rb");
int i;
if (file == NULL) {
printf("\nNenhuma casa registrada.");
fclose(file);
return -1;
}
for (i = 0; i < countHouses(); i++) {
fseek(file, i * sizeof(struct house), SEEK_SET);
fread(&_house, sizeof(struct house), 1, file);
if (_house.reg_imov == _register && _house.status.sigla == 'L')
{
printf("\nCasa Alugada.");