⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 networkdriver.java

📁 Network数据结构与算法分析中 图的实现 相当全面 请需要的朋友们下载
💻 JAVA
字号:
import java.io.*;import java.util.*;public class NetworkDriver implements Process {  protected int lineRead;  // = 0 for the line containing the input file and                           // = 1 for the line containing the output file  protected BufferedReader fileReader; // to read in the input file  protected PrintWriter fileWriter; // to read in the output file  protected Network myNetwork;   // the LinkedCollection instance  protected Iterator itr;        // to iterate through myNetwork  protected GUI gui;    protected final static String IN_FILE_PROMPT = "Enter input filename:";  public NetworkDriver() {    gui = new GUI (this);    gui.println (IN_FILE_PROMPT);    myNetwork = new Network();  }   public void processInput (String s) {    final String FILE_NOT_FOUND_MESSAGE = "The file was not found.";    final String IO_EXCEPTION_MESSAGE = "There was an IO exception.";    final String OUT_FILE_PROMPT = "\nEnter output filename:";    try {        gui.print (s);        if (lineRead == 0) {            fileReader = new BufferedReader (new FileReader (s));            lineRead++;            gui.println (OUT_FILE_PROMPT);        } // input-path line        else if (lineRead == 1) {            fileWriter = new PrintWriter (new FileWriter (s));            readAndProcessInFile();        } // output-path line    } // try    catch (FileNotFoundException e) {        gui.print (FILE_NOT_FOUND_MESSAGE);    }    catch (IOException e) {        gui.print (IO_EXCEPTION_MESSAGE + e);    }} // method processInput// Postcondition: All of the lines in the input file have been read in//                and processed: the corresponding methods have been called.public void readAndProcessInFile() throws IOException {        final String LINE_MESSAGE = "The line is:\n";        final String NETWORK_MESSAGE = "Here is the Network:\n";        final String CLOSE_WINDOW_PROMPT =            "\n\nThe program has ended.  Please close the window.";        Iterator tempItr;        String line = fileReader.readLine();  	while (line != null) {            fileWriter.println (LINE_MESSAGE + line);            callMethod (line);            fileWriter.println (NETWORK_MESSAGE);            tempItr = myNetwork.iterator();            while (tempItr.hasNext())                fileWriter.println (tempItr.next());            fileWriter.println ("\n\n\n");            line = fileReader.readLine();        } // while        fileWriter.close();        gui.println (CLOSE_WINDOW_PROMPT);        gui.freeze();    } // method readAndProcess    // Postcondition: The method just read in has been called.    void callMethod (String line) {        final String IS_EMPTY = "isEmpty";        final String IS_EMPTY_MESSAGE = "That the collection is empty is ";        final String SIZE = "size";        final String SIZE_MESSAGE =            "The number of elements in the collection is ";        final String ADD_VERTEX = "addvertex";        final String ADD_VERTEX_MESSAGE =            "The element added to the collection is ";        final String ADD_EDGE = "addedge";        final String ADD_EDGE_MESSAGE =            "The edge added to the colllection is ";        final String REMOVE_VERTEX = "removevertex";        final String REMOVE_VERTEX_MESSAGE =            "The element removed from the collection is ";        final String REMOVE_VERTEX_ERROR_MESSAGE =            "Element was not found in collection";        final String REMOVE_EDGE = "removeedge";        final String REMOVE_EDGE_MESSAGE =            "The edge removed from the collection is ";        final String REMOVE_EDGE_ERROR_MESSAGE =            "Edge was not found in collection";        final String IS_CONNECTED = "isConnected";        final String IS_CONNECTED_MESSAGE =            "The result of isConnected is ";        final String GET_MINIMUM_SPANNING_TREE = "getMinimumSpanningTree";        final String GET_MINIMUM_SPANNING_TREE_MESSAGE =            "The minimum spanning tree is ";        final String GET_MINIMUM_SPANNING_TREE_ERROR =           "The tree is not connected, so there is no minimum spanning tree";        final String GET_SHORTEST_PATH = "getShortestPath";        final String GET_SHORTEST_PATH_MESSAGE =            "The shortest path between ";        final String GET_SHORTEST_PATH_MESSAGE_ERROR =            "There is no shortest path for those vertices";        final String GET_EDGE_COUNT = "getEdgeCount";        final String GET_EDGE_COUNT_MESSAGE =            "The number of edges in this network is: ";        final String GET_EDGE_WEIGHT = "getEdgeWeight";        final String GET_EDGE_WEIGHT_MESSAGE =            "The edge weight of edge ";        final String GET_EDGE_WEIGHT_ERROR =            "The edge does not exist in this network";        final String GET_CYCLE = "getCycle";                    final String CONTAINS_VERTEX = "containsVertex";        final String CONTAINS_VERTEX_MESSAGE = " is contained in the collection is ";        final String CONTAINS_EDGE = "containsEdge";        final String CONTAINS_EDGE_MESSAGE = " is contained in the collection is ";        final String ITERATOR = "iterator";        final String ITERATOR_MESSAGE =            "The iterator has been constructed.";        final String HAS_NEXT = "hasNext";        final String HAS_NEXT_MESSAGE =            "That the iterator is not positioned beyond the end of " +            "the collection is ";        final String NEXT = "next";        final String NEXT_MESSAGE = "The last element returned was ";        final String BAD_METHOD =            "The string entered does not represent a legal method identifier.";        StringTokenizer tokens = new StringTokenizer (line);        String method = tokens.nextToken();        String element, element2, element3;        Vertex vertex, vertex2;        double weight;        if (method.equals (IS_EMPTY))            fileWriter.println (IS_EMPTY_MESSAGE + myNetwork.isEmpty());        else if (method.equals (SIZE))            fileWriter.println (SIZE_MESSAGE + myNetwork.size());        else if (method.equals (ADD_VERTEX)) {            element = tokens.nextToken();            vertex = new Vertex(element);            fileWriter.println (ADD_VERTEX_MESSAGE + element);            myNetwork.addVertex (vertex);        } // addVertex        else if (method.equals(GET_EDGE_COUNT)) {            fileWriter.println(GET_EDGE_COUNT_MESSAGE +                               myNetwork.getEdgeCount());        } //getEdgeCount        else if (method.equals(GET_EDGE_WEIGHT)) {            element = tokens.nextToken();            element2 = tokens.nextToken();            vertex = new Vertex(element);            vertex2 = new Vertex(element2);            double edgeWeight = myNetwork.getEdgeWeight(vertex, vertex2);            if (edgeWeight != -1)              fileWriter.println(GET_EDGE_WEIGHT_MESSAGE + element + " - " +                                 element2 + " is: " + edgeWeight);            else              fileWriter.println(GET_EDGE_WEIGHT_ERROR);        } //getEdgeWeight        else if (method.equals(IS_CONNECTED)) {          if (myNetwork.isConnected())            fileWriter.println(IS_CONNECTED_MESSAGE + "true");          else            fileWriter.println(IS_CONNECTED_MESSAGE + "false");        } // isConnected        else if (method.equals(GET_MINIMUM_SPANNING_TREE)) {              Network minspantree = new Network();           if (myNetwork.isConnected()) {              minspantree = myNetwork.getMinimumSpanningTree();              fileWriter.println (GET_MINIMUM_SPANNING_TREE_MESSAGE + "\n" +                                  minspantree);           } //if myNetwork is connected           else              fileWriter.println(GET_MINIMUM_SPANNING_TREE_ERROR);        } //minimum spanning tree        else if (method.equals(GET_SHORTEST_PATH)) {           LinkedList shortestPath = new LinkedList();           element = tokens.nextToken();           element2 = tokens.nextToken();           vertex = new Vertex(element);           vertex2 = new Vertex(element2);           shortestPath = myNetwork.getShortestPath(vertex, vertex2);           if (shortestPath.size() > 0) {              fileWriter.println(GET_SHORTEST_PATH_MESSAGE + element + " and " +                                 element2 + " with the total weight of that "                                 "path as the last element is: " + "\n" +                                 shortestPath);           }           else              fileWriter.println(GET_SHORTEST_PATH_MESSAGE_ERROR);        } //getShortestPath        else if (method.equals (ADD_EDGE)) {            element = tokens.nextToken();            element2 = tokens.nextToken();            element3 = tokens.nextToken();            vertex = new Vertex(element);            vertex2 = new Vertex(element2);            weight = Double.parseDouble(element3);            fileWriter.println (ADD_EDGE_MESSAGE + element + " - " +                                element2 + " with weight " + weight);            myNetwork.addEdge(vertex, vertex2, weight);        } // addEdge        else if (method.equals(REMOVE_VERTEX)) {            element = tokens.nextToken();            vertex = new Vertex(element);            if (myNetwork.removeVertex (vertex))               fileWriter.println (REMOVE_VERTEX_MESSAGE + element);            else               fileWriter.println (REMOVE_VERTEX_ERROR_MESSAGE);        } //removeVertex        else if (method.equals(REMOVE_EDGE)) {            element = tokens.nextToken();            element2 = tokens.nextToken();            vertex = new Vertex(element);            vertex2 = new Vertex(element2);            if (myNetwork.removeEdge (vertex, vertex2))               fileWriter.println (REMOVE_EDGE_MESSAGE + element + " - " +                                   element2);            else               fileWriter.println (REMOVE_EDGE_ERROR_MESSAGE);        } //removeEdge        else if (method.equals (CONTAINS_VERTEX)) {            element = tokens.nextToken();            vertex = new Vertex(element);            fileWriter.println (element + CONTAINS_VERTEX_MESSAGE +                                myNetwork.containsVertex(vertex));        } // contains        else if (method.equals (CONTAINS_EDGE)) {            element = tokens.nextToken();            element2 = tokens.nextToken();            vertex = new Vertex(element);            vertex2 = new Vertex(element2);            fileWriter.println ("Edge " + element + " - " + element2 +                                 CONTAINS_VERTEX_MESSAGE +                                myNetwork.containsEdge(vertex, vertex2));        } // contains        else if (method.equals (ITERATOR)) {            itr = myNetwork.iterator();            fileWriter.println (ITERATOR_MESSAGE);        } // iterator        else if (method.equals (HAS_NEXT))            fileWriter.println (HAS_NEXT_MESSAGE + itr.hasNext());        else if (method.equals (NEXT))            fileWriter.println (NEXT_MESSAGE + itr.next());        else            fileWriter.println (BAD_METHOD);    } // method callMethod} //class NetworkDriver

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -