Advertisement
Guest User

shuffle

a guest
Oct 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. void shuffle(FILE *fp, FILE *fi)
  2. {
  3.     int i, num1, num2;
  4.     srand(time(NULL));
  5.     printf("\nSto mischiando...");
  6.     for (i = 0; i < FILESIZE; i++)
  7.     {
  8.         num1 = (rand() %FILESIZE );
  9.         num2 = (rand() %FILESIZE );    
  10.         if(num1 != num2)
  11.         {
  12.             printf("\n%d)%d-%d",i,num1,num2);
  13.             swap(fp, fi, num1, num2);
  14.         }else{
  15.             i--;
  16.         }  
  17.     }
  18.     printf("Terminato!");
  19. }
  20.  
  21. void swap(FILE *fp, FILE *fi, int posX, int posY)
  22. {
  23.     pittore pit1;
  24.     pittore pit2;
  25.     indice in1;
  26.     indice in2;
  27.    
  28.    
  29.     fseek(fp,(sizeof(pittore)*posX), SEEK_SET);
  30.     fread(&pit1, sizeof(pittore), 1, fp);
  31.  
  32.     fseek(fp, (sizeof(pittore)*posY), SEEK_SET);
  33.     fread(&pit2, sizeof(pittore), 1, fp);
  34.  
  35.     fseek(fp, (sizeof(pittore)*posY), SEEK_SET);
  36.     fwrite(&pit1, sizeof(pittore), 1, fp);
  37.  
  38.     fseek(fp, (sizeof(pittore)*posX), SEEK_SET);
  39.     fwrite(&pit2, sizeof(pittore), 1, fp);
  40.  
  41.     in1.CodPittore = pit1.CodPittore;
  42.     in1.PosPittore = posY;
  43.     in2.CodPittore = pit2.CodPittore;
  44.     in2.PosPittore = posX;
  45.    
  46.     fseek(fi, (sizeof(indice)*posX), SEEK_SET);
  47.     fwrite(&in1, sizeof(indice), 1, fi);
  48.    
  49.     fseek(fi, (sizeof(indice)*posY), SEEK_SET);
  50.     fwrite(&in2, sizeof(indice), 1, fi);
  51.     return;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement