Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define CAR_NAME 20
- #define CAR_ID 7
- #define N 2
- typedef struct
- {
- char type[CAR_NAME];
- int number[CAR_ID];
- float weight;
- }car;
- car car_info[N];
- car *sortinfo;
- typedef struct
- {
- int parking[N];
- int entrance[N];
- int top;
- }stack;
- typedef struct
- {
- car car_info[N];
- stack st;
- }Parking;
- Parking CityParking;
- void sort_car_info(car item[], int n);
- void get_info(car info[], int n);
- void push_from_parking(car item[], int n);
- int pop_parking(car item2[], int n);
- void main()
- {
- int temp;
- get_info(car_info, N);
- sort_car_info(car_info, N);
- push_from_parking(car_info, N - 1);
- temp = pop_parking(car_info, 0);
- }
- void get_info(car info[], int n)
- {
- int i, len;
- printf("Please enter car details for the parking slots: \n");
- for (i = 0; i < n; i++)
- {
- do
- {
- _flushall();
- printf("The car type is: \n");
- gets(info[i].type);
- } while (len = strlen(info[i].type) >= 20);
- do
- {
- _flushall();
- printf("The car number ID is: \n");
- scanf("%d", &info[i].number);
- } while (info[i].number <= 1000000 || info[i].number >= 9999999);
- do
- {
- _flushall();
- printf("The weight of ras is: \n");
- scanf("%f", &info[i].weight);
- } while (info[i].weight < 0);
- }
- }
- void push_from_parking(car item[], int n)
- {
- CityParking.st.top++;
- CityParking.st.parking[CityParking.st.top] = item[n].weight;
- }
- int pop(car item2[], int n)
- {
- int item;
- item = item2[n].number;
- CityParking.st.parking[CityParking.st.top];
- CityParking.st.top--;
- return item;
- }
- void sort_car_info(car item[], int n)
- {
- int i, j;
- car *sortinfo = (car*)malloc(1 * sizeof(car));
- if (!sortinfo)
- {
- printf("not enough space\n");
- exit(1);
- }
- for (i = 0; i < n; i++)
- {
- for (j = 0; i < n - j - 1; j++)
- {
- if (item[j].weight > item[j + 1].weight)
- {
- sortinfo[0].weight = item[j].weight;
- sortinfo[0].number = item[j].number;
- strcpy(sortinfo[0].type, item[j].type);
- item[j].weight = item[j + 1].weight;
- item[j].number = item[j + 1].number;
- strcpy(item[j].type, item[j + 1].type);
- item[j + 1].weight = sortinfo[0].weight;
- item[j + 1].number = sortinfo[0].number;
- strcpy(item[j + 1].type, sortinfo[0].type);
- }
- }
- }
- free(sortinfo);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement