Advertisement
HugoBallee

GLIN202/TP5-6

Mar 5th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. // tp56.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <math.h>
  6.  
  7. int identiques (int *a, int *b);
  8. int valeursIidentiques (int *a, int *b);
  9. void echange (int *a, int *b);
  10. void saisieTemps(int *heures, int *minutes, int *secondes);
  11. int tempsValide(int *heures, int *minutes, int *secondes);
  12.  
  13. float discriminant(float a, float b, float c);
  14. int nmbreRacines(float a, float b, float c);
  15.  
  16. int resolutionEqua2ndDegre(float a, float b, float c, float *x1, float *x2);
  17.  
  18. void onePGCDstep(int *a, int *b);
  19. int pgcd(int a, int b);
  20.  
  21.  
  22. int _tmain(int argc, _TCHAR* argv[])
  23. {
  24.     int a, b;
  25.  
  26.     printf("a : ");
  27.     scanf("%d", &a);
  28.     printf("b : ");
  29.     scanf("%d", &b);
  30.  
  31.     printf("pgcd(%d, %d) = %d\n", a, b, pgcd(a, b));
  32.  
  33.     scanf("\n");
  34.     return 0;
  35. }
  36.  
  37. int identiques (int *a, int *b)
  38. {
  39.     if (a == b)
  40.         return 1;
  41.     else
  42.         return 0;
  43. }
  44.  
  45. int valeursIidentiques (int *a, int *b)
  46. {
  47.     printf("pendant a : %d\n", *a);
  48.     printf("pendant a : %d\n", *b);
  49.     if (*a == *b)
  50.         return 1;
  51.     else
  52.         return 0;
  53. }
  54.  
  55. void echange (int *a, int *b) {
  56.     int t = *a;
  57.     *a = *b;
  58.     *b = t;
  59. }
  60.  
  61. void saisieTemps(int *heures, int *minutes, int *secondes)
  62. {
  63.     do {
  64.         printf("hh:mm:ss : ");
  65.         scanf("%d:%d:%d", heures, minutes, secondes);
  66.     } while (!tempsValide(heures, minutes, secondes));
  67. }
  68.  
  69. int tempsValide(int *heures, int *minutes, int *secondes)
  70. {
  71.     if (*secondes < 0 || *secondes > 60)
  72.         return 0;
  73.     if (*minutes < 0 || *minutes > 60)
  74.         return 0;
  75.     if (*heures < 0)
  76.         return 0;
  77.  
  78.     return 1;
  79. }
  80.  
  81. float discriminant(float a, float b, float c)
  82. {
  83.         return b*b - 4*a*c;
  84. }
  85.  
  86. int nmbreRacines(float a, float b, float c)
  87. {
  88.         float discrim = discriminant(a, b, c);
  89.         if (discrim > 0.) {
  90.                 return 2;
  91.         } else if (discrim == 0.) {
  92.                 return 1;
  93.         } else {
  94.                 return 0;
  95.         }
  96. }
  97.  
  98. int resolutionEqua2ndDegre(float a, float b, float c, float *x1, float *x2)
  99. {
  100.     float discri = discriminant(a, b, c);
  101.  
  102.     switch (nmbreRacines(a, b, c)) {
  103.     case 2:
  104.             *x1 = (-b-sqrt(discriminant(a, b, c))) / (2*a);
  105.             *x2 = (-b+sqrt(discriminant(a, b, c))) / (2*a);
  106.             return 2;
  107.             break;
  108.     case 1:
  109.             *x1 = -b/(2.*a);
  110.             *x2 = *x1;
  111.             return 1;
  112.             break;
  113.     default:
  114.             *x1 = -1.;
  115.             *x2 = -1.;
  116.             return 0;
  117.             break;
  118.     }
  119. }
  120.  
  121. void onePGCDstep(int *a, int *b)
  122. {
  123.     int t = *b;
  124.     *b = *a % t;
  125.     *a = t;
  126. }
  127.  
  128. int pgcd(int a, int b)
  129. {
  130.     int c = a;
  131.     int d = b;
  132.     while (d > 0) {
  133.         onePGCDstep(&c, &d);
  134.     }
  135.     return c;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement