Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct al{
  6.     int num;
  7.     char local[50];
  8.     char maq[50];
  9.     int ano;
  10.     char tipo[50];
  11. }VECstruct;
  12.  
  13. VECstruct reg;
  14.  
  15. FILE *fx;
  16.  
  17. int pesquisa(int x){
  18.     int p;
  19.     fx=fopen("fotos.dat","r");
  20.     fread(&reg,sizeof(reg),1,fx);  
  21.     while(x!=reg.num && !feof(fx)){
  22.         fread(&reg,sizeof(reg),1,fx);
  23.     }
  24.     p=ftell(fx);
  25.     fclose(fx);
  26.     if(reg.num==x){
  27.         return p;
  28.     }
  29.     else
  30.         return 0;
  31. }
  32.  
  33. void insere(){
  34.     int n,c;
  35.     do{
  36.         printf("Numero do registo: ");
  37.         scanf("%d",&n);
  38.         if(pesquisa(n)==0){
  39.             fx=fopen("fotos.dat","a+");
  40.             reg.num=n;
  41.             printf("Local: ");
  42.             scanf("%s",reg.local);
  43.             printf("Maquina: ");
  44.             scanf("%s",reg.maq);
  45.             printf("Ano: ");
  46.             scanf("%d",&reg.ano);
  47.             printf("Tipo: ");
  48.             scanf("%s",reg.tipo);
  49.             fwrite(&reg,sizeof(reg),1,fx);
  50.             fclose(fx);
  51.         }
  52.         else printf("Esse registo ja existe\n");
  53.         printf("Continuar?\n");
  54.         scanf("%d",&c);
  55.     }while(c==1);
  56. }
  57.  
  58. void listar(){
  59.     int i,lmi,lms,vano[3000];
  60.     for(i=0;i<3000;i++){vano[i]=0;}
  61.     printf("Limite inferior: ");
  62.     scanf("%d",&lmi);
  63.     printf("Limite superior: ");
  64.     scanf("%d",&lms);
  65.     fx=fopen("fotos.dat","r");
  66.     fread(&reg,sizeof(reg),1,fx);
  67.     while(!feof(fx)){
  68.         if(reg.ano>=lmi && reg.ano<=lms){
  69.         /*    printf("\nNumero de registo: %d\n",reg.num);
  70.             printf("Local: %s\n",reg.local);
  71.             printf("Maquina: %s\n",reg.maq);
  72.             printf("Ano: %d\n",reg.ano);
  73.             printf("Tipo: %s\n",reg.tipo);*/
  74.             vano[reg.ano]++;
  75.         }
  76.         fread(&reg,sizeof(reg),1,fx);
  77.     }
  78.     printf("Fotos por ano:\n");
  79.     for(i=0;i<3000;i++){
  80.         if(vano[i]>0){
  81.             printf("%d-",i);
  82.             printf("%d\n",vano[i]);    
  83.         }
  84.     }
  85.     fclose(fx);
  86. }
  87.  
  88. void altera(){
  89.     char lp[50],nl[50];
  90.     printf("Local a pesquisar: ");
  91.     scanf("%s",lp);
  92.     printf("Local novo: ");
  93.     scanf("%s",nl);
  94.     fx=fopen("fotos.dat","r+");
  95.     fread(&reg,sizeof(reg),1,fx);
  96.     while(!feof(fx)){
  97.         if(strcmp(reg.local,lp)==0){
  98.             strcpy(reg.local,nl);
  99.             fseek(fx,-sizeof(reg),1);
  100.             fwrite(&reg,sizeof(reg),1,fx);
  101.             fseek(fx,sizeof(reg),1);
  102.         }
  103.         fread(&reg,sizeof(reg),1,fx);
  104.     }
  105.     fclose(fx);/*
  106.     fx=fopen("fotos.dat","r+");
  107.     fread(&reg,sizeof(reg),1,fx);
  108.     while(!feof(fx)){
  109.         if(strcmp(reg.local,lp)==0){
  110.             strcpy(reg.local,nl);
  111.             fseek(fx,-sizeof(reg),1);
  112.             fwrite(&reg,sizeof(reg),1,fx);
  113.             fseek(fx,sizeof(reg),1);
  114.         }
  115.         fread(&reg,sizeof(reg),1,fx);
  116.     }
  117.     fclose(fx);*/
  118. }
  119.  
  120. int main(){
  121.     int x;
  122.     if(fopen("fotos.dat","r")==NULL){
  123.         fx=fopen("fotos.dat","w");
  124.         fclose(fx);
  125.     }
  126.     printf("1-Inserir fotos\n");
  127.     printf("2-Listar fotos entre 2 anos e total de fotos por ano \n");
  128.     printf("3-Alterar fotos de um local\n");
  129.     printf("4-Sair");
  130.     do{
  131.         printf("\n: ");
  132.         scanf("%d",&x);
  133.         printf("\n");
  134.         switch(x){
  135.             case 1:insere(); break;
  136.             case 2:listar(); break;
  137.             case 3:altera(); break;
  138.         }
  139.     }while(x!=4);
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement