Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming: Please help with the handleCommand function in the last file. The dynamicArray.h file is as follows: #ifndef DYNAMIC_ARRAY_H #define DYNAMIC_ARRAY_H #define TYPE void*

C Programming: Please help with the handleCommand function in the last file.

The dynamicArray.h file is as follows:

#ifndef DYNAMIC_ARRAY_H #define DYNAMIC_ARRAY_H

#define TYPE void*

typedef struct DynamicArray DynamicArray; typedef int (*compareFunction)(TYPE, TYPE); typedef void (*printFunction)(TYPE);

struct DynamicArray;

DynamicArray* dyNew(int capacity); void dyDelete(DynamicArray* array);

// Dynamic array void dyAdd(DynamicArray* array, TYPE value); void dyAddAt(DynamicArray* array, TYPE value, int position); void dyPut(DynamicArray* array, TYPE value, int position); void dyRemoveAt(DynamicArray* array, int position); TYPE dyGet(DynamicArray* array, int position); int dySize(DynamicArray* array); void dySwap(DynamicArray* array, int position1, int position2);

// Stack void dyStackPush(DynamicArray* stack, TYPE value); void dyStackPop(DynamicArray* stack); TYPE dyStackTop(DynamicArray* stack); int dyStackIsEmpty(DynamicArray* stack);

// Bag void dyBagAdd(DynamicArray* bag, TYPE value); void dyBagRemove(DynamicArray* bag, TYPE value, compareFunction compare); int dyBagContains(DynamicArray* bag, TYPE value, compareFunction compare);

// Ordered bag void dyOrderedAdd(DynamicArray* bag, TYPE value, compareFunction compare); void dyOrderedRemove(DynamicArray* bag, TYPE value, compareFunction compare); int dyOrderedContains(DynamicArray* bag, TYPE value, compareFunction compare);

// Heap void dyHeapAdd(DynamicArray* heap, TYPE value, compareFunction compare); void dyHeapRemoveMin(DynamicArray* heap, compareFunction compare); TYPE dyHeapGetMin(DynamicArray* heap); void dyHeapSort(DynamicArray* heap, compareFunction compare);

// Iterator typedef struct DynamicArrayIterator DynamicArrayIterator;

struct DynamicArrayIterator { DynamicArray* array; int current; };

DynamicArrayIterator* dyIteratorNew(DynamicArray* array); void dyIteratorDelete(DynamicArrayIterator* iterator); int dyIteratorHasNext(DynamicArrayIterator* iterator); TYPE dyIteratorNext(DynamicArrayIterator* iterator); void dyIteratorRemove(DynamicArrayIterator* iterator);

// Utility /** * Prints the size, capacity, and elements of array, calling the print * function on each element. * @param array * @param print */ void dyPrint(DynamicArray* array, printFunction print); void dyCopy(DynamicArray* source, DynamicArray* destination);

#endif

The task.h file is as follows:

#ifndef TASK_H #define TASK_H

#define TASK_NAME_SIZE 128

typedef struct Task Task;

struct Task { int priority; char name[TASK_NAME_SIZE]; };

Task* taskNew(int priority, char* name); void taskDelete(Task* task); int taskCompare(void* left, void* right); void taskPrint(void* value);

#endif /* TASK_H */

Please help with the handlecommand function in the following file:

#include "dynamicArray.h" #include "task.h" #include #include #include

/** * Loads into heap a list from a file with lines formatted like * "priority, name". * @param heap * @param file */ void listLoad(DynamicArray* heap, FILE* file) { const int FORMAT_LENGTH = 256; char format[FORMAT_LENGTH]; snprintf(format, FORMAT_LENGTH, "%%d, %%%d[^ ]", TASK_NAME_SIZE); Task temp; while (fscanf(file, format, &temp.priority, &temp.name) == 2) { dyHeapAdd(heap, taskNew(temp.priority, temp.name), taskCompare); } }

/** * Writes to a file all tasks in heap with lines formatted like * "priority, name". * @param heap * @param file */ void listSave(DynamicArray* heap, FILE* file) { for (int i = 0; i < dySize(heap); i++) { Task* task = dyGet(heap, i); fprintf(file, "%d, %s ", task->priority, task->name); } }

/** * Prints every task in heap. * @param heap */ void listPrint(DynamicArray* heap) { DynamicArray* temp = dyNew(1); dyCopy(heap, temp); while (dySize(temp) > 0) { Task* task = dyHeapGetMin(temp); printf(" "); taskPrint(task); printf(" "); dyHeapRemoveMin(temp, taskCompare); } dyDelete(temp); }

/** * Handles the given command. * @param list * @param command */ void handleCommand(DynamicArray* list, char command) { // FIXME: Implement }

int main() { // Implement printf(" ** TO-DO LIST APPLICATION ** "); DynamicArray* list = dyNew(8); char command = ' '; do { printf("Press: " "'l' to load to-do list from a file " "'s' to save to-do list to a file " "'a' to add a new task " "'g' to get the first task " "'r' to remove the first task " "'p' to print the list " "'e' to exit the program " ); command = getchar(); // Eat newlines while (getchar() != ' '); handleCommand(list, command); } while (command != 'e'); /* free dynamically allocated List pointers in array to avoid memory leaks */ /* Fix it */

dyDelete(list); return 0; }

Instructions:

In this part of the assignment you will implement a to-do list application with a heap-based priority queue. This interactive application allows the user to manage a prioritized to-do list by adding new tasks, viewing the highest priority task, removing the highest priority task, saving the list to a file, and loading the list from a file. When ran, the to-do list must prompt 7 options to the user: l to load the list from a file (function implemented for you). s to save the list to a file (function implemented for you). a to add a new task to the list. g to get the first task from the list. r to remove the first task from the list. p to print the list (function implemented for you). e to exit the program. Once the user picks an option, the program should carry out the operation and then present the user with the 7 options again. This should continue until the user selects e to exit the program. You must implement the option-handling function with the // FIXME: implement comment in toDo.c to complete the application logic. You must also implement some functions in task.c , including a compare function, to complete the Task type interface.

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

More Books

Students also viewed these Databases questions

Question

Solve the integral:

Answered: 1 week ago

Question

What is meant by Non-programmed decision?

Answered: 1 week ago

Question

What are the different techniques used in decision making?

Answered: 1 week ago