Você está na página 1de 7

/*******************************************************************************

****
cs1713p3.c by Jh'on West
Purpose:
The program reads inventory information and a command file. It
processes the commands against the inventory.
This file contains functions:
getInventory - reads and returns the inventory and the count of inventor
y
(most of this is in program #2)
printInventory - prints the inventory (most of this is in program #2)
sortInventory - sorts the inventory using a bubble sort
searchInventory - find a Stock Number in the sorted Inventory array usin
g
a binary search algorithm.

You will use this in your ro

utines.
processOrderCommand - processes the various ORDER commands
(most of this is in program #2)
processInventoryCommand - processes the various INVENTORY commands.
Input:
Inventory
:

Stream input file which contains many records of stock inventory


szStockNumber
6s

lStockQty
8ld

dUnitPrice
10.2f

szStockName
30s (may contain spaces

)
Command
ntains

This is different from the previous assignment. The file co


data in the form of commands (one command per data line):
ORDER BEGIN email,full name
specifies the beginning of an order. It also has the email
and full name of the customer (separated by commas)
ORDER ADDRESS szStreetAddress,szCity,szStateCd,szZipCd
specifies the address for an order (separated by commas)
ORDER ITEM szStockNumber,iRequestQty
specifies a purchase item. The information will be separate

d by commas.
Lookup the stock number. If not found, print an error. If
found, try to
satisfy the enire quantity with the inventory. If not enoug
h in stock,
show a message that the entire quantity is backordered.
If it was satisifed, show the unit price and its total cost.
ORDER COMPLETE
specifies the completion of an order (i.e., marks the end of
the purchase items).
Print the totals for the order.
INVENTORY RECEIVE szStockNumber,quantity
increase the inventory quantity for the specified inventory
item
INVENTORY SHOW szStockNumber
requests a display of an inventory item. Show all of its in
formation.
Results:
Prints the Inventory prior to sorting
Prints the Inventory after sorting.
Processes the commands (see above) and shows any errors.
Prints the resulting Inventory
Returns:
0 normal

-1 invalid command line syntax


-2 show usage only
-3 error during processing, see stderr for more information
Notes:
p3 -?

will provide the usage information. In some shells,


you will have to type p3 -\?
**********************************************************************/
// If compiling using visual studio, tell the compiler not to give its warnings
// about the safety of scanf and printf
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cs1713p3.h"
/******************** getInventory **************************************
int getInventory(Stock stockM[], char * pszInventoryFileName)
Purpose: Populates the stockM[] array with all of the stock that it reads from t
he file
that is given to the program
Parameters: (Stock stockM[], char * pszInventoryFileName)
Returns: returns a number that represents how many items are in the array
Notes:
**************************************************************************/
int getInventory(Stock stockM[], char * pszInventoryFileName)
{
char szInputBuffer[100];
// input buffer for reading data
int i = 0;
// subscript in stockM
int iScanfCnt;
// returned by sscanf
FILE *pfileInventory;
// Stream Input for Inventory data.
char *pszGet;
/* open the Inventory stream data file */
if (pszInventoryFileName == NULL)
exitError(ERR_MISSING_SWITCH, "-i");
pfileInventory = fopen(pszInventoryFileName, "r");
if (pfileInventory == NULL)
exitError(ERR_INVENTORY_FILENAME, pszInventoryFileName);
pszGet = fgets(szInputBuffer, 100, pfileInventory);
/* get inventory data until EOF
** fgets returns null when EOF is reached.
*/
while (pszGet != NULL)
{
iScanfCnt = sscanf(szInputBuffer, "%s %ld %lf %[^\n]\n",
stockM[i].szStockNumber,
&stockM[i].lStockQty,
&stockM[i].dUnitPrice,
stockM[i].szStockName);
if (iScanfCnt < 2)
exitError("Bad Inventory Data", szInputBuffer);
i++;
pszGet = fgets(szInputBuffer, 100, pfileInventory);

}
return i;
}
/******************** sortInventory **************************************
void sortInventory(Stock stockM[], int iStockCnt)
Purpose: Sorts the inventory by stock numbers from least to greatest
Parameters: (Stock stockM[], int iStockCnt)
Returns: returns a sorted inventory
Notes:
**************************************************************************/
void sortInventory(Stock stockM[], int iStockCnt)
{
int i, j;
int bChange = TRUE;
Stock swap;
// iterates through the stockM array
for (i = 0; i < iStockCnt - 1; i++)
{
bChange = FALSE;
for (j = i+1; j < iStockCnt; j++)
{
// swaps stock items into appropriate order
if(strcmp(stockM[i].szStockNumber,stockM[j].szStockNumber) > 0)
{
bChange = TRUE;
swap = stockM[i];
stockM[i] = stockM[j];
stockM[j] = swap;
}
}
if(!bChange)
break;
}
}
/******************** printInventory **************************************
void printInventory(char *pszHeading, Stock stockM[], int iStockCnt)
Purpose: Prints out inventory information such as stock number, price per unit,
stock name, and currenty inventory available
Parameters:
I char *pszHeading
I/o Stock stockM[]
Returns:
Notes:
**************************************************************************/
void printInventory(char *pszHeading, Stock stockM[], int iStockCnt)
{
int i;
printf("%s\n", pszHeading);
printf("
%-10s %-30s %-8s %-10s\n", "Stock #", "Stock Name", " Count",
"Unit Price");
for (i = 0; i < iStockCnt; i++)

{
printf("
%-10s %-30s %8ld %10.2lf\n"
, stockM[i].szStockNumber
, stockM[i].szStockName
, stockM[i].lStockQty
, stockM[i].dUnitPrice);
}
}
/********************processOrderCommand *****************************
void processOrderCommand(Stock stockM[], int iStockCnt
, char *pszSubCommand, char *pszRemainingInput
, Customer *pCustomer, double *pdOrderTotalCost)
Purpose:
Processes the subcommands associated with the ORDER command:
ORDER BEGIN email,full name
specifies the beginning of an order. It also has the email
and full name of the customer (separated by commas)
ORDER ADDRESS szStreetAddress,szCity,szStateCd,szZipCd
specifies the address for an order (separated by commas)
ORDER ITEM szStockNumber,iRequestQty
specifies a purchase item. The information will be separated by c
ommas.
Lookup the stock number. If not found, print an error. If found,
try to
satisfy the enire quantity with the inventory. If not enough in s
tock,
show a message that the entire quantity is backordered.
If it was satisifed, show the unit price and its total cost.
ORDER COMPLETE
specifies the completion of an order (i.e., marks the end of the p
urchase items).
Print the totals for the order.
Parameters:
I/O Stock stockM[]
Array of stock
I int iStockCnt
Number of elments in stockM[]
I char *pszSubCommand
Should be BEGIN, ADDRESS, ITEM or COMPLETE
I char *pzRemainingInput
Points to the remaining characters in the
input
line (i.e., the characters that following
the
subcommand).
I/O Customer *pCustomer
The BEGIN subcommand begins a new order.
The
customer's Order Total Cost must be set to
0.
I/O double *pdOrderTotalCost
The order total cost. This changes depend
ing
on the subcommand:
BEGIN - set to 0
ITEM - add the cost (unless there is
a warning)
Notes:
**************************************************************************/
void processOrderCommand(Stock stockM[], int iStockCnt
, char *pszSubCommand, char *pszRemainingInput
, Customer *pCustomer, double *pdOrderTotalCost)
{
int iScanfCnt;

PurchaseItem purchaseItem;
int boolean;
// Determine what to do based on the subCommand
if (strcmp(pszSubCommand, "BEGIN") == 0)
{
// get the Customer Identification Information
iScanfCnt = sscanf(pszRemainingInput, " %[^,],%[\n]\n",
pCustomer->szEmailAddr,
pCustomer->szFullName);
*pdOrderTotalCost = 0;
}
else if (strcmp(pszSubCommand, "COMPLETE") == 0)
{
printf("Order Total: %.2lf\n", *pdOrderTotalCost);
}
else if (strcmp(pszSubCommand, "ADDRESS") == 0)
{
// get the postal address
iScanfCnt = sscanf(pszRemainingInput, "%[^,],%[^,],%2s,%s"
, pCustomer->szStreetAddress
, pCustomer->szCity
, pCustomer->szStateCd
, pCustomer->szZipCd);
if (iScanfCnt < 4)
exitError(ERR_ORDER_ADDRESS_DATA, pszRemainingInput);
}
else if (strcmp(pszSubCommand, "ITEM") == 0)
{
int i,g;
// get a purchase item
iScanfCnt = sscanf(pszRemainingInput, " %[^,],%d"
, purchaseItem.szStockNumber
, &purchaseItem.iRequestQty);
if (iScanfCnt < 2)
exitError(ERR_ORDER_ITEM_DATA, pszRemainingInput);
i = search(stockM, iStockCnt, purchaseItem.szStockNumber);
if(i >= 0)
{
if(stockM[i].lStockQty < purchaseItem.iRequestQty)
{
printf("Items will be backordered: insufficient stock.\n");
*pdOrderTotalCost += (stockM[i].dUnitPrice * purchaseItem.iReque
stQty);
}
else
{
printf("Total for %d %s at %.2lf/unit: %.2lf\n"
, purchaseItem.iRequestQty
, stockM[i].szStockName
, stockM[i].dUnitPrice
, (purchaseItem.iRequestQty * stockM[i].dUnitPrice));
stockM[i].lStockQty -= purchaseItem.iRequestQty;
*pdOrderTotalCost += (stockM[i].dUnitPrice * purchaseItem.iReque
stQty);
}
}
else if(i = -1)

printf("Item %s was not found in the inventory\n", purchaseItem.szSt


ockNumber);
}
else printf(" invalid subcommand for ORDER\n");
}
/********************processInventoryCommand *****************************
void processInventoryCommand(Stock stockM[], int iStockCnt
, char *pszSubCommand, char *pszRemainingInput)
Purpose:
Processes the subcommands associated with the INVENTORY command:
INVENTORY RECEIVE szStockNumber,quantity
increase the inventory quantity for the specified inventory item
INVENTORY SHOW szStockNumber
requests a display of an inventory item. Show all of its informatio
n.
Parameters:
I/O Stock
I int
I char
I char
input

stockM[]
iStockCnt
*pszSubCommand
*pzRemainingInput

Array of stock
Number of elments in stockM[]
Should be RECEIVE or SHOW
Points to the remaining characters in the
line (i.e., the characters that following

the
subcommand).
Notes:
**************************************************************************/
void processInventoryCommand(Stock stockM[], int iStockCnt, char *pszSubCommand,
char *pszRemainingInput)
{
Stock inventoryItem;
int iScanfCnt;
iScanfCnt = sscanf(pszRemainingInput, "%s"
, inventoryItem.szStockName);
if (iScanfCnt < 2)
{
printf("Error reading incoming stock information.");
}
if (strcmp(pszSubCommand, "RECEIVE") == 0)
{
int k = search(stockM, iStockCnt, inventoryItem.szStockNumber);
stockM[k].lStockQty += inventoryItem.lStockQty;
}
else if (strcmp(pszSubCommand, "SHOW") == 0)
{
int i = search(stockM, iStockCnt, inventoryItem.szStockNumber);
printf("%-8s %-10ld $%-11.2lf %s\n"
, stockM[i].szStockNumber
, stockM[i].lStockQty
, stockM[i].dUnitPrice
, stockM[i].szStockName);
}
else
printf("Invalid Inventory subcommand.");
}
/******************** search *****************************
int search(Stock stockM[], int iStockCnt, char *pszMatchStockNumber)

Purpose:
Parameters:
I Stock stockM[]
Array of stock
I int iStockCnt
Number of elments in stockM[]
I char *pszMatchStockNumber
Stock number to find in the array
Returns:
>= 0 subscript of where the match value was found
-1
not found
Notes:
**************************************************************************/
int search(Stock stockM[], int iStockCnt, char *pszMatchStockNumber)
{
int i;
// loop that iterates through the array of stock items
for (i = 0; i < iStockCnt; i++)
{
// compares the stock number of the array element to the search item
if (strcmp(stockM[i].szStockNumber, pszMatchStockNumber) == 0)
{
return i;
}
}
return -1; // not found
}

Você também pode gostar