Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Program in C language. Add comments to code as well. Objectives: - Structures and Structure Pointers - Strings - Linked Lists - Function Pointers -

Program in C language. Add comments to code as well.

Objectives:

- Structures and Structure Pointers

- Strings

- Linked Lists

- Function Pointers

- Passing Command Line Arguments to main()

- Header files and Make files

AN AUTOMATIC GRADE OF ZERO. No exceptions will be made for this rule to achieve even a single point on a lab, your code must minimally build (compile to an executable) on stdlinux and execute on stdlinux without crashing, using the followingcommand:

% make

LAB DESCRIPTION:

PART 1. BOOKSTORE INVENTORY SYSTEM:

Mandatory file names: 1) lab3main.c will hold main()

2) lab3.h will hold any local typedefs or struct definitions, function prototypes, etc. NO VARIABLES can be declared here.

3) you must have an additional file name for every function that you write. (e.g. insert_node.c, delete_node.c, etc.)

You will write a program to store and process data in order to manage inventory and to keep business records for a bookstore which deals in (mostly) classic book titles.

The program will execute in the following format:

lab3 filename1 filename2

where lab3 is the name of the executable

filename1 is the data file that contains the inventory to input

filename2 is a data file that your program will create with the output inventory

This means that you will have to read two parameters from the command line.

You will be supplied with a test inventory file called book_inventory. All of the data for the book titles to be stored in the inventory system will be in this file. Once the program has read in the book data, a user will be able to select options related to what to do with the data. Youdo not know the number of different book titles which will be in the file, so your code must be able to deal with an indeterminate number of different books (up to the limits of memory). First, your program should open the file specified by the filename1 parameter and read in the initial inventory. There will be options for adding or deleting books provided to the user later by the program after the initial data is read in and used to build the linked list. The data will be entered in the following format, with each item of data listed entered on a different line:

- Book title (We will assume the title may possibly contain white spaces; check the initial book_inventory file on Piazza)

- Author's name (We will assume there will be a single author line for each book, with the name(s) possibly containing white spaces; see the sample data posted on Piazza)

- Book stock number (1 - 10000) - Wholesale price of the book in dollars (a floating point value)

- Retail price of the book in dollars (a floating point value) - Wholesale quantity purchased by bookstore (a whole number greater than or equal to zero)

- Retail quantity purchased by customers (a whole number greater than or equal to zero)

Data about each book will not be separated from the data for the following book; that is, the first line of input for the next book will immediately follow the book which precedes it on the following line. The end of the input for the books will be indicated when an fscanf() function returns EOF. There will be data about at least one book in the file. The linked list that your program builds for the books should store them in order of increasing book stock number; the books will not necessarily appear in the input in this order. After reading the input data about the books, and constructing the linked list, your program should

a) Tell the user that the inventory of books has been read in from the file and how many book titles were read in.

b) Prompt the user to select from the following options for processing of the data. (REQUIRED: Use an Array of Function Pointers to call the Users Choice of Function for options 1 through 8.

1. Determine and print total revenue (from all books): This is the sum of (retail price * retail quantity) for all books;

2. Determine and print total wholesale cost (for all books):This is the sum of (wholesale price * wholesale quantity) for all books;

3. Determine and print the current investment in books: This is the sum of (wholesale price * (wholesale quantity retail quantity));

4. Determine and print total profit (for all books): This is total revenue (#1) minus total wholesale cost (#2) plus cost of current inventory (#3);

5. Determine and print total number of sales (total number of books sold): This is the sum of the retail quantities for all books;

6. Determine and print average profit per sale: This is total profit (#4) divided by total number of sales (#5);

7. Print books in stock: This function should print each book on Book list where (Wholesale quantity Retail quantity >0). The output would be a Title Line that says something like Books in Stock then on subsequent lines, the number of books currently in stock (Wholesale Retail) followed by a tab character then the book stock number followed by a tab character, then the author of the book followed by the tab character then the title of the book;

8. Print books out of stock: This function should print each book on Book list where (Wholesale quantity Retail quantity == 0). The output would be a Title Line that says something like Books out of Stock then on subsequent lines the book stock number followed by a tab character, then the author of the book followed by the tab character then the title of the book;

9. Add a single book (prompt the user to enter the book data for a single book, in the format given above from the keyboard;

10.Delete book (prompt the user to enter a book stock number to delete from inventory: if the book is not found in the linked list, print a message indicating an error, or if the list is empty, print an error indicating that);

11.Exit the program. (This option would write the current inventory in the linked list out to disk using filename2 from the command line and would also free all dynamically allocated memory.)

The user will enter a choice of one of these eleven options by entering 1 - 11 immediately followed by enter (new line). You can assume that all user input will be correct, except that the user may inadvertently attempt to delete a book which has not been added to the list of books. You should write a separate function to do the necessary computations and print output for each of these options. Some of these functions may call some of the other functions. The goal is to make main() as small and succinct as possible, while creating functions that make it as easy as possible to monitor, modify and execute the code. You should also write a separate function to read the data for a single book from stdin into a node after allocating storage for the node.

Be sure, for each of the functions above, which is required to print output, to print a label which describes the output, along with the appropriate output on the same line (except for the function to print the books in stock list or books out of stock list, where each title/author pair should be printed on separate line).

GUIDANCE:

Declare the struct Node shown below at file scope:

struct Data {

char title[100];

char author[50];

int stockNumber;

float wholesalePrice;

float retailPrice;

int wholesaleQuantity;

int retailQuantity;

};

typedef struct Node {

struct Data book;

struct Node *next;

} Node;

You should write and debug the following functions first:

main (adding items as needed)

the program to read in the inventory data from disk

the functions to print the nodes in the list (i.e. in-stock list and out-of-stock list); find and carefully follow the sketch of the algorithm in the class slides on linked lists.

a function insert, to add a node to the list; find and carefully follow the sketch of the algorithm in the class slides.

DO NOT WRITE THE CODE FOR OTHER FUNCTIONS UNTIL THE FUNCTIONS ABOVE ARE WORKING!!!

If the output for either print statement is empty (e.g. the print out-of-stock list has no books to print, you should print a statement to that effect. Similarly, if you are asked to delete a book with a stock number that is not found in the list, print a statement that says that.

Until you are ready to write the code for the other functions, you can write stub functions with empty parameters and empty blocks for the other functions in the program; if these functions are called in the program, they will do nothing, so this will create no problems for testing and debugging. For example, a stub function for delete_node() might look like this:

void delete_node(){

return;

}

After the functions above are working, write a function called delete, to delete a single node from the list; find and carefully follow the sketch of the algorithm in the class slides.

Then, write the remaining functions (they are similar to the function to print in or out of stock books in the list.)

CONSTRAINTS :

The book data must be stored in a singly-linked list, where each node in the list contains the data for one book title. The head of the list cannot be a NODE, it must be a list head (e.g.NODE *)

You are not permitted to declare any variables at file scope; you should, however, declare the Node type used to store the data for the linked list at file scope (but no variables can be declared as part of this declaration). See the description of the declaration of the Node type above.

All floating point data will be entered with two digits of precision and should also be output with two digits of precision and using monetary symbols and commas where appropriate. You must allocate the storage for each book node structure dynamically.

The book name and author's name should each be stored in strings declared to be of type array of char of size 100 and 50, respectively. (See the description of the declaration of the Node type above.)

If a book is deleted, you should call free() to free the storage for the node for the book.

You must write the inventory list to disk (filename2) and then free the space for the individual nodes that still contain data before the program terminates when the user selects option 11. See the text file posted on Piazza with sample input for Part 1.

You must create a Makefile that creates your executable lab3. Insure that file contains all appropriate dependencies

Link to book_inventory ---> https://piazza-resources.s3.amazonaws.com/jaa8tbynz0s1yp/jdbzubwrmeq2yj/book_inventory?AWSAccessKeyId=AKIAIEDNRLJ4AZKBW6HA&Expires=1518769514&Signature=fe8dlVt70NUXFlClii%2BxFugP3PI%3D

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions