Advertisement
wojtas626

[C] Jubiler (C)

Dec 20th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void clrScr()
  5. {
  6.     system("cls");
  7. }
  8.  
  9. int loadDiamonds(double **diamonds)
  10. {
  11.     int numberOfDiamonds, i;
  12.     double *pointer;
  13.     pointer = *diamonds;
  14.  
  15.     printf("Wprowadz liczbe diamentow: ");
  16.     scanf("%d", &numberOfDiamonds);
  17.     clrScr();
  18.  
  19.     pointer = (double*)malloc(sizeof(double) * numberOfDiamonds);
  20.  
  21.     for(i = 0; i < numberOfDiamonds; i++)
  22.     {
  23.         printf("Wprowadz wartosc %i diamentu: ", i + 1);
  24.         scanf("%lf", &pointer[i]);
  25.         clrScr();
  26.     }
  27.     *diamonds = pointer;
  28.  
  29.     return numberOfDiamonds;
  30. }
  31.  
  32.  
  33. int calculatePairs(double **prices, int numberOfDiams, double *diams)
  34. {
  35.     double *pricesPtr;
  36.     int numberOfPairs;
  37.     int i, n, k;
  38.  
  39.     if(numberOfDiams < 2)
  40.     {
  41.         numberOfPairs = 0;
  42.     }
  43.     else
  44.     {
  45.         n = 2;
  46.         for(i = 3; i <= numberOfDiams; i++)
  47.         {
  48.             n *= i;
  49.         }
  50.  
  51.         k = 1;
  52.         for(i = 2; i <= (numberOfDiams - 2); i++)
  53.         {
  54.             k *= i;
  55.         }
  56.         k *= 2;
  57.  
  58.         numberOfPairs = n / k;
  59.  
  60.         pricesPtr = *prices;
  61.         pricesPtr = (double*)malloc(sizeof(double)*numberOfPairs);
  62.  
  63.         if(numberOfPairs == 1)
  64.         {
  65.             pricesPtr[0] = diams[0] + diams[1];
  66.         }
  67.         else
  68.         {
  69.             n = 0;
  70.             for(i = 0; i < (numberOfDiams - 1); i++)
  71.             {
  72.                 for(k = i + 1; k < numberOfDiams; k++)
  73.                 {
  74.                     pricesPtr[n] = diams[i] + diams[k];
  75.                     n++;
  76.                 }
  77.             }
  78.         }
  79.     }
  80.     *prices = pricesPtr;
  81.  
  82.     return numberOfPairs;
  83. }
  84.  
  85. void checkPrice(double *prices, int diamonds, int pairs)
  86. {
  87.     int a, b, n, i;
  88.  
  89.     printf("Wprowadz numer 1 diamentu: ");
  90.     scanf("%d", &a);
  91.     clrScr();
  92.     printf("Wprowadz numer 2 diamentu: ");
  93.     scanf("%d", &b);
  94.     clrScr();
  95.  
  96.     if(b == a)
  97.     {
  98.         printf("BLAD!");
  99.     }
  100.     else
  101.     {
  102.         if(a > b)
  103.         {
  104.             int c;
  105.             c = b;
  106.             b = a;
  107.             a = c;
  108.         }
  109.  
  110.         n = 0;
  111.  
  112.         for(i = 0; i < (a - 1); i++)
  113.         {
  114.             n += diamonds - i - 1;
  115.         }
  116.         n += (b - a) - 1;
  117.  
  118.         printf("Cena za %d i %d diament to %lf", a, b, prices[n]);
  119.     }
  120. }
  121.  
  122.  
  123. int main()
  124. {
  125.     int numberOfDiamonds, numberOfUniquePairs;
  126.     double *diamonds;
  127.     double *prices;
  128.  
  129.     numberOfDiamonds = loadDiamonds(&diamonds);
  130.  
  131.     numberOfUniquePairs = calculatePairs(&prices, numberOfDiamonds, diamonds);
  132.  
  133.     checkPrice(prices, numberOfDiamonds, numberOfUniquePairs);
  134.  
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement