Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <cmath>
- #include <cassert>
- #include <algorithm>
- #include <fstream>
- class CommandBase{
- protected:
- std::string m_name;//The internal name
- std::string m_help;//The internal help line
- public:
- //The public interface for name.
- const std::string &name = m_name;
- //The public interface for the help line.
- const std::string &help = m_help;
- virtual void execute(const std::string &line){}
- CommandBase(){
- m_name = "BASE_COMMAND";
- m_help = "BASE_HELP_MESSAGE";
- }
- };
- class CommandExit : public CommandBase{
- public:
- CommandExit(){
- m_name = "exit";
- m_help = "exit ~ Will cause the program to terminate. ";
- }
- virtual void execute(const std::string &line){
- std::cout<<"Exiting"<<std::endl;
- exit(0);
- }
- }commandExit;
- class CommandDouble : public CommandBase{
- public:
- CommandDouble(){
- m_name = "double";
- m_help = "double <number1> ~ Will output number1 times two. ";
- }
- virtual void execute(const std::string &line){
- int i = std::atoi(line.c_str());
- std::cout<<"The double of: "<<i<<" is "<<i*2<<std::endl;
- }
- }commandDouble;
- int split(const std::string& line, const std::string& seperator, std::vector<std::string> * values){
- std::string tString = "";
- unsigned counter = 0;
- for(unsigned l = 0; l < line.size(); ++l){
- for(unsigned i = 0; i < seperator.size(); ++i){
- if(line[l+i]==seperator[i]){
- if(i==seperator.size()-1){
- values->push_back(tString);
- tString = "";
- ++counter;
- }else continue;
- }else{
- tString.push_back(line[l]);
- break;
- }
- }
- }
- if(tString!="")values->push_back(tString);
- return counter;
- }
- class CommandPower : public CommandBase{
- public:
- CommandPower(){
- m_name = "pow";
- m_help = "pow <number1> <number2> ~ Will raise number 1 to the power of number 2. ";
- }
- virtual void execute(const std::string &line){
- double d, exp;
- std::cout<<"Param:"<<line<<std::endl;
- std::vector<std::string> vals;
- split(line," ",&vals);
- assert(vals.size()>=2);//We don't want a nasty Out Of Bounds.
- for(unsigned i = 0; i < vals.size(); ++i){
- std::cout<<"Vals["<<i<<"] = "<<vals[i]<<std::endl;
- }
- d = std::atof(vals[0].c_str());
- exp = std::atof(vals[1].c_str());
- std::cout<<d<<" raised to the power of: "<<exp<<" is "<<pow(d,exp)<<std::endl;
- }
- }commandPower;
- class CommandFileWrite : public CommandBase{
- public:
- CommandFileWrite(){
- m_name = "write";
- m_help = "write <filename> [message] ~ Will append the message to the specified file. ";
- }
- virtual void execute(const std::string &line){
- std::cout<<"Param:"<<line<<std::endl;
- std::vector<std::string> vals;
- split(line," ",&vals);
- assert(vals.size()>=1);//We don't want a nasty Out Of Bounds.
- for(unsigned i = 0; i < vals.size(); ++i){
- std::cout<<"Vals["<<i<<"] = "<<vals[i]<<std::endl;
- }
- //The stream for the file we will save.
- std::ofstream fts;
- fts.open(vals[0].c_str(), std::ios::out|std::ios::app);
- if(fts.is_open()){
- for(unsigned i = 1; i < vals.size(); ++i){
- if(i+1<vals.size()) fts<<vals[i]<<" ";
- else fts<<vals[i]<<std::endl;
- }
- }else{
- std::cerr<<"Cannot open the file: "<<vals[0]<<std::endl;
- return;
- }
- }
- }commandFileWrite;
- void help(const std::vector<CommandBase*> &commandList){
- std::cout<<"---------------Operating Instructions---------------"<<std::endl;
- for(unsigned i = 0; i < commandList.size(); ++i){
- std::cout<<commandList[i]->help<<std::endl;
- }
- std::cout<<"---------------------------------------------------"<<std::endl<<std::endl;
- }
- void foo(){
- std::vector<CommandBase*> commandList;
- commandList.push_back(&commandExit);
- commandList.push_back(&commandDouble);
- commandList.push_back(&commandPower);
- commandList.push_back(&commandFileWrite);
- std::string ourCommand;
- std::string ourParameters;
- help(commandList);
- while(true){
- std::cin>>ourCommand;
- std::getline(std::cin,ourParameters);
- //Remove any preceeding whitespace.
- ourParameters.erase(ourParameters.begin(), std::find_if(ourParameters.begin(), ourParameters.end(), std::bind1st(std::not_equal_to<char>(), ' ')));
- std::cout<<"We will execute the command: "<<ourCommand<<std::endl;
- std::cout<<"We will use the parameters: "<<ourParameters<<std::endl;
- bool foundCommand = false;
- for(unsigned i = 0; i < commandList.size(); ++i){
- if(commandList[i]->name == ourCommand){
- foundCommand = true;
- commandList[i]->execute(ourParameters);
- break;
- }else continue;
- }
- if(!foundCommand){
- std::cout<<"The command: "<<ourCommand<<" was not reconized. Please try again."<<std::endl;
- help(commandList);
- }
- }
- }
- int main(){
- foo();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement