Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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 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)

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)

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 , you can call assert in the following manner:

 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems For Integration Of Media And User Environments 98

Authors: Yahiko Kambayashi, Akifumi Makinouchi, Shunsuke Uemura, Katsumi Tanaka, Yoshifumi Masunaga

1st Edition

9810234368, 978-9810234362

More Books

Students also viewed these Databases questions