Advertisement
hugol

Untitled

Dec 11th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <vector>
  5. #include <cmath>
  6. #define M_PI 3.14159265358979323846
  7. using namespace std;
  8.  
  9. double cFloatToD(string cfloat){
  10.     int pos = string::npos;
  11.     while((pos = cfloat.find(",")) != string::npos){
  12.         cfloat.replace(pos, 1, ".");
  13.     }
  14.     return atof(cfloat.c_str());
  15. }
  16.  
  17. vector<string>* tokenize(vector<string>* intok, string str){
  18.     intok->clear();
  19.     char *pch = new char[str.length()];
  20.     for (int i=0; i<str.length(); i++) 
  21.         pch[i] = str.c_str()[i];
  22.     pch[str.length()-1] = 0;
  23.     pch = strtok(pch,";");
  24.     while (pch != NULL){
  25.         intok->push_back(pch);
  26.         pch = strtok(NULL, ";");
  27.     }
  28.     delete[] pch;
  29.     return intok;
  30. }
  31.  
  32. int main(){
  33.     string line,MinName;    double selfx, selfy, posx, posy, distx, disty, dist, MinDist=10000;
  34.     cin >> line;    selfx = cFloatToD(line);    selfx=(selfx/180)*M_PI;
  35.     cin >> line;    selfy = cFloatToD(line);    selfy=(selfy/180)*M_PI;
  36.     int n;  cin >> n;
  37.     cin.ignore();
  38.     for (int i=0; i<n; i++){
  39.         getline(cin, line);
  40.         vector<string>* tokens = new vector<string>;
  41.         tokens = tokenize(tokens, line);
  42.         posx = cFloatToD((*tokens)[tokens->size()-2].c_str());  posx=(posx/180)*M_PI;
  43.         posy = cFloatToD((*tokens)[tokens->size()-1].c_str());  posy=(posy/180)*M_PI;
  44.         distx = (posx - selfx) * cos((selfy+posy)/2);
  45.         disty = (posy - selfy);
  46.         dist = (pow(distx,2) + pow(disty,2)) * 6371;
  47.         if (dist < MinDist){
  48.             MinName = (*tokens)[1];
  49.             MinDist = dist;
  50.         }
  51.     }
  52.     cout << MinName;
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement