Description
In a computer game each player has an inventory of items she carries around. We can represent each item as follows:
struct inventory_item {
int item_key;
int item_type; /* 0 – unknown, 1- potion, 2- scroll, 3- weapon, 4 -armor, etc. */
char description [15];
float power; /* zero is useless, negative has opposite effect */
int modifier; /* +1, -2, etc. */
};
We need an inventory management system with the following API:
struct inventory *inventory_create();
int inventory_add(struct inventory *, struct inventory_item *);
struct inventory_item *inventory_lookup(struct inventory *, int);
int inventory_delete(struct inventory *, int);
struct inventory_item *inventory_first(struct inventory *);
struct inventory_item *inventory_next(struct Inventory *);
int inventory_destroy(struct inventory *);
A struct inventory refers to the entire inventory. There can be more than one inventory in the game at once. The int arguments refer to the item_key field, used as a unique locator for the item.
The inventory must be implemented as an array. Each inventory array occupies one array slot. Your code must manage the slots. When the Inventory is full inventory_add calls return an error. Each slot of the array should contain a pointer to one of the inventory_item structs above.
All functions returning int should return 0 on success and -1 on error. All functions returning a pointer should return NULL on error. Write a program that tests the database of items. It should read from the command line one of four commands:
> ADD key-data
> LOOK key-data
> DEL key-data
> LIST
> QUIT
The first word of each command must be written exactly as shown. The remaining words are strings and may be of any length. Commands with an unexpected first word, or with an incorrect number of arguments is ignored and a suitable 1-line error message is printed. The ADD command prompts for each field of the item data, one field on each line, and then prints “Data added”. The item_key should be assigned automatically, unique to each item. The LOOK command prints the key-data and the item data fields, one per line, or the message “Data not found” if an item with the key-data is not present. The DEL message prints “Data deleted” unless an item with the key-data is not present, in which case is prints “Data not found”. The LIST command should print the whole inventory, one item per line. The command “QUIT” should end the program. This program must use an array of pointers to C structures that contain the information about each item as its storage medium. The code must consist of the following files:
inventory.c – which contains the abstract data type code. The interface functions must be exactly as described above. The data must be stored in an array.
inventory.h – the interface declarations for the ADT.
lab1.c – which contains the main() function, menu code, commands, user interface, input and output and all other functions not part of the ADT.
All items specified in the ECE223 Programming guide are included as part of this assignment. All code and documents must be turned in by emailing it to assign@assign.ece.clemson.edu with subject line:
ECE223-1,#1