$29
Readings
A Computer Science Tapestry Section 7.4
A Computer Science Tapestry Sections 8.1 - 8.3
A Computer Science Tapestry Sections 9.1 - 9.3
A Computer Science Tapestry Sections 11.1 and 11.5
Starter Code
Please download the starter code from above.
Your Task
Do all three of the programming exercises described on the following pages. Exercise 2 uses a modified form of the solution to Exercise 1.
Exercise 1: Practice with Files and String Streams
Complete the program command.cpp so that the program repeatedly recognizes and processes the commands from the following list.
update word number list names list quantities batch file-name quit
Each legal command must appear on a single line, and have the correct number and type of arguments.
The update command must be followed by a word—a sequence of nonblanks—and an integer, with no other nonblank characters on the line.
The list command must be followed by either the word names or the word quantities.
The batch command must be followed by the name of an existing file.
The quit command must appear by itself on the line.
You are only required to check the validity of input commands. For an illegal command, merely print an informative error message to cerr. For one of the commands update or list, merely echo the command and its arguments. (You don't need to maintain any actual inventory—you do that in exercise 2.) For the quit command, terminate the program using the one-argument exit function declared in <cstdlib. For the batch command, do the following: read, recognize, and process commands from the specified file, then (assuming that a quit command has not been read) resume reading commands from cin. Assume that the file does not contain any batch commands.
Miscellaneous Information
You should fill in the blanks in the InterpretCommands function given in the command.cpp, according to the accompanying comments, and supply definitions for the other Interpret... functions. Don't change any of the existing code. To analyze each input line, read it using a line-getting function described on the previous page, convert it to an input string stream, and then extract its contents using .
The easiest way to get information about the success or failure of an input operation—the method we want you to use for this assignment—is merely to test the result of the input extraction. For example,
string s; int k; if (cin s k) { // string and integer values were successfully read } else { // string and integer values were not successfully read }
Passing stream arguments by value rather than by reference often leads to incomprehensible error messages; try to remember not to pass streams by value.
An input string stream provides a way to use the input extraction operator ("<<") and associated type checking and error handling with strings. An input string stream is invaluable for processing command-line input, and output string streams have interesting uses as well. Here's an annotated function that reads a line from cin and returns true if it contains exactly two integers.
bool ContainsExactly2Ints() { string line; getline(cin, line); // read the line istringstream in(line.c_str()); // initialize a string stream to contain // the same things as line int val1, val2; string temp; if (in val1 val2) { return !(in temp); // we have at least two ints; // make sure that's all we have } else { return false; } }
Both the istringstream and the ifstream constructors require C-style strings as arguments rather than string objects. The string::c_strmember function used in the example above returns the C-style string corresponding to a string object.
The name of the string stream library has changed over the years. The current library name is sstream:
#include <sstream
Testing
Your test cases should include at least one legal instance of each command, provided both in cin and in the batch file. You should also test illegal commands:
a blank line;
a line with a correct command word but too few arguments;
a line with a correct command word but too many arguments;
a line with a correct command word and the correct number of arguments, but with an argument of the wrong type (word instead of number).
Checklist
Correct implementations of the Interpret... functions, with no changes to existing code other than filling in blanks.
Sufficient testing, with output sufficient to verify test correctness:
tests with all legal commands, both in and out of a batch file (except the batch command);
tests of illegal commands as specified (blank line, correct command word but too many and too few arguments, correct command word and number of arguments with an argument of the wrong type);
tests sufficient to exercise all statements in the program.
Listings of program text, tests, and test results.
Adherence to CS 9F style standards:
appropriate use of indenting and white space;
avoidance of "forbidden C++";
variable and function names that reflect their use;
informative comments at the head of each function;
no routine more than twenty-four lines of code.
Clean case analysis and simple loop structuring.
Exercise 2: Practice with Vectors of Structs
Design an Inventory class that keeps track of an inventory of donated items. Each entry in the inventory has a name and a quantity. The Inventoryclass has four public member functions, as declared in the file inventory.h:
a constructor, which creates an empty inventory.
void
Update(string item, int amount)
adds amount to the quantity of the named item in the inventory, or inserts the item into the inventory with the given quantity if it's not already in stock. Quantities in the inventory are allowed to be negative.
void
ListByName()
prints inventory entries—name plus quantity—sorted alphabetically by name.
void
ListByQuantity()
prints inventory entries—name plus quantity—sorted in increasing order by quantity.
Thus the inventory is maintained only in memory, and has no external file counterpart.
Miscellaneous Requirements
Maintain the inventory as a vector of structs. Your inventory class should be able to handle any number of items. If an update would cause the inventory to overflow, it should be expanded to make room for the new item.
Your program must support all the commands described in vector exercise 1.
Test your code by combining it with a modified version of the program from vector exercise 1. Your tests should collectively do the following:
update existing entries;
update (i.e. insert) new entries, with at least one test that causes the inventory to expand;
sort entries originally in reverse order;
sort entries already in order;
sort a sequence of entries with a large value between two smaller values;
sort a sequence of entries with a small value between two larger values;
process a batch command correctly.
Sorting routines are described in A Computer Science Tapestry Sections 11.1 and 11.5.
Checklist
Correctly working code.
Sufficient testing, with output sufficient to verify test correctness:
tests in all specified categories;
tests sufficient to exercise all statements in the program.
Listings of program text, tests, and test results.
Adherence to CS 9F style standards:
appropriate use of indenting and white space;
avoidance of "forbidden C++";
variable and function names that reflect their use;
informative comments at the head of each function;
no routine more than twenty-four lines of code.
Clean case analysis and simple loop structuring.
Exercise 3: Practice with Vectors of Vectors
In a game like chess or checkers, the game board is normally placed between the players, and they sit on opposite sides. Thus, the two players see the board from different perspectives. Opposite views of a 3-by-3 tic-tac-toe board, for instance, might be as follows:
xox oxo ox xo oxo xox
Implement and test a class representing a 19-by-19 game board. Your representation should be a vector of vectors of characters, all initially set to a period ("."). Your class should include three public member functions:
a constructor, which initializes all the cells to a period.
void
SetCell(int player, int row, int col, char c) stores the given character argument at board position [row][col], computed from the given player's perspective. For example, on the 3-by-3 board that, viewed from player 0's perspective, contains
xox ox. oxo
changing board cell [0][2] to 'M' from player 0's perspective would result in the board
xoM ox. oxo
while changing board cell [0][2] to 'M' from player 1's perspective would result in the board
xox ox. Mxo
void
Print(int player) prints the board from the given player's perspective.
The players are numbered 0 and 1, and the rows and columns of the board are numbered from 0 to 18. You need not error-check the arguments toSetCell and Print.
Your test cases should include calls to SetCell and Print for each of the two players.
Checklist
Correctly working code.
Sufficient testing, with output sufficient to verify test correctness:
tests of SetCell and Print for each player.
Adherence to CS 9F style standards:
appropriate use of indenting and white space;
avoidance of "forbidden C++";
variable and function names that reflect their use;
informative comments at the head of each function;
no routine more than twenty-four lines of code.
Clean case analysis and simple loop structuring.