Advertisement
mcgizmo

Untitled

Dec 4th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define CAR_NAME 20
  5. #define CAR_ID 7
  6. #define N 2
  7.  
  8. typedef struct
  9. {
  10. char type[CAR_NAME];
  11. int number[CAR_ID];
  12. float weight;
  13. }car;
  14. car car_info[N];
  15. car *sortinfo;
  16.  
  17. typedef struct
  18. {
  19. int parking[N];
  20. int entrance[N];
  21. int top;
  22. }stack;
  23.  
  24. typedef struct
  25. {
  26. car car_info[N];
  27. stack st;
  28. }Parking;
  29. Parking CityParking;
  30. void sort_car_info(car item[], int n);
  31. void get_info(car info[], int n);
  32. void push_from_parking(car item[], int n);
  33. int pop_parking(car item2[], int n);
  34.  
  35. void main()
  36. {
  37. int temp;
  38. get_info(car_info, N);
  39. sort_car_info(car_info, N);
  40. push_from_parking(car_info, N - 1);
  41. temp = pop_parking(car_info, 0);
  42. }
  43.  
  44. void get_info(car info[], int n)
  45. {
  46. int i, len;
  47. printf("Please enter car details for the parking slots: \n");
  48. for (i = 0; i < n; i++)
  49. {
  50. do
  51. {
  52. _flushall();
  53. printf("The car type is: \n");
  54. gets(info[i].type);
  55.  
  56. } while (len = strlen(info[i].type) >= 20);
  57.  
  58. do
  59. {
  60. _flushall();
  61. printf("The car number ID is: \n");
  62. scanf("%d", &info[i].number);
  63. } while (info[i].number <= 1000000 || info[i].number >= 9999999);
  64.  
  65. do
  66. {
  67. _flushall();
  68. printf("The weight of ras is: \n");
  69. scanf("%f", &info[i].weight);
  70. } while (info[i].weight < 0);
  71. }
  72. }
  73.  
  74. void push_from_parking(car item[], int n)
  75. {
  76. CityParking.st.top++;
  77. CityParking.st.parking[CityParking.st.top] = item[n].weight;
  78. }
  79.  
  80. int pop(car item2[], int n)
  81. {
  82. int item;
  83. item = item2[n].number;
  84. CityParking.st.parking[CityParking.st.top];
  85. CityParking.st.top--;
  86.  
  87. return item;
  88.  
  89. }
  90.  
  91. void sort_car_info(car item[], int n)
  92. {
  93. int i, j;
  94. car *sortinfo = (car*)malloc(1 * sizeof(car));
  95. if (!sortinfo)
  96. {
  97. printf("not enough space\n");
  98. exit(1);
  99. }
  100. for (i = 0; i < n; i++)
  101. {
  102. for (j = 0; i < n - j - 1; j++)
  103. {
  104. if (item[j].weight > item[j + 1].weight)
  105. {
  106. sortinfo[0].weight = item[j].weight;
  107. sortinfo[0].number = item[j].number;
  108. strcpy(sortinfo[0].type, item[j].type);
  109. item[j].weight = item[j + 1].weight;
  110. item[j].number = item[j + 1].number;
  111. strcpy(item[j].type, item[j + 1].type);
  112. item[j + 1].weight = sortinfo[0].weight;
  113. item[j + 1].number = sortinfo[0].number;
  114. strcpy(item[j + 1].type, sortinfo[0].type);
  115.  
  116. }
  117. }
  118. }
  119. free(sortinfo);
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement