Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. typedef struct
  5. {
  6.     char product_name[25];
  7.     double price;
  8. } Product;
  9.  
  10. void ordenation(double array[], int i, int j, int aux, int tamanho) {
  11.     if (i != tamanho - 1)
  12.     {
  13.         if (j < tamanho)
  14.         {
  15.             if (array[i] > array[j])
  16.             {
  17.                 aux = array[i];
  18.                 array[i] = array[j];
  19.                 array[j] = aux;
  20.             }
  21.             ordenation(array, i, ++j, aux, tamanho);
  22.         }
  23.         ordenation(array, ++i, j, aux, tamanho);
  24.     }
  25. }
  26.  
  27. int main()
  28. {
  29.     double money, products_sum = 0; /* products_sum vai ser a soma dos preços dos produtos para verificar até qual o usuário pode comprar */
  30.     int number_of_items, i;
  31.  
  32.     scanf("%lf\n", &money);
  33.     scanf("%d\n", &number_of_items);
  34.  
  35.     Product products[number_of_items];
  36.  
  37.     for (i = 0; i < number_of_items; i++)
  38.     {
  39.         scanf("%s %lf", products[i].product_name, &products[i].price);
  40.     }
  41.  
  42.     ordenation(products.price, 0, 0, 0, number_of_items);
  43.  
  44.     for (i = 0; i < number_of_items; i++)
  45.     {
  46.         printf("%s %.2lf\n", products[i].product_name, products[i].price);
  47.     }
  48.  
  49.     for (i = 0; i < number_of_items; i++)
  50.     {
  51.         if ((money - products_sum) > 0)
  52.         {
  53.             products_sum += products[i].price;
  54.         }
  55.         else
  56.         {
  57.             /* TODO: imprimir todos os itens que, somados, não ultrapassem o valor em dinheiro */
  58.         }
  59.     }
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement