I’ve included some of the code I’ve already completed. If you could please help in either editing or explaining where I am going wrong, I’d appreciate. I also included one test case to test the code.
C++:
You will perform modular design, provide a Makefile to compile various modules to generate the executable file named run. Among other things, you need to have
1. a main program, which coordinates all other modules;
2. a module that provides utility services including command line interpretation;
3. a module that implements the heap data structure (not all heap functions yet);
4. a Makefile which compiles all modules and link them into the executable.
For each module other than the main program, you should have a header file which specifies the data structures and the prototypes of the functions in the module, and an implementation file which implements all of the functions specified in the header file.
You need to define the following data types. • ELEMENT is a struct that contains a field named key, which is of type int. In later assignments, you will add other fields to ELEMENT, without having to change the functions. Note that ELEMENT should not be of type int.
The main program should react to each of the above command in the following way.
S: On reading S, the program 1. Writes the following line to stdout: COMMAND: S where S is the character S. 2. Stops.
C: On reading C n, the program 1. Writes the following line to stdout: COMMAND: C n where C is the character C and n is replaced by the value of n. 2. Calls a function in the heap module to create a heap with capacity equal to n and size equal to 0, and return a pointer to this heap object to the caller. 3. Waits for the next command from stdin.
R: On reading R, the program 1. Writes the following line to stdout: 2 COMMAND: R where R is the character R. 2. Opens the file ”HEAPinput.txt” in read mode. If the file is not opened successfully, writes the following line to stdout: Error: cannot open file for reading and waits for the next command from stdin. 3. Reads in the first integer n from the file opened. If heap is NULL or heap->capacity is smaller than n, writes the following line to stdout: Error: heap overflow and waits for the next command from stdin. 4. Reads in the next n integers key1, key2, . . ., keyn from the file, dynamically allocates memory for an ELEMENT, sets it key to keyj , and let heap->H[j]
P: On reading P, the program 1. Writes the following line to stdout: COMMAND: P where P is the character P. 2. If heap is NULL, writes the following line to stdout: Error: heap is NULL and waits for the next command from stdin. 3. Writes the information of the heap pointed to by heap to stdout. Refer to the posted test cases for the output format. 4. Waits for the next command from stdin.
W: On reading W, the program 1. Writes the following line to stdout: COMMAND: W 3 where W is the character W. 2. Opens the file ”HEAPout.txt” in write mode. If the file is not opened successfully, writes the following line to stdout: Error: cannot open file for writing and waits for the next command from stdin. 3. If heap is NULL, writes the following line to stdout: Error: heap is NULL and waits for the next command from stdin. 4. Writes the information of the heap pointed to by heap to the file ”HEAPoutput.txt”. ”HEAPoutput.txt” should have exactly the same format as ”HEAPinput.txt”. 5. Waits for the next command from stdin.
The file HEAPinput.txt is a text file. The first line of the file contains an integer n, which indicates the number of array elements. The next n lines contain n integers, one integer per line. These integers are the key values of the n array elements, from the first element to the nth element. Refer to the posted test cases for the exact format of ”HEAPinput.txt”.
I’ve included one of the test case files
0 comments