Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need some help with this homework project :( Introduction You work for a manufacturer of N95 respirators. Orders for cases of these masks come in

Need some help with this homework project :(

Introduction

You work for a manufacturer of N95 respirators. Orders for cases of these masks come in from various states and your company is filling them as quickly as it can. The orders and their status of being filled or unfilled are encoded in a string that you will need to process. We will describe the format of the string and what you must do to process it.

First, we define some terms.Astate codeis one of the following 53 two-letter codes for U.S. states and some territories, with each letter being in either upper or lower case (so CA Ca cA and ca are all state codes): AL AK AZ AR CA CO CT DE DC FL GA GU HI ID IL IN IA KS KY LA ME MD MA MI MN MP MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA PR RI SC SD TN TX UT VT VA WA WV WI WYAdigitis one of the ten digit characters0through9.Anorder statusis a+, indicating that an order has been filled, or a-, indicating that it is has not been filled.Astate orderis a state code immediately followed by a sequence of one or more digits immediately followed by an order status. For example, CA132+ and ms6- are state orders; HI24+ is not, because of the space character between the I and the 2. (The intent is that the digit(s) represent the number of cases of masks in this order for the state.)Anorder stringis a sequence of zero or more state orders (with no character that is not part of a state order in that string). For example, TX38-CA132+Ms6-nY290-UT006+ is an order string consisting of five state orders; TX38-CA132+ is not an order string, because the space character between the - and the C is not part of any state order. The empty string is an order string consisting of zero state orders.These are the semantics of an order string: An order string represents a collection of orders, one for each state order in that string. Each state order represents one order of masks for that state; the digits in the state order represent the number of cases of masks for that order. For example, TX38- represents an order from the state TX for 38 cases of masks, and that order has not yet been filled.Your task

Your assignment is essentially to take an order string and an order status, and compute the total number of cases of masks for the orders in that order string that have that status. For example, for the order string TX38-CA132+Ms6-nY290-UT006+MS8+CA15+ the total for the status + is 161 (132 for CA plus 6 for UT plus 8 for MA plus another 15 for CA); for the status - it's 334 (38 for TX plus 6 for MS plus 290 for NY).

For this project, you will implement the following two functions, using the exact function names, parameter types, and return types shown in this specification. (The parameternamesmay be different if you wish.)

bool hasValidSyntax(string orders)

This function returns true if its parameter is an order string (i.e., it meets the definition above), or false otherwise.

int countCases(string orders, char status, int& caseCount)

If the parameterordersis not an order string (i.e., it does not meet the definition above), this function returns 1. Ifordersis an order string in which at least one state order specifies zero cases of masks (e.g., GA0+), this function returns 2. If the parameterstatusis not a + or -, this function returns 3. (If more than one of these situations occur, return one of those occurring situations' return value, your choice which one.) If any of the preceding situations occur,caseCountmust be left unchanged. If none of those situations occurs, then the function returns 0 after settingcaseCountto the total number of cases of masks for the state orders inordersthat have the status indicated by thestatusparameter.

These are the only two functions you are required to write. (Hint:countCasesmay well callhasValidSyntax.) Your solution may use functions in addition to these two 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 the functions you write, you'll want to create 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

Your functions must not use any global variables whose values may be changed during execution (so globalconstantsare allowed).

When you turn in your solution, neither of the two required functions, nor any functions you write that they call, may read any input fromcinor create any output tocout. (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 tocerrinstead ofcout.cerris the standard error destination; items written to it by default go to the screen. When we test your program, we will cause everything written tocerrto 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. Your program must never access out of range positions in a string. Your program must not, for example, assume anything aboutn's value at the point indicated, or even whether or not the program crashes:

 int main() { string s = "Hello"; int n; // n is uninitialized s.at(5*n/n) = '!'; // undefined behavior! ... 

Be sure that your program builds successfully, and try to ensure that your functions do something reasonable for at least a few test cases under both g++ with Linux and either Visual C++ or clang++. That way, you can get some partial credit for a solution that does not meet the entire specification.

If you wish, you may use thisisValidUppercaseStateCode.txtfunction as part of your solution. (We can't imagine why you would not want to use it, since it does some of the work of valididating a supposed state code.)

You do not need to know anything about arrays to create this program. You may use arrays if you wish, but the most straightforward solutions to this project actually don't use arrays.

There are a number of ways you might create your main routine to test your functions. One way is to interactively accept test strings:

 int main() { for (;;) { cout << "Enter order string: "; string os; getline(cin, os); if (os == "quit") break; cout << "hasValidSyntax returns "; if (hasValidSyntax(os)) cout << "true"; else cout << "false"; cout << endl; } } 

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 (hasValidSyntax("TX38-CA132+")) cout << "Passed test 1: hasValidSyntax(\"TX38-CA132+\")" << endl; if (!hasValidSyntax("MX38-CA132+")) cout << "Passed test 2: !hasValidSyntax(\"MX38-CA132+\")" << endl; int cases; cases = -999; // so we can detect whether countCases sets cases if (countCases("TX38-CA132+Ms6-nY290-UT006+MS8+CA15+", '+', cases) == 0 && cases == 161) cout << "Passed test 3: countCases(\"TX38-CA132+Ms6-nY290-UT006+MS8+CA15+", '+', cases)" << endl; cases = -999; // so we can detect whether countCases leaves cases unchanged if (countCases("TX38-CA132+", '%', cases) == 3 && cases == -999) cout << "Passed test 4: countCases(\"TX38-CA132+\", '%', cases)" << endl; ... 

This can get rather tedious. Fortunately, the library has a facility to make this easier:assert. If you include the header, you can callassertin 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 telling you the text and location of the failed assertion, and the program is terminated. Usingassert, we can write the tests above more easily:

 #include  #include  #include  using namespace std; bool hasValidSyntax(string orders) { ... Your code goes here ... } int countCases(string orders, char status, int& caseCount) { ... Your code goes here ... } int main() { assert(hasValidSyntax("TX38-CA132+")); assert(!hasValidSyntax("MX38-CA132+")); int cases; cases = -999; // so we can detect whether countCases sets cases assert(countCases("TX38-CA132+Ms6-nY290-UT006+MS8+CA15+", '+', cases) == 0 && cases == 161); cases = -999; // so we can detect whether countCases leaves cases unchanged assert(countCases("TX38-CA132+", '%', cases) == 3 && cases == -999); ... cout << "All tests succeeded" << endl; } 

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Have pattern like hair on their skin?

Answered: 1 week ago

Question

What are aquatic animal?

Answered: 1 week ago

Question

Animals that eat plant is called?

Answered: 1 week ago