Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include<math.h>
  4.  
  5. struct coordinates
  6. {
  7.     int x;
  8.     int y;
  9. };
  10.  
  11. int findHeight(FILE* file){
  12.     int h=0;
  13.     char string[100];
  14.     while(fgets(string , 101, file)) {
  15.     h+=1;
  16.     }
  17.     return h;
  18. }
  19.  
  20. int findWidth(FILE* file){
  21.     char string[100];
  22.     int w=0;
  23.     fgets(string , 101, file);
  24.     w=strlen(string)-1;
  25.     return w;
  26. }
  27.  
  28. int main()
  29. {
  30.     typedef struct coordinates coordinates;
  31.     char fileName[] = "buildingSmallMap.txt";
  32.     FILE *file = fopen(fileName, "r");
  33.     int height=findHeight(file);
  34.     fclose(file);
  35.     file = fopen(fileName, "r");
  36.     int width=findWidth(file);
  37.     fclose(file);
  38.     file = fopen(fileName, "r");
  39.     printf("%i, %i\n", width, height);
  40.     char buildings[]={'I','L','E','F','C','H','P','M','B','\0'};
  41.     char mapTemp[height*2][width+1];
  42.     while(!feof(file)){
  43.         for (int i=0; i<height*2; i++){
  44.             fgets(mapTemp[i], width+1, file);
  45.         }
  46.     }
  47.     char map[height][width+1];
  48.     strcpy(map[0],mapTemp[0]);
  49.     int j = 0;
  50.     for(int i=0; i<height; i++){
  51.         strcpy(map[i],mapTemp[i+j]);
  52.         j++;
  53.     }
  54.     for(int i=0; i<height; i++){
  55.         printf("%i: ",i+1 );
  56.         printf("%s\n", map[i]);
  57.  
  58.     }
  59.     int treeCount=0;
  60.     for(int i=0;i<height;i++){
  61.         for(int j=0;j<width;j++){
  62.             if(map[i][j]=='#')
  63.                 treeCount++;
  64.         }
  65.     }
  66.     printf("Number of trees: %i\n", treeCount);
  67.     int buildingCount=0;
  68.     for(int i=0;i<height;i++){
  69.         for(int j=0;j<width;j++){
  70.             char buil;
  71.             for(int b=0;b<9;b++){
  72.                 buil = buildings[b];
  73.                 if(map[i][j]==buil)
  74.                     buildingCount++;
  75.             }
  76.         }
  77.     }
  78.     printf("Number of buildings: %i\n", buildingCount);
  79.     double pathCount=0;
  80.     for(int i=0;i<height;i++){
  81.         for(int j=0;j<width;j++){
  82.             if(map[i][j]=='x')
  83.                 pathCount++;
  84.         }
  85.     }
  86.     int pathPercentage = round((pathCount/(height*width))*100);
  87.     printf("path percentage: %i%%", pathPercentage);
  88.     fclose(file);
  89.     printf("\nEnter a building: ");
  90.     char input = getchar();
  91.     int found = 0;
  92.     for(int i=0;i<height;i++){
  93.         for(int j=0;j<width;j++){
  94.             if(map[i][j]==input){
  95.                 printf("%i, %i", j, i);
  96.                 found = 1;
  97.             }
  98.         }
  99.     }
  100.     if(found==0){
  101.         printf("Building not found");
  102.     }
  103.     printf("\nEnter first building: ");
  104.     char firstBuilding;
  105.     scanf(" %c", &firstBuilding);
  106.     printf("\nEnter another building: ");
  107.     char secondBuilding;
  108.     scanf(" %c", &secondBuilding);
  109.     struct coordinates first;
  110.     struct coordinates second;
  111.     for(int i=0;i<height;i++){
  112.         for(int j=0;j<width;j++){
  113.             if(map[i][j]==firstBuilding){
  114.                 first.x=i;
  115.                 first.y=j;
  116.             }
  117.         }
  118.     }
  119.     for(int i=0;i<height;i++){
  120.         for(int j=0;j<width;j++){
  121.             if(map[i][j]==secondBuilding){
  122.                 second.x=i;
  123.                 second.y=j;
  124.             }
  125.         }
  126.     }
  127.     long distance = round(sqrt((second.x-first.x)*(second.x-first.x)+(second.y-first.y)*(second.y-first.y)));
  128.     printf("distance: %i\nPath: ", distance );
  129.     struct coordinates pos;
  130.     pos=first;
  131.  
  132.     int z = 0;
  133.     int changed = 0;
  134.     while(pos.x!=second.x){
  135.         if(pos.x>second.x){
  136.             pos.x=pos.x-1;
  137.             changed = 1;
  138.         }
  139.         else if(pos.x<second.x){
  140.             pos.x=pos.x+1;
  141.             changed = 1;
  142.         }
  143.         if(changed==1)
  144.             if(pos.x!=second.x || pos.y!=second.y)
  145.                 printf("%i: %c",z+1, map[pos.x][pos.y]);
  146.        
  147.     changed=0;
  148.     z++;
  149.     }
  150.     while(pos.y!=second.y){
  151.        
  152.         if(pos.y>second.y){
  153.             pos.y=pos.y-1;
  154.             changed = 1;
  155.         }
  156.         else if(pos.y<second.y){
  157.             pos.y=pos.y+1;
  158.             changed = 1;
  159.         }
  160.          if(changed ==1)
  161.             if(pos.x!=second.x || pos.y!=second.y)
  162.                  printf("%i: %c",z+1, map[pos.x][pos.y]);
  163.        
  164.     changed = 0;
  165.     z++;
  166.     }
  167.  
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement