// CSE143 Sp01 Homework 1 Sample Solution // hw1sol.cpp - main program // HP 4/01 #include #include #include #include using namespace std; #include "placelist.h" // Read place data from "places.txt" and store in places // Returns true if successful; otherwise returns false bool readPlaceList(PlaceList &places) { double x, y; // current location and place name from file string name; // Attempt to open file; quit if unsuccessful ifstream data("places.txt"); if (!data) { return false; } // read data from file data >> x >> y >> name; while (name != ".") { addPlace(places, x, y, name); data >> x >> y >> name; } return true; } // = distance from (0,0) to (x,y) double distance(double x, double y) { return sqrt(x*x + y*y); } // Read place names and report distance from the center of the universe void answerQueries(PlaceList &places) { string name; // place name entered by user bool found; // = "place name was found in places" double x, y; // location of place, if found double dist; // distance between (0,0) and (x,y) cout << "Please enter place names or a period to stop" << endl; cin >> name; while (name != ".") { // report results for current name findCoordinates(places, name, x, y, found); if (!found) { cout << "Never heard of " << name << endl; } else { dist = distance(x,y); if (dist == 0.0) { cout << name << " is the Center of the Universe" << endl; } else { cout << name << " is " << dist << " miles from the Center of the Universe" << endl; } } // read next name cin >> name; } cout << "Bye" << endl; } // main program int main() { PlaceList places; // list of place names and locations bool success; // = "input file read successfully" makePlaceListEmpty(places); success = readPlaceList(places); if (!success) { cout << "Unable to read input file" << endl; return 1; } answerQueries(places); return 0; }