CS246留学生编程 写作、C++语言程序调试

” CS246留学生编程 写作、C++语言程序调试ASSIGNMENT #3: C++ CLASSES CS246, FALL 2020Assignment #3: C++ ClassesDue Date 1: Wednesday, October 28, 2020, 5:00 pmDue Date 2: Wednesday, November 04, 2020, 5:00 pmOnline Quiz: Wednesday, November 11, 2020, 5:00 pmTopics that must have been completed before starting Due Date 1:1. Software Testing, produceOutputs and runSuite from A1Topics that must have been completed before starting Due Date 2:1. Object-Oriented Programming: Introduction2. Object-Oriented Programming: Special class members3. Object-Oriented Programming: Advanced object uses4. Object-Oriented Programming: Invariants and Encapsulation5. Design Patterns: IteratorLearning objectives: Object-Oriented programming, Invariants and Encapsulation, Iterator design pattern C++ classes, constructors, destructors, and operations Dynamic memory allocation for C++ objects and arrays Questions 1a and 2a are due on Due Date 1; the remaining questions are due on Due Date 2. You mustsubmit the online quiz on Learn by the Quiz date. On this and subsequent assignments, you will take responsibility for your own testing. This assignment isdesigned to get you into the habit of thinking about testing before you start writing your program. If youlook at the deliverables and their due dates, you will notice that there is no C++ code due on Due Date 1.Instead, you will be asked to submit test suites for C++ programs that you will later submit by Due Date 2.Test suites will be in a format compatible with that of the latter questions of Assignment 1, so if you did agood job writing your runSuite script, that experience will serve you well here. Design your test suites with care; they are your primary tool for verifying the correctness of your code. Notethat test suite submission zip files are restricted to contain a maximum of 40 tests. The size of each input(.in|.args) file is also restricted to 300 bytes, and each output file (.out) is restricted to 1,000 bytes.This is to encourage you not combine all of your testing eggs in one basket. You must use the standard C++ I/O streaming and memory management (MM) facilities on this assignment;you may not use C-style I/O or MM. More concretely, you may #include the following C++ libraries(and no others!) for the current assignment: iostream, fstream, sstream, iomanip, string, andutility. Marmoset will be setup to reject submissions that use C-style I/O or MM, or libraries other thanthe ones specified above. Note that this means you cannot use vector or other data structures that requirean additional library to be imported. We will manually check that you follow a reasonable standard of documentation and style, and to verifyany assignment requirements that are not automatically enforced by Marmoset. Code to a standardthat you would expect from someone else if you had to maintain their code. Further comments on codingguidelines can be found here: httpss://www.student.cs.uwaterloo.ca/cs246/F20/codingguidelines.shtmlPage 1 of 7ASSIGNMENT #3: C++ CLASSES CS246, FALL 2020 We have provided some code and sample executables in the subdirectory codeForStudents under theappropriate subdirectories. These executables have been compiled in the CS student environment andwill not run anywhere else. You may not ask public questions on Piazza about what the programs that make up the assignmentare supposed to do. A major part of this assignment involves designing test cases, and questions that askwhat the programs should do in one case or another will give away potential test cases to the rest of theclass. Questions found in violation of this rule will be marked private or deleted; repeat offences could besubject to discipline.Coding QuestionsQuestions 1, 2, and 3 are part of the coding assessment and may be publicly discussed on Piazza so long as solutions areneither discussed nor revealed.Question 1(40% of DD1; 32% of DD2) In this exercise, you will write a C++ class to control a simple robotic drone exploring someterrain. Your drone starts at coordinates (0,0), facing North.Some starter code has been provided for you in the folder a3/codeForStudents/drone, along with a sample executable.You may not change the contents of drone.h and position.h other than by adding private instancemembers and comments, i.e., the public interface must stay exactly the same.Use the class Position declared in position.h for the coordinates.class Position {int ew, ns;// public methods; see position.h for details};The east-west direction is the first component of a position (east is positive, west is negative). The north-south direction isthe second (north is positive, south is negative).The operator for a Position must also be implemented, which should print the current values of the position inthe format: (ew,ns) (no spaces or line breaks). Please ensure that your output matches that of the sample executable.Use the class Drone declared in drone.h to implement the drones functionality.class Drone {Position cur; // current positionint dir; // 0 = N, 1 = E, 2 = S, 3 = Wint numSteps; // total number of steps walked by the drone// public methods; see drone.h for details};The operator for a Drone must also be implemented, which should print the current status of the drone as:Current Position: (ew,ns), Facing direction, where direction is North, East, South, orWest (no line breaks). Please ensure that your output matches that of the sample executable.You must create the files position.cc and drone.cc and implement all the operations specified in position.hand drone.h.The test harness main.cc is provided with which you may interact with your drone for testing purposes. The testharness is not robust and you are not to devise tests for it, just for the Drone class. Do not change this file.The test harness supports the following commands:Page 2 of 7ASSIGNMENT #3: C++ CLASSES CS246, FALL 2020Command Descriptionf n Moves forward by n steps in the current direction. n must be positive.b n Moves backward by n steps in the current direction. n must be positive.l Turns 90 degrees to the left.r Turns 90 degrees to the right.c Prints the current status of the drone.t Prints the total number of steps walked by the drone.m Prints the Manhattan distance of the current position to the origin.Notes: The total number of steps walked by the drone is a simple sum of all the forward and backward steps. For example, ifthe drone walks 5 steps forward, 2 backward, turns right, and walks 5 steps forward, the total number of steps is just5 + 2 + 5 = 12. The Manhattan distance is calculated as the absolute north-south displacement plus the absolute east-west displacement.For example, the Manhattan distance of the position (3,4) to the origin is 7 (i.e., 3 + 4). The Manhattan distance ofthe position (-3,4) to the origin is also 7. See diagram below.Submission:(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 drone.h, drone.cc, position.h, and position.ccin the zip file, a3q1b.zip. We will provide the Makefile and main.cc to test your submission.Page 3 of 7ASSIGNMENT #3: C++ CLASSES CS246, FALL 2020Question 2(60% of DD1; 60% of DD2) A markup language is a system for annotating documents. In markup languages such as HTMLand XML, annotations consist of tags, identified by the starting and end characters . Tags can further contain attributesor children tags/text and are closed by / . For example, in the following excerpt, document, body, and text are tags.The text tag contains a id attribute, currently set to t1, and a value attribute, currently set to Hello CS 246.documentbodytext id=t1 value=Hello CS 246/text/body/documentIn this exercise, you will write a class to manage tags in a document. Our markup language is loosely inspired by HTMLand XML, but it is quite simplified. Our tags can be of any type (such as document, body, or text) and can optionallycontain the attributes id and value (and no other). Additionally, they can contain any number of children tags. There isalways a top-level document tag. The example document above is a valid document in our markup language and also anexample of the formatting that should be used by the program when printing the tags.We have provided the declaration of the Tag class in the file tag.h. You may not change the contents of tag.h otherthan by adding private instance members and comments, i.e., the public interface must stay exactly the same.class Tag {std::string type; // the type of the tagstd::string id; // the id attribute of the tagstd::string value; // the value attribute of the tagTag *parent; // pointer to the parent tagTag **children; // array of children tag pointersint childrenLength; // current number of children tagsint childrenCapacity; // current capacity of the children array// public methods; please check tag.h for details};Your job is to implement all the methods of the Tag and TagIterator classes in the file tag.cc. Please read carefullythe documentation for each method in tag.h. The comments explain what they must do.Implementation details: The children array contains the children Tags. When a Tag is created, initialize children to nullptr. InitializechildrenLength and childrenCapacity to zero. When the first child Tag is added, create an array in the heapwith size one. You must keep track of the current capacity (size) of the array in childrenCapacity and the currentnumber of children tags (positions used in the array) in childrenLength. You will increase the capacity of the arrayusing a doubling strategy. Each time a new child Tag is added, if the length of the array is already equal to the capacity,you must double the current capacity of the array (i.e., create a new array with double the capacity of the current oneand move all the children Tags over to the new array). When children tags are removed from the array, you do not needto reduce the capacity. Because we test your submission by comparing the output of your program with the expected output of the correctprogram, you will lose marks if your Tag::print() does not precisely match the output of our implementation.You can use the document at the top of the page as an example of how the formatted output should look like. Notethat each tag start markup and tag end markup should be printed on a new line. Children tags must be indented withtwo spaces per level. We are also providing you a sample executable that produces the correct output. Please testthe provided executable for every potential type of output and ensure that your solutions matches the sample outputprecisely (use an utility like diff to ensure that the outputs are identical; dont rely only on a visual inspection). TheproduceOutputs and runSuite scripts that you wrote as part of A1 can help you do that.Page 4 of 7ASSIGNMENT #3: C++ CLASSES CS246, FALL 2020 We also provided the declaration of TagIterator, which is a implementation of the Iterator design patternfor the children array. You must also implement the methods of this class. Used together with the methodsTag::begin() and Tag::end(), it can be used by a client to iterate through all the children of a tag. You can implement the operator for Tags (also declared in tag.h) by just calling the tags print method. Your program must not leak any memory.A test harness is provided in main.cc (which uses controller.[h|cc]). You can use it to test your Tag class. Thetest harness is not robust and you are not to devise tests for it, just for the Tag class. Do not change these files. We havealso provided a sample executable (markup) that you can use for tests.The test harness supports the following commands. Please read the contents of main.cc and controller.[h|cc]if you need more details about them. Note that the harness keeps track of the current tag at any moment and most of thecommands operate on it. Also, note that the harness automatically creates a top-level document tag. You can set its attributesand add children to it. However, you cannot delete, copy, or move the document tag.Command Descriptionprint Prints the current tag to the standard output.add type Creates a new tag of the type specified by the argument, adds it as a child of the current tag, andmakes it the new current tag. Note that type cannot be document.delete Deletes the current tag, removes it from its parent, and makes its parent the new current tag.parent Sets the parent of the current tag as the new current tag.up Sets the topmost tag (document) as the current tag.id val Sets the id attribute of the current tag to val. Note that val cannot contain whitespaces.value val Sets the value attribute of the current tag to val. Note that val cannot contain whitespaces.find type Finds the first tag that is a child of the current tag and has the specified type. If such a tag if found,it is set as the current tag. Otherwise, nothing happens.list Lists all the children of the current tag.cut Copies the current tag to the programs internal clipboard and marks it as cut.copy Copies the current tag to the programs internal clipboard.paste Replaces the contents of the current tag with the contents of the tag currently in the clipboard.The tag in the clipboard is moved or copied depending on whether it was originally cut or copied.Note that you cannot cut and paste a tag into itself or a child of itself (but you can copy it).pasteChild Adds a new child to the current tag, whose contents comes from the tag currently in the clipboard.The tag in the clipboard is moved or copied depending on whether it was originally cut or copied.Note that you cannot cut and paste a tag into itself or a child of itself (but you can copy it).quit Terminates the program.Submission:(a) Due on Due Date 1: Design the test suite suiteq2.txt for this program and zip the suite into a3q2a.zip.(b) Due on Due Date 2: Implement this in C++ and place the files tag.h and tag.cc in the zip file, a3q2b.zip. Wewill provide the Makefile, main.cc, and controller.[h|cc] to test your submission.Page 5 of 7ASSIGNMENT #3: C++ CLASSES CS246, FALL 2020Question 3*** BONUS *** This question is worth 5% as a bonus, i.e., if the assignment is out of 100 marks, and you obtain full marksin this question, your maximum assignment grade is 105 marks.In this question, you will extend your solution from Question 2 to support these two additional commands:Command Descriptionnext If the current tag is not the last child tag in its parent, then set the next sibling (i.e., the tagthat comes right after the current one in its parents children array) as the current tag in thecontroller and call Controller::printCurrent() to print the current statusto cout. If the current tag is already the last child tag in its parent, just print Current tag isalready the last child of its parent. to cerr and do nothing else. If the current tag is the top-level tag (i.e., it does not have a parent), just print Currenttag is the upper level tag. to cerr and do nothing else.previous If the current tag is not the first child tag in its parent, then set the previous sibling (i.e., thetag that comes right before the current one in its parents children array) as the current tagin the controller and call Controller::printCurrent() to print the currentstatus to cout. If the current tag is already the first child tag in its parent, just print Current tag isalready the first child of its parent. to cerr and do nothing else. If the current tag is the top-level tag (i.e., it does not have a parent), just print Currenttag is the upper level tag. to cerr and do nothing else.Because this is a bonus question, you are not given any starting code for it. Therefore, you should use your solution fromQuestion 2 as the starting code. For this question, you are allowed to modify any file in the solution. But please note that yourmodifications must follow the C++ and object-oriented programming guidelines taught in this course.You will need to modify the main() function to support the two additional commands and make any other changesnecessary to the program to make them work. You must ensure that the other commands of the test harness continue workingas before, i.e., the only change in the programs behaviour is that two new commands are now supported. Your updatedsolution must also not leak any memory.We have provided a sample executable (markup bonus) that you can use for tests.Submission:(a) Due on Due Date 1: Nothing.(b) Due on Due Date 2: Implement this in C++ and place all the files necessary to compile your program (including theMakefile) in a3q3b.zip. Your Makefile must create an executable named markup. Note that the executable nameis case-sensitive.Page 6 of 7ASSIGNMENT #3: C++ CLASSES CS246, FALL 2020Written AssessmentQuestions 4, 5, and 6 are part of the written assessment, and may not be publicly discussed on Piazza (or anywhere else).Question 4(2% of DD2)(a) Explain in your own words what a class and an object are with respect to object-oriented programming (not C++specific).(b) Provide a code snippet in C++ that includes a class and an object and label each with a comment.Question 5(2% of DD2) Consider the following program. If there are any errors, identify them (in what line they occur and what is theproblem). Otherwise, just state No error.struct Point {int x, y;Point(int x, int y) : x{x}, y{y} {}};int main() {Point p1[3];Point p2[3] = {Point{1,1}, Point{2,2}, Point{3,3}};Point *p3 = new Point[3];Point p4 = {Point{1,1}, Point{2,2}, Point{3,3}};}Question 6(4% of DD2) Consider the following Pixel class that should enforce the class invariant that the values for the data fields r,g, and b must be in the range 0 to 255.struct Pixel {unsigned int r; // value for redunsigned int g; // value for greenunsigned int b; // value for blue};Explain how you would change the definition, without changing the types of the fields, so that the invariant is enforced.Page 7 of 7如有需要,请加QQ:99515681 或邮箱:99515681@qq.com

添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导