Question: #include #include #include typedef struct ProductDescription { char productName[25]; float productPrice; float productWeight; } Product; void CreateProductList(Product **productList, int size) { *productList = (Product *)

#include  #include  #include  typedef struct ProductDescription { char productName[25]; float productPrice; float productWeight; } Product; void CreateProductList(Product **productList, int size) { *productList = (Product *) malloc(sizeof(Product) * size); } void ResizeProductList(Product **productList, int newSize) { *productList = (Product *) realloc(*productList, newSize); } Product SetProductInfo() { Product product; printf(" Please Enter the Product Name: "); scanf(" %[^ ]s", product.productName); printf(" Please Enter the Product Price: "); scanf(" %f", &product.productPrice); printf(" Please Enter the Product Weight: "); scanf(" %f", &product.productWeight); return product; } int CompareProduct(Product a, Product b, char c) { int result; switch (c) { case 'p': result = a.productPrice > b.productPrice; break; case 'w': result = a.productWeight > b.productWeight; break; case 'n': result = strcmp(a.productName, b.productName); break; default: result = 0; } return result; } void SortProductList(Product *productList, int size, char sortOption) { Product temp; for (int i = 0; i < size - 1; i++) { for (int j = i + 1; j < size; j++) { if (CompareProduct(*(productList + i), *(productList + j), sortOption) > 0) { temp.productPrice = (productList + i)->productPrice; temp.productWeight = (productList + i)->productWeight; strcpy(temp.productName, (productList + i)->productName); (productList + i)->productPrice = (productList + j)->productPrice; (productList + i)->productWeight = (productList + j)->productWeight; strcpy((productList + i)->productName, (productList + j)->productName); (productList + j)->productPrice = temp.productPrice; (productList + j)->productWeight = temp.productWeight; strcpy((productList + j)->productName, temp.productName); } } } } void AddProductToProductList(Product *productList, int size) { for (int i = 0; i < size; i++) { printf(" Please enter product info for product %d: ", i); *(productList + i) = SetProductInfo(); } } void PrintProductList(Product *productList, int size) { for (int i = 0; i < size; i++) { printf(" Product ID: %d Name: %s", i, (productList + i)->productName); printf(" Product ID: %d Price: %f", i, (productList + i)->productPrice); printf(" Product ID: %d Weight: %f", i, (productList + i)->productWeight); printf(" ---------------------------------------------------------------- "); } } int main() { printf(" "); Product *list; int listSize; char sortOption; char resize; printf("Please Enter the size of the list: "); scanf("%d", &listSize); CreateProductList(&list, listSize); AddProductToProductList(list, listSize); PrintProductList(list, listSize); printf(" To sort the product list press: 'p' to sort by price, or 'w' to sort by weight, or 'n' to sort by name : "); scanf(" %c", &sortOption); SortProductList(list, listSize, sortOption); PrintProductList(list, listSize); printf(" To resize the product list press 'y' or any other key to escape: "); scanf(" %c", &resize); if (resize == 'y' || resize == 'Y') { printf("Please Enter the new size of the list: "); scanf("%d", &listSize); ResizeProductList(&list, listSize); PrintProductList(list, listSize); } return 0; }

Extend the C program from the code above that allows the user to create and sort a list of products. Each product is a C Structure that contains the product name, the product price, and the product weight and implements the following functions and additional requirements

1. Rather than Storing the product list into an array store the product list in a dynamic linked list

2. Implement a function named SaveListIntoFile(*List list, char *filename). This function allows the user to store the data of product list into a file. The filename is chosen by the user.

3. Implement a function named LoadListFromFile(*List list, char *filename). This function allows the user to load the data of product list into from an input file. The filename is chosen by the user.

4. Extend the main function to implement menu options that allow the user to interact with the program, here are the required options:

a. Add a new Product

b. Remove Product

c. Display Product List

d. Save Product List

e. Load Product List

f. Delete Product List

g. Exit

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!