实现Position和Matrix两个类,以及对应的overload方法,通过测试即可。
Note
You must use the C++ I/O streaming and memory management facilities on this assignment. Moreover, the only standard headers you may #include are iostream, fstream, sstream, iomanip, string, and utility. Marmoset will be programmed to reject submissions that violate these restrictions.
Each question on this assignment asks you to write a C++ program, and the programs you write on this assignment each span multiple files. Moreover, each question asks you to submit a Makefile for building your program. For these reasons, we strongly recommend that you develop your solution for each question in a separate directory. Just remember that, for each question, you should be in that directory when you create your zip file, so that your zip file does not contain any extra directory structure.
Questions on this assignment will be hand-marked to ensure that you are writing high quality code, and to ensure that your solutions employ the programming techniques mandated by each question.
Position
In this exercise, you will write a C++ class (implemented as a struct) to control a simple robotic drone exploring some terrain. Your drone starts at coordinates (0,0), facing north. Use the following structure definition for coordinates:
1 | struct Position { |
The east-west direction is the first component of a position, and the north-south direction is the second. Your Drone class must be properly initialized via a constructor, and must provide the following methods:
Method | Description |
---|---|
void forward() | Move the drone one unit forward. |
void backward() | Moves the drone one unit backward. |
void left() | Turns the drone 90 degrees to the left, while remaining in the same location. |
void right() | Turns the drone 90 degrees to the right, while remaining in the same location. |
Position current() | Returns the current position of the drone. |
int totalDistance() | Returns the Manhattan distance between the current position and the origin where the Manhattan distance defined as the absolute north-south displacement plus the absolute east-west displacement. |
int manhattanDistance() | Returns the total units of distance travelled by the drone. |
bool repeated() | Returns true if the current position is one that the drone has previously visited. |
For simplicity, you may assume that the drone will never visit more than 50 positions before running out of fuel or otherwise breaking down.
Implement the specified operations for the Drone. (Some starter code has been provided for you in the file drone.h, along with a sample executable.) You may not change the contents of drone.h other than by adding your instance variables and comments i.e. the interface must stay exactly the same.
The test harness a3q1.cc is provided with which you may interact with your drone for testing purposes. The test harness is not robust and you are not to devise tests for it, just for the Drone class. Do not change this file.
Matrix
Consider the following class definition for a two-dimensional integer Matrix class:
1 | class Matrix { |
Implement the specified constructors, destructor and assignment operators for the Matrix. (Some starter code has been provided for you in the file Matrix.h, along with a sample executable.) Further, you are to overload the input, output, addition, and multiplication operators as follows:
1 | // Creates an empty matrix whose dimensions are 0x0 and the 2-D pointer is |
which produces the following output.
$ ./matrix < in.txtm0 = []m1 + m2 = 2 0 00 0 0m2 * m3 = 1 00 0$ cat in.txt3 21 1 12 2 2
Some implementation notes follow:
In order to work more easily with the move operations (move constructor, move assignment operator), we have defined an empty matrix as one where the row and column dimensions are both set to 0 and the two-dimensional array pointer is set to nullptr.
Thus, a matrix whose information is stolen becomes an empty matrix. The matrix stealing the information must be able to grow or shrink as necessary and not throw an error.Use the language features to simplify how you handle reading in the matrix dimensions and values. It should be possible for any white space to be used to separate the numbers (spaces, tabs, newlines, etc.) and have the input operator work properly. You shouldnt need to do anything complex, so dont over-think it.
When outputting the values of the matrix, an empty matrix produces the string []. A non-empty matrix outputs each rows values on a separate line, and sets the width of the value to 4 (see the setw operator in the iomanip library). You are not required to handle values whose width (including the sign) exceed this size.
The declaration of the Matrix type can be found in the provided Matrix.h file. For your submission you must add all requisite declarations to Matrix.h and all routine and member definitions to Matrix.cc. The public interface to Matrix may not be changed.
The provided test harness, a3q2.cc, can be compiled with your solution to test (and then debug) your code. The test harness is not robust and you are not to devise tests for it, just for the Matrix class. Do not change this file. The test harness allows you to have up to 10 matrices defined at one time, identified as m0 to m9. If a matrix has not been initialized, it consists of a nullptr. Most of the test harness commands cannot be performed upon an uninitialized matrix, and the harness enforces this. Additionally, the user prompts are printed to standard error so that they will not interfere with the output produced, and thus make it easier to write your test files. The test harness also provides some simple error checking, such as ensuring that get/set are within the bounds of the matrix,and that matrix dimensions are compatible for addition and multiplication. If the commands do not meets its criteria, they are silently ignored. Thus, you are not required to test these cases and only need to test valid input.
Your Makefile must create an executable named matrix. Note that the executable name is case-sensitive.