Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Ex. 4
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <stdint.h>
- #include <math.h>
- #include <string.h>
- #define COUNT 10
- typedef struct {
- char model[21];
- uint8_t maxspeed;
- double price;
- } Car;
- int randint(int,int);
- double randReal(double,double);
- int compareMaxspeedASC(const void* ptrcar1, const void* ptrcar2);
- int compareMaxspeedDESC(const void* ptrcar1, const void* ptrcar2);
- int comparePriceDESC(const void* ptrcar1, const void* ptrcar2);
- int comparePriceASC(const void* ptrcar1, const void* ptrcar2);
- int compareModelASC(const void* ptrcar1, const void* ptrcar2);
- int compareModelDESC(const void* ptrcar1, const void* ptrcar2);
- int main(){
- srand(time(NULL));
- Car cars[COUNT];
- for (int i = 0; i < COUNT; i++){
- cars[i].maxspeed = randint(50, 250);
- cars[i].price = randReal(1, 2);
- cars[i].model[0] = randint('A', 'Z');
- int symCount = randint(4, 10);
- for (int j = 1; j <= symCount; j++){
- cars[i].model[j] = randint('a', 'z');
- }
- cars[i].model[symCount + 1] = '\0';
- }
- int opt;
- scanf("%d", &opt);
- switch (opt){
- case 1:
- qsort(cars, COUNT, sizeof(*cars), compareModelASC);
- break;
- case 2:
- qsort(cars, COUNT, sizeof(*cars), compareModelDESC);
- break;
- case 3:
- qsort(cars, COUNT, sizeof(*cars), comparePriceDESC);
- break;
- case 4:
- qsort(cars, COUNT, sizeof(*cars), compareModelDESC);
- break;
- case 5:
- qsort(cars, COUNT, sizeof(*cars), compareModelASC);
- break;
- case 6:
- qsort(cars, COUNT, sizeof(*cars), compareModelDESC);
- break;
- }
- for (int i = 0; i < COUNT; i++){
- printf("Model: %15s, Maxspeed: %3d, Price: %4.2lf\n", cars[i].model, cars[i].maxspeed, cars[i].price);
- }
- }
- int randint(int min, int max){
- return min + rand() % (max - min + 1); // [min; max]
- }
- double randReal(double min, double max){
- return min + ((double)rand() / RAND_MAX) * (max - min);
- }
- int compareMaxspeedASC(const void* ptrcar1, const void* ptrcar2){
- // <0 0 >0
- Car car1 = *(Car*)ptrcar1;
- Car car2 = *(Car*)ptrcar2;
- return car1.maxspeed - car2.maxspeed;
- }
- int compareMaxspeedDESC(const void* ptrcar1, const void* ptrcar2){
- // <0 0 >0
- Car car1 = *(Car*)ptrcar1;
- Car car2 = *(Car*)ptrcar2;
- return car2.maxspeed - car1.maxspeed;
- }
- int comparePriceDESC(const void* ptrcar1, const void* ptrcar2){
- // <0 0 >0
- Car car1 = *(Car*)ptrcar1;
- Car car2 = *(Car*)ptrcar2;
- if (fabs(car2.price - car1.price) < 0.01){
- return 0;
- } else if (car2.price > car1.price){
- return 1;
- }
- return -1;
- }
- int comparePriceASC(const void* ptrcar1, const void* ptrcar2){
- // <0 0 >0
- Car car1 = *(Car*)ptrcar1;
- Car car2 = *(Car*)ptrcar2;
- if (fabs(car2.price - car1.price) < 0.01){
- return 0;
- } else if (car2.price > car1.price){
- return -1;
- }
- return 1;
- }
- int compareModelASC(const void* ptrcar1, const void* ptrcar2){
- // <0 0 >0
- Car car1 = *(Car*)ptrcar1;
- Car car2 = *(Car*)ptrcar2;
- return strcmp(car1.model, car2.model);
- }
- int compareModelDESC(const void* ptrcar1, const void* ptrcar2){
- // <0 0 >0
- Car car1 = *(Car*)ptrcar1;
- Car car2 = *(Car*)ptrcar2;
- return strcmp(car2.model, car1.model);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement