Advertisement
vencinachev

Exam-Ex.4

Mar 28th, 2022
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.54 KB | None | 0 0
  1. // Ex. 4
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <stdint.h>
  7. #include <math.h>
  8. #include <string.h>
  9.  
  10. #define COUNT 10
  11.  
  12. typedef struct {
  13.     char model[21];
  14.     uint8_t maxspeed;
  15.     double price;
  16. } Car;
  17.  
  18.  
  19. int randint(int,int);
  20. double randReal(double,double);
  21.  
  22. int compareMaxspeedASC(const void* ptrcar1, const void* ptrcar2);
  23. int compareMaxspeedDESC(const void* ptrcar1, const void* ptrcar2);
  24. int comparePriceDESC(const void* ptrcar1, const void* ptrcar2);
  25. int comparePriceASC(const void* ptrcar1, const void* ptrcar2);
  26. int compareModelASC(const void* ptrcar1, const void* ptrcar2);
  27. int compareModelDESC(const void* ptrcar1, const void* ptrcar2);
  28.  
  29. int main(){
  30.     srand(time(NULL));
  31.     Car cars[COUNT];
  32.     for (int i = 0; i < COUNT; i++){
  33.         cars[i].maxspeed = randint(50, 250);
  34.         cars[i].price = randReal(1, 2);
  35.  
  36.         cars[i].model[0] = randint('A', 'Z');
  37.         int symCount = randint(4, 10);
  38.         for (int j = 1; j <= symCount; j++){
  39.             cars[i].model[j] = randint('a', 'z');
  40.         }
  41.         cars[i].model[symCount + 1] = '\0';
  42.     }
  43.     int opt;
  44.     scanf("%d", &opt);
  45.  
  46.     switch (opt){
  47.        case 1:
  48.                 qsort(cars, COUNT, sizeof(*cars), compareModelASC);
  49.                 break;
  50.        case 2:
  51.                 qsort(cars, COUNT, sizeof(*cars), compareModelDESC);
  52.                 break;
  53.        case 3:
  54.                 qsort(cars, COUNT, sizeof(*cars), comparePriceDESC);
  55.                 break;
  56.        case 4:
  57.                 qsort(cars, COUNT, sizeof(*cars), compareModelDESC);
  58.                 break;
  59.        case 5:
  60.                 qsort(cars, COUNT, sizeof(*cars), compareModelASC);
  61.                 break;
  62.        case 6:
  63.                 qsort(cars, COUNT, sizeof(*cars), compareModelDESC);
  64.                 break;
  65.     }
  66.  
  67.     for (int i = 0; i < COUNT; i++){
  68.         printf("Model: %15s, Maxspeed: %3d, Price: %4.2lf\n", cars[i].model, cars[i].maxspeed, cars[i].price);
  69.     }
  70.  
  71. }
  72.  
  73. int randint(int min, int max){
  74.     return min + rand() % (max - min + 1);       // [min; max]
  75. }
  76.  
  77. double randReal(double min, double max){
  78.     return min + ((double)rand() / RAND_MAX) * (max - min);
  79. }
  80.  
  81. int compareMaxspeedASC(const void* ptrcar1, const void* ptrcar2){
  82.     // <0  0 >0
  83.     Car car1 = *(Car*)ptrcar1;
  84.     Car car2 = *(Car*)ptrcar2;
  85.     return car1.maxspeed - car2.maxspeed;
  86. }
  87.  
  88. int compareMaxspeedDESC(const void* ptrcar1, const void* ptrcar2){
  89.     // <0  0 >0
  90.     Car car1 = *(Car*)ptrcar1;
  91.     Car car2 = *(Car*)ptrcar2;
  92.     return car2.maxspeed - car1.maxspeed;
  93. }
  94.  
  95. int comparePriceDESC(const void* ptrcar1, const void* ptrcar2){
  96.     // <0  0 >0
  97.     Car car1 = *(Car*)ptrcar1;
  98.     Car car2 = *(Car*)ptrcar2;
  99.     if (fabs(car2.price - car1.price) < 0.01){
  100.         return 0;
  101.     } else if (car2.price > car1.price){
  102.         return 1;
  103.     }
  104.     return -1;
  105. }
  106.  
  107. int comparePriceASC(const void* ptrcar1, const void* ptrcar2){
  108.     // <0  0 >0
  109.     Car car1 = *(Car*)ptrcar1;
  110.     Car car2 = *(Car*)ptrcar2;
  111.     if (fabs(car2.price - car1.price) < 0.01){
  112.         return 0;
  113.     } else if (car2.price > car1.price){
  114.         return -1;
  115.     }
  116.     return 1;
  117. }
  118.  
  119. int compareModelASC(const void* ptrcar1, const void* ptrcar2){
  120.     // <0  0 >0
  121.     Car car1 = *(Car*)ptrcar1;
  122.     Car car2 = *(Car*)ptrcar2;
  123.     return strcmp(car1.model, car2.model);
  124. }
  125.  
  126. int compareModelDESC(const void* ptrcar1, const void* ptrcar2){
  127.     // <0  0 >0
  128.     Car car1 = *(Car*)ptrcar1;
  129.     Car car2 = *(Car*)ptrcar2;
  130.     return strcmp(car2.model, car1.model);
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement