Grading Log for CS113 -- HW3 Name: Gierymski, Igor SCRIPT FILES ====== ===== h3.scr =================================================== > %cat h3main.c > /* File name: h3main.c > * Name: Igor Gierymski > * Assignment: 3 > * Date: 3/4/99 > * > * This program maintains inventory in a virtual store > */ > > #include > #include > #include "h3invent.h" > > void Add(inventoryT *listP); /* Menu choice 'a' */ > > void Remove(inventoryT *listP); /* Menu choice 'r' */ > > void Show(inventoryT *listP); /* Menu choice 'p' */ > > void Change(inventoryT *listP); /* Menu choice 'c' */ > > int main(void) > { > char choice[4]; > > inventoryT list; > InventoryInitialize(&list); > > if (InventoryReadFromFile(&list, "inventory.dat") == INVENT_FILE_ERROR) > printf("\nInventory.dat file could not be opened.\n"); > else printf("Initial inventory read from: \n~/inventory.dat\n\n"); > > printf("Please choose one of the following operations by typing its\n"); > printf("letter and then pressing :\n\n"); > printf(" a) Add some items.\n"); > printf(" r) Remove (sell) some items.\n"); > printf(" p) Show the price of an item.\n"); > printf(" c) Change the price of an item.\n"); > printf(" q) Quit inventory.\n\n"); > printf("Choice> "); > scanf("%s", choice); > > while (choice[0] != 'q') > { > switch(choice[0]) > { > case 'a': > Add(&list); > break; > case 'r': > Remove(&list); > break; > case 'p': > Show(&list); > break; > case 'c': > Change(&list); > break; > } > > printf("\nChoice> "); /* prompts for next selection */ > scanf("%s", choice); /* scans in choice */ > } > > printf("Bye! Thanks for using inventory.\n"); /* Goodbye message */ > > return 0; > } > > void Add(inventoryT *list) /* * Call parameter listP, since it is something different than "list" * in main(). -TF */ > { > itemT item; > int count; > > printf("\nItem name: "); > scanf("%s", item.name); > > printf("Add quantity: "); > scanf("%d", &item.count); > while (item.count <= 0) { > printf("Please enter a quantity greater than zero: "); > scanf("%d", &item.count); } > > printf("Unit price (in cents): "); > scanf("%d", &item.price); > while (item.price <= 0) { > printf("Please enter a price greater than zero: "); > scanf("%d", &item.price); } > > count = InventoryAddItems(list, item); > > if (count == INVENT_PRICE_MISMATCH) { > printf("\nNew price not same as stored price!\n"); > return; } > > if (count == INVENT_NO_ROOM) { > printf("\nInventory is full! Please remove something before adding.\n"); > return; } > > printf("\nThere are now %d units of \"%s\".\n", count, item.name); > } > > void Remove(inventoryT *list) > { > itemT item; > int count; > > printf("\nItem name: "); > scanf("%s", item.name); > > printf("Sell quantity: "); > scanf("%d", &item.count); > while (item.count <= 0) { > printf("Please enter a quantity greater than zero: "); > scanf("%d", &item.count); } > > count = InventoryRemoveItems(list, item); > > if (count == INVENT_NOT_ENOUGH) { > printf("\nPlease select a lesser quantity.\n"); > return; } > > if (count == INVENT_NO_SUCH_ITEM) { > printf("\n\"%s\" could not be found in inventory.\n", item.name); > return; } > > printf("\nThere are now %d units of \"%s\".\n", count, item.name); > } > > void Show(inventoryT *list) > { > itemT item; > int price; > > printf("\nItem name: "); > scanf("%s", item.name); > > price = InventoryGetPrice(list, item); > > if (price == INVENT_NO_SUCH_ITEM) { > printf("\n\"%s\" could not be found in inventory.\n", item.name); > return; } > > printf("\nA unit of \"%s\" goes for $%.2f.\n", item.name, price / 100.0); > } > > void Change(inventoryT *list) > { > itemT item; > int price, old_price; > > printf("\nItem name: "); > scanf("%s", item.name); > > printf("New unit price (in cents): "); > scanf("%d", &item.price); > while (item.price <= 0) { > printf("Please enter a price greater than zero: "); > scanf("%d", &item.price); } > > old_price = InventoryGetPrice(list, item); /* gets current price */ > price = InventorySetPrice(list, item); /* sets new price */ > > if (price == INVENT_NO_SUCH_ITEM) { > printf("\n\"%s\" could not be found in inventory.\n", item.name); > return; } > > printf("\nPrice changed by $%.2f.\n", (price-old_price) / 100.0); > } > %cat h3invent.h > /* File name: h3invent.h > * Name: Igor Gierymski > * Assignment: 3 > * Date: 3/4/99 > * > * This is the inventory interface file > */ > > #ifndef _h3invent_h > #define _h3invent_h > > #define INVENT_NO_ROOM -1 /* Inventory full */ > #define INVENT_NO_SUCH_ITEM -2 /* Item does not exist*/ > #define INVENT_PRICE_MISMATCH -3 /* Price doesn't match */ > #define INVENT_NOT_ENOUGH -4 /* Not enough items to remove */ > #define INVENT_FILE_ERROR -5 /* Cannot open file */ > > #define MAX_INVENTORY 200 /* 200 items can be inventoried */ > > #define NAME_MAX 20 /* An item's name cannot exceed 20 char */ > > typedef struct { > char name[NAME_MAX+1]; /* One extra for the '\0' character. */ > int count; /* Number of units in stock. */ > int price; /* Cost of a single unit in cents. */ > } itemT; > > typedef struct { > itemT items[MAX_INVENTORY]; /* Stores all the items. */ > int howmany; /* How many items currently in inventory. */ > } inventoryT; > > /* > * Function: GetIndex > * Usage: index = GetIndex(&list, item); > * -------------------------- > * This procedure finds the location of an item in inventory. > */ > int GetIndex(inventoryT *listP, itemT item); /* Prototype module helper functions in the implementation file. -TF */ > > /* > * Function: InventoryInitialize > * Usage: InventoryInitialize(&list); > * -------------------------- > * This procedure initializes an inventory variable so that it > * represents an empty inventory. > */ > void InventoryInitialize(inventoryT *listP); > > /* > * Function: InventoryAddItems > * Usage: count = InventoryAddItems(&list, item); > * ---------------------------- > * This procedure updates the count variable of an item in inventory > * to indicate that items have been added. > */ > int InventoryAddItems(inventoryT *listP, itemT item); > > /* > * Function: InventoryRemoveItems > * Usage: count = InventoryRemoveItems(&list, item); > * ---------------------------- > * This procedure updates the count variable of an item in inventory > * to indicate that items have been removed, and in some cases > * removed completely from inventory. > */ > int InventoryRemoveItems(inventoryT *listP, itemT item); > > /* > * Function: InventoryGetPrice > * Usage: price = InventoryGetPrice(&list, item); > * ---------------------------- > * This procedure returns the price of a given item in inventory. > */ > int InventoryGetPrice(inventoryT *listP, itemT item); > > /* > * Function: InventorySetPrice > * Usage: price = InventorySetPrice(&list, item); > * ---------------------------- > * This procedure updates the price of a given item in inventory > */ > int InventorySetPrice(inventoryT *listP, itemT item); > > /* > * Function: InventoryReadFromFile > * Usage: count = InventoryReadFromFile(&list, "file-name"); > * ---------------------------- > * This procedure reads item data into an inventory struct variable > * from a file. > */ > int InventoryReadFromFile(inventoryT *listP, char filename[]); > > #endif /* not defined _h3invent_h */ > > > %cat h3invent.c > /* File name: h3invent.c > * Name: Igor Gierymski > * Assignment: 3 > * Date: 3/4/99 > * > * This is the inventory implementation file > */ > > #include /* For opening/closing, reading files */ > #include /* For string manipulation */ > > /* > * Get the constants, types and prototypes associated > * with the inventory module. > */ > #include "h3invent.h" > > /* > * Function: GetIndex > * -------------------------- > * We loop through the inventory array and try to match the > * item's name to those items stored in inventory. If a match is > * found index takes on the value of that array location. If no > * match if found index remains negative. > */ > int GetIndex(inventoryT *listP, itemT item); > > /* > * Function: InventoryInitialize > * -------------------------- > * We make the inventory empty by initializing the 'howmany' field > * of the inventory structure to 0. > */ > void InventoryInitialize(inventoryT *listP) > { > listP->howmany = 0; > } > > /* > * Function: InventoryAddItems > * -------------------------- > * I add a number of units of an item to the list. > */ > > int InventoryAddItems(inventoryT *listP, itemT item) > { > int index; > index = GetIndex(listP, item); > > if (index > -1) /* item already in inventory */ > { > if (item.price != listP->items[index].price) > return INVENT_PRICE_MISMATCH; > listP->items[index].count += item.count; /* updates count */ > } > > else /* item new to inventory */ > { > if (listP->howmany == 200) /* is array full? */ > return INVENT_NO_ROOM; > index = listP->howmany; /* location of new item in array */ > listP->howmany++; /* inventory expands by one */ > strcpy(listP->items[index].name, item.name); > listP->items[index].count = item.count; > listP->items[index].price = item.price; /* Just use structure assignment. -TF */ > } > > return listP->items[index].count; /* returns number of items */ > } > > /* > * Function: InventoryRemoveItems > * -------------------------- > * I remove a certain number of units of an item from the list. > */ > > int InventoryRemoveItems(inventoryT *listP, itemT item) > { > int i, index = GetIndex(listP, item); > > if (index == -1) return INVENT_NO_SUCH_ITEM; > > if (item.count > listP->items[index].count) return INVENT_NOT_ENOUGH; > > if (item.count == listP->items[index].count) /* removes item */ > { > listP->howmany--; /* inventory shrinks by one */ > > /* moves all remaining items down one in the inventory array */ > for (i = index; i < listP->howmany; i++) > { > strcpy(listP->items[index].name, listP->items[index+1].name); > listP->items[index].count = listP->items[index+1].count; > listP->items[index].price = listP->items[index+1].price; > } > return 0; > } > > listP->items[index].count -= item.count; /* updates count */ > > return listP->items[index].count; /* returns number of items */ > } > > /* > * Function: InventoryGetPrice > * -------------------------- > * I return the price of an item. > */ > > int InventoryGetPrice(inventoryT *listP, itemT item) > { > int index = GetIndex(listP, item); > > if (index == -1) return INVENT_NO_SUCH_ITEM; > > else return listP->items[index].price; /* returns item's price */ > } > > /* > * Function: InventorySetPrice > * -------------------------- > * I change the price of an item. > */ > > int InventorySetPrice(inventoryT *listP, itemT item) > { > int index = GetIndex(listP, item); > > if (index == -1) return INVENT_NO_SUCH_ITEM; > > listP->items[index].price = item.price; /* updates item's price */ > > return listP->items[index].price; /* returns item's updated price */ > } > > /* > * Function: InventoryReadFromFile > * -------------------------- > * I read in a bunch of inventory records from a file. > */ /* * More generic if you don't assume inventory is empty when read items * into it from file. -TF */ > int InventoryReadFromFile(inventoryT *listP, char filename[]) > { > int index = 0; /* array location variable */ > itemT item; > > FILE *ifp; > > ifp = fopen(filename, "r"); /* opens input file in read only mode */ > > if (ifp == NULL) return INVENT_FILE_ERROR; /* opened successfully? */ > > /* scans items into inventory array line by line */ > while (fscanf(ifp, "%s %d %d", item.name, &item.count, &item.price) == 3) > { > strcpy(listP->items[index].name, item.name); > listP->items[index].count = item.count; > listP->items[index].price = item.price; > index++; > } > > fclose(ifp); /* closes input file */ > > listP->howmany = index; /* sets howmany list struct variable */ > > return listP->howmany; /* returns the no of inventory items read in */ > } > > int GetIndex(inventoryT *listP, itemT item) > { > int i, index = -1; > > /* searches inventory for item */ > for (i = 0; i < listP->howmany; i++) > if (strcmp(item.name, listP->items[i].name) == 0) > index = i; > > return index; > } > > %make -f h3makefile > gcc -ansi -pedantic -Wall -g -c h3main.c > gcc -ansi -pedantic -Wall -g -c h3invent.c > gcc -ansi -pedantic -Wall -g -o h3inventory h3main.o h3invent.o > %h3inventory > Initial inventory read from: > ~/inventory.dat > > Please choose one of the following operations by typing its > letter and then pressing : > > a) Add some items. > r) Remove (sell) some items. > p) Show the price of an item. > c) Change the price of an item. > q) Quit inventory. > > Choice> a > > Item name: Cheerios > Add quantity: -1 > Please enter a quantity greater than zero: 1 > Unit price (in cents): 395 > > There are now 25 units of "Cheerios". > > Choice> a > > Item name: Cheerios > Add quantity: 9 > Unit price (in cents): 9 > > New price not same as stored price! > > Choice> a > > Item name: Twix > Add quantity: 90 > Unit price (in cents): 99 > > There are now 90 units of "Twix". > > Choice> r > > Item name: Twix > Sell quantity: 20 > > There are now 70 units of "Twix". > > Choice> r > > Item name: Twix > Sell quantity: 70 > > There are now 0 units of "Twix". > > Choice> r > > Item name: Twix > Sell quantity: 100 > > "Twix" could not be found in inventory. > > Choice> r > > Item name: Cheerios > Sell quantity: 1000 > > Please select a lesser quantity. > > Choice> p > > Item name: Pinesol > > A unit of "Pinesol" goes for $1.99. > > Choice> p > > Item name: Twix > > "Twix" could not be found in inventory. > > Choice> c > > Item name: Twix > New unit price (in cents): 199 > > "Twix" could not be found in inventory. > > Choice> c > > Item name: Cheerios > New unit price (in cents): 999 > > Price changed by $6.04. > > Choice> p > > Item name: Cheerios > > A unit of "Cheerios" goes for $9.99. > > Choice> q > Bye! Thanks for using inventory.