本主要目的是让您练习在C++中创建和操作链表。在这个过程中,赋值重新实施了以前使用构造函数和析构函数编写类的概念。它还可以作为一个关于在没有内存泄漏的情况下动态分配和取消分配数据的练习。这是通过编写一个程序(以下称为“程序”)来完成的,该程序扩展了您之前的实验室任务,将Shapes数据库表示为一组链表,但使用了一组简化的命令,其中(大部分)没有输入错误。与以前的作业相比,此作业将需要更多的代码编写。我们鼓励你早点出发。问题陈述作业由两部分组成。在第一部分中,您将编写一个类似于前一个实验室的命令解析器。该解析器为程序提供了一个文本输入接口。与以前的实验室不同,您不需要检查输入中的错误(除了一些例外)。这些命令创建形状、创建形状组、在组之间移动形状以及删除形状/组。ECE244 Linked Lists
1. Objectives C++
The main objective of this assignment is to give you practice in creating and manipulating linked lists in C++. In the process, the assignment re-enforces earlier concepts of writing classes with constructors and the destructor. It also serves as an exercise on dynamic allocation and de-allocation of data without memory leaks. This is done by writing a program (called “the program” hereafter) extending your previous lab assignment to represent the Shapes database as a set of linked lists, but with a simplified set of commands in which there are (mostly) no errors in input. This assignment will require more code writing compared to the previous assignments. You are encouraged to start early.
2. Problem Statement
The assignment consists of two parts. In the first part, you will write a command parser similar to that of the previous lab. The parser provides a textual input interface to your program. In contrast to the previous lab, you need not check for errors in the input (with some exceptions). These commands create shapes, create groups of shapes, move shapes across groups and delete shapes/groups.Each command consists of an operation keyword followed by arguments. The command and the arguments are separated by one or more spaces. Thus, the code you will write for this part of the assignment should take input from the standard input, parse it, and print responses. The command parser loops, processing input as long as input isavailable.In the second part of the assignment, you will implement a linked list based data structure to serve as a simple “database” of objects that store the created shapes. To do so, you will implement several classes: Shape, ShapeNode, ShapeList, GroupNode andGroupList.
3.Specifications
Similar to the previous lab, it is important that you follow the specifications below carefully. Where the specification says shall or must (or their negatives), following the instruction is required to receive credit for the assignment. If instead it says may or can, these are optional suggestions. The use of should indicates a recommendation; compliance is not specifically required. However, some of the recommendations may hint at a known-good way to do something or pertain to good programming style.
Example input and output for the program are provided in Section 4 for your convenience. They do not cover all parts of the specification. You are responsible for making sure your program meets the specification by reading and applying the description below.
4.Coding Requirements
The code you will write shall be contained in only the source files named Parser.cpp, Shape.cpp, ShapeNode.cpp, ShapeList.cpp, GroupNode.cpp and GroupList.cpp. Skeletons of these files are released with the assignment’s zip file on the course website. The zip file also contains corresponding .h files: globals.h, Shape.h, ShapeNode.h. ShapeList.h, GroupNode.h and GroupList.h. These .h files contain comments that describe the classes defined in them and their methods. Please note that the .h files are NOT to be modified in any way. Modifying these files almost always results in a mark of 0 for the assignment. You may make use of helper functions to split up the code for readability and to make it easier to re-use. These functions (and their prototypes) must be in one of the aforementioned .cpp files. That is, you must not add any new .h or .cpp files.Input and output must be done only using the C++ standard library streams cin and cout.The stream input operator >> and associated functions such as fail() and eof() shall be used for all input. C-style IO such as printf and scanf shall not be used.Strings shall be stored using the C++ library type string, and operations shall be done using its class members, not C-style strings.C-library string-to-integer conversions (including but not limited to atoi, strtol, etc.) shall not be used.The Standard Template Library (STL) shall not be used. The point of this assignment is for you to create your own linked list implementation.Your program shall not leak memory. Unlike the previous assignment, you will be penalized for leaking memory.Command
5.Line Input
Input will be given one command on one line at a time. The entire command must appear on one line. All input must be read using the C++ standard input cin. The program shall indicate that it is ready to receive user input by prompting with a greater-than sign followed by a single space (> ); see Section 4 for an example. Input shall always be accepted one line at a time, with each line terminated by a newline character.Unlike the previous lab, you shall assume that input values are error-free (i.e., commands are always valid, integers are always valid, no arguments are missing, no extra arguments, etc.). Nonetheless, there will be some errors you must check for and print an appropriate error message (see Section 3.3). In this case, the input line shall be discarded, and processing shall resume at the next line. The program shall continue to accept and process input until an end-of-file (eof) condition is received.
Each line of valid input shall start with a command name, followed by zero or more arguments, each separated by one or more spacecharacters. The number and type of arguments accepted depend on the command. The arguments and their permissible types/ranges are shown below inTable 1.Argument Description, type, and rangename a string consisting of any non-whitespace characters3 ; except strings that represent commands, shape types or reserved words (e.g., pool).type a string that represents the type of a shape and must be one of: ellipse, rectangle or triangleloc a positive integer (0 or larger) that represents the location of the shape in either the x or y dimensionsize a positive integer (0 or larger) that represents the size of a shape in either the x or y dimensionThe valid commands, their arguments, and their output if the command and its arguments are all legal are shown below in Table 2. In the case of the draw command, which takes no arguments, the program prints not only the message shown in the table, but also all the groups and shapes in the database (see the example in Section 4).Command Arguments Output if Command is Validshape name type loc loc size size name: type loc deletedIf there is an error, a message shall be displayed asdescribed in Section 3.3. Otherwise, a successful command produces a single line of output on the C++ standard output, cout, as shown in Table 2. The values in italics in Table 2 must be replaced with the values given by the command argument. Strings must be reproduced exactly as entered. Where locs or sizes are printed, they shall appear on the order entered in the command.
6.Error Checking
Errors shall cause a message to be printed to cout, consisting of the text “error:” followed by a single space and the error message from Table 3. In the messages, italicized values such as nameinvalid name The name used for a shape or a group is a reserved word (e.g., a command word, a shape type or pool)name name exists A shape/group with the name name exists in the database, i.e., has once been created and has not been deletedshape name not found A shape with the name name specified in a command does not existgroup name not found A group with the name name specified in a command does not existshould be replaced by the value causing the error. Error message output must comply exactly (content, case, and spacing) with the table below to receive credit. There are no trailing spaces following the text.The program is not required to deal with errors other than those listed in Table 3. Please note:The commands and the shape types are case sensitive. Thus, while a shape or a group cannot be named pool or triangle, it can be named Pool or triAngle.For every line of input, your program must output something. Either a message indicating success (Table 2) or an error (Table 3). For an empty line of input (nothing or just whitespace) your program should just output the command prompt.You are guaranteed that autotester will not use test cases that have any errors other than the ones listed above. However, You may reuse your code from lab assignment 3 along with its error checking, if you wish. This part of your code will not be exercised or tested.Program UseUsers of the program use the command line to create and delete shapes, similar to the previous assignment. In addition, users can create and delete shape groups, where a group is a collection of already created shapes. Finally, users can “draw” the groups and shapes, meaning to print them to the standard output.
A database is used to organize the shapes and the groups. When the database is initially created, there is only one group called pool. A shape command creates a new Shape object and this object is by default a member of the pool group. The group command is used to create a new group, initially empty. The move command is used to move a shape from the group it is currently in to a new group (which can be the one it is currently in). A shape cannot belong to more than one group.The delete command can be used to delete shapes or groups. If used to delete a shape, only the shape specified in the command is deleted from the group it currently in. If the command is used to delete a group, then all the shapes in the group are moved to the pool group and the group is deleted. The pool group cannot be deleted (pool is a reserved word that cannot be used in commands or as shape/group names).The examples in Section 4 further illustrate the above commands, the actions they take and the output they produce.
7.The Database
The program shall keep track of all shapes and groups using a linked list based data structure. An example of which is shown in Figure 1. There is a linked list of type GroupList s that represents the list of groups created during program execution. Each node in the list is a GroupNode, which in turn has a pointer to a ShapeList that represents the shapes belonging to the group. A ShapeList is a linked list of ShapeNodes, where each ShapeNode has a pointer to the Shape object it represents.At the onset of execution, a GroupList object is created to represent a linked list of GroupNodes. The list contains exactly one GroupNode with the name pool. The ShapeList of the pool is initially empty. As shapes are created, they are added the pool group by inserting them at the end of its ShapeList. When new groups are created, they are added to the end of the GroupList. Thus, the order of the groups on the GroupList is that of the order in which they are created.When a shape is deleted, it is removed from the ShapeList of the group it currently belongs to. When a group is deleted, all the ShapeNodes in it ShapeList are added at the end of the pool’s ShapeList, keeping their order, and the corresponding GroupNode is deleted from the GroupList.It is critical that your program deletes all dynamic data it allocates so as no memory leaks occur. In this assignment, memory leaks will be checked for and reported by exercise and the autotester, and there is penalty for having memory leaks. It is highly recommended that you use the valgrind memory checking program. A tutorial on valgrind as released with the previous assignment. Please learn and use this tool. It is used by exercise to check for memory leaks in your code.Program Classes and FilesThe Shape ClassThe Shape class holds the properties of a shape, including its name, type, location, and size. It is mostly identical to the Shape class in the previous assignment. The definition of the class appears in Shape.h. Examine the file and read through the comments to understand the variables and methods of the class. You must implement this class in the file Shape.cpp.
9.The ShapeNode Class
This class defines a node in a linked list of ShapeNodes. It has two data members: myShape, which points to the Shape object associated with the node, and next, which is a pointer to the next ShapeNode in a list. It is defined in the file ShapeNode.h and you must implement this class in the file ShapeNode.cpp. Read through the comments in ShapeNode.h to find out what the methods of the class do.
10.The GroupNode Class
This class defines a node in a linked list of GroupNodes. It has three data members: name, which stores the name of the group, myShapeList, which points to the ShapeList object associated with the group, and next, which is a pointer to the next GroupNode in a list. It is defined in the file GroupNode.h and you must implement this class in the file GroupNode.cpp. Again, read through the comments in GroupNode.h to find out what the methods of the class do.This class defines a linked list of ShapeNodes. It contains a single data member, head, which points to the first ShapeNode in the list. It is defined in the file ShapeList.h and the files contains comments that describe what the methods of the class do. You must implement this class in ShapeList.cpp.The GroupList ClassThis class defined a linked list of GroupNodess. It contains one data member called head, which is a pointer to the first GroupNode in the list. The class is defined in the file GroupList.h and you must implement it in the file GroupList.cpp. The comments in the GroupList.h indicate what the methods of the class should do.The Command Line ParserA suggested (but not mandatory) structure for your command line parser appears in the skeleton Parser.cpp file released within the assignment’s zip file. It is identical to that of the previous assignment and it is recommended that you reuse your code from that assignment.
WX:codehelp
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。