?? main.cpp
字號:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include "ContainerImpl.h"
class PrintN : public Container::Functor {
std::ostream& o;
mutable int n;
public:
explicit PrintN( int n = 0, std::ostream& o = std::cout ) : o( o ), n( n ) { }
bool operator()( const Key& key ) const {
o << key << ' ';
return n <= 0 || --n;
}
};
class mystring : public std::string {
public:
bool isPrefixOf( const char* const s ) { return size() > 0 && !compare( 0, size(), s, 0, size() ); }
operator Container::Order( ) {
return isPrefixOf( "ascending" ) ? Container::ascending : isPrefixOf( "descending" ) ? Container::descending : Container::dontcare;
}
};
const char* helpstr =
"new ............................... create new Container\n"
"delete ............................ delete Container\n"
"add <key> [...] ................... add <key>(s) with Container::add( const Key& )\n"
"remove <key> [...] ................ remove <key>(s) with Container::remove( const Key& )\n"
"isMember <key> .................... call Container::isMember( <key> )\n"
"size .............................. call Container::size()\n"
"isEmpty ........................... call Container::isEmpty()\n"
"minKey ............................ call Container::minKey()\n"
"maxKey ............................ call Container::maxKey()\n"
"teamNr ............................ call Container::teamNr()\n"
"themeNr ........................... call Container::themNr()\n"
"print ............................. print container with operator<<()\n"
"trace ............................. toggle tracing on/off\n"
"foreach [asc|desc|dontcare [<n>]] . traverse container with print functor\n"
"fadd <filename> ................... add values read from file <filename>\n"
"fremove <filename> ................ remove values read from file <filename>\n"
"radd [<n> [<seed>]] ............... add <n> random values, reset generator to <seed>\n"
"rremove [<n> [<seed>]] ............ remove <n> random values, reset generator to <seed>\n"
"quit .............................. quit program\n\n"
"arguments surrounded by [] are optional\n";
int main() {
Container* c = 0;
bool traceIt = false;
std::cout.setf( std::ios_base::boolalpha );
while (true) {
if (traceIt && c) std::cout << std::endl << "Container: " << *c;
std::cout << std::endl << "> ";
std::string cmdline;
if (!getline( std::cin, cmdline )) break;
std::istringstream cmdstream( cmdline );
mystring cmd;
cmdstream >> cmd;
try {
if (cmd.isPrefixOf( "quit" )) {
break;
} else if (cmd.isPrefixOf( "new" )) {
if (c) {
std::cerr << "container exists, 'delete' it first";
} else {
c = new ContainerImpl;
}
} else if (cmd.isPrefixOf( "help" ) || cmd == "?") {
std::cout << helpstr;
} else if (!c) {
std::cout << "no container (use 'new')";
} else {
Key key;
if (cmd.isPrefixOf( "delete" )) {
delete c;
c = 0;
} else if (cmd.isPrefixOf( "add" )) {
while (cmdstream >> key) { c->add( key ); }
} else if (cmd.isPrefixOf( "remove" )) {
while (cmdstream >> key) { c->remove( key ); }
} else if (cmd.isPrefixOf( "isMember" )) {
cmdstream >> key;
std::cout << "returns " << c->isMember( key );
} else if (cmd.isPrefixOf( "size" )) {
std::cout << "returns " << c->size( );
} else if (cmd.isPrefixOf( "isEmpty" )) {
std::cout << "returns " << c->isEmpty( );
} else if (cmd.isPrefixOf( "minKey" )) {
std::cout << "returns " << c->minKey( );
} else if (cmd.isPrefixOf( "maxKey" )) {
std::cout << "returns " << c->maxKey( );
} else if (cmd.isPrefixOf( "teamNr" )) {
std::cout << "returns " << c->teamNr( );
} else if (cmd.isPrefixOf( "print" )) {
std::cout << *c;
} else if (cmd.isPrefixOf( "themeNr" )) {
std::cout << "returns " << c->themeNr( );
} else if (cmd.isPrefixOf( "trace" )) {
std::cout << "trace " << ((traceIt = !traceIt) ? "on" : "off");
} else if (cmd.isPrefixOf( "radd" )) {
int seed = -1, count = 1;
cmdstream >> count >> seed;
if (seed != -1) KeyFactory::srand( seed );
while (count-- > 0) c->add( KeyFactory::newKey() );
} else if (cmd.isPrefixOf( "rremove" )) {
int seed = -1, count = 1;
cmdstream >> count >> seed;
if (seed != -1) KeyFactory::srand( seed );
while (count-- > 0) c->remove( KeyFactory::newKey() );
} else if (cmd.isPrefixOf( "foreach" )) {
int n = 0;
mystring order;
cmdstream >> order >> n;
c->foreach( PrintN( n, std::cout ), Container::Order( order ) );
} else if (cmd.isPrefixOf( "fadd" )) {
std::string filename;
cmdstream >> filename;
std::ifstream keystream( filename.c_str() );
while (keystream >> key) { c->add( key ); }
} else if (cmd.isPrefixOf( "fremove" )) {
std::string filename;
cmdstream >> filename;
std::ifstream keystream( filename.c_str() );
while (keystream >> key) { c->remove( key ); }
} else {
std::cout << cmd << "? try 'help'";
}
}
} catch( Container::Exception e ) {
std::cout << "Exception " << e;
} catch( ... ) {
std::cout << "OOPS!";
}
}
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -