?? driver.cpp
字號:
/*
File driver.cpp shinnerl@ucla.edu
Graph class Driver -- menu based graph operations.
There are very few safeguards against improperly formatted input.
*/
#include "graph.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <deque>
#include <algorithm>
using std::string;
using std::cout;
using std::cin;
using std::ifstream;
using std::ofstream;
using std::endl;
using std::copy;
const int LINE = 82;
const int MAX_TRY = 3;
// ==================== BEGIN mainMenu() ====================
void mainMenu( void )
{
cout << "\n\nMAIN MENU. Enter: \n"
<< " R to READ the new graph from a file \n"
<< " W to WRITE the current graph to a file \n"
<< " IE to INSERT a new edge or change an old edge\n"
<< " IV to INSERT a new vertex \n"
<< " DE to DELETE an EDGE \n"
<< " DV to DELETE a VERTEX \n"
<< " FE to FIND an EDGE \n"
<< " FV to FIND an VERTEX and view its neighbors \n"
// << " C to get the connected COMPONENT for a vertex \n"
<< " V to VIEW the graph as a list of adjacency lists \n"
<< " P to find the least-weight PATH between two vertices \n"
<< " M or H (HELP) to reread this MENU \n"
<< " or Q to QUIT this program. \n";
}
// ==================== END mainMenu() ====================
inline void promptUser(){
cout << "\nEnter R W IE IV DE DV FE FV V P M(Menu) or Q(Quit):> ";
}
// ==================== BEGIN main() module ======================
int main()
{
typedef string Vtype; // Vertex type.
typedef int Wtype; // Edge weight type.
Graph<Vtype,Wtype> G;
Wtype *Wptr;
std::map<Vtype,Wtype> *AmapPtr;
ifstream inFile;
ofstream outFile;
Vtype i,j;
char selection[3] = "\0\0";
char buffer[LINE];
mainMenu();
while ( *selection != 'Q' && *selection != 'q' )
{
promptUser();
cin >> selection;
cin.get();
switch ( *selection )
{
case 'I': case 'i':
{
switch( selection[1] )
{
case 'E': case 'e':
{
cout << "\nEnter the first vertex identifier: ";
cin >> i;
cout << "\nEnter the second vertex identifier: ";
cin >> j;
cout << "\nEnter the weight for the edge: ";
Wtype temp;
cin >> temp;
if ( G.findVertex(i) && G.findVertex(j) )
G.setEdge(i,j,temp); // Preserves symmetry.
else
cout << "Vertex or vertices not found." << endl;
break;
}
case 'V': case 'v':
{
cout << "Enter the identifier for the new vertex: ";
cin >> i;
if (!G.findVertex(i))
{
G.insertVertex(i);
cout << "Vertex " << i
<< " has been inserted into the graph." << endl;
}
else
cout << "Vertex " << i << " exists. Remove it or "
<< "choose a different identifier." << endl;
break;
}
default:
cout << "\nThat option is not available.\n"
<< "Enter 'M' or 'H' to see the help menu.\n";
}
break;
}
case 'D': case 'd':
{
switch( selection[1] )
{
case 'E': case 'e':
{
cout << "\nEnter the first vertex identifier: ";
cin >> i;
cout << "\nEnter the second vertex identifier: ";
cin >> j;
G.removeEdge(i,j); // Preserves symmetry.
break;
}
case 'V': case 'v':
{
cout << "Enter the identifier for the vertex: ";
cin >> i;
G.removeVertex(i);
break;
}
default:
cout << "\nThat option is not available.\n"
<< "Enter 'M' or 'H' to see the help menu.\n";
}
break;
}
case 'F': case 'f':
{
switch( selection[1] )
{
case 'E': case 'e':
{
cout << "\nEnter the first vertex identifier: ";
cin >> i;
cout << "\nEnter the second vertex identifier: ";
cin >> j;
Wptr = G.findEdge(i,j);
if (Wptr)
cout << "(" << i << ", " << j << "): "
<< *Wptr << endl;
else
cout << "Edge (" << i << ", " << j << ") "
<< "does not exist." << endl;
break;
}
case 'V': case 'v':
{
cout << "\nEnter the vertex identifier: ";
cin >> i;
AmapPtr = G.findVertex(i);
if (AmapPtr)
{
cout << i << " { ";
std::map<Vtype, Wtype>::iterator q;
for (q = AmapPtr->begin(); q!=AmapPtr->end(); ++q)
cout << " (" << q->first
<< ", " << q->second << ") ";
cout << " } " << endl;
}
else
cout << "Vertex " << i << " does not exist." << endl;
break;
}
default:
cout << "\nThat option is not available.\n"
<< "Enter 'M' or 'H' to see the help menu.\n";
}
break;
}
case 'V': case 'v':
{
cout << G;
break;
}
case 'P': case 'p':
{
Wtype cost;
cout << "\nEnter INITIAL vertex identifier: ";
cin >> i;
cout << "Enter FINAL vertex identifier: ";
cin >> j;
deque<Vtype> myPath;
if (G.findVertex(i) && G.findVertex(j))
{
cost = G.leastCostPath(i,j,myPath);
if ( !myPath.empty() )
{
cout << "\nThe shortest path between your vertices is:\n";
copy( myPath.begin(), myPath.end(),
std::ostream_iterator<Vtype>(cout, " "));
cout << "\nThe path's weight is: " << cost << endl;
}
}
else
cout << "\nAt least one of your vertices "
<< "is not in the Graph.\n";
break;
}
case 'R': case 'r':
{
cout << "\nEnter the name of the input file: ";
while ( cin.peek() == '\n')
cin.get();
cin.getline( buffer, LINE );
inFile.open( buffer );
if( inFile ){
cout << "\nFile " << buffer << " opened. Reading... ";
try{
inFile >> G;
cout << "\nGraph read. \n";
}
catch(Graph<Vtype,Wtype>::Error& E){
cout << string(E) << std::endl;
}
}
else
cout << "\nFile " << buffer << " not found! " << endl;
inFile.close();
inFile.clear();
break;
}
case 'W': case 'w':
{
cout << "\nEnter the name of the output file. \n"
<< "This file will be *overwritten* if it already exists."
<< "\n";
while ( cin.peek() == '\n')
cin.get();
cin.getline( buffer, LINE);
outFile.open( buffer );
assert( outFile );
outFile << G;
outFile.close();
cout << "\nYour graph has been written to file "
<< buffer << ".\n";
break;
}
case 'M': case 'm': case 'H': case 'h':
{
mainMenu();
break;
}
case 'Q': case 'q':
{
break;
}
default:
{
cout << "\nThat option is not available.\n"
<< "Enter 'M' or 'H' to see the help menu.\n";
break;
}
} // end switch()
} // end while()
return 0;
} // end main()
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -