From 819064a0de208a8d73991528eeb17b2271fafae7 Mon Sep 17 00:00:00 2001 From: djohoe28 Date: Fri, 28 May 2021 14:56:57 +0300 Subject: [PATCH 1/2] Updated files Added comments & documentation. Moved menu command printing to separate method to avoid redundant code. Changed menu functionality to read a word, then decide if a second word needs to be read (as to avoid relying on ',' delimiter.) All code changes marked with "//Jonathan" --- Port.cpp | 109 ++++++++++++++++++++++++++++++---------------- Port.h | 46 +++++++++++--------- Route.cpp | 26 +++++++---- Route.h | 22 +++++----- main.cpp | 15 ++++--- my_utility.h | 120 +++++++++++++++++++++++++++++++++------------------ 6 files changed, 215 insertions(+), 123 deletions(-) diff --git a/Port.cpp b/Port.cpp index ab55d90..7427fbd 100644 --- a/Port.cpp +++ b/Port.cpp @@ -1,53 +1,86 @@ #include "Port.h" -Port::Port() -{ - this->name=" "; - this->departure_time="00/00/00"; - this->arrival_time="00/00/00"; +using namespace std; + +Port::Port() { + // Default Constructor + // Input - N/A + // Print - N/A + // Output - Constructed Port {this} + this->name = " "; + this->departure_time = "00/00/00"; + this->arrival_time = "00/00/00"; } -Port::Port(const std::string& name,const std::string& arrival_time,const std::string& departure_time) -{ - this->name=name; - this->arrival_time=arrival_time; - this->departure_time=departure_time; +Port::Port(const string &name, const string &arrival_time, const string &departure_time) { + // Value Constructor + // Input - + // string &name = The name of the Port + // string &arrival_time = Arrival time at Port, in "DD/MM hh:mm" format. + // string &departure_time = Departure time from Port, in "DD/MM hh:mm" format. + // Print - N/A + // Output - Constructed Port {this} + this->name = name; + this->arrival_time = arrival_time; + this->departure_time = departure_time; } -Port::Port(const Port& source) -{ - this->name=source.name; - this->arrival_time=source.arrival_time; - this->departure_time=source.departure_time; - this->outbound=source.outbound; - +Port::Port(const Port &source) { + // Copy Constructor + // Input - const Port &source = Reference of Port to copy. + // Print - N/A + // Output - Constructed Port {this} + this->name = source.name; + this->arrival_time = source.arrival_time; + this->departure_time = source.departure_time; + this->outbound = source.outbound; + operator=(source); } -Port& Port::operator=(const Port& source) -{ - if(this!=&source) - { - this->name=source.name; - this->arrival_time=source.arrival_time; - this->departure_time=source.departure_time; - this->outbound=source.outbound; - } - return *this; +Port &Port::operator=(const Port &source) { + // Assignment Operator + // Input - const Port &source = Reference of Port to copy. + // Print - N/A + // Output - Newly copied Port& {this} + // TODO: Same function as Copy Constructor. Could it be used instead? + if (this != &source) { + this->name = source.name; + this->arrival_time = source.arrival_time; + this->departure_time = source.departure_time; + this->outbound = source.outbound; + } + return *this; } -bool Port:: operator==(const Port& source){return (this->name==source.name);} -bool Port::operator!=(const Port& source){return!(this->name==source.name);} - - - - - - - +Port &Port::operator=(const Port &&source) { + // Move Constructor + // Input - const Port &&source = Reference of Port to move. + // Print - N/A + // Output - Newly moved Port& {this} + // TODO: Apply move semantics. +} +bool Port::operator==(const Port &source) { + // Equals Operator + // Input - const Port &source = Port to compare this to. + // Print - bool = Are they equal? (Do they have the same name?) + // Output - N/A + return (this->name == source.name); +} +bool Port::operator!=(const Port &source) { + // Unequals Operator + // Input - const Port &source = Port to compare this to. + // Print - bool = Are they unequal? (Do they have different names?) + // Output - N/A + return !(this->name == source.name); +} -Port::~Port() -{ +Port::~Port() { + // Destructor + // Input - N/A + // Print - N/A + // Output - N/A + // TODO: Is vector correctly removed from memory using smart pointers? } \ No newline at end of file diff --git a/Port.h b/Port.h index b7f5a0c..2078d48 100644 --- a/Port.h +++ b/Port.h @@ -7,36 +7,42 @@ #include #include +using namespace std; -class Port -{ - private: - std::string name; - std::vector>outbound; - std::string arrival_time; - std::string departure_time; +class Port { +private: + string name; // Name of Port + vector> outbound; // Vector of Outbound connections to Port + string arrival_time; // Arrival time at Port ("DD/MM hh:mm" format) + string departure_time; // Departure time at Port ("DD/MM hh:mm" format) - public: - Port(); - Port(const std::string& name,const std::string& arrival_time,const std::string& departure_time); - Port(const Port& source); - Port(Port&& source); +public: + Port(); //Default Constructor - std::string get_name(){return this->name;} - std::string get_arrival_time(){return this->arrival_time;} - std::string get_departure_time(){return this->departure_time;} + Port(const string &name, const string &arrival_time, const string &departure_time); // Value Constructor - Port& operator=(const Port& source); - Port& operator=(const Port&& source); + Port(const Port &source); // Copy Constructor - bool operator==(const Port& source); - bool operator!=(const Port& source); + Port(const Port &&source); // Move Constructor + string getName() { return this->name; } // {name} getter + string getArrivalTime() { return this->arrival_time; } // {arrival_time} getter + string getDepartureTime() { return this->departure_time; } // {departure_time} getter - ~Port(); + Port &operator=(const Port &source); // Assignment Operator + + Port &operator=(const Port &&source); // Move Operator + + bool operator==(const Port &source); // Equals Operator + + bool operator!=(const Port &source); // Unequals Operator + + + ~Port(); // Destructor }; + #endif diff --git a/Route.cpp b/Route.cpp index 2a3ff89..0bc189a 100644 --- a/Route.cpp +++ b/Route.cpp @@ -1,13 +1,23 @@ #include "Route.h" -Route::Route(const Port& src,const Port& des,int weight) -{ - this->src=src; - this->des=des; - this->weight=weight; +using namespace std; + +Route::Route(const Port &src, const Port &des, int weight) { + // Value Constructor + // Input - + // const Port &src = Reference of source Port to set. + // const Port &des = Reference of destination Port to set. + // int weight = Weight of route (avg. # of minutes to cross) + // Print - N/A + // Output - Constructed Route + this->src = src; + this->des = des; + this->weight = weight; } -void Route::printRoute() -{ - std::cout<src.get_name()<<"------->"<des.get_name()<<" "<get_weihgt()< {des.name} {weight}" format.) + std::cout << this->src.get_name() << "------->" << this->des.get_name() << " " << this->get_weight() << endl; } \ No newline at end of file diff --git a/Route.h b/Route.h index 2a6cf62..c57aa73 100644 --- a/Route.h +++ b/Route.h @@ -1,18 +1,18 @@ #include "Port.h" -class Route -{ - private: - Port src; //source port - Port des; //destination port - int weight; +class Route { +private: + Port src; // Source Port + Port des; // Destination Port + int weight; //Route Weight - public: - Route(const Port& src,const Port& des,int weight); +public: + Route(const Port &src, const Port &des, int weight); // Value Constructor - int get_weihgt(){return this->weight;} + int getWeight() { return this->weight; } // {weight} getter. - int updateWeight(int newWeight); - void printRoute(); + int setWeight(int newWeight); // {weight} setter. + + void printRoute(); // Print function. }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index 9e8210c..008e7fb 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,15 @@ #include "my_utility.h" #include "Route.h" - -int main(int argc, char *argv[]) -{ - checkInput(argc,argv); - //printMenu(); +//|-> "Port.h" +// |--> +// |--> +// |--> +// |--> +// |--> + +int main(int argc, char *argv[]) { + checkInput(argc, argv); + //printMenu(); } diff --git a/my_utility.h b/my_utility.h index 8f9f117..abc96ab 100644 --- a/my_utility.h +++ b/my_utility.h @@ -1,51 +1,89 @@ -#ifndef MY_UTILITY_H_ -#define MY_UTILITY_H_ +#ifndef MY_UTILITY_H_ +#define MY_UTILITY_H_ #include +using namespace std; +void printCommands(); // Prints list of available commands to {cout}. + +void printMenu(); // The main loop. + +void checkInput(int argc, char *argv[]) throw(); // Check whether boot-time input arguments are valid. -void printMenu(); -void checkInput(int argc,char *argv[]) throw(); //-------------------------------------------------------------------------------------------------------------------------------------------------- +void printCommands() { + // Prints list of available commands to {cout}. + // Input - N/A + // Print - List of available commands. + // Output - N/A + cout << "COMMANDS: " << endl + << "‘load’ " << endl // Loads a file into memory. + << ",'inbound'" << endl // Sets a new inbound connection to . + << ",’outbound’" << endl // Sets a new outbound connection to . + << ",’balance’,DD/MM hh:mm" << endl // Sets a new weight to (input in "DD/MM hh:mm" format). + << "‘print’" << endl // Prints current status. + << "‘exit’" << endl; // Exits the program. +} + +void printMenu() { + // The main loop. + // Input - N/A + // Print - See {printCommands} comments. + // Output - N/A + // TODO: Execute the commands called. + string input; + string currentWord; + /* string secondWord; */ //Jonathan + + while (1) { + printCommands(); + + getline(cin, currentWord/*, ','*/); //Jonathan + /* getline(cin, secondWord); */ //Jonathan + + + if (currentWord == "load") { + cout << "load" << endl; + getline(cin, currentWord); //Jonathan + } + else if (currentWord == "outbound") { + cout << "outbound" << endl; + getline(cin, currentWord); //Jonathan + } + else if (currentWord == "inbound") { + cout << "inbound" << endl; + getline(cin, currentWord); //Jonathan + } + else if (currentWord == "balance") { + cout << "balance" << endl; + getline(cin, currentWord); //Jonathan + } + else if (currentWord == "print") { + cout << "print" << endl; + } + else if (currentWord == "exit") { + cout << "exit" << endl; + exit(0); //Jonathan + } else { + printCommands(); //Jonathan + } + + + } -void checkInput(int argc,char *argv[]) throw(){} - -void printMenu() -{ - - - std::string input; - std::string firstWord; - std::string secondWord; - - while(1){ - std::cout<<"load, \n"; - std::cout<<",outbound \n"; - std::cout<<",inbound \n"; - std::cout<<",balance \n"; - std::cout<<"print, \n"; - std::cout<<"exit, \n"; - - std::getline(std::cin,firstWord,','); - std::getline(std::cin,secondWord); - - - if(firstWord == "load"){ std::cout<<"load\n";} - else if(secondWord == "outbound"){std::cout<<"outbound\n";} - else if(secondWord == "inbound"){std::cout<<"inbound\n";} - else if(secondWord == "balance"){std::cout<<"balance\n";} - else if(firstWord == "print"){std::cout<<"print\n";} - else if(firstWord == "exit"){exit(-1);} - else{std::cout<<"USAGE: ‘load’ *or* \n ,'inbound' *or* \n ,’outbound’ *or* \n ,’balance’,dd/mm HH:mm *or* \n ‘print’ *or* \n‘exit’ *to terminate*";} - - - - - - } - } -#endif \ No newline at end of file + +void checkInput(int argc, char *argv[]) throw() { + // Checks whether the boot-time input arguments are valid. + // Input - + // int argc = Number of arguments given (>= 1) + // char *argv[] = Array of string-like input arguments. (argv[0] = executable name. + // Print - N/A + // Output - N/A (terminates program if arguments are invalid.) + // TODO: What arguments should be supported? +} + +#endif \ No newline at end of file From 3cdd712c40a3a8dbe6ae8d8505565737126710c8 Mon Sep 17 00:00:00 2001 From: djohoe28 Date: Fri, 28 May 2021 15:48:40 +0300 Subject: [PATCH 2/2] Word-Splitting Added word-splitting to menu. (getNextWord) Removed "//Jonathan" tags. --- Port.cpp | 32 ++++++++++++------------ Route.cpp | 8 +++--- Route.h | 2 +- main.cpp | 13 +++++++++- my_utility.h | 69 +++++++++++++++++++++++++++++++--------------------- 5 files changed, 74 insertions(+), 50 deletions(-) diff --git a/Port.cpp b/Port.cpp index 7427fbd..b10c182 100644 --- a/Port.cpp +++ b/Port.cpp @@ -5,8 +5,8 @@ using namespace std; Port::Port() { // Default Constructor // Input - N/A - // Print - N/A - // Output - Constructed Port {this} + // Prints - N/A + // Returns - Constructed Port {this} this->name = " "; this->departure_time = "00/00/00"; this->arrival_time = "00/00/00"; @@ -18,8 +18,8 @@ Port::Port(const string &name, const string &arrival_time, const string &departu // string &name = The name of the Port // string &arrival_time = Arrival time at Port, in "DD/MM hh:mm" format. // string &departure_time = Departure time from Port, in "DD/MM hh:mm" format. - // Print - N/A - // Output - Constructed Port {this} + // Prints - N/A + // Returns - Constructed Port {this} this->name = name; this->arrival_time = arrival_time; this->departure_time = departure_time; @@ -28,8 +28,8 @@ Port::Port(const string &name, const string &arrival_time, const string &departu Port::Port(const Port &source) { // Copy Constructor // Input - const Port &source = Reference of Port to copy. - // Print - N/A - // Output - Constructed Port {this} + // Prints - N/A + // Returns - Constructed Port {this} this->name = source.name; this->arrival_time = source.arrival_time; this->departure_time = source.departure_time; @@ -40,8 +40,8 @@ Port::Port(const Port &source) { Port &Port::operator=(const Port &source) { // Assignment Operator // Input - const Port &source = Reference of Port to copy. - // Print - N/A - // Output - Newly copied Port& {this} + // Prints - N/A + // Returns - Newly copied Port& {this} // TODO: Same function as Copy Constructor. Could it be used instead? if (this != &source) { this->name = source.name; @@ -55,24 +55,24 @@ Port &Port::operator=(const Port &source) { Port &Port::operator=(const Port &&source) { // Move Constructor // Input - const Port &&source = Reference of Port to move. - // Print - N/A - // Output - Newly moved Port& {this} + // Prints - N/A + // Returns - Newly moved Port& {this} // TODO: Apply move semantics. } bool Port::operator==(const Port &source) { // Equals Operator // Input - const Port &source = Port to compare this to. - // Print - bool = Are they equal? (Do they have the same name?) - // Output - N/A + // Prints - bool = Are they equal? (Do they have the same name?) + // Returns - N/A return (this->name == source.name); } bool Port::operator!=(const Port &source) { // Unequals Operator // Input - const Port &source = Port to compare this to. - // Print - bool = Are they unequal? (Do they have different names?) - // Output - N/A + // Prints - bool = Are they unequal? (Do they have different names?) + // Returns - N/A return !(this->name == source.name); } @@ -80,7 +80,7 @@ bool Port::operator!=(const Port &source) { Port::~Port() { // Destructor // Input - N/A - // Print - N/A - // Output - N/A + // Prints - N/A + // Returns - N/A // TODO: Is vector correctly removed from memory using smart pointers? } \ No newline at end of file diff --git a/Route.cpp b/Route.cpp index 0bc189a..f1dc869 100644 --- a/Route.cpp +++ b/Route.cpp @@ -8,16 +8,16 @@ Route::Route(const Port &src, const Port &des, int weight) { // const Port &src = Reference of source Port to set. // const Port &des = Reference of destination Port to set. // int weight = Weight of route (avg. # of minutes to cross) - // Print - N/A - // Output - Constructed Route + // Prints - N/A + // Returns - Constructed Route this->src = src; this->des = des; this->weight = weight; } void Route::printRoute() { - // Print function + // Prints function // Input - N/A - // Print - Route data ("{src.name} ----> {des.name} {weight}" format.) + // Prints - Route data ("{src.name} ----> {des.name} {weight}" format.) std::cout << this->src.get_name() << "------->" << this->des.get_name() << " " << this->get_weight() << endl; } \ No newline at end of file diff --git a/Route.h b/Route.h index c57aa73..b515ce2 100644 --- a/Route.h +++ b/Route.h @@ -13,6 +13,6 @@ class Route { int setWeight(int newWeight); // {weight} setter. - void printRoute(); // Print function. + void printRoute(); // Prints function. }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index 008e7fb..71fbfbd 100644 --- a/main.cpp +++ b/main.cpp @@ -8,7 +8,18 @@ // |--> int main(int argc, char *argv[]) { - checkInput(argc, argv); + //checkInput(argc, argv); + cout << "Enter Word #1" << endl; + string nextLine; + string nextWord; + getline(cin, nextLine); + nextWord = getNextWord(nextLine, nextLine); + + cout << "Word #1 = " << nextWord << endl; + //cout << "Enter Word #2" << endl; + cout << "Rest of Line = " << nextLine << endl; + cout << "Press Enter to terminate." << endl; + getline(cin, nextWord); //printMenu(); } diff --git a/my_utility.h b/my_utility.h index abc96ab..70f87e3 100644 --- a/my_utility.h +++ b/my_utility.h @@ -6,6 +6,8 @@ using namespace std; +const string DELIMITER = ","; + void printCommands(); // Prints list of available commands to {cout}. void printMenu(); // The main loop. @@ -17,8 +19,8 @@ void checkInput(int argc, char *argv[]) throw(); // Check whether boot-time i void printCommands() { // Prints list of available commands to {cout}. // Input - N/A - // Print - List of available commands. - // Output - N/A + // Prints - List of available commands. + // Returns - N/A cout << "COMMANDS: " << endl << "‘load’ " << endl // Loads a file into memory. << ",'inbound'" << endl // Sets a new inbound connection to . @@ -28,47 +30,58 @@ void printCommands() { << "‘exit’" << endl; // Exits the program. } +string getNextWord(string &input, string &output) { + // Returns the next word from {input}, until a delimiter/end of line is found, then cut it from {input} as {output}. + // Input - + // string& input = String (line) to read. + // string& output = {input} substring, starting after the first {delimiter}. + // string& delimiter = String to use as delimiter (separator). + // Prints - N/A + // Returns - The next word (excluding delimiter). + int index = input.find(DELIMITER); + if (index < 0) { // No delimiter found + index = input.length(); //Delimiter is end of input + } + string word = input.substr(0, index); + output = input.substr(index + 1); //TODO: nullptr? + return word; +} + void printMenu() { // The main loop. // Input - N/A - // Print - See {printCommands} comments. - // Output - N/A + // Prints - See {printCommands} comments. + // Returns - N/A // TODO: Execute the commands called. string input; - string currentWord; - /* string secondWord; */ //Jonathan + string nextLine; + string nextWord; while (1) { printCommands(); - getline(cin, currentWord/*, ','*/); //Jonathan - /* getline(cin, secondWord); */ //Jonathan - + getline(cin, nextLine); + nextWord = getNextWord(nextLine, nextLine); - if (currentWord == "load") { + if (nextWord == "load") { cout << "load" << endl; - getline(cin, currentWord); //Jonathan - } - else if (currentWord == "outbound") { + nextWord = getNextWord(nextLine, nextLine); + } else if (nextWord == "outbound") { cout << "outbound" << endl; - getline(cin, currentWord); //Jonathan - } - else if (currentWord == "inbound") { + nextWord = getNextWord(nextLine, nextLine); + } else if (nextWord == "inbound") { cout << "inbound" << endl; - getline(cin, currentWord); //Jonathan - } - else if (currentWord == "balance") { + nextWord = getNextWord(nextLine, nextLine); + } else if (nextWord == "balance") { cout << "balance" << endl; - getline(cin, currentWord); //Jonathan - } - else if (currentWord == "print") { + nextWord = getNextWord(nextLine, nextLine); + } else if (nextWord == "print") { cout << "print" << endl; - } - else if (currentWord == "exit") { + } else if (nextWord == "exit") { cout << "exit" << endl; - exit(0); //Jonathan + exit(0); } else { - printCommands(); //Jonathan + printCommands(); } @@ -81,8 +94,8 @@ void checkInput(int argc, char *argv[]) throw() { // Input - // int argc = Number of arguments given (>= 1) // char *argv[] = Array of string-like input arguments. (argv[0] = executable name. - // Print - N/A - // Output - N/A (terminates program if arguments are invalid.) + // Prints - N/A + // Returns - N/A (terminates program if arguments are invalid.) // TODO: What arguments should be supported? }