Introduction

In response to the emergence of the novel coronavirus, testing has become widely available with results being shared in the community to help it track and monitor the extent of the problem.

For this assignment, suppose that a sensor is recording virus test results in batches in a results string. For example,

R2+1-1

would indicate that a single batch of two tests are being reported with one test being reported as positive for the virus and one test being reported as negative for the virus. From this information, note that the character R is being used to start a batch of test results, the character + is being used to identify positive results and the character - is being used to report negative results. More than one batch of results can be reported in a single results string. For example,

R2-1+1R5+3-2

would indicate two batches of results, one with two test results and one with five test results with a total of four positives tests and three negative tests being reported.

Precisely, to be a valid test results string,

  • a batch of results must begin with the character R
  • a batch of results must report both a positive and negative number of test results with + and - in either order
  • no leading zeros are allowed in any numeric value being reported
  • the total number of tests in a batch must equal the number of positive and negative test results
  • a single result string may include multiple batches of results

All of the following are examples of valid result strings:

  • R1+0-1R1-0+1 (two batches of results, two total tests being reported, one being positive, one being negative)
  • R5-2+3 (one batch of results, five total tests being reported, two being negative, three being positive)

All of the following are examples of invalid result strings:

  • r1+0-1 (batch must be reported with R)
  • R1+-1 (a number of positive tests is required)
  • R1+1- (a number of negative tests is required)
  • R1+0-1 asdfR (extra characters not allowed)
  • R5+00003-0002 (leading zeros not allowed)
  • R5+0-0 (positive and negative results must equal the total number of tests being reported)

Your task

For this project, you will implement the following five 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 isValidResultString(string results)

This function returns true if its parameter is a well-formed test result string as described above, or false otherwise.

int positiveTests(string results)

If the parameter is a well-formed test result string, this function should return the total number of positive tests from all the batches being reported in the string. If the parameter is not valid, return -1 .

int negativeTests(string results)

If the parameter is a well-formed test result string, this function should return the total number of negative tests from all the batches being reported in the string If the parameter is not valid, return -1.

int totalTests(string results)

If the parameter is a well-formed test result string, this function should return the total number of tests being reported from all the batches in the string. If the parameter is not a valid, return -1.

int batches(string results)

If the parameter is a well-formed test result string, this function should return the total number of batches being reported in the string. If the parameter is not a valid, return -1.

These are the only five functions you are required to write. Your solution may use functions in addition to these five 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).

Clearly, I am expecting the last four functions to invoke the first function as they complete their assigned task. This code reuse is one of the major benefits of having functions. So please do that.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.