Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need this converted into C++ // ass0.c #define _CRT_SECURE_NO_WARNINGS #define _CRTDBG_MAP_ALLOC // need this to get the line identification //_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); // in main, after

I need this converted into C++

// ass0.c #define _CRT_SECURE_NO_WARNINGS #define _CRTDBG_MAP_ALLOC // need this to get the line identification //_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); // in main, after local declarations //NB must be in debug build

#include #include #include

typedef enum { FALSE = 0, TRUE } BOOL;

struct Frame { char* frameName; struct Frame* pNext; };

typedef struct { char* animationName; struct Frame* frames; }Animation;

// Forward declarations void InitAnimation(Animation*); void InsertFrame(Animation*); void DeleteFrame(Animation*); void EditFrame(Animation*); void ReportAnimation(Animation*); void CleanUp(Animation*);

int main(void) { char response; BOOL RUNNING = TRUE; Animation RG; _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); InitAnimation(&RG);

while (RUNNING) { printf("MENU 1. Insert a Frame at the front 2. Delete last Frame 3. Edit a Frame 4. Report the Animation 5. Quit "); scanf(" %c", &response); switch (response) { case '1':InsertFrame(&RG); break; case '2':DeleteFrame(&RG); break; case '3':EditFrame(&RG); break; case '4':ReportAnimation(&RG); break; case '5':RUNNING = FALSE; CleanUp(&RG); break; default:printf("Please enter a valid option "); } } return 0; }

/*DO NOT CHANGE ANYTHING BEFORE THIS LINE*/

void InitAnimation(Animation* animation) { char str[15] = " ";

//creates a temp Animation object and allocates memory to it Animation *temp = (Animation*)malloc(sizeof(Animation)); if (temp == NULL){ printf(" failed to allocate memory for animation"); return; }

temp->frames = NULL;

printf("Please enter the animation name "); scanf("%s", str); //takes user input and assigns input to str

temp->animationName = (char*)malloc(strlen(str) + 1); //uses strlen(str) to dynamically allocate memory for animationName if (temp->animationName == NULL) { printf(" failed to allocate memory for animation name "); return; } strcpy(temp->animationName, str); //if memory allocation is successful, str is copied into the temp Animation object *animation = *temp; // copies the temp data into the real animation free(temp); // frees the temp Animation object }

void InsertFrame(Animation* animation) { char fN[15] = " "; // creates an object of struct Frame, and allocates memory for frame struct Frame* newFrame = (struct Frame*)malloc(sizeof(struct Frame)); if (newFrame == NULL) { printf("Failed to allocate memory for new frame"); return; }

printf("Insert a Frame in the Animation Please enter the Frame frameName: "); scanf("%s", fN);//takes user input and assigns input to fN

newFrame->frameName = (char*)malloc(strlen(fN) + 1); //uses strlen(fN) to dynamically allocate memory for frameName if (newFrame->frameName == NULL) { printf("Failed to allocate memory for frame name "); return; } strcpy(newFrame->frameName, fN); //fN is copied into the new Frame object newFrame->pNext = animation->frames; //sets the new Frame's pNext to point to the current head of the linked list animation->frames = newFrame; //sets newFrame as the new head of linked list }

void DeleteFrame(Animation* animation) { struct Frame * current = animation->frames;

/*begins by checking if there are any frames in the animation, returns to menu if there are not*/ if (current == NULL) { printf("Animation is empty, no frames to delete "); return; }

/*checks if theres only one frame, removes if true and returns to menu*/ if (current->pNext == NULL) { current = NULL; animation->frames = current; free(current); return; }

/*iterates through the animation until it reaches the second last frame, sets that frame's pNext = NULL, cutting off the last frame from the linked list*/ while (current->pNext->pNext != NULL) { current = current->pNext; } free(current->pNext->frameName); //frees frameName of the last frame free(current->pNext); //frees the last frame current->pNext = NULL; return;

}

void EditFrame(Animation* animation) { int counter = 1; int delIndex = 0; char newName[15] = ""; struct Frame * edit = NULL; struct Frame * frameCount = animation->frames;

//returns to menu if there are no frames if (frameCount == NULL) { printf("There are no frames in the animation to edit "); return; }

//iterates through the frames, incrementing counter each frame while (frameCount->pNext != NULL) { counter++; frameCount = frameCount->pNext; }

edit = animation->frames; printf("Edit a Frame in the Animation "); //do-while loop to get a valid input for a frame index do { if (delIndex >= counter) printf("There is no frame at that index. "); printf("There are %d Frame(s) in the list. Please specify the index (<= 0) to edit at : ", counter); scanf("%d", &delIndex); } while (delIndex >= counter);

if (delIndex == 0) { // index value of 0 means that the first frame is being replaced printf("The name of this Frame is %s. What do you wish to replace it with? ", edit->frameName); free(edit->frameName); //frees the memory that was allocated for the former string } else if (delIndex > 0) { for (int i = 0; i < delIndex; i++) { // linked list is iterated through until it reaches the frame at the chosen index edit = edit->pNext; } printf("The name of this Frame is %s. What do you wish to replace it with? ", edit->frameName); free(edit->frameName); } else { printf("Enter a valid index"); }

scanf("%s", newName); edit->frameName = malloc(strlen(newName) + 1); //allocates memory for the new frame name strcpy(edit->frameName, newName);

}

void ReportAnimation(Animation* animation) { struct Frame * currentFrame = animation->frames; int counter = 0;

printf("Animation name is: %s ", animation->animationName); printf("Report the animation ");

if (currentFrame == NULL) { //checks if there are frames in the animation printf("There are no frames in the animation "); return; } //iterates through the linked list, printing off the Image # and the frame name while (currentFrame != NULL) { printf("Image #%d, file name = %s ", counter, currentFrame->frameName); currentFrame = currentFrame->pNext; counter++; } }

void CleanUp(Animation* animation) { struct Frame * frameDelete = NULL; //starting at the first frame, frees the memory allocated for frameName and the frame object itself, the do { frameDelete = animation->frames; //assigns frameDelete to the first frame animation->frames = animation->frames->pNext; //points animation->frames to the next frame free(frameDelete->frameName); //frees the first frameName free(frameDelete); //frees the Frame object } while (animation->frames != NULL); //loops until there are no more frames free(animation->animationName); // frees animationName }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions

Question

State the uses of job description.

Answered: 1 week ago

Question

Explain in detail the different methods of performance appraisal .

Answered: 1 week ago