Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include<math.h>
- struct coordinates
- {
- int x;
- int y;
- };
- int findHeight(FILE* file){
- int h=0;
- char string[100];
- while(fgets(string , 101, file)) {
- h+=1;
- }
- return h;
- }
- int findWidth(FILE* file){
- char string[100];
- int w=0;
- fgets(string , 101, file);
- w=strlen(string)-1;
- return w;
- }
- int main()
- {
- typedef struct coordinates coordinates;
- char fileName[] = "buildingSmallMap.txt";
- FILE *file = fopen(fileName, "r");
- int height=findHeight(file);
- fclose(file);
- file = fopen(fileName, "r");
- int width=findWidth(file);
- fclose(file);
- file = fopen(fileName, "r");
- printf("%i, %i\n", width, height);
- char buildings[]={'I','L','E','F','C','H','P','M','B','\0'};
- char mapTemp[height*2][width+1];
- while(!feof(file)){
- for (int i=0; i<height*2; i++){
- fgets(mapTemp[i], width+1, file);
- }
- }
- char map[height][width+1];
- strcpy(map[0],mapTemp[0]);
- int j = 0;
- for(int i=0; i<height; i++){
- strcpy(map[i],mapTemp[i+j]);
- j++;
- }
- for(int i=0; i<height; i++){
- printf("%i: ",i+1 );
- printf("%s\n", map[i]);
- }
- int treeCount=0;
- for(int i=0;i<height;i++){
- for(int j=0;j<width;j++){
- if(map[i][j]=='#')
- treeCount++;
- }
- }
- printf("Number of trees: %i\n", treeCount);
- int buildingCount=0;
- for(int i=0;i<height;i++){
- for(int j=0;j<width;j++){
- char buil;
- for(int b=0;b<9;b++){
- buil = buildings[b];
- if(map[i][j]==buil)
- buildingCount++;
- }
- }
- }
- printf("Number of buildings: %i\n", buildingCount);
- double pathCount=0;
- for(int i=0;i<height;i++){
- for(int j=0;j<width;j++){
- if(map[i][j]=='x')
- pathCount++;
- }
- }
- int pathPercentage = round((pathCount/(height*width))*100);
- printf("path percentage: %i%%", pathPercentage);
- fclose(file);
- printf("\nEnter a building: ");
- char input = getchar();
- int found = 0;
- for(int i=0;i<height;i++){
- for(int j=0;j<width;j++){
- if(map[i][j]==input){
- printf("%i, %i", j, i);
- found = 1;
- }
- }
- }
- if(found==0){
- printf("Building not found");
- }
- printf("\nEnter first building: ");
- char firstBuilding;
- scanf(" %c", &firstBuilding);
- printf("\nEnter another building: ");
- char secondBuilding;
- scanf(" %c", &secondBuilding);
- struct coordinates first;
- struct coordinates second;
- for(int i=0;i<height;i++){
- for(int j=0;j<width;j++){
- if(map[i][j]==firstBuilding){
- first.x=i;
- first.y=j;
- }
- }
- }
- for(int i=0;i<height;i++){
- for(int j=0;j<width;j++){
- if(map[i][j]==secondBuilding){
- second.x=i;
- second.y=j;
- }
- }
- }
- long distance = round(sqrt((second.x-first.x)*(second.x-first.x)+(second.y-first.y)*(second.y-first.y)));
- printf("distance: %i\nPath: ", distance );
- struct coordinates pos;
- pos=first;
- int z = 0;
- int changed = 0;
- while(pos.x!=second.x){
- if(pos.x>second.x){
- pos.x=pos.x-1;
- changed = 1;
- }
- else if(pos.x<second.x){
- pos.x=pos.x+1;
- changed = 1;
- }
- if(changed==1)
- if(pos.x!=second.x || pos.y!=second.y)
- printf("%i: %c",z+1, map[pos.x][pos.y]);
- changed=0;
- z++;
- }
- while(pos.y!=second.y){
- if(pos.y>second.y){
- pos.y=pos.y-1;
- changed = 1;
- }
- else if(pos.y<second.y){
- pos.y=pos.y+1;
- changed = 1;
- }
- if(changed ==1)
- if(pos.x!=second.x || pos.y!=second.y)
- printf("%i: %c",z+1, map[pos.x][pos.y]);
- changed = 0;
- z++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement