HabKaffee

Untitled

Apr 3rd, 2020
116
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Bus.h"
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. int current = 0;
  9. const int size = 50;
  10.  
  11. void declareBus(BUS *bus, char* num, char** stops, int count, int n) {
  12. bus[n].num = (char*)malloc((strlen(num) + 1)* sizeof(char));
  13. strcpy(bus[n].num, num);
  14. bus[n].count = count;
  15. for (int i = 0; i < count; i++) {
  16. bus[n].stops[i] = (char*)malloc((strlen(stops[i]) + 1)* sizeof(char));
  17. strcpy(bus[n].stops[i], stops[i]);
  18. }
  19. }
  20.  
  21. void nullingArrays(char* num, char** stops) {
  22. for (int i = 0; i < size; i++) {
  23. num[i] = { '0' };
  24. for (int j = 0; j < size * 2; j++) {
  25. stops[i][j] = { '0' };
  26. }
  27. }
  28. }
  29.  
  30. void printBus(BUS *bus, int n) {
  31. printf("Bus number = ");
  32. puts(bus[n].num);
  33. printf("Stops");
  34. printf("\n");
  35. for (int i = 0; i < (bus[n].count); i++) {
  36. puts(bus[n].stops[i]);
  37. printf(" <==> ");
  38. }
  39. }
  40.  
  41. int maxN(int a, int b) {
  42. if (a > b) {
  43. return a;
  44. }
  45. return b;
  46. }
  47.  
  48. int maxOfLen(int a, int b) {
  49. if (a > b) {
  50. return a;
  51. }
  52. return b;
  53. }
  54. void SwapStr(char* a, char* b) {
  55. int c = maxOfLen(strlen(a), strlen(b));
  56. char* swp = (char*)malloc(c * sizeof(char));
  57. a > b ? b = (char*)realloc(b, c) : a = (char*)realloc(a, c);
  58. strcpy(swp, a);
  59. strcpy(a, b);
  60. strcpy(b, swp);
  61. }
  62.  
  63. int addStops(char* stopAdd, BUS *bus, int n){
  64. bus[n].stops[bus[n].count] = (char*)malloc(strlen(stopAdd+1)*sizeof(char));
  65. strcpy(bus[n].stops[bus[n].count], stopAdd);
  66. bus[n].count++;
  67. return bus[n].count;
  68. }
  69.  
  70. void changeStops(BUS* bus, char* stopChange, int number, int n) {
  71. strcpy(bus[n].stops[number], stopChange);
  72. }
  73.  
  74. int addStopsToWay(char* stopAdd, BUS* bus, int n, int place) {
  75. if (bus[n].max_stops_size == bus[n].count) {
  76. *bus[n].stops = (char*)realloc(*bus[n].stops, bus[n].count * 2);
  77. bus[n].max_stops_size *= 2;
  78. }
  79. int c = 0;
  80. for (int i = 0; i < bus[n].count; i++) {
  81. c = maxOfLen(c, strlen(bus[n].stops[i]));
  82. }
  83. char* swp = (char*)malloc(c * sizeof(char));
  84. strcpy(swp, stopAdd);
  85. char* swp1 = (char*)malloc(c * sizeof(char));
  86. for (int i = place; i < bus[n].count+1; i++) {
  87. strcpy(swp1, bus[n].stops[i]);
  88. strcpy(bus[n].stops[i], swp);
  89. strcpy(swp, swp1);
  90. }
  91. free(swp);
  92. free(swp1);
  93. bus[n].count++;
  94. return bus[n].count;
  95. }
  96.  
  97. void changeStopsOrder(BUS* bus, int n, int num1, int num2) {
  98. SwapStr(bus[n].stops[num1], bus[n].stops[num2]);
  99. }
  100.  
  101. void deleteBusStopByNum(BUS* bus, int num, int n) {
  102. int c = 0;
  103. for (int i = 0; i < bus[n].count; i++) {
  104. c = maxOfLen(c, strlen(bus[n].stops[i]));
  105. }
  106. for (int i = num; i < bus[n].count-1; i++) {
  107. strcpy(bus[n].stops[i], bus[n].stops[i+1]);
  108. }
  109. free(bus[n].stops[bus[n].count]);
  110. bus[n].count--;
  111. }
  112.  
  113. void deleteBusStopByName(BUS* bus, char* name, int n) {
  114. int c = 0;
  115. if (!strcmp(bus[n].num, name)) { // if num == name -> 0
  116. c++;
  117. }
  118. deleteBusStopByNum(bus, c, n);
  119. }
  120.  
  121.  
  122. void printBusArr(BUS_ARRAY *busArr) {
  123. for (int i = 0; i < current; i++) {
  124. printBus(busArr[i].bus, i);
  125. }
  126. }
  127.  
  128. void addBusToBusArr(BUS_ARRAY *busArr, BUS *bus,int n) {
  129. strcpy(busArr[current].bus[current].num, bus[n].num);
  130. for (int i = 0; i < bus[n].count; i++) {
  131. busArr[current].bus[current].stops[i] = (char*)malloc((strlen(bus[n].stops[i]) + 1) * sizeof(char));
  132. strcpy(busArr[current].bus[current].stops[i], bus[n].stops[i]);
  133. }
  134. busArr[current].bus[current].count = bus[n].count;
  135. current++;
  136. };
  137.  
  138.  
  139. void deleteBusFromArr(BUS_ARRAY *busArr, int number) {
  140. int maxNumLen = 0;
  141. int maxStopLen = 0;
  142. for (int i = 0; i < current; i++) {
  143. maxNumLen = maxOfLen(maxNumLen, strlen(busArr[i+1].bus[i+1].num));
  144. for (int j = 0; j < busArr[i].bus[i].count; j++) {
  145. maxStopLen = maxOfLen(maxStopLen, (strlen(busArr[i].bus[i].stops[j])));
  146. }
  147. }
  148. for (int i = 0; i < current; i++) {
  149. busArr[i].bus[i].num = (char*)realloc(busArr[i].bus[i].num, maxNumLen);
  150. for (int j = 0; j < busArr[i].bus[i].count; j++) {
  151. busArr[i].bus[i].stops[j] = (char*)realloc(busArr[i].bus[i].stops[j], maxStopLen);
  152. }
  153. }
  154. for (int i = number; i < busArr[current].count-1; i++) {
  155. strcpy(busArr[i].bus[i].num, busArr[i + 1].bus[i + 1].num);
  156. for (int j = 0; j < (busArr[i].bus[i].count); j++) {
  157. strcpy(busArr[i].bus[i].stops[j], busArr[i+1].bus[i+1].stops[j]);
  158. }
  159. busArr[i].bus[i].count = busArr[i + 1].bus[i + 1].count;
  160. }
  161. current--;
  162. }
  163.  
  164. void changeBus(BUS_ARRAY *busArr,BUS *bus, int number, int n) {
  165. int c = 0;
  166. int mxnum = 0;
  167. mxnum = maxN(busArr[number].bus[number].count, busArr[n].bus[n].count);
  168.  
  169. int f = maxOfLen(strlen(busArr[number].bus[number].num), strlen(busArr[n].bus[n].num));
  170. busArr[number].bus[number].num = (char*)realloc(busArr[number].bus[number].num, f);
  171. strcpy(busArr[number].bus[number].num, busArr[n].bus[n].num);
  172.  
  173. for (int i = 0; i < mxnum; i++) {
  174. f = maxOfLen(strlen(busArr[number].bus[number].stops[i]), strlen(busArr[n].bus[n].stops[i]));
  175. }
  176. for (int i = 0; i < mxnum; i++) {
  177. busArr[number].bus[number].stops[i] = (char*)realloc(busArr[number].bus[number].stops[i], f);
  178. }
  179. for (int i = 0; i < mxnum; i++) {
  180. strcpy(busArr[number].bus[number].stops[i], busArr[n].bus[n].stops[i]);
  181. }
  182. free(bus);
  183. }
RAW Paste Data Copied