Advertisement
mcgizmo

Untitled

Dec 7th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1.  
  2. /****************************************************************************************/
  3. /*Name: Liav Vaknin, Class: 41/5, ID: 303030340.*/
  4. /*Name: Alex Naidyonkov, Class: 41/5, ID: 321055071 */
  5. /****************************************************************************************/
  6. /*A car park built so cars could get him only through one exit. The cars are in the parking slot on one line, so that when want to get a car found in garage remove all cars parked in the parking slot between a parking.
  7.  
  8. A parking prefer small cars, will soon be develop as the largest and most comfortable moving them a more inserted inside. That is a bigger car as it will be deeper in the parking slot.
  9.  
  10. Make parking holder data structure that allows to maintain all the details of the cars in the parking slot.
  11. Data structure to support the following actions:
  12. • Exiting Car from parking. Action should accept the proposed data structure and details of the vehicle and put it in the parking slot.
  13. • removing vehicles from the parking lot. Action should accept the proposed data structure and number of the vehicle wants to getting out and take it.
  14.  
  15. For every car has to keep the following information:
  16. Type of car (20 characters maximum)
  17. Vehicle number (integer has 7 digits)
  18. (Kg) weight of the vehicle (a real number)
  19. */
  20. #include<stdio.h>
  21. #include<stdlib.h>
  22. #include<string.h>
  23.  
  24. #define N 20
  25. #define SIZE 20
  26. #define PARK_IS_FULL 1
  27. #define CAR_CALLING 2
  28. #define CAR_PULL_OUT 3
  29. #define FIRST_CAR_DATA 4
  30. #define CASE_EXIT 5
  31.  
  32. static int RES = 0;
  33.  
  34. typedef struct // Data structure for cars
  35. {
  36. char type[SIZE];
  37. int number;
  38. float weight;
  39.  
  40. }car_data;
  41.  
  42. typedef struct // Data structure for parking
  43. {
  44. car_data place[N]; // Parking slots
  45. int top; // Index of head of parking
  46. }stack_car_data;
  47.  
  48. // Statement of Functions
  49. int create_stack(stack_car_data *st);
  50. int stack_is_empty(stack_car_data *st);
  51. int stack_is_full(stack_car_data *st);
  52. void CAR_INFO(car_data *c);
  53. int pop(stack_car_data *st, car_data *i);
  54. int push(stack_car_data *st, car_data *i);
  55. int top(stack_car_data *st, car_data *i);
  56.  
  57. void main()
  58. {
  59. stack_car_data parking, temp_parking;
  60. car_data car, c; // creating car information
  61. int prk, n, i;
  62. prk = create_stack(&parking); //creating a parking
  63. prk = create_stack(&temp_parking); // creating a temporary parking
  64. while (prk)
  65. {
  66. printf("\n***************************************************************\n\n");
  67. printf("Menu choice parking and car information:\n\n");
  68. printf("Please choice from the menu:\n\n");
  69. // Menu for user choice
  70. printf("[1] Cheking if parking is full or not\n");
  71. printf("[2] Enter a car to parking slot\n");
  72. printf("[3] Take out car from parking slot by number\n");
  73. printf("[4] Print data information about first car in parking\n");
  74. printf("[5] Exit from menu\n\n");
  75. scanf("%d", &i);
  76. _flushall();
  77.  
  78. switch (i) // switch for cheking a parking slots for empty or full
  79. {
  80. case PARK_IS_FULL: // parking full cheking and return a empty slots
  81. {
  82. if (!stack_is_empty(&parking))
  83. {
  84. system("cls");
  85. printf("Full parking with %d\n", RES);
  86. }
  87. else
  88. {
  89. int x;
  90. x = N - RES;
  91. printf("Available places: %d\n", x);
  92. }
  93. break;
  94. }
  95. case CAR_CALLING:
  96. {
  97. system("cls");
  98. if ((stack_is_empty(&parking)) || (!(stack_is_full(&parking)))) // cheking stack is empty
  99. {
  100. CAR_INFO(&car); // function for create a data information about a car
  101. if (!(stack_is_empty(&parking)))
  102. {
  103. top(&parking, &c); // cheking a top parking slot
  104. while (((c.weight)<(car.weight)) && ((parking.top) != (-1))) // comparing a weight of cars
  105. {
  106. prk = pop(&parking, &c); // removing a car from the parking slot
  107. prk = push(&temp_parking, &c); // add a car from in to the temporary parking slot
  108. top(&parking, &c); // cheking the car on top parging slot
  109. }
  110. }
  111. prk = push(&parking, &car); // removing a car from the parking slot
  112. while (temp_parking.top != -1) // do a loop if we have cars on temporary slot
  113. {
  114. prk = pop(&temp_parking, &c); // removing a car from the temporary parking slot
  115. prk = push(&parking, &c); // add a car from in to the parking slot
  116. }
  117. }
  118.  
  119. break; }
  120. case CAR_PULL_OUT: // removing a car from parking
  121. {
  122. system("cls");
  123. printf("Plese enter a number of car what you need remove: ");
  124. scanf("%d", &n);
  125. if (!(stack_is_empty(&parking)) || (top(&parking, &c))) // Checking the parking slots not full or have on top a car
  126.  
  127. {
  128. while ((n != c.number) && !(parking.top == -1)) //Checking the car with number at the top
  129. {
  130. prk = pop(&parking, &c); // removing a car from the parking slot
  131. prk = push(&temp_parking, &c); // add a car from in to the temporary parking slot
  132. top(&parking, &c); // cheking the car on top parging slot
  133. }
  134. if (parking.top == -1) // cheking if we still have a car on parking
  135. printf("Error!Car not finding"); // if not print error
  136. else
  137. prk = pop(&parking, &c); // removing a car from the parking slot
  138. while (temp_parking.top != -1) // do the loop if we still have a cars on temporary parking
  139. {
  140. prk = pop(&temp_parking, &c); // removing a car from the temporary parking slot
  141. prk = push(&parking, &c); // add a car from in to the parking slot
  142. }
  143. }
  144. break; }
  145. case FIRST_CAR_DATA: // printing a data information about car in top parking place
  146. {
  147. system("cls");
  148. if (top(&parking, &c))
  149. printf("The type is:%s with number:%07d ,car withe: %.2f", c.type, c.number, c.weight);
  150.  
  151. break; }
  152. case CASE_EXIT: // exiting from menu
  153. {
  154. prk = 0;
  155. break;
  156. }
  157. }
  158. }
  159. }
  160.  
  161. int create_stack(stack_car_data *st) // creating a stack for car parking
  162. {
  163.  
  164. return(st->top = -1);
  165. }
  166.  
  167. int stack_is_empty(stack_car_data *st) //Function that checks if the parking slot is empty
  168. {
  169. return(st->top == -1);
  170. }
  171.  
  172. int stack_is_full(stack_car_data *st) //Function that checks if the parking slot is full
  173. {
  174.  
  175. return(st->top == (N - 1));
  176. }
  177.  
  178. void CAR_INFO(car_data *c) // function for entering a car information
  179. {
  180. int length;
  181. do
  182. {
  183. _flushall();
  184. printf("Please enter a car type: ");
  185. gets(c->type);
  186. } while (length = strlen(c->type) >= 20);
  187. do
  188. {
  189. _flushall();
  190. printf("\nEnter car number (7 digits): ");
  191. scanf("%d", &c->number);
  192. } while (((c->number) >= 10000000) || ((c->number) < 0)); // the id number of car
  193.  
  194. printf("\nEnter weight of car: ");
  195. _flushall();
  196. scanf("%f", &c->weight);
  197. while ((c->weight) <= 0)
  198. {
  199. printf("\nERROR!Please renter weight of car: ");
  200. _flushall();
  201. scanf("%f", &c->weight);
  202. }
  203. }
  204.  
  205.  
  206. int pop(stack_car_data *st, car_data *i) //Function that moved out a car from parking
  207. {
  208. if (!(stack_is_empty(st)));
  209. {
  210. *i = st->place[st->top--];
  211. RES = 1;
  212. }
  213. return(RES);
  214. }
  215.  
  216. int push(stack_car_data *st, car_data *i) //A function that take in a car int ot the parking
  217. {
  218. if (!(stack_is_full(st))) // cheking if stack is not full
  219. {
  220. st->place[++st->top] = *i;
  221. RES = 1; // car count increases in stack
  222. }
  223. return (RES);
  224. }
  225.  
  226. int top(stack_car_data *st, car_data *i) // Function that checks a car at the top parking
  227. {
  228. if (!(stack_is_empty(st)))
  229. {
  230. *i = st->place[st->top];
  231. RES = 1;
  232. }
  233. return(RES);
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement