Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.52 KB | None | 0 0
  1. ve to the this vehicle
  2. *@param toTheRight Checks if this vehicle is to the right of main vehicle
  3. **/
  4. int isToTheRight(Vehicle *main, Vehicle *toTheRight);
  5.  
  6. /**Function determines if a vehicle B is in front of vehicle A
  7. *@return True of false value
  8. *@pre Both vehicles must exist, and contian direction value.
  9. *@param main In front relative to the this vehicle
  10. *@param toTheRight Checks if this vehicle is in front of the main vehicle
  11. **/
  12. int isInFront(Vehicle *main, Vehicle *inFront);
  13.  
  14. /**Determines the enum direction from a character
  15. *@return Returns the direction associated with the character
  16. *@param i Char in which you want the associated direction of
  17. **/
  18. Direction getDirection(char i);
  19.  
  20. /**Determines the enum turn from a character
  21. *@return Returns the turn associated with the character
  22. *@param i Char in which you want the associated turn of
  23. **/
  24. Turn getTurn(char i);
  25.  
  26. /**Gets the direction list associated with the vehicle
  27. *@return direction list associated with the vehicle
  28. *@pre Vehicle must exist, and contian a direciton value.
  29. *@pre All lists must exist.
  30. *@param vehicle A vehicle to get the queue of
  31. *@param north List that repersents the north queue
  32. *@param south List that repersents the south queue
  33. *@param east List that repersents the east queue
  34. *@param west List that repersents the west queue
  35. **/
  36. TrafficQueue *getVehiclesQ(Vehicle *vehicle, TrafficQueue *north, TrafficQueue *south, TrafficQueue *east, TrafficQueue *west);
  37.  
  38. /**Determines the right of way for two vehicles head on
  39. *@return Vehicle that has the right of way
  40. *@pre Vehicle must exist, and contian direction values.
  41. *@param aQueue Any traffic queue
  42. *@param bQueue Any traffic queue
  43. **/
  44. TrafficQueue *headOnRightOfWay(TrafficQueue *aQueue, TrafficQueue *bQueue);
  45.  
  46. /**Determines the lowest wait time of all four vehicles
  47. *@return The lowest wait time value (returns 0 if all vehicles do not exist)
  48. *@param a A vehicle
  49. *@param b A vehicle
  50. *@param c A vehicle
  51. *@param d A vehicle
  52. **/
  53. double getLowestWait(Vehicle *a, Vehicle *b, Vehicle *c, Vehicle *d);
  54.  
  55. /**
  56. *Main function which simulates a four stop
  57. **/
  58. int main(int argc, char *argv[])
  59. {
  60. FILE *file;
  61.  
  62. Vehicle *vehicles;
  63.  
  64. int i;
  65. int numOfVechicles = 0;
  66.  
  67. double tick;
  68. double averageWait = 0;
  69. double maxWait = 0;
  70.  
  71. double interClear = 0;
  72.  
  73. char line[MAX];
  74.  
  75. Node *tempNode;
  76.  
  77. if (argc != 2) {
  78. printf("usage: %s filename\n", argv[0]);
  79. return 0;
  80. }
  81.  
  82. file = fopen( argv[1], "r" );
  83.  
  84. if (!file) {
  85. printf("Could not find file!\n");
  86. return 0;
  87. }
  88.  
  89. vehicles = malloc(sizeof (Vehicle));
  90.  
  91. while (fgets(line, sizeof(line), file)) {
  92.  
  93. numOfVechicles ++;
  94. void *tmp = realloc (vehicles, (numOfVechicles) * sizeof (Vehicle));
  95.  
  96. if (tmp == NULL) {
  97. free(vehicles);
  98. } else {
  99.  
  100. int j = numOfVechicles - 1;
  101. char *text = strtok(line, " ");
  102. vehicles = tmp;
  103. vehicles[j].id = numOfVechicles;
  104.  
  105. for (i = 0; i < 3; i++) {
  106.  
  107. switch (i) {
  108. case 0:
  109. vehicles[j].direction = getDirection(text[0]);
  110. break;
  111. case 1:
  112. vehicles[j].turn = getTurn(text[0]);
  113. break;
  114. case 2:
  115. vehicles[j].waitTime = vehicles[j].arrivalTime = atof(text);
  116. break;
  117. }
  118.  
  119. text = strtok(NULL, " ");
  120. }
  121. }
  122. }
  123. List *final = initializeList(printVehicle, NULL, NULL);
  124.  
  125. TrafficQueue *north = initializeTrafficQueue();
  126. TrafficQueue *south = initializeTrafficQueue();
  127. TrafficQueue *east = initializeTrafficQueue();
  128. TrafficQueue *west = initializeTrafficQueue();
  129.  
  130. for (i = 0; i < numOfVechicles; i ++) {
  131. insertSorted(getVehiclesQ(&vehicles[i], north, south, east, west)->trafficData, &vehicles[i]);
  132. }
  133.  
  134. for(tick = 0;;tick += 0.5) {
  135.  
  136. int rightOfWayCount = 0;
  137.  
  138. fillQueue(tick, north);
  139. fillQueue(tick, south);
  140. fillQueue(tick, east);
  141. fillQueue(tick, west);
  142.  
  143. if (isTrafficQueueEmpty(north)
  144. && isTrafficQueueEmpty(south)
  145. && isTrafficQueueEmpty(east)
  146. && isTrafficQueueEmpty(west)) {
  147. break;
  148. }
  149.  
  150. if (tick < interClear) continue;
  151.  
  152. double lowWait = getLowestWait(north->headVehicle, south->headVehicle, east->headVehicle, west->headVehicle);
  153.  
  154. rightOfWayCount += verifiyHeadVehicle(north, lowWait, tick);
  155. rightOfWayCount += verifiyHeadVehicle(south, lowWait, tick);
  156. rightOfWayCount += verifiyHeadVehicle(east, lowWait, tick);
  157. rightOfWayCount += verifiyHeadVehicle(west, lowWait, tick);
  158.  
  159. TrafficQueue *rightOfWayQueue = NULL;
  160.  
  161. switch (rightOfWayCount) {
  162. case 4:
  163. rightOfWayQueue = north;
  164. break;
  165. case 3:
  166.  
  167. if (north->headVehicle == NULL) {
  168. rightOfWayQueue = east;
  169. } else if (east->headVehicle == NULL) {
  170. rightOfWayQueue = south;
  171. } else if (south->headVehicle== NULL) {
  172. rightOfWayQueue = west;
  173. } else {
  174. rightOfWayQueue = north;
  175. }
  176.  
  177. break;
  178. case 2:
  179. if (isToTheRight(north->headVehicle, west->headVehicle)) {
  180. rightOfWayQueue = west;
  181. } else if (isToTheRight(west->headVehicle, south->headVehicle)) {
  182. rightOfWayQueue = south;
  183. } else if (isToTheRight(south->headVehicle, east->headVehicle)) {
  184. rightOfWayQueue = east;
  185. } else if (isToTheRight(east->headVehicle, north->headVehicle)) {
  186. rightOfWayQueue = north;
  187. } else if (isInFront(south->headVehicle, north->headVehicle)) {
  188. rightOfWayQueue = headOnRightOfWay(south, north);
  189. } else {
  190. rightOfWayQueue = headOnRightOfWay(east, west);
  191. }
  192. break;
  193. case 1:
  194. if (north->headVehicle != NULL) {
  195. rightOfWayQueue = north;
  196. } else if (south->headVehicle != NULL) {
  197. rightOfWayQueue = south;
  198. } else if (east->headVehicle != NULL) {
  199. rightOfWayQueue = east;
  200. } else {
  201. rightOfWayQueue = west;
  202. }
  203. break;
  204. default:
  205. break;
  206. }
  207.  
  208. if (rightOfWayQueue != NULL) {
  209.  
  210. insertBack(final, rightOfWayQueue->headVehicle);
  211. interClear = tick + getTurnTime(rightOfWayQueue->headVehicle->turn);
  212.  
  213. averageWait += moveVehicleIntoIntersection(rightOfWayQueue, tick);
  214.  
  215. if (maxWait < rightOfWayQueue->maxWaitTime) {
  216. maxWait = rightOfWayQueue->maxWaitTime;
  217. }
  218.  
  219. }
  220.  
  221. }
  222.  
  223. fclose(file);
  224.  
  225. file = fopen("report.txt", "w+");
  226.  
  227. fprintf(file, "Four Way Stop Siumlator\n");
  228. fprintf(file, "* Assumptions are found in the readme file\n\n");
  229.  
  230. printf("Four Way Stop Siumlator\n");
  231. printf("* Assumptions are found in the readme file\n\n");
  232.  
  233. printf("Order of Vehicles:\n");
  234. printf("Vehicle ID\tDirection \tTurn \tArrival Time \tDepature Time\n");
  235. fprintf(file, "\t\tDirection \t\tTurn \tArrival Time\n");
  236. printForward(final);
  237.  
  238. tempNode = final->head;
  239. i = 0;
  240. while (tempNode != NULL) {
  241. Vehicle *tempVehicle = (Vehicle *)tempNode->data;
  242. i++;
  243. fprintf(file, "%d.\t\t%c \t\t\t\t%c \t\t%.1lf\n", i, tempVehicle->direction, tempVehicle->turn, tempVehicle->arrivalTime);
  244. tempNode = tempNode->next;
  245. }
  246.  
  247. averageWait /= (double)numOfVechicles;
  248.  
  249. printf("Average Wait Time: %.1lf second(s)\n", averageWait);
  250. fprintf(file, "\n\nAverage Wait Time: %.1lf second(s)\n", averageWait);
  251. printf("Max Wait Time: %.1lf second(s)\n\n", maxWait);
  252. fprintf(file, "Max Wait Time: %.1lf second(s)\n\n", maxWait);
  253.  
  254. printf("North Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(north));
  255. fprintf(file, "North Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(north));
  256. printf("South Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(south));
  257. fprintf(file, "South Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(south));
  258. printf("East Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(east));
  259. fprintf(file, "East Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(east));
  260. printf("West Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(west));
  261. fprintf(file, "West Average Wait Time: %.1lf second(s)\n", getAverageWaitTime(west));
  262.  
  263. deleteList(final);
  264.  
  265. deleteTrafficQueue(north);
  266. deleteTrafficQueue(south);
  267. deleteTrafficQueue(east);
  268. deleteTrafficQueue(west);
  269.  
  270. free(vehicles);
  271. fclose(file);
  272.  
  273. return 0;
  274. }
  275.  
  276. double getLowestWait(Vehicle *a, Vehicle *b, Vehicle *c, Vehicle *d)
  277. {
  278. Vehicle *lowVehicle = a;
  279.  
  280. if (lowVehicle == NULL || (b != NULL && b->waitTime < lowVehicle->waitTime)) {
  281. lowVehicle = b;
  282. }
  283.  
  284. if (lowVehicle == NULL || (c != NULL && c->waitTime < lowVehicle->waitTime)) {
  285. lowVehicle = c;
  286. }
  287.  
  288. if (lowVehicle == NULL || (d != NULL && d->waitTime < lowVehicle->waitTime)) {
  289. lowVehicle = d;
  290. }
  291.  
  292. return lowVehicle != NULL ? lowVehicle->waitTime : 0;
  293. }
  294.  
  295.  
  296.  
  297. TrafficQueue *headOnRightOfWay(TrafficQueue *aQueue, TrafficQueue *bQueue)
  298. {
  299. if (aQueue->headVehicle->turn == FORWARD) {
  300. return aQueue;
  301. } else if (bQueue->headVehicle->turn == FORWARD) {
  302. return bQueue;
  303. } else if (aQueue->headVehicle->turn == RIGHT) {
  304. return aQueue;
  305. } else if (bQueue->headVehicle->turn == RIGHT) {
  306. return bQueue;
  307. } else {
  308. return aQueue;
  309. }
  310. }
  311.  
  312. TrafficQueue *getVehiclesQ(Vehicle *vehicle, TrafficQueue *north, TrafficQueue *south, TrafficQueue *east, TrafficQueue *west)
  313. {
  314. switch (vehicle->direction) {
  315. case NORTH:
  316. return north;
  317. break;
  318. case SOUTH:
  319. return south;
  320. break;
  321. case EAST:
  322. return east;
  323. break;
  324. default:
  325. return west;
  326. break;
  327. }
  328. }
  329.  
  330. int isToTheRight(Vehicle *main, Vehicle *toTheRight)
  331. {
  332. if (main == NULL || toTheRight == NULL) {
  333. return FALSE;
  334. }
  335.  
  336. switch (main->direction) {
  337. case NORTH:
  338. if (toTheRight->direction == WEST) {
  339. return TRUE;
  340. }
  341.  
  342. case SOUTH:
  343. if (toTheRight->direction == EAST) {
  344. return TRUE;
  345. }
  346.  
  347. case EAST:
  348. if (toTheRight->direction == NORTH) {
  349. return TRUE;
  350. }
  351.  
  352. case WEST:
  353. if (toTheRight->direction == SOUTH) {
  354. return TRUE;
  355. }
  356. }
  357.  
  358. return FALSE;
  359. }
  360.  
  361. int isInFront(Vehicle *main, Vehicle *inFront)
  362. {
  363. if (main == NULL || inFront == NULL) {
  364. return FALSE;
  365. }
  366.  
  367. switch (main->direction) {
  368. case NORTH:
  369. if (inFront->direction == SOUTH) {
  370. return TRUE;
  371. }
  372.  
  373. case SOUTH:
  374. if (inFront->direction == NORTH) {
  375. return TRUE;
  376. }
  377.  
  378. case EAST:
  379. if (inFront->direction == WEST) {
  380. return TRUE;
  381. }
  382.  
  383. case WEST:
  384. if (inFront->direction == EAST) {
  385. return TRUE;
  386. }
  387. }
  388.  
  389. return FALSE;
  390. }
  391.  
  392. Direction getDirection(char i) {
  393. switch (i) {
  394. case 'N':
  395. return NORTH;
  396. case 'E':
  397. return EAST;
  398. case 'S':
  399. return SOUTH;
  400. default:
  401. return WEST;
  402. }
  403. }
  404.  
  405. Turn getTurn(char i) {
  406. switch (i) {
  407. case 'F':
  408. return FORWARD;
  409. case 'R':
  410. return RIGHT;
  411. default:
  412. return LEFT;
  413. }
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement