Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define NOME_F "treasure-island.txt"
- #define DIM_MAX 30000+1
- int controlla_carattere(char mat[][DIM_MAX], int r, int c);
- int controlla_coordinate(int x, int y);
- int occ_destra=0, occ_sinistra=0, occ_sopra=0, occ_sotto=0,
- occ_sopra_destra=0, occ_sotto_destra=0, occ_sopra_sinistra=0,
- occ_sotto_sinistra=0;
- int main()
- {
- char mat[7][DIM_MAX];
- FILE *fp;
- int num_occ = 0, r, c;
- fp = fopen(NOME_F, "r");
- if(fp == NULL){
- printf("POLLLL\n");
- return -1;
- }
- printf("Tapirulan\n");
- for(r=0; r<7; r++){
- mat[r][0] = '\0';
- }
- for(r=-3; r<DIM_MAX; r++){
- //for(r=-3; r<1; r++){
- for(c=0; c<DIM_MAX; c++){
- fscanf(fp, "%c", mat[(r+3)%7] + c);
- }
- if(r >= 0){
- for(c=0; c<DIM_MAX; c++){
- if(mat[r%7][c] == 'A'){
- num_occ += controlla_carattere(mat, r, c);
- }
- }
- }
- }
- fclose(fp);
- fp = fopen("RES.txt", "w");
- if(fp == NULL){
- printf("Err.\n");
- return -1;
- }
- printf("\n\nNumero occorrenze: %d\n", num_occ);
- fprintf(fp, "---Risultato: %d\n", num_occ);
- fprintf(fp, "Destra: %d\n", occ_destra);
- fprintf(fp, "Sotto-destra: %d\n", occ_sotto_destra);
- fprintf(fp, "Sotto: %d\n", occ_sotto);
- fprintf(fp, "Sotto-sinistra: %d\n", occ_sotto_sinistra);
- fprintf(fp, "Sinistra: %d\n", occ_sinistra);
- fprintf(fp, "Sopra-sinistra: %d\n", occ_sopra_sinistra);
- fprintf(fp, "Sopra: %d\n", occ_sopra);
- fprintf(fp, "Sopra-destra: %d\n", occ_sopra_destra);
- return 0;
- }
- int controlla_carattere(char mat[][DIM_MAX], int r, int c){
- int i, occ=0, x, y;
- int vr[8]={1, 1, 0,-1,-1,-1, 0, 1};
- int vc[8]={0, 1, 1, 1, 0,-1,-1,-1};
- for(i=0; i<8; i++){
- if(!controlla_coordinate(r + vr[i]*3,c + vc[i]*3)){
- continue;
- }
- x = r + vr[i];
- y = c + vc[i];
- if(mat[x%7][y]=='R'){
- x += vr[i];
- y += vc[i];
- if(mat[x%7][y]=='R'){
- x += vr[i];
- y += vc[i];
- if(mat[x%7][y]=='!'){
- occ++;
- switch(i){
- case 0:
- occ_sotto++;
- break;
- case 1:
- occ_sotto_destra++;
- break;
- case 2:
- occ_destra++;
- break;
- case 3:
- occ_sopra_destra++;
- break;
- case 4:
- occ_sopra++;
- break;
- case 5:
- occ_sopra_sinistra++;
- break;
- case 6:
- occ_sinistra++;
- break;
- case 7:
- occ_sotto_sinistra++;
- break;
- }
- }
- }
- }
- }
- return occ;
- }
- int controlla_coordinate(int x, int y){
- if(x >= DIM_MAX){
- return 0;
- }
- else if(x < 0){
- return 0;
- }
- else if(y >= DIM_MAX){
- return 0;
- }
- else if(y < 0){
- return 0;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment