简体   繁体   English

C编程:创建一个程序以从typedef结构中获取经度和纬度

[英]C Programming: Creating a Program to Pull Latitude and Longitude from a typedef struct

So I have a project for my class where I need to write a program where the User will type in a City then the program will look up this city from a typedef structure then print that said city's latitude and longitude coordinates. 因此,我为班级准备了一个项目,我需要编写一个程序,用户将在其中键入城市,然后程序将从typedef结构中查找该城市,然后打印出该城市的纬度和经度坐标。 My idea is to take the cities name as an input then somehow compare the city name with the ones in the structure (pointers maybe?) then get the latitude and the longitude when the city matches. 我的想法是将城市名称作为输入,然后以某种方式将城市名称与结构中的城市名称(可能是指针)进行比较,然后在城市匹配时获得经度和纬度。 I think that I will need to create a loop program to read the data until I find a match. 我认为我将需要创建一个循环程序来读取数据,直到找到匹配项。 Can you tell me if I am on the right track, or how you would go about writing the program? 您能告诉我我走的路是否正确,或者您打算如何编写程序? I attached a small portion of the structure (full one has 200 cities) if someone wants to try it out. 如果有人想尝试一下,我会附加一小部分结构(整个结构有200个城市)。

  #define MAX_CITY_LEN   35

   typedef struct { 
            char  name[MAX_CITY_LEN];
            float latitude;
            float longitude;
           } PLACE_T;

   PLACE_T places[] =
     {{"Aberdeen,Scotland",57.15,-2.15},
      {"Adelaide,Australia",-34.92,138.6},
      {"Albany,NY,USA",42.67,-73.75},
      {"Albuquerque,NM,USA",35.08,-106.65},
      {"Algiers,Algeria",36.83,3.0},
      {"Amarillo,TX,USA",35.18,-101.83},

Thanks, Alex 谢谢,亚历克斯

do like this: 这样做:

 typedef struct { 
            char  name[MAX_CITY_LEN];
            float latitude;
            float longitude;
 } PLACE_T;

 PLACE_T places[] ={
      {"India,kochi",57.15,-2.15},
      {"Adelaide,Australia",-34.92,138.6},
      {"Albany,NY,USA",42.67,-73.75},
      {"Albuquerque,NM,USA",35.08,-106.65},
      {"Algiers,Algeria",36.83,3.0},
      {"Amarillo,TX,USA",35.18,-101.83},
   };

int main(){
   int j;
  int found=0;
  char city[MAX_CITY_LEN];
  printf("enter city name:");
  fgets(city,MAX_CITY_LEN,stdin);
  city[strlen(city)-1]='\0';
  int citySize=(sizeof(places)/sizeof(places[0]));
  for(j=0;j<citySize;j++){
     if(strcmp(city,places[j].name)==0){
        found=1;                                
        printf("lat = %f, long = %f",places[j].latitude,places[j].longitude); 
        break;                              
     }
   }
   if(!found)
     printf("City not found");

    return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM