// // Sloppy I/O Routines for Assignment 4 // free of charge from Prof. Rogaway! // #include #include #include #include #include #include #include //--------------------------------------------------------------------------- // abort - // Utility routine that outputs an error message and exits. //--------------------------------------------------------------------------- void abort(char* s) { cerr << "Error occurred, aborting." << endl; if (s != 0) cerr << s << endl; exit(1); } //--------------------------------------------------------------------------- // Graph - // a very simple Graph class, supporting a constructor which reads in // a graph and forms an adjaceny list, and member functions to get // the number of verticies, the name of each vertex, and the length // of each edge. Verticies are referred to by numbers 0,1,..., n-1. //--------------------------------------------------------------------------- class Graph { int n; // number of verticies; double **Adj; // n x n adjacency matrix char** names; // "names" for the n verticies public: Graph(const char* ="\0"); // constructor. If no argument, read std input int numnodes() {return n;}; // accessor function -- number of verticies char* name(int i) // returns the "name" of vertex i {if (i<0 || i>=n) abort("Bad argument to graph::name"); return names[i];}; double length(int i, int j) // returns the length of edge (i,j) {if (i<0 || i>= n || j<0 || j>=n) abort("Bad argument to graph::length"); return Adj[i][j];}; }; //--------------------------------------------------------------------------- // Graph::Graph - // constructor -- reads in n and then n lines of data, each containing // a vertex "name" and a (latitude, longitude) on the earth's surface. //--------------------------------------------------------------------------- Graph::Graph(const char* name) { const n_max = 100; char line[80]; double *latitude, *longitude; int i,j; double dist_earth(double, double, double, double); ifstream infile(name); if (name[0] && !infile) abort("Can't open input file"); if (name[0]) infile.getline(line,sizeof(line)); else cin.getline(line,sizeof(line)); if (sscanf(line, "%d", &n)!=1) abort("Bad input file"); latitude = new double[n]; longitude = new double[n]; names = new char*[n]; Adj = new double*[n]; for (i=0; i