IN2029 Programming in C++
c++代写 | C++ | CS作业代写 | CS代写 – 这是一个关于c++的题目, 主要考察了关于c++的内容,是一个比较经典的题目, 是比较有代表性的c++等代写方向
There is a single coursework in this module, counting for 30% of the overall module mark. This coursework is due at 5pm on Sunday 25th November. As with all modules, this deadline is hard, and extensions may only be requested via the standard Extenuating Circumstances procedure. This coursework provides practice with classes and the c++ Standard Library. It does not require any features covered after session 6.
You are to implement two classes that form part of a simple game, with robots moving around in a 2-dimensional space. I have prescribed a particular external structure and names for your classes: please follow these precisely, because I will be testing your solutions automatically. You may also add private members to your classes, and any other functions you find useful.
You should implement and test the following classes.
representing a named robot moving around in a two-dimensional space. Your class should have a single constructor
explicit robot(const string &n) set up a robot with namen, placed at the origin.
and the following public methods:
void movenorth() moves the robot one step to the north.
void moveeast() moves the robot one step to the east.
void movesouth() moves the robot one step to the south.
void movewest() moves the robot one step to the west.
const string & name() constreturns the name of the robot (as supplied to the con- structor).
int north() constreturns the current distance north of the robot. (This could be nega- tive, if the robot has moved south more often than north.)
int east() const returns the current distance east of the robot. (As with the previous function, this value might also be negative.)
int travelled() const returns the total distance travelled by this robot since it was cre- ated.
You should also define an external function (not a member function):
int distance(const robot &r)returns the distance of robotrfrom the origin according theManhattan metric: the least number of steps it would take to move the robot back to the origin. The standard functionabs(n), from thestandard header, may be useful here.
A class holding many named robots. Your class should have a default constructor and public methods
int numrobots() const returns the total number of robots in the game (initially none).
void move(const string &name, int dir) move the named robot one step in the spec- ified direction (0 = north, 1 = east, 2 = south, 3 = west). If there is no robot of that name, one should be created at the origin and then moved as above.
int numclose() const returns the number of robots no more than 10 steps from the origin.
int maxdistance() const returns the furthest distance of any robot from the origin.
string furthest() constreturns the name of the robot that is the furthest distance from the origin.
vectorrobotsbytravelled() const returns a collection of all the robots in the system, arranged in increasing order of total distance travelled.
You should use library algorithms where appropriate. In order to implementmove()efficiently, your class should hold the robots in a map. You may use the following member functions ofmapnot covered in the lectures:
count(k) returns 1 if the keykis in the map, and otherwise 0.
at(k) returns a reference to the value in the map corresponding to the keyk. If the key is not present, it throws an exception (unlike[k], which would create an entry).
emplace(k, v) creates a new entry in the map with keykand corresponding valuev.
The classes will be worth 40% each. The remaining 20% of the marks will be for clean programming style, including consis- tent layout, sensible identifier names, useful comments, avoidance of superfluous variables and data members, and general clarity of code. You may use language features not covered in the lectures, but your code must be standard C++. (If using Visual Studio, set the Disable Language Extensions property under C/C++ Language to Yes.)
Submit a ZIP file containing your source files only, to Moodle.