Write program in C++ programming language
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 max-heap data structure (not all the functions yet); 4. a Makefile which compiles all modules and link them into the executable. ELEMENT is a data type that contains a field named key, which is of type int. Note that ELEMENT should not be of type int. HEAP is a data type that contains three fields named capacity (of type int), size (of type int), and H (an array of type ELEMENT with index ranging from 0 to capacity). The functions that you are required to implement are: Initialize(n) which creates an object of type HEAP with capacity n, size 0, and H points to a dynamically allocated array of n +1 pointers. It then returns a pointer to this object. This function requires you to perform dynamic memory allocation, given the demand n. printHeap(heap) which prints out the heap information, including capacity, size, and the key fields of the elements in the array with index going from 1 to size. You should implement a module that takes the following commands from the key-board and feeds to the main program: .S . Cn .R . W .P On reading S, the program stops. On reading C n, the program (1) creates a heap with capacity equal to n, and size equal to 0, and (2) waits for the next command. Note that dynamic memory allocation is required here. On reading R, the program (1) opens the file "HEAPinput.txt" in read mode; (2) reads in the first integer n in the file; (3) reads in the next n integers key, key2,..., keyn from the file, dynam- ically allocates memory for an ELEMENT, sets it key to key;, and let the pointer heap->H[j] points to this ELEMENT; and (4) waits for the next command. On reading P, the program (1) writes the current heap information to the screen, and (2) waits for the next command. The output should be in the same format as in the file HEAP input.txt, proceeded by the heap capacity. On reading W, the program (1) opens the file "HEAPoutput.txt for writing, (2) writes the current heap size and the keys of the elements into "HEAPoutput.txt", and (3) waits for the next command. The file "HEAPoutput.txt" should be in the same format as the file HEAP input.txt. The file HEAPinput.txt is a text file. The first line of the file contains an integer n, which indi- cates 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. As an aid, the following is a partial program for reading in the commands from the keyboard. You need to understand it and to expand it. typedef struct TAG_ELEMENT int key; }ELEMENT; typedef ELEMENT *ElementT; typedef struct TAG_HEAP{ int capacity; /* max size of the heap */ int size; /* current size of the heap / ElementT *H; /* pointer to pointers to elements */ }HEAP; #include "util.h" //=========== int nextCommand(int *n, int *f) { char c; while(1) { scanf("%c", &c); if (c == ' ' || c == '\t II c == ' ') { continue; } if (c == 'S'){ break; } if (c == 'C' || c == 'c'){ scanf("%d", n); break; } if (...){ } } return c; } //===== The following is a partial program that calls the above program. #include
#include #include "util.h" int main() { // variables for the parser... char c; int i, v; while(1) { c = nextCommand(&n, &f); switch (c) { case 's': case 'S': printf("COMMAND: %c ", c); exit(0); case 'c': case 'C': printf ("COMMAND: %c %d ", c, n); heap - heapInit(n); break; case 'r': case 'R': printf ("COMMAND: %c ", c); ifile fopen("HEAP input.txt", "r"); if (!ifile) { } fscanf (ifile, "%d", &n); default: break; } } exit(0); } // The following is a partial Makefile. EXEC - run CC - g++ CFLAGS - -c-Wall # $ (EXEC) has the value of shell variable EXEC, which is run. # run depends on the files main.o util.o heap.o $(EXEC) main.o util.o heap.o # run is created by the command g++ -o run main.o util.o # note that the TAB before $(CC) is REQUIRED... $(CC) -0 $(EXEC) main.o util.o heap.o # main.o depends on the files main.h main.cpp main.o: main.h main.cpp # main.o is created by the command g++ -c -Wall main.cpp # note that the TAB before $(CC) is REQUIRED... $(CC) $ (CFLAGS) main.cpp util.o util.h util.cpp $(CC) $ (CFLAGS) util.cpp heap.o heap.h heap.cpp $(CC) $(CFLAGS) heap.cpp clean : rm *.0 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 max-heap data structure (not all the functions yet); 4. a Makefile which compiles all modules and link them into the executable. ELEMENT is a data type that contains a field named key, which is of type int. Note that ELEMENT should not be of type int. HEAP is a data type that contains three fields named capacity (of type int), size (of type int), and H (an array of type ELEMENT with index ranging from 0 to capacity). The functions that you are required to implement are: Initialize(n) which creates an object of type HEAP with capacity n, size 0, and H points to a dynamically allocated array of n +1 pointers. It then returns a pointer to this object. This function requires you to perform dynamic memory allocation, given the demand n. printHeap(heap) which prints out the heap information, including capacity, size, and the key fields of the elements in the array with index going from 1 to size. You should implement a module that takes the following commands from the key-board and feeds to the main program: .S . Cn .R . W .P On reading S, the program stops. On reading C n, the program (1) creates a heap with capacity equal to n, and size equal to 0, and (2) waits for the next command. Note that dynamic memory allocation is required here. On reading R, the program (1) opens the file "HEAPinput.txt" in read mode; (2) reads in the first integer n in the file; (3) reads in the next n integers key, key2,..., keyn from the file, dynam- ically allocates memory for an ELEMENT, sets it key to key;, and let the pointer heap->H[j] points to this ELEMENT; and (4) waits for the next command. On reading P, the program (1) writes the current heap information to the screen, and (2) waits for the next command. The output should be in the same format as in the file HEAP input.txt, proceeded by the heap capacity. On reading W, the program (1) opens the file "HEAPoutput.txt for writing, (2) writes the current heap size and the keys of the elements into "HEAPoutput.txt", and (3) waits for the next command. The file "HEAPoutput.txt" should be in the same format as the file HEAP input.txt. The file HEAPinput.txt is a text file. The first line of the file contains an integer n, which indi- cates 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. As an aid, the following is a partial program for reading in the commands from the keyboard. You need to understand it and to expand it. typedef struct TAG_ELEMENT int key; }ELEMENT; typedef ELEMENT *ElementT; typedef struct TAG_HEAP{ int capacity; /* max size of the heap */ int size; /* current size of the heap / ElementT *H; /* pointer to pointers to elements */ }HEAP; #include "util.h" //=========== int nextCommand(int *n, int *f) { char c; while(1) { scanf("%c", &c); if (c == ' ' || c == '\t II c == ' ') { continue; } if (c == 'S'){ break; } if (c == 'C' || c == 'c'){ scanf("%d", n); break; } if (...){ } } return c; } //===== The following is a partial program that calls the above program. #include #include #include "util.h" int main() { // variables for the parser... char c; int i, v; while(1) { c = nextCommand(&n, &f); switch (c) { case 's': case 'S': printf("COMMAND: %c ", c); exit(0); case 'c': case 'C': printf ("COMMAND: %c %d ", c, n); heap - heapInit(n); break; case 'r': case 'R': printf ("COMMAND: %c ", c); ifile fopen("HEAP input.txt", "r"); if (!ifile) { } fscanf (ifile, "%d", &n); default: break; } } exit(0); } // The following is a partial Makefile. EXEC - run CC - g++ CFLAGS - -c-Wall # $ (EXEC) has the value of shell variable EXEC, which is run. # run depends on the files main.o util.o heap.o $(EXEC) main.o util.o heap.o # run is created by the command g++ -o run main.o util.o # note that the TAB before $(CC) is REQUIRED... $(CC) -0 $(EXEC) main.o util.o heap.o # main.o depends on the files main.h main.cpp main.o: main.h main.cpp # main.o is created by the command g++ -c -Wall main.cpp # note that the TAB before $(CC) is REQUIRED... $(CC) $ (CFLAGS) main.cpp util.o util.h util.cpp $(CC) $ (CFLAGS) util.cpp heap.o heap.h heap.cpp $(CC) $(CFLAGS) heap.cpp clean : rm *.0