AlexnIco

Orribile

Oct 6th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define NOME_F "treasure-island.txt"
  5. #define DIM_MAX 30000+1
  6.  
  7. int controlla_carattere(char mat[][DIM_MAX], int r, int c);
  8. int controlla_coordinate(int x, int y);
  9. int occ_destra=0, occ_sinistra=0, occ_sopra=0, occ_sotto=0,
  10.     occ_sopra_destra=0, occ_sotto_destra=0, occ_sopra_sinistra=0,
  11.     occ_sotto_sinistra=0;
  12.  
  13.  
  14. int main()
  15. {
  16.     char mat[7][DIM_MAX];
  17.     FILE *fp;
  18.     int num_occ = 0, r, c;
  19.    
  20.     fp = fopen(NOME_F, "r");
  21.     if(fp == NULL){
  22.         printf("POLLLL\n");
  23.         return -1;
  24.     }
  25.     printf("Tapirulan\n");
  26.    
  27.     for(r=0; r<7; r++){
  28.         mat[r][0] = '\0';
  29.     }
  30.    
  31.     for(r=-3; r<DIM_MAX; r++){
  32.     //for(r=-3; r<1; r++){
  33.         for(c=0; c<DIM_MAX; c++){
  34.             fscanf(fp, "%c", mat[(r+3)%7] + c);
  35.         }
  36.         if(r >= 0){
  37.             for(c=0; c<DIM_MAX; c++){
  38.                 if(mat[r%7][c] == 'A'){
  39.                     num_occ += controlla_carattere(mat, r, c);
  40.                 }
  41.             }
  42.         }
  43.     }
  44.    
  45.     fclose(fp);
  46.     fp = fopen("RES.txt", "w");
  47.     if(fp == NULL){
  48.         printf("Err.\n");
  49.         return -1;
  50.     }
  51.     printf("\n\nNumero occorrenze: %d\n", num_occ);
  52.     fprintf(fp, "---Risultato: %d\n", num_occ);
  53.     fprintf(fp, "Destra: %d\n", occ_destra);
  54.     fprintf(fp, "Sotto-destra: %d\n", occ_sotto_destra);
  55.     fprintf(fp, "Sotto: %d\n", occ_sotto);
  56.     fprintf(fp, "Sotto-sinistra: %d\n", occ_sotto_sinistra);
  57.     fprintf(fp, "Sinistra: %d\n", occ_sinistra);
  58.     fprintf(fp, "Sopra-sinistra: %d\n", occ_sopra_sinistra);
  59.     fprintf(fp, "Sopra: %d\n", occ_sopra);
  60.     fprintf(fp, "Sopra-destra: %d\n", occ_sopra_destra);
  61.    
  62.    
  63.     return 0;
  64. }
  65.  
  66. int controlla_carattere(char mat[][DIM_MAX], int r, int c){
  67.     int i, occ=0, x, y;
  68.     int vr[8]={1, 1, 0,-1,-1,-1, 0, 1};
  69.     int vc[8]={0, 1, 1, 1, 0,-1,-1,-1};
  70.    
  71.     for(i=0; i<8; i++){
  72.         if(!controlla_coordinate(r + vr[i]*3,c + vc[i]*3)){
  73.             continue;
  74.         }
  75.         x = r + vr[i];
  76.         y = c + vc[i];
  77.        
  78.         if(mat[x%7][y]=='R'){
  79.             x += vr[i];
  80.             y += vc[i];
  81.             if(mat[x%7][y]=='R'){
  82.                 x += vr[i];
  83.                 y += vc[i];
  84.                 if(mat[x%7][y]=='!'){
  85.                     occ++;
  86.                     switch(i){
  87.                     case 0:
  88.                         occ_sotto++;
  89.                         break;
  90.                     case 1:
  91.                         occ_sotto_destra++;
  92.                         break;
  93.                     case 2:
  94.                         occ_destra++;
  95.                         break;
  96.                     case 3:
  97.                         occ_sopra_destra++;
  98.                         break;
  99.                     case 4:
  100.                         occ_sopra++;
  101.                         break;
  102.                     case 5:
  103.                         occ_sopra_sinistra++;
  104.                         break;
  105.                     case 6:
  106.                         occ_sinistra++;
  107.                         break;
  108.                     case 7:
  109.                         occ_sotto_sinistra++;
  110.                         break;
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.     }
  116.    
  117.     return occ;
  118. }
  119.  
  120. int controlla_coordinate(int x, int y){
  121.     if(x >= DIM_MAX){
  122.         return 0;
  123.     }
  124.     else if(x < 0){
  125.         return 0;
  126.     }
  127.     else if(y >= DIM_MAX){
  128.         return 0;
  129.     }
  130.     else if(y < 0){
  131.         return 0;
  132.     }
  133.    
  134.     return 1;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment