In C Programming: /*************************************************************
ID: 672362 • Letter: I
Question
In C Programming:
/**********************************************************************
cs2123p3.h
Purpose:
Defines constants:
max constants
error constants
event type constants
boolean constants
Defines typedef for
Token
Person
Event (instead of Element)
For Linked List
NodeLL
LinkedListImp
LinkedList
For the simulation
SimulationImp
Simulation
Protypes
Functions provided by student
Other functions provided by Larry previously (program 2)
Utility functions provided by Larry previously (program 2)
Notes:
**********************************************************************/
/*** constants ***/
// Maximum constants
#define MAX_TOKEN 50 // Maximum number of actual characters for a token
#define MAX_LINE_SIZE 100 // Maximum number of character per input line
// Error constants (program exit values)
#define ERR_COMMAND_LINE 900 // invalid command line argument
#define ERR_ALGORITHM 903 // Error in algorithm - almost anything else
#define ERR_BAD_INPUT 503 // Bad input
// Error Messages
#define ERR_MISSING_SWITCH "missing switch"
#define ERR_EXPECTED_SWITCH "expected switch, found"
#define ERR_MISSING_ARGUMENT "missing argument for"
// Event Constants
#define EVT_ARRIVE 1 // when a person arrives
#define EVT_DEPART 2 // when a person departs the simulation
// exitUsage control
#define USAGE_ONLY 0 // user only requested usage information
#define USAGE_ERR -1 // usage error, show message and usage information
// boolean constants
#define FALSE 0
#define TRUE 1
/*** typedef ***/
// Token typedef used for operators, operands, and parentheses
typedef char Token[MAX_TOKEN + 1];
// Person typedef
typedef struct
{
char szName[16]; // Name
int iDepartUnits; // time units representing how long he/she stays around
} Person;
// Event typedef (aka Element)
typedef struct
{
int iEventType; // The type of event as an integer:
// EVT_ARRIVE - arrival event
// EVT_DEPART - departure event
int iTime; // The time the event will occur
Person person; // The person invokved in the event.
} Event;
// NodeLL typedef - these are the nodes in the linked list
typedef struct NodeLL
{
Event event;
struct NodeLL *pNext; // points to next node in the list
} NodeLL;
// typedefs for the ordered link list
typedef struct
{
NodeLL *pHead; // Points to the head of the ordered list
} LinkedListImp;
typedef LinkedListImp *LinkedList;
// typedefs for the Simulation
typedef struct
{
int iClock; // clock time
LinkedList eventList;
} SimulationImp;
typedef SimulationImp *Simulation;
/********** prototypes ***********/
// linked list functions - you must provide the code for these (see course notes)
int removeLL(LinkedList list, Event *pValue);
NodeLL *insertOrderedLL(LinkedList list, Event value);
NodeLL *searchLL(LinkedList list, int match, NodeLL **ppPrecedes);
LinkedList newLinkedList();
NodeLL *allocateNodeLL(LinkedList list, Event value);
// simulation functions - you must provide code for this
void runSimulation(Simulation simulation, int iTimeLimit);
// functions in most programs, but require modifications
void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo);
// Utility routines provided by Larry (copy from program #2)
void ErrExit(int iexitRC, char szFmt[], ...);
char * getToken(char *pszInputTxt, char szToken[], int iTokenSize);
Explanation / Answer
C code(CS2123.c):
Note: After placing the provided "CS2123.h" code, then use to create the below code:
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "cs2123p3.h"
// Main program
int main(int argc, char *argv[])
{
// Create a simulation's object and time limit
Simulation sim = (Simulation) malloc(sizeof(SimulationImp));
int iTimeLimit = 1000;
sim->eventList = newLinkedList();
runSimulation(sim, iTimeLimit);
free(sim->eventList);
free(sim);
return 0;
}
//Method definition of runSimulation
void runSimulation(Simulation sim, int iTimeLimit)
{
char inputBuffer[100];
char tempToken[15];
int iEvent;
Event event;
sim->iClock = 0;
while((fgets(inputBuffer, 100, stdin) != NULL) && sim->iClock < iTimeLimit)
{
getToken(inputBuffer, event.person.szName, MAX_TOKEN);
getToken(inputBuffer, tempToken, MAX_TOKEN);
event.person.iDepartUnits = atoi(tempToken);
event.iTime = sim->iClock;
event.iEventType = EVT_ARRIVE;
insertOrderedLL(sim->eventList, event);
event.iEventType = EVT_DEPART;
event.iTime = sim->iClock + event.person.iDepartUnits;
insertOrderedLL(sim->eventList, event);
getToken(inputBuffer, tempToken, MAX_TOKEN);
iEvent = atoi(tempToken);
sim->iClock += iEvent;
}
printf("%6s %-12s %-10s ", "Time", "Person", "Event");
NodeLL *p;
for(p = sim->eventList->pHead; p != NULL; p = p->pNext)
{
if((p->event.iEventType) == EVT_ARRIVE)
printf("%6d %-12s %-10s ", p->event.iTime, p->event.person.szName, "Arrive");
if((p->event.iEventType) == EVT_DEPART)
printf("%6d %-12s %-10s ", p->event.iTime, p->event.person.szName, "Depart");
}
printf("%6d %-12s %-10s ", p->event.iTime, "SIMULATION", "TERMINATES");
}
//Method definition of newLinkedList
LinkedList newLinkedList()
{
LinkedList list = (LinkedList) malloc(sizeof(LinkedListImp));
list->pHead = NULL;
return list;
}
//Method definition of searchLL
NodeLL *searchLL(LinkedList list, int match, NodeLL **ppPrecedes)
{
NodeLL *p;
*ppPrecedes = NULL;
for (p = list->pHead; p != NULL; p = p->pNext)
{
if (match == p->event.iTime)
return p;
if (match < p->event.iTime)
return NULL;
*ppPrecedes = p;
}
return NULL;
}
//Method definition of insertOrderedLL
NodeLL *insertOrderedLL(LinkedList list, Event value)
{
NodeLL *pNew, *pFind, *pPrecedes;
pFind = searchLL(list, value.iTime, &pPrecedes);
if (pFind != NULL)
{
pNew = allocateNodeLL(list, value);
pNew->pNext = pFind->pNext;
pFind->pNext = pNew;
return pNew;
}
pNew = allocateNodeLL(list, value);
if (pPrecedes == NULL)
{
pNew->pNext = list->pHead;
list->pHead = pNew;
}
else
{
pNew->pNext = pPrecedes->pNext;
pPrecedes->pNext = pNew;
}
return pNew;
}
//Method definition of allocateNodeLL
NodeLL *allocateNodeLL(LinkedList list, Event value)
{
NodeLL *pNew;
pNew = (NodeLL *)malloc(sizeof(NodeLL));
if (pNew == NULL)
ErrExit(ERR_ALGORITHM, "No available memory for linked list");
pNew->event = value;
pNew->pNext = NULL;
return pNew;
}
//Method definition of removeLL
int removeLL(LinkedList list, Event *pValue)
{
NodeLL *p;
if (list->pHead == NULL)
return FALSE;
*pValue = list->pHead->event;
p = list->pHead;
list->pHead = list->pHead->pNext;
free(p);
return TRUE;
}
//Method definition of ErrExit
void ErrExit(int iexitRC, char szFmt[], ... )
{
va_list args;
va_start(args, szFmt);
printf("ERROR: ");
vprintf(szFmt, args);
va_end(args);
printf(" ");
exit(iexitRC);
}
//Method definition of exitUsage
void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo)
{
switch (iArg)
{
case USAGE_ERR:
fprintf(stderr, "Error: %s %s "
, pszMessage
, pszDiagnosticInfo);
break;
case USAGE_ONLY:
break;
default:
fprintf(stderr, "Error: bad argument #%d. %s %s "
, iArg
, pszMessage
, pszDiagnosticInfo);
}
fprintf(stderr, "p2 -c customerFileName -q queryFileName ");
if (iArg == USAGE_ONLY)
exit(USAGE_ONLY);
else
exit(ERR_COMMAND_LINE);
}
//Method definition of getToken
char * getToken(char *pszInputTxt, char szToken[], int iTokenSize)
{
int iDelimPos;
int iCopy;
char szDelims[20] = " ";
szToken[0] = '';
if (pszInputTxt == NULL)
ErrExit(ERR_ALGORITHM
, "getToken passed a NULL pointer");
if (*pszInputTxt == '')
return NULL;
iDelimPos = strcspn(pszInputTxt, szDelims);
if (iDelimPos == 0)
return NULL;
if (iDelimPos > iTokenSize)
iCopy = iTokenSize;
else
iCopy = iDelimPos;
memcpy(szToken, pszInputTxt, iCopy);
szToken[iCopy] = '';
while(*pszInputTxt == ' ')
{
pszInputTxt += iDelimPos;
}
if (*pszInputTxt == '')
return pszInputTxt;
else
return pszInputTxt + 1;
}