C++代写:CS246-Streaming-and-Memory-Management

Requirement

Note: You must use the C++ I/O streaming and memory management facilities on this request. 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.

Note: Each question on this request asks you to write a C++ program, and the programs you write on this request 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.

Note: Questions on this request will be hand-marked to ensure that you are writing highquality code, and to ensure that your solutions employ the programming techniques mandated by each question.

Note: You are not permitted to ask any public questions on Piazza about what the programs that make up the request are supposed to do. A major part of this request involves designing test cases, and questions that ask what the programs should do in one case or another will give away potential test cases to the rest of the class. Instead, we will provide compiled executables, suitable for running on linux.student.cs, that you can use to check intended behaviour. Questions found in violation of this rule will be marked private or deleted; repeat offences could be subject to discipline.

Exercise 1

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
2
3
4
struct Position {
int ew, ns;
Position( int ew = 0, int ns = 0 );
};

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

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.

  • (a) Due on Due Date 1: Design the test suite suiteq1.txt for this program and zip the suite into a3q1a.zip.
  • (b) Due on Due Date 2: Implement this in C++ and place the files Makefile, a3q1.cc, drone.h and drone.cc in the zip file, a3q1b.zip. Your Makefile must create an executable named drone. Note that the executable name is case-sensitive.

Exercise 2

The standard Unix tool make is used to automate the separate compilation process. When you ask make to build a program, it only rebuilds those parts of the program whose source has changed, and any parts of the program that depend on those parts etc. In order to accomplish this, we tell make (via a Makefile) which parts of the program depend on which other parts of the program. Make then uses the Unix “last modified” timestamps of the files to decide when a file is older than a file it depends on, and thus needs to be rebuilt. In this problem, you will simulate the dependency-tracking functionality of make. We provide a test harness (main.cc) that accepts the following commands:

  • target: source indicates that the file called target depends on the file called source
  • touch file indicates that the file called file has been updated. Your program will respond with file updated at time n where n is a number whose significance is explained below
  • make file indicates that the file called file should be rebuilt from the files it depends on. Your program will respond with the names of all targets that must be rebuilt in order to rebuild file.

A target should be rebuilt whenever any target it depends on is newer than the target itself.

In order to track ages of files, you will maintain a virtual “clock” (just an int) that “ticks” every time you issue the touch command (successful or not). When a target is rebuilt, its lastmodified time should be set to the current clock time. Every target starts with a last-modified time of 0. For example:

1
2
3
4
a: b
touch b
touch b
touch b

will produce the output (on stdout)

1
2
3
b updated at time 1
b updated at time 2
b updated at time 3

It is not valid to directly update a target that depends on other targets. If you do, your program should issue an error message on stdout, as illustrated below:

1
2
3
4
a: b
touch a
(Output:)
Cannot update non-leaf object

When you issue the make file (build) command, the program should rebuild any files within the dependency graph of file that are older than the files they depend on. For example:

1
2
3
4
5
6
a: b
a: c
b: d
c: e
touch e
make a

will produce the output

1
2
3
e updated at time 1
Building c
Building a

because file c depends on e, and a depends on c. Note that b is not rebuilt. The order in which the Building messages appear is not important.

A file may depend on at most 10 other files. If you attempt to give a file an 11th dependency, issue the error message