The C++ Program: north.cpp
1 #include <iostream>
2 #include <stdlib.h>
3 #include <string>
4 #include <vector>
5
6 using namespace std;
7
8 class city{
9 private:
10 vector<city*> norths;
11
12 public:
13 string name;
14
15 city(string n);
16 void make_south_of(city* n);
17 bool is_south_of(city* n);
18 };
19
20 city::city(string n) {
21 vector<city*> norths;
22
23 name = n;
24 }
25
26
27 void city::make_south_of(city* n) {
28 norths.push_back(n);
29 }
30
31 bool city::is_south_of(city* other) {
32 for (int i = 0; i < norths.size(); i++) {
33 if (other == norths[i]) return true;
34 }
35
36 for (int i = 0; i < norths.size(); i++) {
37 if (norths[i]->is_south_of(other)) return true;
38 }
39 return false;
40 }
41
42 int index_of(string name, vector<city*> & country);
43
44 int main (int argc, char *argv[]) {
45 vector<city*> country;
46
47 string s_name, n_name;
48
49 cin >> s_name;
50 while(!s_name.empty() && s_name[0] != '#') {
51 cin >> n_name;
52 int s_ind = index_of(s_name,country);
53 int n_ind = index_of(n_name,country);
54 country[s_ind]->make_south_of((country[n_ind]) );
55
56 cin >> s_name;
57 }
58
59
60 cin >> s_name;
61 while (!s_name.empty() && s_name[0] != '#') {
62 cin >> n_name;
63 int s_ind = index_of(s_name,country);
64 int n_ind = index_of(n_name,country);
65 if(country[s_ind]->is_south_of(country[n_ind])) {
66 cout << country[s_ind]->name << " is south of " << country[n_ind]->name;
67 } else if(country[n_ind]->is_south_of(country[s_ind]))
68 cout << country[s_ind]->name << " is north of " << country[n_ind]->name;
69 } else {
70 cout << "The relative position of " << country[s_ind]->name << " and "
71 << country[n_ind]->name << " is unknown";
72 }
73 cout << "." << endl;
74
75
76 cin >> s_name;
77 }
78
79 }
80
81 int index_of(string name, vector<city*> & country) {
82 int i;
83 for(i = 0; i < country.size(); i++) {
84 if(country[i]->name == name)
85 return i;
86 }
87 city* new_city = new city(name);
88 country.push_back(new_city);
89 return i;
90 }