#include #include #include #include #include /* typedef struct cvisa { char nome[20]; int bi; int VISA; struct cvisa *seg; }*CVISA; typedef struct svisa { char nome[20]; int bi; struct svisa *seg; }*SVISA; */ typedef struct hotel { char nome[20]; int cod; int pag; long int seg; long int aptc; }*HOT; typedef struct area { char nome[20]; int cod; long int seg; }*AREA; int menu3() { int op3; printf("\n-------Hoteis-------"); printf("\n 1-Inserir Hotel"); printf("\n 2-Eliminar Hotel"); printf("\n 3-Lista de Hoteis"); printf("\n 4-Voltar"); do{ printf("\n\n Escolha a opo: "); scanf("%d",&op3); }while(op3<1 || op3>4); return op3; } int menu2() { int op2; printf("\n-----------------Areas-----------------"); printf("\n 1-Inserir Nova Area de Investigao"); printf("\n 2-Eliminar Area de Investigaao"); printf("\n 3-Lista Areas de Investigaao"); printf("\n 4-Menu Principal"); do{ printf("\n\n Escolha a opo: "); scanf("%d",&op2); }while(op2<1 || op2>4); return op2; } void ler(ele) HOT ele; { int p; printf("\n Nome: "); scanf("%s",ele->nome); printf("\n Codigo: "); scanf("%d",&ele->cod); printf("\n Pagamento (com VISA (1), sem VISA (2): "); do{ scanf("%d",&ele->pag); p=ele->pag; }while (p<1 || p>2); } void lerarea(ele) AREA ele; { printf("\n Nome: "); scanf("%s",ele->nome); printf("\n Codigo: "); scanf("%d",&ele->cod); } long int tamanho(fp) FILE *fp; { long pos,t; pos=ftell(fp); fseek(fp,0L,SEEK_END); t=ftell(fp); fseek(fp,pos,SEEK_SET); return t; } void inserir(ele) HOT ele; { long int tamanho(),pos_ant,pos_act; int av=1; HOT cab,ant,act; FILE *fp; cab=(HOT)malloc(sizeof(struct hotel)); ant=(HOT)malloc(sizeof(struct hotel)); act=(HOT)malloc(sizeof(struct hotel)); fp=fopen("hoteis.dat","r+"); ele->seg=-1; if(tamanho(fp)==0) fwrite(ele,sizeof(struct hotel),1,fp); else { fread(cab,sizeof(struct hotel),1,fp); if(cab->cod > ele->cod) { fseek(fp,0L,SEEK_END); ele->seg=ftell(fp); fwrite(cab,sizeof(struct hotel),1,fp); fseek(fp,0L,SEEK_SET); fwrite(ele,sizeof(struct hotel),1,fp); } else { pos_ant=0; pos_act=cab->seg; *ant=*act=*cab; while(av) { if(pos_act==-1) av=0; else { fseek(fp,pos_act,SEEK_SET); fread(act,sizeof(struct hotel),1,fp); if(act->cod > ele->cod) av=0; else { pos_ant=pos_act; pos_act=act->seg; *ant=*act; } } } ele->seg=pos_act; fseek(fp,0L,SEEK_END); ant->seg=ftell(fp); fwrite(ele,sizeof(struct hotel),1,fp); fseek(fp,pos_ant,SEEK_SET); fwrite(ant,sizeof(struct hotel),1,fp); } } fclose(fp); } void inserirarea(ele) AREA ele; { long int tamanho(),pos_ant,pos_act; int av=1; AREA cab,ant,act; FILE *fp; cab=(AREA)malloc(sizeof(struct area)); ant=(AREA)malloc(sizeof(struct area)); act=(AREA)malloc(sizeof(struct area)); fp=fopen("areas.dat","r+"); ele->seg=-1; if(tamanho(fp)==0) fwrite(ele,sizeof(struct area),1,fp); else { fread(cab,sizeof(struct area),1,fp); if(cab->cod > ele->cod) { fseek(fp,0L,SEEK_END); ele->seg=ftell(fp); fwrite(cab,sizeof(struct area),1,fp); fseek(fp,0L,SEEK_SET); fwrite(ele,sizeof(struct area),1,fp); } else { pos_ant=0; pos_act=cab->seg; *ant=*act=*cab; while(av) { if(pos_act==-1) av=0; else { fseek(fp,pos_act,SEEK_SET); fread(act,sizeof(struct area),1,fp); if(act->cod > ele->cod) av=0; else { pos_ant=pos_act; pos_act=act->seg; *ant=*act; } } } ele->seg=pos_act; fseek(fp,0L,SEEK_END); ant->seg=ftell(fp); fwrite(ele,sizeof(struct area),1,fp); fseek(fp,pos_ant,SEEK_SET); fwrite(ant,sizeof(struct area),1,fp); } } fclose(fp); } HOT remover(chave) int chave; { int av=1; HOT rem,ant,act,cab; long int tamanho(),posant,posact; FILE *fp; cab=(HOT)malloc(sizeof(struct hotel)); ant=(HOT)malloc(sizeof(struct hotel)); act=(HOT)malloc(sizeof(struct hotel)); fp=fopen("hoteis.dat","r+"); if(tamanho(fp)==0) rem=NULL; else { fread(cab,sizeof(struct hotel),1,fp); if(cab->cod==chave) { rem=cab; if(cab->seg!=-1) { fseek(fp,cab->seg,SEEK_SET); fread(act,sizeof(struct hotel),1,fp); fseek(fp,0L,SEEK_SET); fwrite(act,sizeof(struct hotel),1,fp); } else { fclose(fp); fp=fopen("hoteis.dat","w"); } } else { posant=0; posact=cab->seg; *ant=*act=*cab; while(av) { if(posact==-1) av=0; else { fseek(fp,posact,SEEK_SET); fread(act,sizeof(struct hotel),1,fp); if(act->cod==chave) av=0; else { posant=posact; posact=act->seg; *ant=*act; } } if(posact!=-1) { rem=act; ant->seg=act->seg; fseek(fp,posant,SEEK_SET); fwrite(ant,sizeof(struct hotel),1,fp); } else rem=NULL; } } } fclose(fp); return(rem); } AREA removerarea(chave) int chave; { int av=1; AREA rem,ant,act,cab; long int tamanho(),posant,posact; FILE *fp; cab=(AREA)malloc(sizeof(struct area)); ant=(AREA)malloc(sizeof(struct area)); act=(AREA)malloc(sizeof(struct area)); fp=fopen("areas.dat","r+"); if(tamanho(fp)==0) rem=NULL; else { fread(cab,sizeof(struct area),1,fp); if(cab->cod==chave) { rem=cab; if(cab->seg!=-1) { fseek(fp,cab->seg,SEEK_SET); fread(act,sizeof(struct area),1,fp); fseek(fp,0L,SEEK_SET); fwrite(act,sizeof(struct area),1,fp); } else { fclose(fp); fp=fopen("areas.dat","w"); } } else { posant=0; posact=cab->seg; *ant=*act=*cab; while(av) { if(posact==-1) av=0; else { fseek(fp,posact,SEEK_SET); fread(act,sizeof(struct area),1,fp); if(act->cod==chave) av=0; else { posant=posact; posact=act->seg; *ant=*act; } } if(posact!=-1) { rem=act; ant->seg=act->seg; fseek(fp,posant,SEEK_SET); fwrite(ant,sizeof(struct area),1,fp); } else rem=NULL; } } } fclose(fp); return(rem); } void mostrar() { int i=0; HOT ele; FILE *fp; long int pos; fp=fopen("hoteis.dat","a+"); if(tamanho(fp)==0) printf("\n Lista vazia!!"); else { ele=(HOT)malloc(sizeof(struct hotel)); ele->seg=0; while(ele->seg!=-1) { i++; fseek(fp,(long int)ele->seg,SEEK_SET); pos=ftell(fp); fread(ele,sizeof(struct hotel),1,fp); printf("\n %3d Elemento (guardado apartir do byte %d)",i,pos); printf("\n Nome: %s \n Codigo: %d \n Pagamento: %d", ele->nome,ele->cod,ele->pag); } } fclose(fp); } void mostrararea() { int i=0; AREA ele; FILE *fp; long int pos; fp=fopen("areas.dat","a+"); if(tamanho(fp)==0) printf("\n Lista vazia!!"); else { ele=(AREA)malloc(sizeof(struct area)); ele->seg=0; while(ele->seg!=-1) { i++; fseek(fp,(long int)ele->seg,SEEK_SET); pos=ftell(fp); fread(ele,sizeof(struct area),1,fp); printf("\n %3d Elemento (guardado apartir do byte %d)",i,pos); printf("\n Nome: %s \n Codigo: %d", ele->nome,ele->cod); } } fclose(fp); } int main2() { void inserirarea(),lerarea(),mostrararea(); int menu2(),op,codigo; AREA removerarea(),el; FILE *fp; fp=fopen("areas.dat","a+"); fclose(fp); do { op=menu2(); switch(op) { case 1: el=(AREA)malloc(sizeof(struct area)); lerarea(el); inserirarea(el); break; case 2: printf("\n Introduza o codigo da area a remover: "); scanf("%d",&codigo); el=removerarea(codigo); if(el) { printf("\n Removeu a Area!"); printf("\n Nome: %s \n Codigo: %d",el->nome,&el->cod); free(el); } break; case 3: mostrararea(); break; case 4: return(1); } }while(1); return 0; } int main3() { void inserir(),ler(),mostrar(); int menu3(),op,codigo; HOT remover(),el; FILE *fp; fp=fopen("hoteis.dat","a+"); fclose(fp); do { op=menu3(); switch(op) { case 1: el=(HOT)malloc(sizeof(struct hotel)); ler(el); inserir(el); break; case 2: printf("\n Introduza o codigo do hotel a remover: "); scanf("%d",&codigo); el=remover(codigo); if(el) { printf("\n Removeu o Hotel!"); printf("\n Nome: %s \n Codigo: %d \n Pagamento: %s\n",el->nome,&el->cod,el->pag); free(el); } break; case 3: mostrar(); break; case 4: return(1); } }while(1); return 0; }