Question: Help needed creating the code for these public functions to create the following output without modifying the main: SPECIFICATIONS: The Mississauga Institute of Technology (M.I.T.)

 Help needed creating the code for these public functions to create

the following output without modifying the main: SPECIFICATIONS: The Mississauga Institute of

Technology (M.I.T.) would like you to demonstrate your knowledge of C++ to

help them rewrite their robotics software that was originally written in FORTRAN.

They would like you to write the code for a class called

"Command" which will be used to encapsulate a keyword (a string no

longer than 40 characters) and a series of modifiers (an array of

5 strings, each being no longer than 20 characters). // class declaration:

class Command { private: char keyword [41]; char modifiers[5][21]; int wordCount, modNum;

public: int words (const char *sentence); int match(const char *str, const char

*sentence, int wordNum); Command(); Command(const char *data); int modifierCount(); int validCommand (const

Help needed creating the code for these public functions char *phrase); void getKeyword(char *s); void getModifier(char *s, int pos); A "Command"

to create the following output without modifying the main:

has the following publicly accessible characteristics (member functions): int words (const char

SPECIFICATIONS: The Mississauga Institute of Technology (M.I.T.) would like you to demonstrate your knowledge of C++ to help them rewrite their robotics software that was originally written in FORTRAN. They would like you to write the code for a class called "Command" which will be used to encapsulate a keyword (a string no longer than 40 characters) and a series of modifiers (an array of 5 strings, each being no longer than 20 characters). // class declaration: class Command { private: char keyword [41]; char modifiers[5][21]; int wordCount, modNum; public: int words (const char *sentence); int match(const char *str, const char *sentence, int wordNum); Command(); Command(const char *data); int modifierCount(); int validCommand (const char *phrase); void getKeyword(char *s); void getModifier(char *s, int pos); A "Command" has the following publicly accessible characteristics (member functions): int words (const char *sentence) which returns the number of words in the string "sentence". For the purpose of this assignment, a "word" is defined to be a sequence of non-null, non-whitespace characters, separated from other words by whitespace. (Recall that whitespace is any sequence of one or more spaces, tabs and newlines). For example, words ("Welcome to the in \t\t\t robotics department") returns the value 5, and words(" \t \t It ") returns 0. Command(const char *data) This function is passed a constant string "data" which contains one (1) keyword and may contain a series of up to five (5) modifiers, each separated by the ';' semi-colon character. This function initializes the object's keyword and its modifiers to the values passed. If more than 5 modifiers are given, only the first 5 are used. An example of the constant string "data" might be: "turn;left;right;east;west;back" -1 |1_| _2_1 13_1 14_1 15_1 IL-LL-T-I series of 5 modifiers (note the order). keyword Another example of "data" might be: "turn;left" L_1 11_1 I - only 1 modifier keyword NOTE: You may assume that the array "data" will ALWAYS contain a keyword, however it may contain 0, 1, 2, 3, 4, or 5 modifiers. int modifierCount() This function returns the number of modifiers that have been properly initialized in the Command object. int match(const char *str, const char *sentence, int wordNum) which returns a true value if the string "str" matches the part of the string "sentence" starting at word number "wordNum" of "sentence", and a false value otherwise. Special rules for determining a match are as follows: (1) the match is case insensitive. For example, 'A' matches both 'a' and 'A'. (2) if there are any embedded whitespace characters in "str", then those exact whitespace characters must appear in the corresponding positions of "sentence". (One implication of this is that if "str" begins with whitespace, it will not match anything). (3) the end of "str" must not fall in the middle of a word in "sentence", for it to be considered a match. (4) The word number in "wordNum" is a value starting at 1, so that 1 refers to the first word, 2 refers to the second word, and so on. If "wordNum" is less than 0 or greater than the number in "sentence", then no match will be found. turn left", 7) For example, match("left", "Proceed three units forward and returns a true value, while match("left", "Proceed three units forward and returns false. Similarly, turn left", 3) match("*r2-d2*", "Good morning \t *R2 - D2* & C3-00 ", 3) returns true, and match("*r2-d2* & C3-PO", "Good morning \t *R2-D2* & 3 - p ", 3) also returns true. However, match("robot", "Welcome to the \t\t\t robotics department", 4) returns false, since "robot" ends in the middle of the word "robotics", and match("H.A.L. 9000", "Hello H.A.L. 9000", 2) also returns false, but only because there are too many spaces between "H.A.L." and "9000" in the second parameter. Command() This function initializes the object's keyword to "go", and its modifiers to "north", "south", "east", "west", and "start" in this order respectively. int validCommand(const char *phrase) This function will determine if any keyword or keyword-modifier combination is present in the string "phrase". HINT: You can use the match() function written earlier to help complete this function. The function returns the following integer values: return combination: value: 1 Only the keyword is present in "phrase" and no modifiers follow it. NOTE: modifiers may precede the keyword, but are considered meaningless and do not impact the result. (e.g.) keyword: "turn" modifers: "left", "right", "east", "west", "back" Then a phrase of: "do not turn here", would return a value of 1. 2 - 6 The keyword AND a modifier are present in "phrase". The return value here will depend on what position in the array the modifier is located. A value of 2 will be returned when the keyword and the first (1st) modifier are present. A value of 3 is returned when the keyword is followed the second (2nd) modifer, etc. NOTE: Modifiers preceding the keyword, or following the first valid modifier are to be ignored and will not impact the result. (e.g.) A phrase of: "turn left here" | modifier (index=0, value=1) keyword (value=1) returns a value of 2 (1 + 1). (e.g.) A phrase of: "left turn to the west then right then back" I ignored I modifier ignored modifiers 1st valid modifier found (index=3, value=4) keyword (value=1) returns a value of 5 (1 + 4). No keyword is present in the string "phrase". Modifiers by themselves without a keyword are considered meaningless. NOTE: The string "phrase" can be of any size and may contain any number of words. If more than one (1) modifier follows the keyword, only the first modifier encountered is used. NOTE: You are expected to use the match() and words() functions you wrote for assignment #1. void getKeyword(char s[]) Fills "s" with the object's keyword. You may assume that the array "s" will be large enough to hold the keyword. void getModifier(char s[], int pos) Fills "s" with the object's modifier at number "pos" (where a "pos" of O is the first modifier, 1 is the second modifier, and so on). You may assume that the array "s" will be large enough to hold the modifier. If "pos" is not the number of one of the object's modifiers, then s will be filled with an empty string(""). // You must not modify this file! // 1/ // // Your solution may ONLY use functions from the following // included C and C++ library header files. #include using namespace std; #include #include // for character handing functions class Command { private: char keyword [41]; char modifiers[5][21]; int wordCount, modNum; public: int words (const char *sentence); int match(const char *str, const char *sentence, int wordNum); Command(); Command (const char *data); int modifierCount(); int validCommand(const char *phrase); void getKeyword(char *s); void getModifier(char *s, int pos); }; #define SIZE 5 int checkInit(Command &, int , const char *, const char [ ][21], int); void errMessage(int); int main() { char data[SIZE][121] = { "go; north; south; east;west;start;left;right;home", "look", "pick-up;gold;platinum; crystal;magic-sword", "drop; gold;platinum; crystal;magic-sword", "attack; crystal;magic-sword" }; char keywords[SIZE] [41] = { "go", "look", "pick-up", "drop", "attack" }; char modifiers[SIZE][SIZE][21] = {{ "north", "south", "east", "west", "start" }, }, { "gold", "platinum", "crystal", "magic-sword", }, { "gold", "platinum", "crystal", "magic-sword", { "crystal", "magic-sword", }; 11 2 11 1 181 over char phrases [SIZE*3][81] = { Hello Robot! Please GO \t\t to the In other side.", "Hello Robot can you \t do something? "Robot It go over to the Right, then over to the left, then start", "Look over to the In \t \t right", "Robot, can you see what I see?", "Did you see that? LOOK over In there! No not there, LOOK here! ", "Can you pick up the platinum gold and crystal over there?", "Over there, can you Pick-Up the \t GOLD In?", "OK, I will get the crystal gold platinum and pick-UP the Magic-Sword", " drop the crystal "Robot, please drop the in \t\t PLATINUM "Look can you see any gold platinum or crystal ?", Attack with the crystal and the magic sword", "You must look around, move up then attack with the MAGIC-sword", "ATTACK ATTACK ATTACK! First with a crystal, then with a Magic-Sword!" }; int rvalues[SIZE*3] = { 1, 0, 6, 1, 0, 1, 0, 2, 5, 4, 3, 0, 2, 3, 1 }; int modc[SIZE] = { 5, 0, 4, 4, 2 }; int words InPhrase[SIZE*3] = { 9, 6, 13, 5, 7, 12, 11, 8, 12, 3, 5, 10, 8, 11, 11 }; int phrase Index [SIZE+1] = { 2, 6, 99, 2, 7, 11 }; char wordToFind[SIZE+1][21] = { "Robot!", "something", "start", "the", "see?", "LOOK here!" }; int matchRV[SIZE+1] = { 1, 0, 0, 0, 1, 1 }; class Command *robots[SIZE]; // SIZE Command pointers char tData[61]; int i, j, valid=1, rc, count=0, pos=0, k, robotMods; Command tester; // first check the words() member function for(i=0; imodifierCount(); cout validCommand (phrases[pos]); if(rc != rvalues[pos]) { cout " rc " rvalues[pos] keyword IH " " using namespace std; #include #include // for character handing functions class Command { private: char keyword [41]; char modifiers[5][21]; int wordCount, modNum; public: int words (const char *sentence); int match(const char *str, const char *sentence, int wordNum); Command(); Command (const char *data); int modifierCount(); int validCommand(const char *phrase); void getKeyword(char *s); void getModifier(char *s, int pos); }; #define SIZE 5 int checkInit(Command &, int , const char *, const char [ ][21], int); void errMessage(int); int main() { char data[SIZE][121] = { "go; north; south; east;west;start;left;right;home", "look", "pick-up;gold;platinum; crystal;magic-sword", "drop; gold;platinum; crystal;magic-sword", "attack; crystal;magic-sword" }; char keywords[SIZE] [41] = { "go", "look", "pick-up", "drop", "attack" }; char modifiers[SIZE][SIZE][21] = {{ "north", "south", "east", "west", "start" }, }, { "gold", "platinum", "crystal", "magic-sword", }, { "gold", "platinum", "crystal", "magic-sword", { "crystal", "magic-sword", }; 11 2 11 1 181 over char phrases [SIZE*3][81] = { Hello Robot! Please GO \t\t to the In other side.", "Hello Robot can you \t do something? "Robot It go over to the Right, then over to the left, then start", "Look over to the In \t \t right", "Robot, can you see what I see?", "Did you see that? LOOK over In there! No not there, LOOK here! ", "Can you pick up the platinum gold and crystal over there?", "Over there, can you Pick-Up the \t GOLD In?", "OK, I will get the crystal gold platinum and pick-UP the Magic-Sword", " drop the crystal "Robot, please drop the in \t\t PLATINUM "Look can you see any gold platinum or crystal ?", Attack with the crystal and the magic sword", "You must look around, move up then attack with the MAGIC-sword", "ATTACK ATTACK ATTACK! First with a crystal, then with a Magic-Sword!" }; int rvalues[SIZE*3] = { 1, 0, 6, 1, 0, 1, 0, 2, 5, 4, 3, 0, 2, 3, 1 }; int modc[SIZE] = { 5, 0, 4, 4, 2 }; int words InPhrase[SIZE*3] = { 9, 6, 13, 5, 7, 12, 11, 8, 12, 3, 5, 10, 8, 11, 11 }; int phrase Index [SIZE+1] = { 2, 6, 99, 2, 7, 11 }; char wordToFind[SIZE+1][21] = { "Robot!", "something", "start", "the", "see?", "LOOK here!" }; int matchRV[SIZE+1] = { 1, 0, 0, 0, 1, 1 }; class Command *robots[SIZE]; // SIZE Command pointers char tData[61]; int i, j, valid=1, rc, count=0, pos=0, k, robotMods; Command tester; // first check the words() member function for(i=0; imodifierCount(); cout validCommand (phrases[pos]); if(rc != rvalues[pos]) { cout " rc " rvalues[pos] keyword IH " "

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!