Advertisement
ksyshshot

кт_4(структуры)

Nov 24th, 2023
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | Source Code | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <locale.h>
  6.  
  7. struct toy {
  8.     char name[50];
  9.     int price;
  10.     int count;
  11. };
  12.  
  13. void inputShop(struct toy* shop, int n)
  14. {
  15.     for (int i = 0; i < n; i++)
  16.     {
  17.         printf("Input toy №%d: ", (i + 1));
  18.         scanf("%s %d %d", &shop[i].name, &shop[i].price, &shop[i].count);
  19.     }
  20. }
  21.  
  22. void printStructs(struct toy* shop, int n)
  23. {
  24.     for (int i = 0; i < n; i++)
  25.     {
  26.         printf("Name: %s\t Price: %d\t Count: %d\n", shop[i].name, shop[i].price, shop[i].count);
  27.     }
  28. }
  29.  
  30. void main()
  31. {
  32.     //struct toy shop[3] = { {"Horse", 58, 15}, {"Princess", 72, 20}, {"Flowers", 24, 50}};
  33.     setlocale(LC_ALL, "Rus");
  34.     struct toy shop[3];
  35.     int n = sizeof(shop) / sizeof(shop[0]);
  36.     struct toy temp;
  37.     inputShop(shop, n);
  38.     printf("\n Shop assortment:\n\n");
  39.     printStructs(shop, n);
  40.     for (int i = 0; i < n; i++)
  41.     {
  42.         for (int j = 0; j < (n - 1); j++)
  43.         {
  44.             if (shop[j].price > shop[j + 1].price)
  45.             {
  46.                 temp = shop[j];
  47.                 shop[j] = shop[j + 1];
  48.                 shop[j + 1] = temp;
  49.             }
  50.         }
  51.     }
  52.     printf("\n Sorted:\n\n");
  53.     printStructs(shop, n);
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement