Question: For this assignment, suppose that a fence is recording entry and exit into the park via a string as the gate swings open and folks
For this assignment, suppose that a fence is recording entry and exit into the park via a string as the gate swings open and folks walk in or out with their pet. For example, C++
DP+dp+CP+cp
would indicate that an adult dog and an adult entered the park, then a puppy and child entered the park, then an adult cat and an adult entered the park and finally a kitten and a child entered the park. From this information, note that the character D is being used to represent an adult dog, the character P is being used to represent an adult person, the character d is being used to represent a puppy, the character p is being used to represent a child, the character C is being used to represent an adult cat and the character c is being used to represent a kitten. After processing this string, the park will have a dog and a puppy, a cat and a kitten and two adults and two children left inside. For example,
DP+cp-DP-cp
would indicate that an adult dog and an adult entered the park, then a kitten and a child entered the park, then an adult dog and an adult left the park and then a kitten and a child left the park. After processing this string, the park will not have any dogs, cats or people left inside. For example,
DdPp+ccPP
would indicate that an adult dog and puppy entered the park with an adult and a child followed by two kittens who entered with two adults. After processing this string, the park will have a puppy and an adult dog plus two kittens and three adults and a child left inside.
Precisely, to be a valid animal park string,
- pet(s) must be followed by owner(s) - pet(s) and owner(s) must leave the park together, but only once they have entered - cats and dogs cannot be mixed on a single entry into the park - cats and dogs cannot be mixed on a single exit from the park - the only characters allowed in string are: + - D d C c P p No spaces or any other character besides the 6 characters you see listed to the left. - the string cannot start with +
All of the following are examples of valid animal park strings:
- CP+dp-CP-dp (no dogs, no cats and no people left in the park after the string is processed)
- dp+cp (one puppy, one kitten and two children left in the park after the string is processed)
- CP+dp-CP (one puppy and one child left in the park after the string is processed)
- ccCP+ddDP (two kittens, one cat, two puppies, one dog and two adults left in the park after the string is processed)
- dP+dp-ddPp (animals entering separately can leave together, as long as they leave after having entered and dogs and cats are not entering or leaving together)
- DP+CP+cp+dp-Dp-Cp-dP-cP (any present group of pets and people can leave, once they have entered the park, whether they arrived with that pet or not)
- CCP-CP (pets and people in any combination may be left befind after the string is fully processed)
All of the following are examples of invalid animal park strings:
- asdf1ABC000:2-55 (no characters allowed besides c, C, d, D, p, P, + or -)
- +dp+cp (no leading + allowed)
- d p + c p (no spaces allowed)
- -dp+dp (no leading - allowed and a pet cannot leave before it has entered)
- dp-CP (a pet cannot leave before it has entered)
- cCcDP (dogs and cats cannot be mixed on a single entry into the park)
- cP+dP-cdPP (dogs and cats cannot be mixed on a single exit from the park)
- cpP-P (you cannot leave without having a pet with you)
- cp+P-cpP (you cannot enter without having a pet with you)
Your task
For this project, you will implement the following four functions, using the exact function names, parameter types, and return types shown in this specification. (The parameter names may be different if you wish).
bool isValidAnimalParkString(string animalparkString)
This function returns true if its parameter is a well-formed animal park string as described above, or false otherwise.
int dogsLeft(string animalparkString)
If the parameter is a well-formed animal park string, this function should return the number of dogs (both puppies and adult dogs) left after the string is fully processed. If the parameter is not a valid animal park string, return -1.
int catsLeft(string animalparkString)
If the parameter is a well-formed animal park string, this function should return the number of cats (both kittens and adult cats) left after the string is fully processed. If the parameter is not a valid animal park string, return -1.
int peopleLeft(string animalparkString)
If the parameter is a well-formed animal park string, this function should return the number of people (both children and adults) left after the string is fully processed. If the parameter is not a valid animal park string, return -1.
These are the only four functions you are required to write. Your solution may use functions in addition to these four if you wish. While we won't test those additional functions separately, using them may help you structure your program more readably. Of course, to test them, you'll want to write a main routine that calls your functions. During the course of developing your solution, you might change that main routine many times. As long as your main routine compiles correctly when you turn in your solution, it doesn't matter what it does, since we will rename it to something harmless and never call it (because we will supply our own main routine to thoroughly test your functions).
Programming Guidelines
The functions you write must not use any global variables whose values may be changed during execution (so global constants are allowed).
When you turn in your solution, neither of the four required functions, nor any functions they call, may read any input from cin or write any output to cout. (Of course, during development, you may have them write whatever you like to help you debug.) If you want to print things out for debugging purposes, write to cerr instead of cout. cerr is the standard error destination; items written to it by default go to the screen. When we test your program, we will cause everything written to cerr to be discarded instead we will never see that output, so you may leave those debugging output statements in your program if you wish.
The correctness of your program must not depend on undefined program behavior. For example, you can assume nothing about c 's value at the point indicated, nor even whether or not the program crashes:
int main() { string s = "Hello"; char c = s[5]; // c's value is undefined Be sure that your program builds successfully, and try to ensure that your functions do something reasonable for at least a few test cases. That way, you can get some partial credit for a solution that does not meet the entire specification.
There are a number of ways you might write your main routine to test your functions. One way is to interactively accept test strings:
int main() { string s; cout.setf( ios::boolalpha ); // prints bool values as "true" or "false"
for(;;) { cout << "Enter a possible animal park string: "; getline(cin, s); if (s == "quit") break; cout << "isValidAnimalParkString returns "; cout << isValidAnimalParkString(s) << endl; cout << "dogsLeft(s) returns "; cout << dogsLeft(s) << endl; cout << "catsLeft(s) returns "; cout << catsLeft(s) << endl; cout << "peopleLeft(s) returns "; cout << peopleLeft(s) << endl; }
return 0; }
While this is flexible, you run the risk of not being able to reproduce all your test cases if you make a change to your code and want to test that you didn't break anything that used to work.
Another way is to hard-code various tests and report which ones the program passes:
int main() { if (!isValidAnimalParkString("")) cout << "Passed test 1: !isValidAnimalParkString(\"\")" << endl; if (!isValidAnimalParkString(" ")) cout << "Passed test 2: !isValidAnimalParkString(\" \")" << endl; This can get rather tedious. Fortunately, the library has a facility to make this easier: assert . If you #include the header
assert(some boolean expression);
During execution, if the expression is true, nothing happens and execution continues normally; if it is false, a diagnostic message is written to cerr telling you the text and location of the failed assertion, and the program is terminated. As an example, here's a very incomplete set of tests:
#include#include #include using namespace std; int main() { assert( ! isValidAnimalParkString("")); assert( ! isValidAnimalParkString(" ")); assert( dogsLeft( " " ) == -1 ); assert( peopleLeft( " " ) == -1 );
assert( isValidAnimalParkString( "CP+dp" ) == true ); assert( dogsLeft( "dp+DP" ) == 2 ); assert( peopleLeft( "dp+DP" ) == 2 ); assert( catsLeft( "dp+DP" ) == 0 ); assert( catsLeft( "CP+cp-CP" ) == 1 ); cerr << "All tests succeeded" << endl; return 0; }
The reason for writing one line of output at the end is to ensure that you can distinguish the situation of all tests succeeding from the case where one function you're testing silently crashes the program.
-
- Create a function that reads a single animal character c, C, d or D. HINT: such a function was supplied in class
- Use this function to read the opening pet from the animal park string. If it fails to find an animal character, the string is not a valid animal park string.
- Create a function that reads a single person character p or P.
- Use this second function to read a person character after reading the opening pet from the animal park string. If it fails to find an person character, the string is not a valid animal park string.
- Like the previous simplification, but make sure a + or - follows the person character.
- Build a loop in your functions so you can read more than one pet or person. Adjust your function arguments your code returns the number of pets or people it has read.
- Build a loop in your code so that you can repetitively read + or - followed by an animal character followed by a person character.
- As your code loops, track the number of dogs, puppies, cats, kitten, adults and children. Make sure that none of these numbers go negative.
- Make sure once your code stops looping that the string is fully read all the way to the end of the string.
- Be sure not to go off the edge of your animal park string.
- Now that isValidAnimalParkString is correctly implemented, proceed in a similar fashion to implement dogsLeft, starting with an extreme simplification, then working your way through successive removals of simplifying assumptions. Proceed in a similar fashion to implement catsLeft and the other function. And yes, these later functions can call the isValidAnimalParkString as part of what they do. This is intended to show you the power of reuse, a key benefit to writing modularized code with functions.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
