Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.14 KB | None | 0 0
  1. int charToInt(char num[]) {
  2. int i, res = 0, len = strlen(num);
  3. for (i = 0; i < len; i++)
  4. res = (res * 10) + (int)num[i] - 48;
  5. return res;
  6. }
  7. char* intToChar(int num, int digits) {
  8. int i = 0, tempnum = num, tempdigits = 0;
  9. while (tempnum) { tempdigits++; tempnum /= 10; }
  10. char* numstr = (char*)malloc(sizeof(char) * digits + 1);
  11. if (numstr) {
  12. while (num) {
  13. numstr[i++] = (int)(num % 10) + 48;
  14. num /= 10;
  15. }
  16. for (; i < digits; i++)
  17. numstr[i] = ' ';
  18. numstr[i] = 0;
  19. numstr = _strrev(numstr);
  20. if (tempdigits < digits)
  21. for (i = 0; i < digits - tempdigits; i++)
  22. numstr[i] = '0';
  23. }
  24. return numstr;
  25. }
  26. int openMapsFile() { // Gil Sorek
  27. FILE* mapfile;
  28. fopen_s(&mapfile, MAP, "rt"); // trys to read MAP file
  29. if (mapfile) { // if read succeeded, return 1;
  30. fclose(mapfile);
  31. return 1;
  32. }
  33. return 0; // when fails to read maps, returns 0
  34. }
  35. int openLetsPlay() { // Gil Sorek
  36. FILE* file;
  37. fopen_s(&file, LETSPLAY, "rt"); // trys to read letsplay file
  38. if (file) { // if read succeeded, return 1;
  39. fclose(file);
  40. return 1;
  41. }
  42. return 0; // when fails to read, returns 0
  43. }
  44. void option4PrintsPathes() { // Gilad Wolf
  45. store* stores;//pointer struct of stores
  46. char routes[34] = { 0 };//array for saving the routes for printing
  47. stores = (store*)malloc(sizeof(store) * 11);//creating an array of stores (struct store)
  48. if (!openMapsFile()) printf_s("file map.txt not found\n");
  49. if (stores) {
  50. resetingStores(stores);//reseting details
  51. mapArrayCreator(stores);//creating stores array with the details
  52. if (stores[0].exists) generatingPaths(stores, routes, 0, NULL);//generating the routes with recurtion
  53. else printf_s("\n");
  54. free(stores);
  55. }
  56. }
  57. void resetingStores(store* stores) { // Gilad Wolf
  58. //and empty array of stores
  59. int i = 0;
  60. for (i = 0; i < 11; i++) {
  61. stores[i].c1 = stores[i].c2 = stores[i].c3 = NULL;//putting null in the next stores
  62. stores[i].exists = stores[i].r1 = stores[i].r2 = stores[i].r3 = 0;//putting 0 in exist (no one exist)
  63. stores[i].num = i;//ordering the numbers of the stores to the orgins of the array
  64. }
  65. }
  66. void mapArrayCreator(store* stores) { // Gilad Wolf
  67. //accepting an empty array and reading from the map the relative details
  68. FILE* maps;//the map file
  69. int result = 1, i, length, from, to;//result - fgets return value. length - length of the word. from - word in 1column. row - word in 2column
  70. char line[9], *token;//line - word, token - rest of the line
  71. fopen_s(&maps, MAP, "rt");//open the map
  72. //if (!maps) printf_s("file map.txt not found\n");//checking if it exist
  73. if (maps) {
  74. while (result) { // while fgets get new line
  75. result = (int)fgets(line, 9, maps);
  76. if ((strcmp(line, "\n") != 0) && result) {//if its not '\n' and exist
  77. token = NULL;//putting null at token
  78. for (i = 0, length = strlen(line); i < length; i++) {//run all the line
  79. if (line[i] == '\t') line[i] = ' ';//if the char is '\t' put " "
  80. if (line[i - 1] == '\n') line[i - 1] = 0;//if the char is '\n' finish the line.
  81. }
  82. from = charToInt(strtok_s(line, " ", &token));//copying the word to from
  83. to = charToInt(strtok_s(token, " ", &token));//copying the word to to
  84. if (!stores[from].exists)//if the stores is exist
  85. stores[from].exists = 1;//put 1
  86. if (!stores[from].r1) {//if there isn't next stores from this store
  87. stores[from].c1 = &stores[to];//put the next store on c1
  88. stores[from].r1 = 1;
  89. }//sign that there is 1 next store
  90. else if (stores[from].r1 && !stores[from].r2) {//if there is only 1 next store
  91. stores[from].c2 = &stores[to];//put the next store on c2
  92. stores[from].r2 = 1;
  93. }//sign that there is 2 next stores
  94. else if (stores[from].r1 && stores[from].r2) {//if there is 2 stores
  95. stores[from].c3 = &stores[to];//put the next store on c3
  96. stores[from].r3 = 1;
  97. }//sign that there is 3 stores
  98. }
  99. }fclose(maps);//closing the file
  100. }
  101. }
  102. char updateRouteStr(char routes[], int num) { // Gilad Wolf
  103. //accepting the routes array for saving the printing lines and the number of the store
  104. int i, len = strlen(routes), lenNum; //len - length of the line in routes array. lenNum - how many digits
  105. char number[3] = { 0 };//an empty array of chars all origins is '\0'
  106. (num == 10) ? strncpy_s(number, 3, intToChar(num, 2), 3) : strncpy_s(number, 3, intToChar(num, 1), 3);//if number is 10 copying 3 digits, else copying 2 digits include '\0'
  107. lenNum = strlen(number);//length of the number
  108. for (i = 0; i < lenNum; i++)
  109. routes[len++] = number[i];//put the number in the correct location in the array
  110. routes[len] = '-';//put '-' next
  111. routes[len + 1] = '>';//put '>' next
  112. routes[len + 2] = 0;//put '\0'(closing line till now)
  113. return *routes;//returning the updateRouteStrd routes
  114. }
  115. int checkIfWas(char routes[], int num) // Gilad Wolf
  116. ////////////////////////////////////////////////////////////
  117. {
  118. int i = 0, length = strlen(routes);
  119. for (i = 0; i < length; i += 3) {
  120. if (num == 10) {
  121. if (1 == ((routes[i] + 48)) && 0 == routes[i + 1])
  122. return(1);
  123. }
  124. else {
  125. if ((num + 48) == routes[i])
  126. return(1);
  127. }
  128. }return(0);
  129. }
  130. void printPaths(store* store, char routes[], char* temproute, int flag, FILE* routesfile) { // Gilad Wolf
  131. //accepting the stores array, the routes array (the printing lines),a pointer with the correct line for printing. flag and file for creating file to func5
  132. if (store->num == 0) {//if store is store number 0
  133. routes[strlen(temproute)] = 48;//putting 0 in the printing line
  134. routes[strlen(temproute) + 1] = 0;//closing the line
  135. strncpy_s(temproute, 34, routes, 34);//copying line from routes to the temp route
  136. (!flag) ? printf_s("%s\n", temproute) : fprintf_s(routesfile, "%s\n", temproute);//if flag is 0 printing the route
  137. routes[strlen(temproute) - 1] = 0;//closing the routes instead of the 0 number;
  138. free(temproute);//free the temp route
  139. }
  140. else {
  141. routes[strlen(temproute) - 2] = 0;//if we've been in the this store
  142. strncpy_s(temproute, 34, routes, 34);//copying to the temp route the line from routes
  143. if (!flag) printf_s("%s\n", temproute);//printing the route
  144. routes[strlen(temproute)] = '-';//put '-' next
  145. routes[strlen(temproute) + 1] = '>';//put '>' next
  146. free(temproute);//free the temp route
  147. }
  148. }
  149. void generatingPaths(store* store, char routes[], int flag, FILE* routesfile) { // Gilad Wolf
  150. //accepting the stores, sevivon(reality reminder), routes, flag and file for func5
  151. char* temproute = calloc(34, 1);//creating a dinamic line
  152. strncpy_s(temproute, 34, routes, 34);//copying routes to temproute
  153. if (!checkIfWas(routes, store->num)) {//if we have never been in this store
  154. updateRouteStr(temproute, store->num);//updating the temproute where we are now
  155. if (store->r1)generatingPaths(store->c1, temproute, flag, routesfile);//if there is next store, go there.
  156. if (store->r2 && checkIfWas(routes, store->c2->num) == 0) generatingPaths(store->c2, temproute, flag, routesfile);// if there is next second store and we never been there, go there.
  157. if (store->r3 && checkIfWas(routes, store->c3->num) == 0) generatingPaths(store->c3, temproute, flag, routesfile);// if there is next third store, go there.
  158. if (!store->r1) {// if there isn't next stores
  159. strncpy_s(routes, 34, temproute, 34);//copying temproute line to routes array for saving it
  160. routes[strlen(temproute) - 2] = 0;//closing the line in routes
  161. strncpy_s(temproute, 34, routes, 34);//copying routes line to temproute
  162. if (!flag) printf_s("%s\n", temproute);//if flag is 0 printing the route
  163. routes[strlen(temproute)] = '-';//put '-' next
  164. routes[strlen(temproute) + 1] = '>';//put '>' next
  165. free(temproute);//free the temproute
  166. }
  167. }
  168. else
  169. printPaths(store, routes, temproute, flag, routesfile);//if we have been in this store. printing the route
  170. }
  171. // Option 5 functions
  172. void option5DriverPath(int mode) { // Gil Sorek
  173. FILE* routes = NULL;
  174. path_t* paths; // paths array for routes starting and ending in the factory
  175. int maxroutelen = 0, numOfRoutes = 0, routesflag = 1; // variables for calculating how many routes are and the longest route length
  176. char* routestr = NULL; // string for saving routes from routes.txt
  177. routes = readRoutesFromMaps(routes, &routesflag, mode); // trying to read routes.txt to check if there are any relevant routes
  178. if (!routes) {
  179. if (routesflag) printf_s("Read error.\n"); // if cant read routes.txt
  180. else if ((!routesflag) && (mode != 2)) printf_s("No path from factory 0\n\n"); // if there are no roads going out from the factory
  181. else if ((!routesflag) && (mode == 2) && openMapsFile()) printf_s("\n");
  182. }
  183. else {
  184. routestr = genRouteStr(routes, &maxroutelen, &numOfRoutes); // generating longest route string
  185. paths = (path_t*)malloc(sizeof(path_t)*(numOfRoutes + 1)); // allocating memory for routes array (1 extra to work with coordinated indexes)
  186. if (paths && routestr) { // if memory allocated good and routestr string was created
  187. if (numOfRoutes) { // if there is at least one complete route (going out and back to factory)
  188. if (genPathNodes(paths, routestr, maxroutelen, numOfRoutes, routes)) // generates all routes nodes
  189. chooseBestPath(paths, numOfRoutes + 1, mode); // chooses best route
  190. freePaths(paths, numOfRoutes); // free all nodes
  191. }
  192. else {
  193. if (openLetsPlay()) printf_s("can't find file letsPlay.txt\n");
  194. printf_s("No best path found\n\n"); free(paths); free(routestr); }
  195. }
  196. }
  197. }
  198. FILE* readRoutesFromMaps(FILE* routes, int* routesflag, int mode) { // Gil Sorek
  199. store* stores;
  200. char routesStr[34] = { 0 }; //route string with longest possible route length
  201. fopen_s(&routes, ROUTE, "w+");
  202. if (!routes) return NULL; // if cant read file, return NULL
  203. stores = (store*)malloc(sizeof(store) * 11); // allocating memory for all stores(and factory)
  204. if (mode == 1) {
  205. if (!openLetsPlay()) printf_s("can't find file letsPlay.txt\n");
  206. if (!openMapsFile()) printf_s("file map.txt not found\n");
  207. }
  208. else
  209. if (!openMapsFile()) printf_s("file map.txt not found\n\n");
  210. if (stores) {
  211. resetingStores(stores); // resetting stores list
  212. mapArrayCreator(stores); // creating stores array with info from map.txt
  213. if (stores[0].exists) generatingPaths(stores, routesStr, 1, routes); // generating all routes (run-mode 1 for function 5 mode)
  214. else { fclose(routes); free(stores); *routesflag = 0; return NULL; } // if no roads going out from factory, return NULL with routesflag=0 for error printing
  215. fseek(routes, 0, SEEK_SET);
  216. return routes; // if successful read, return routes FILE
  217. }
  218. fclose(routes);
  219. return NULL;
  220. }
  221. char* genRouteStr(FILE* routes, int* routemax, int* numOfRoutes) { // Gil Sorek
  222. int res = 0, routelen = 1; // res is fscanf_s result variable, routelen is the longest route length
  223. char ch = '\n', lastch, *routestr=NULL; // ch&lastch for chars comparison
  224. while (res != EOF) {
  225. if (ch) lastch = ch; // save last char before getting new
  226. res = fscanf_s(routes, "%c", &ch, 1);
  227. if ((ch != '\n') && (res != EOF)) routelen++; // counting chars in each line
  228. else { // end of line
  229. if (routelen > *routemax) *routemax = routelen; // if current line is the longest, save length
  230. routelen = 1;
  231. if (lastch != '\n') (*numOfRoutes)++; // saving total number of routes
  232. }
  233. }
  234. fseek(routes, 0, SEEK_SET);
  235. routestr = calloc(*routemax + 1, 1); // allocating memory for longest route length string
  236. return routestr; // returning routestr string address if calloc succeeded, else returning NULL
  237. }
  238. void create_PathNode(path_t* head, path_t* path, char route[]) { // Gil Sorek
  239. path_t *store = NULL; // creating new store variable for the node list
  240. removeLastStore(route, path->store); // removing last store from route string
  241. if (route[0] != '0') { // if current store is not the last one (factory)
  242. store = malloc(sizeof(path_t)); // allocating memory for the new store
  243. store->store = (route[1] == '-') ? (int)route[0] - 48 : 10; // saving store number from route string
  244. store->packCount = packagesForStore(store->store); // saving number of packages appointed for the current store
  245. store->distTotal = path->distTotal + distanceBetweenStores(path->store, store->store); // summing of the total distance of the route
  246. store->packTotal = path->packTotal + store->packCount; // summing the total packages for the route
  247. store->next = path;
  248. path->next = store; // adding store to node list
  249. if (strcmp(route, "0") != 0) create_PathNode(head, path->next, route); // sending new node list again to continue list building
  250. }
  251. else { // if this is the last store (arrived back to factory)
  252. head->distTotal = path->distTotal + distanceBetweenStores(path->store, 0); // save total distance in the head of the list including way back to factory
  253. head->packTotal = path->packTotal; // save total packages in the head of the list
  254. path->next = NULL;
  255. }
  256. }
  257. void removeLastStore(char route[], int store) { // Gil Sorek
  258. int i, len; // len is variable for how many chars needed to be removed (depends on store number)
  259. for (i = 0, len = (store == 10) ? strlen(route) - 2 : strlen(route) - 3; i < len; i++)
  260. route[i] = (store == 10) ? route[i + 4] : route[i + 3]; // over-writing the store wanting to remove
  261. route[i] = 0; // setting last char as '\0'
  262. }
  263. int genPathNodes(path_t* paths, char routestr[], int maxroutelen, int numOfRoutes, FILE* routes) { // Gil Sorek
  264. // paths=routes array, routestr=longest route string, maxroutelen=longest route length, numOfRoutes=total number of routes, routes=FILE*
  265. int i;
  266. char** routesArr = genRoutesArr(routes, maxroutelen, numOfRoutes); // generates arr with all complete routs (going from and back to factory)
  267. if (routesArr) {
  268. for (i = 1; i < numOfRoutes + 1; i++) { // running on all routes
  269. paths[i].distTotal = paths[i].packCount = paths[i].packTotal = paths[i].store = 0; // defining the head of each route
  270. paths[i].path = malloc(sizeof(char)*strlen(routesArr[i]) + 1); // allocating memory for route string
  271. strncpy_s(paths[i].path, maxroutelen, routesArr[i], strlen(routesArr[i]) + 1); // saving route string in each head
  272. strncpy_s(routestr, maxroutelen, routesArr[i], strlen(routesArr[i]) + 1); // saving copy of route for recurtion function
  273. create_PathNode(&paths[i], &paths[i], routestr); // Creating path node for every route
  274. }
  275. for (int i = 1; i < numOfRoutes + 1; i++) // free memory
  276. free(routesArr[i]);
  277. free(routesArr);
  278. //freopen_s(routes, ROUTE, "wt", routes);
  279. fclose(routes);
  280. if (paths[1].next != NULL) // if at least 1 node exists
  281. return 1;
  282. }
  283. return 0;
  284. }
  285. char** genRoutesArr(FILE* routes, int maxRoutelen, int numOfRoutes) { // Gil Sorek
  286. int i, res = 1; // res for fgets return value
  287. char* tempstr = calloc(maxRoutelen, 1); // temp string for saving every route
  288. char** routesArr=NULL; // char** array for all complete routs (going from and back to factory)
  289. routesArr = (char**)malloc(sizeof(char*) * (numOfRoutes + 1)); // allocating memory for array
  290. for (i = 0; i < numOfRoutes + 1; i++) //allocating string for every route
  291. routesArr[i] = (char*)malloc(sizeof(char)*maxRoutelen);
  292. if (tempstr && routesArr) { // if mallocs succeded, creates array
  293. fseek(routes, 0, SEEK_SET);
  294. i = 1;
  295. while (res) { // while getting routes from file
  296. res = (int)fgets(tempstr, maxRoutelen, routes); // save route in tempstr
  297. if (res && strcmp(tempstr, "\n")) {
  298. strncpy_s(routesArr[i], maxRoutelen, tempstr, strlen(tempstr)); //saving route in array
  299. if (routesArr[i][strlen(routesArr[i])-1] == '\n') // removes '\n' at the end of every line
  300. routesArr[i][strlen(routesArr[i])-1] = 0;
  301. i++; // promotes i for next route
  302. }
  303. }
  304. free(tempstr); //free tempstr memory
  305. }
  306. fclose(routes);
  307. return routesArr; // return arr
  308. }
  309. int packagesForStore(int store) { // Gil Sorek
  310. int res = 1, packages = 0; // res is fgets return value, packages for counting how many packages appointed to store
  311. char package[13]; // package string to save from file
  312. FILE* file;
  313. fopen_s(&file, LETSPLAY, "rt");
  314. if (!file) return 0; // if cant read file, return 0 packages
  315. while (res) {
  316. res = (int)fgets(package, 13, file);
  317. if ((getPackageStore(package) == store) && res) packages++; // if store of package from file matches to store variable
  318. fseek(file, 2, SEEK_CUR); // skip '\n' at the end of each line
  319. }
  320. fclose(file);
  321. return packages;
  322. }
  323. int distanceBetweenStores(int store1, int store2) { // Gil Sorek
  324. FILE* maps;
  325. int res = 1, i, len, from, to; // res for fgets return value, from&to for store numbers in map.txt
  326. char line[9], *token; // line string for saving every line from file
  327. fopen_s(&maps, MAP, "rt");
  328. while (res) { // while getting new lines
  329. res = (int)fgets(line, 9, maps);
  330. if ((strcmp(line, "\n") != 0) && res) { // if line is not '\n'
  331. token = NULL;
  332. for (i = 0, len = strlen(line); i < len; i++)
  333. if (line[i] == '\t') line[i] = ' '; // replace tabs with spaces
  334. if (line[i - 1] == '\n') line[i - 1] = 0; // set end of line in '\0'
  335. from = charToInt(strtok_s(line, " ", &token)); // extract from city
  336. to = charToInt(strtok_s(token, " ", &token)); // extract to city
  337. if (((from == store1) && (to == store2)) || ((from == store2) && (to == store1))) { // if cities is a match, return distance (whats left in token str)
  338. fclose(maps);
  339. return charToInt(token);
  340. }
  341. }
  342. }
  343. fclose(maps);
  344. return 0; // return 0 if no match
  345. }
  346. void chooseBestPath(path_t* paths, int numOfRoutes, int mode) { // Gil Sorek
  347. int i, bestroute = 1; //setting first route as best route at the starting
  348. for (i = 2; i < numOfRoutes; i++) { // running on all other routes
  349. if (paths[i].packTotal > paths[bestroute].packTotal) // if total packs of current route is bigger than current best route, replace indexes
  350. bestroute = i;
  351. else if (paths[i].packTotal == paths[bestroute].packTotal) // in case total packages are equal
  352. if (paths[i].distTotal < paths[bestroute].distTotal) // if total distance is lower, replace indexes
  353. bestroute = i;
  354. }
  355. if (mode == 2)
  356. fuelConsumption(paths[bestroute]); // if run-mode is 2, send best route to function 6 (fuel report)
  357. printRouteFile(paths[bestroute], mode); // else print best route
  358. }
  359. void printRouteFile(path_t bestpath, int mode) { // Gil Sorek
  360. FILE* route;
  361. path_t* ptr = bestpath.next; // creates path_t ptr for running on all stores
  362. fopen_s(&route, ROUTE, "wt");// creates route file
  363. if (!route)
  364. printf_s("problem writing to route.txt\n");
  365. else {
  366. fprintf_s(route, "Driver Route:\n%s\nStore\tPackages Distance so far[km]\n---- -------- -------------------\n", bestpath.path); // prints headlines
  367. if (mode == 1) printf_s("Driver Route:\n%s\n", bestpath.path);
  368. if (mode == 1) printf_s("The total packages available for this route is %d\nThe total suggested route distance is %d\n\n", bestpath.packTotal, bestpath.distTotal); // prints to screen total packages and distance
  369. while (ptr->next) { // for every store in node list
  370. fprintf_s(route, "%d\t%d\t %d\n", ptr->store, ptr->packCount, ptr->distTotal); // prints to file every store's number, pack count and distance
  371. ptr = ptr->next; // promotes ptr to next store
  372. }
  373. fprintf_s(route, "%d\t%d\t %d\n%d\t%d\t %d\n", ptr->store, ptr->packCount, ptr->distTotal, bestpath.store, bestpath.packCount, bestpath.distTotal); // prints last store's number, pack count and distance
  374. fprintf_s(route, "The total packages available for this route is %d\nThe total suggested route distance is %d\n", bestpath.packTotal, bestpath.distTotal); // prints total packages for route and total distance
  375. }
  376. fclose(route); // closes file
  377. }
  378. void freePaths(path_t* paths, int routes) { // Gil Sorek
  379. path_t* ptr1, *ptr2; // creates 2 path_t pointers for running on node list and free memory
  380. int i;
  381. for (i = 1; i < routes + 1; i++) { // for every store (starting from first store, not includes factory(head) )
  382. ptr1 = paths[i].next; // at the start of every route, ptr1 first equals to the first store (after factory)
  383. while (ptr1) { // while ptr1 exists (doesnt equals NULL)
  384. ptr2 = ptr1->next; // stores next store after ptr1
  385. free(ptr1); // free memory of first store
  386. ptr1 = ptr2;// promotes first ptr to what was ptr1->next
  387. }
  388. ptr1 = ptr2 = NULL; // resets pointers
  389. }
  390. free(paths); // free all factory heads for all routes
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement