// To read a graph (in argv[1], say) from a file, do the following // // GraphInfo g; // readGraph(argv[1], g); // // somewhere in your code. This method reads the graph into a GraphInfo // object which has four members: // // name - the name of the graph (a character array of size 80) // nNodes - the number of nodes (an integer) // nEdges - the number of edges (an integer) // edges - an array of EdgeInfo objects // // Each EdgeInfo object has two members, integers u and v, which are the // labels for the nodes on that edge. // // Note that this method doesn't actually construct the graph data // structure. You will need to do this yourself. This method simply // extracts all of the necessary data from the file, so you don't have to // deal with I/O. // // Also note that the readGraph function returns false when either the // file is not found, or it is improperly formatted. #include #include class EdgeInfo { public: int u; int v; ~EdgeInfo() { } void print() { printf("(%d,%d)", u, v); } }; class GraphInfo { public: char name[80]; int nNodes; int nEdges; EdgeInfo *edges; GraphInfo() { edges = 0; } ~GraphInfo() { if (edges != 0) { delete[] edges; } } void print() { printf("name: %s\n", name); printf("nNodes: %d\n", nNodes); printf("nEdges: %d\n", nEdges); printf("edges: {"); for (int i=0; i> g.nNodes)) return false; if (!(ifs >> g.nEdges)) return false; g.edges = new EdgeInfo[g.nEdges]; for (int i=0; i> g.edges[i].u)) return false; if (!(ifs >> g.edges[i].v)) return false; } return true; }