• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Jan 24th, 2020 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <string.h>
4. #include <math.h>
5.
6. #define BUFFER_SIZE 1024
7.
8. #define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
10.
11. double getDistance(double lat1, double lon1, double lat2, double lon2)
12. {
13.     double a = (sin((lat2 - lat1) / 2) * sin((lat2 - lat1) / 2)) + cos(lat1) * cos(lat2) * (sin((lon2 - lon1) / 2) * sin((lon2 - lon1) / 2));
14.     return 6371. * 2. * atan2(sqrt(a), sqrt(1 - a));
15. }
16.
17. int main(int argc, char *argv[]) {
18.     FILE* input = fopen(argv[1], "r");
19.     FILE *output = fopen(argv[4], "w");
20.     char line[BUFFER_SIZE];
21.
22.     double input_lat;
23.     double input_lon;
24.     int total = 0;
25.
26.     while (fgets(line, BUFFER_SIZE, input))
27.     {
28.         char* tmp = strdup(line);
29.         int i = 0;
30.         const char* tok;
31.
32.         const char* city;
33.         double lat;
34.         double lon;
35.
36.         for (tok = strtok(line, ";");
37.              tok && *tok;
38.              tok = strtok(NULL, ";\n"))
39.         {
40.             switch(i) {
41.                 case 0:
42.                     city = tok;
43.                     break;
44.                 case 2:
46.                     break;
47.                 case 3:
49.                     break;
50.             }
51.             i++;
52.         }
53.         if(strcmp(city, argv[2]) == 0) {
54.             input_lon = lon;
55.             input_lat = lat;
56.         }
57.
58.         free(tmp);
59.     }
60.
61.     fseek(input, 0, SEEK_SET);
62.
63.     while (fgets(line, BUFFER_SIZE, input))
64.     {
65.         char* tmp = strdup(line);
66.         int i = 0;
67.         const char* tok;
68.
69.         const char* city;
70.         int population;
71.         double lat;
72.         double lon;
73.
74.         for (tok = strtok(line, ";");
75.              tok && *tok;
76.              tok = strtok(NULL, ";\n"))
77.         {
78.             switch(i) {
79.                 case 0:
80.                     city = tok;
81.                     break;
82.                 case 1:
83.                     population = strtol(tok, NULL, 10);
84.                     break;
85.                 case 2:
87.                     break;
88.                 case 3:
90.                     break;
91.             }
92.             i++;
93.         }
94.
95.         double distance = getDistance(input_lat, input_lon, lat, lon);
96.
97.         if(distance <= strtod(argv[3], NULL)) {
98.             fprintf(output, "%s %d %.2f km\n", city, population, distance);
99.             total += population;
100.         }
101.
102.         free(tmp);
103.     }
104.
105.     printf("total population: %d", total);
106.
107.     fclose(input);
108.     fclose(output);
109.     return 0;
110. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top