Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ve to the this vehicle
- *@param toTheRight Checks if this vehicle is to the right of main vehicle
- **/
- int isToTheRight(Vehicle *main, Vehicle *toTheRight);
- /**Function determines if a vehicle B is in front of vehicle A
- *@return True of false value
- *@pre Both vehicles must exist, and contian direction value.
- *@param main In front relative to the this vehicle
- *@param toTheRight Checks if this vehicle is in front of the main vehicle
- **/
- int isInFront(Vehicle *main, Vehicle *inFront);
- /**Determines the enum direction from a character
- *@return Returns the direction associated with the character
- *@param i Char in which you want the associated direction of
- **/
- Direction getDirection(char i);
- /**Determines the enum turn from a character
- *@return Returns the turn associated with the character
- *@param i Char in which you want the associated turn of
- **/
- Turn getTurn(char i);
- /**Gets the direction list associated with the vehicle
- *@return direction list associated with the vehicle
- *@pre Vehicle must exist, and contian a direciton value.
- *@pre All lists must exist.
- *@param vehicle A vehicle to get the queue of
- *@param north List that repersents the north queue
- *@param south List that repersents the south queue
- *@param east List that repersents the east queue
- *@param west List that repersents the west queue
- **/
- TrafficQueue *getVehiclesQ(Vehicle *vehicle, TrafficQueue *north, TrafficQueue *south, TrafficQueue *east, TrafficQueue *west);
- /**Determines the right of way for two vehicles head on
- *@return Vehicle that has the right of way
- *@pre Vehicle must exist, and contian direction values.
- *@param aQueue Any traffic queue
- *@param bQueue Any traffic queue
- **/
- TrafficQueue *headOnRightOfWay(TrafficQueue *aQueue, TrafficQueue *bQueue);
- /**Determines the lowest wait time of all four vehicles
- *@return The lowest wait time value (returns 0 if all vehicles do not exist)
- *@param a A vehicle
- *@param b A vehicle
- *@param c A vehicle
- *@param d A vehicle
- **/
- double getLowestWait(Vehicle *a, Vehicle *b, Vehicle *c, Vehicle *d);
- /**
- *Main function which simulates a four stop
- **/
- int main(int argc, char *argv[])
- {
- FILE *file;
- Vehicle *vehicles;
- int i;
- int numOfVechicles = 0;
- double tick;
- double averageWait = 0;
- double maxWait = 0;
- double interClear = 0;
- char line[MAX];
- Node *tempNode;
- if (argc != 2) {
- printf("usage: %s filename\n", argv[0]);
- return 0;
- }
- file = fopen( argv[1], "r" );
- if (!file) {
- printf("Could not find file!\n");
- return 0;
- }
- vehicles = malloc(sizeof (Vehicle));
- while (fgets(line, sizeof(line), file)) {
- numOfVechicles ++;
- void *tmp = realloc (vehicles, (numOfVechicles) * sizeof (Vehicle));
- if (tmp == NULL) {
- free(vehicles);
- } else {
- int j = numOfVechicles - 1;
- char *text = strtok(line, " ");
- vehicles = tmp;
- vehicles[j].id = numOfVechicles;
- for (i = 0; i < 3; i++) {
- switch (i) {
- case 0:
- vehicles[j].direction = getDirection(text[0]);
- break;
- case 1:
- vehicles[j].turn = getTurn(text[0]);
- break;
- case 2:
- vehicles[j].waitTime = vehicles[j].arrivalTime = atof(text);
- break;
- }
- text = strtok(NULL, " ");
- }
- }
- }
- List *final = initializeList(printVehicle, NULL, NULL);
- TrafficQueue *north = initializeTrafficQueue();
- TrafficQueue *south = initializeTrafficQueue();
- TrafficQueue *east = initializeTrafficQueue();
- TrafficQueue *west = initializeTrafficQueue();
- for (i = 0; i < numOfVechicles; i ++) {
- insertSorted(getVehiclesQ(&vehicles[i], north, south, east, west)->trafficData, &vehicles[i]);
- }
- for(tick = 0;;tick += 0.5) {
- int rightOfWayCount = 0;
- fillQueue(tick, north);
- fillQueue(tick, south);
- fillQueue(tick, east);
- fillQueue(tick, west);
- if (isTrafficQueueEmpty(north)
- && isTrafficQueueEmpty(south)
- && isTrafficQueueEmpty(east)
- && isTrafficQueueEmpty(west)) {
- break;
- }
- if (tick < interClear) continue;
- double lowWait = getLowestWait(north->headVehicle, south->headVehicle, east->headVehicle, west->headVehicle);
- rightOfWayCount += verifiyHeadVehicle(north, lowWait, tick);
- rightOfWayCount += verifiyHeadVehicle(south, lowWait, tick);
- rightOfWayCount += verifiyHeadVehicle(east, lowWait, tick);
- rightOfWayCount += verifiyHeadVehicle(west, lowWait, tick);
- TrafficQueue *rightOfWayQueue = NULL;
- switch (rightOfWayCount) {
- case 4:
- rightOfWayQueue = north;
- break;
- case 3:
- if (north->headVehicle == NULL) {
- rightOfWayQueue = east;
- } else if (east->headVehicle == NULL) {
- rightOfWayQueue = south;
- } else if (south->headVehicle== NULL) {
- rightOfWayQueue = west;
- } else {
- rightOfWayQueue = north;
- }
- break;
- case 2:
- if (isToTheRight(north->headVehicle, west->headVehicle)) {
- rightOfWayQueue = west;
- } else if (isToTheRight(west->headVehicle, south->headVehicle)) {
- rightOfWayQueue = south;
- } else if (isToTheRight(south->headVehicle, east->headVehicle)) {
- rightOfWayQueue = east;
- } else if (isToTheRight(east->headVehicle, north->headVehicle)) {
- rightOfWayQueue = north;
- } else if (isInFront(south->headVehicle, north->headVehicle)) {
- rightOfWayQueue = headOnRightOfWay(south, north);
- } else {
- rightOfWayQueue = headOnRightOfWay(east, west);
- }
- break;
- case 1:
- if (north->headVehicle != NULL) {
- rightOfWayQueue = north;
- } else if (south->headVehicle != NULL) {
- rightOfWayQueue = south;
- } else if (east->headVehicle != NULL) {
- rightOfWayQueue = east;
- } else {
- rightOfWayQueue = west;
- }
- break;
- default:
- break;
- }
- if (rightOfWayQueue != NULL) {
- insertBack(final, rightOfWayQueue->headVehicle);
- interClear = tick + getTurnTime(rightOfWayQueue->headVehicle->turn);
- averageWait += moveVehicleIntoIntersection(rightOfWayQueue, tick);
- if (maxWait < rightOfWayQueue->maxWaitTime) {
- maxWait = rightOfWayQueue->maxWaitTime;
- }
- }
- }
- fclose(file);
- file = fopen("report.txt", "w+");
- fprintf(file, "Four Way Stop Siumlator\n");
- fprintf(file, "* Assumptions are found in the readme file\n\n");
- printf("Four Way Stop Siumlator\n");
- printf("* Assumptions are found in the readme file\n\n");
- printf("Order of Vehicles:\n");
- printf("Vehicle ID\tDirection \tTurn \tArrival Time \tDepature Time\n");
- fprintf(file, "\t\tDirection \t\tTurn \tArrival Time\n");
- printForward(final);
- tempNode = final->head;
- i = 0;
- while (tempNode != NULL) {
- Vehicle *tempVehicle = (Vehicle *)tempNode->data;
- i++;
- fprintf(file, "%d.\t\t%c \t\t\t\t%c \t\t%.1lf\n", i, tempVehicle->direction, tempVehicle->turn, tempVehicle->arrivalTime);
- tempNode = tempNode->next;
- }
- averageWait /= (double)numOfVechicles;
- printf("Average Wait Time: %.1lf second(s)\n", averageWait);
- fprintf(file, "\n\nAverage Wait Time: %.1lf second(s)\n", averageWait);
- printf("Max Wait Time: %.1lf second(s)\n\n", maxWait);
- fprintf(file, "Max Wait Time: %.1lf second(s)\n\n", maxWait);
- printf("North Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(north));
- fprintf(file, "North Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(north));
- printf("South Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(south));
- fprintf(file, "South Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(south));
- printf("East Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(east));
- fprintf(file, "East Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(east));
- printf("West Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(west));
- fprintf(file, "West Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(west));
- deleteList(final);
- deleteTrafficQueue(north);
- deleteTrafficQueue(south);
- deleteTrafficQueue(east);
- deleteTrafficQueue(west);
- free(vehicles);
- fclose(file);
- return 0;
- }
- double getLowestWait(Vehicle *a, Vehicle *b, Vehicle *c, Vehicle *d)
- {
- Vehicle *lowVehicle = a;
- if (lowVehicle == NULL || (b != NULL && b->waitTime < lowVehicle->waitTime)) {
- lowVehicle = b;
- }
- if (lowVehicle == NULL || (c != NULL && c->waitTime < lowVehicle->waitTime)) {
- lowVehicle = c;
- }
- if (lowVehicle == NULL || (d != NULL && d->waitTime < lowVehicle->waitTime)) {
- lowVehicle = d;
- }
- return lowVehicle != NULL ? lowVehicle->waitTime : 0;
- }
- TrafficQueue *headOnRightOfWay(TrafficQueue *aQueue, TrafficQueue *bQueue)
- {
- if (aQueue->headVehicle->turn == FORWARD) {
- return aQueue;
- } else if (bQueue->headVehicle->turn == FORWARD) {
- return bQueue;
- } else if (aQueue->headVehicle->turn == RIGHT) {
- return aQueue;
- } else if (bQueue->headVehicle->turn == RIGHT) {
- return bQueue;
- } else {
- return aQueue;
- }
- }
- TrafficQueue *getVehiclesQ(Vehicle *vehicle, TrafficQueue *north, TrafficQueue *south, TrafficQueue *east, TrafficQueue *west)
- {
- switch (vehicle->direction) {
- case NORTH:
- return north;
- break;
- case SOUTH:
- return south;
- break;
- case EAST:
- return east;
- break;
- default:
- return west;
- break;
- }
- }
- int isToTheRight(Vehicle *main, Vehicle *toTheRight)
- {
- if (main == NULL || toTheRight == NULL) {
- return FALSE;
- }
- switch (main->direction) {
- case NORTH:
- if (toTheRight->direction == WEST) {
- return TRUE;
- }
- case SOUTH:
- if (toTheRight->direction == EAST) {
- return TRUE;
- }
- case EAST:
- if (toTheRight->direction == NORTH) {
- return TRUE;
- }
- case WEST:
- if (toTheRight->direction == SOUTH) {
- return TRUE;
- }
- }
- return FALSE;
- }
- int isInFront(Vehicle *main, Vehicle *inFront)
- {
- if (main == NULL || inFront == NULL) {
- return FALSE;
- }
- switch (main->direction) {
- case NORTH:
- if (inFront->direction == SOUTH) {
- return TRUE;
- }
- case SOUTH:
- if (inFront->direction == NORTH) {
- return TRUE;
- }
- case EAST:
- if (inFront->direction == WEST) {
- return TRUE;
- }
- case WEST:
- if (inFront->direction == EAST) {
- return TRUE;
- }
- }
- return FALSE;
- }
- Direction getDirection(char i) {
- switch (i) {
- case 'N':
- return NORTH;
- case 'E':
- return EAST;
- case 'S':
- return SOUTH;
- default:
- return WEST;
- }
- }
- Turn getTurn(char i) {
- switch (i) {
- case 'F':
- return FORWARD;
- case 'R':
- return RIGHT;
- default:
- return LEFT;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement