fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define COUNT_ITEMS 4
  4.  
  5. typedef struct Product {
  6. char name[256];
  7. int day;
  8. int month;
  9. int year;
  10. int cost;
  11. int count;
  12. } Product;
  13. void printinfo(const Product *prod)
  14. {
  15. printf("Product name: %s\n", prod->name);
  16. printf("Date of purchase : %d.%d.%d\n", prod->day, prod->month, prod->year);
  17. printf("Product price: %d\n", prod->cost);
  18. printf("Number of products: %d\n", prod->count);
  19. }
  20.  
  21. Product *prodRead() {
  22. Product *prod;
  23. prod = malloc(sizeof(Product));
  24. printf("Enter name of a Product:\n");
  25. scanf("%s", &prod->name);
  26. printf("Enter day, month(number), year of purchase through a space:\n");
  27. scanf("%d%d%d", &prod->day, &prod->month, &prod->year);
  28. printf("Enter cost of product:\n");
  29. scanf("%d", &prod->cost);
  30. printf("Enter number of product:\n");
  31. scanf("%d", &prod->count);
  32. return prod;
  33. }
  34. int compMon(const void *a, const void *b) {
  35. const Product *partOne = *(const Product **)a;
  36. const Product *partTwo = *(const Product **)b;
  37. return ((*partOne).month > (*partTwo).month) - ((*partOne).month < (*partTwo).month);
  38. }
  39. int main() {
  40. Product *prod[COUNT_ITEMS];
  41. for (int i = 0; i < COUNT_ITEMS; i++) {
  42. prod[i]=prodRead();
  43. printf("\n");
  44. }
  45. qsort(prod, COUNT_ITEMS, sizeof(Product*), compMon);
  46. printf("Structura POSLE sortirovki \n");
  47. for (int i = 0; i < COUNT_ITEMS; i++) {
  48. printinfo(prod[i]);
  49. printf("\n");
  50. }
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 4148KB
stdin
A           
10 11 2020  
1000        
2           
B           
5 5 2019 
500         
3           
C           
6 6 2020    
600         
1           
D           
3 3 2018    
200         
2  
stdout
Enter name of a Product:
Enter day, month(number), year of purchase through a space:
Enter cost of product:
Enter number of product:

Enter name of a Product:
Enter day, month(number), year of purchase through a space:
Enter cost of product:
Enter number of product:

Enter name of a Product:
Enter day, month(number), year of purchase through a space:
Enter cost of product:
Enter number of product:

Enter name of a Product:
Enter day, month(number), year of purchase through a space:
Enter cost of product:
Enter number of product:

Structura POSLE sortirovki 
Product name: D
Date of purchase : 3.3.2018
Product price: 200
Number of products: 2

Product name: B
Date of purchase : 5.5.2019
Product price: 500
Number of products: 3

Product name: C
Date of purchase : 6.6.2020
Product price: 600
Number of products: 1

Product name: A
Date of purchase : 10.11.2020
Product price: 1000
Number of products: 2