Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************************/
- /*Name: Liav Vaknin, Class: 41/5, ID: 303030340.*/
- /*Name: Alex Naidyonkov, Class: 41/5, ID: 321055071 */
- /****************************************************************************************/
- /*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.
- 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.
- Make parking holder data structure that allows to maintain all the details of the cars in the parking slot.
- Data structure to support the following actions:
- • Exiting Car from parking. Action should accept the proposed data structure and details of the vehicle and put it in the parking slot.
- • 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.
- For every car has to keep the following information:
- Type of car (20 characters maximum)
- Vehicle number (integer has 7 digits)
- (Kg) weight of the vehicle (a real number)
- */
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define N 20
- #define SIZE 20
- #define PARK_IS_FULL 1
- #define CAR_CALLING 2
- #define CAR_PULL_OUT 3
- #define FIRST_CAR_DATA 4
- #define CASE_EXIT 5
- static int RES = 0;
- typedef struct // Data structure for cars
- {
- char type[SIZE];
- int number;
- float weight;
- }car_data;
- typedef struct // Data structure for parking
- {
- car_data place[N]; // Parking slots
- int top; // Index of head of parking
- }stack_car_data;
- // Statement of Functions
- int create_stack(stack_car_data *st);
- int stack_is_empty(stack_car_data *st);
- int stack_is_full(stack_car_data *st);
- void CAR_INFO(car_data *c);
- int pop(stack_car_data *st, car_data *i);
- int push(stack_car_data *st, car_data *i);
- int top(stack_car_data *st, car_data *i);
- void main()
- {
- stack_car_data parking, temp_parking;
- car_data car, c; // creating car information
- int prk, n, i;
- prk = create_stack(&parking); //creating a parking
- prk = create_stack(&temp_parking); // creating a temporary parking
- while (prk)
- {
- printf("\n***************************************************************\n\n");
- printf("Menu choice parking and car information:\n\n");
- printf("Please choice from the menu:\n\n");
- // Menu for user choice
- printf("[1] Cheking if parking is full or not\n");
- printf("[2] Enter a car to parking slot\n");
- printf("[3] Take out car from parking slot by number\n");
- printf("[4] Print data information about first car in parking\n");
- printf("[5] Exit from menu\n\n");
- scanf("%d", &i);
- _flushall();
- switch (i) // switch for cheking a parking slots for empty or full
- {
- case PARK_IS_FULL: // parking full cheking and return a empty slots
- {
- if (!stack_is_empty(&parking))
- {
- system("cls");
- printf("Full parking with %d\n", RES);
- }
- else
- {
- int x;
- x = N - RES;
- printf("Available places: %d\n", x);
- }
- break;
- }
- case CAR_CALLING:
- {
- system("cls");
- if ((stack_is_empty(&parking)) || (!(stack_is_full(&parking)))) // cheking stack is empty
- {
- CAR_INFO(&car); // function for create a data information about a car
- if (!(stack_is_empty(&parking)))
- {
- top(&parking, &c); // cheking a top parking slot
- while (((c.weight)<(car.weight)) && ((parking.top) != (-1))) // comparing a weight of cars
- {
- prk = pop(&parking, &c); // removing a car from the parking slot
- prk = push(&temp_parking, &c); // add a car from in to the temporary parking slot
- top(&parking, &c); // cheking the car on top parging slot
- }
- }
- prk = push(&parking, &car); // removing a car from the parking slot
- while (temp_parking.top != -1) // do a loop if we have cars on temporary slot
- {
- prk = pop(&temp_parking, &c); // removing a car from the temporary parking slot
- prk = push(&parking, &c); // add a car from in to the parking slot
- }
- }
- break; }
- case CAR_PULL_OUT: // removing a car from parking
- {
- system("cls");
- printf("Plese enter a number of car what you need remove: ");
- scanf("%d", &n);
- if (!(stack_is_empty(&parking)) || (top(&parking, &c))) // Checking the parking slots not full or have on top a car
- {
- while ((n != c.number) && !(parking.top == -1)) //Checking the car with number at the top
- {
- prk = pop(&parking, &c); // removing a car from the parking slot
- prk = push(&temp_parking, &c); // add a car from in to the temporary parking slot
- top(&parking, &c); // cheking the car on top parging slot
- }
- if (parking.top == -1) // cheking if we still have a car on parking
- printf("Error!Car not finding"); // if not print error
- else
- prk = pop(&parking, &c); // removing a car from the parking slot
- while (temp_parking.top != -1) // do the loop if we still have a cars on temporary parking
- {
- prk = pop(&temp_parking, &c); // removing a car from the temporary parking slot
- prk = push(&parking, &c); // add a car from in to the parking slot
- }
- }
- break; }
- case FIRST_CAR_DATA: // printing a data information about car in top parking place
- {
- system("cls");
- if (top(&parking, &c))
- printf("The type is:%s with number:%07d ,car withe: %.2f", c.type, c.number, c.weight);
- break; }
- case CASE_EXIT: // exiting from menu
- {
- prk = 0;
- break;
- }
- }
- }
- }
- int create_stack(stack_car_data *st) // creating a stack for car parking
- {
- return(st->top = -1);
- }
- int stack_is_empty(stack_car_data *st) //Function that checks if the parking slot is empty
- {
- return(st->top == -1);
- }
- int stack_is_full(stack_car_data *st) //Function that checks if the parking slot is full
- {
- return(st->top == (N - 1));
- }
- void CAR_INFO(car_data *c) // function for entering a car information
- {
- int length;
- do
- {
- _flushall();
- printf("Please enter a car type: ");
- gets(c->type);
- } while (length = strlen(c->type) >= 20);
- do
- {
- _flushall();
- printf("\nEnter car number (7 digits): ");
- scanf("%d", &c->number);
- } while (((c->number) >= 10000000) || ((c->number) < 0)); // the id number of car
- printf("\nEnter weight of car: ");
- _flushall();
- scanf("%f", &c->weight);
- while ((c->weight) <= 0)
- {
- printf("\nERROR!Please renter weight of car: ");
- _flushall();
- scanf("%f", &c->weight);
- }
- }
- int pop(stack_car_data *st, car_data *i) //Function that moved out a car from parking
- {
- if (!(stack_is_empty(st)));
- {
- *i = st->place[st->top--];
- RES = 1;
- }
- return(RES);
- }
- int push(stack_car_data *st, car_data *i) //A function that take in a car int ot the parking
- {
- if (!(stack_is_full(st))) // cheking if stack is not full
- {
- st->place[++st->top] = *i;
- RES = 1; // car count increases in stack
- }
- return (RES);
- }
- int top(stack_car_data *st, car_data *i) // Function that checks a car at the top parking
- {
- if (!(stack_is_empty(st)))
- {
- *i = st->place[st->top];
- RES = 1;
- }
- return(RES);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement