Advertisement
dllbridge

Untitled

Dec 2nd, 2022
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.29 KB | None | 0 0
  1.  
  2. #include   <stdio.h>
  3.  
  4.  
  5.  
  6.  
  7. struct Fraction
  8. {
  9.    
  10.     int    Numerator,                //    Числитель
  11.          Denominator;                //  Знаменатель
  12.    
  13. } ;
  14.  
  15.  
  16. Fraction aTest[10];
  17.  
  18. int test(int); //  Тестирует функцию "checking_the_correct_fraction()"
  19.                //  несколькими предопределёнными дробями.
  20.                //  Если дроби все правильные, то возвращает  1  (один)
  21.                //  Если хотя бы одна неправильная, то вернёт 0  (ноль)
  22.  
  23. int checking_the_correct_fraction(int k, int p);
  24.  
  25. ////////////////////////////////////////////////////
  26. int main()                                        //
  27. {
  28.  
  29.     if(test(2) == 1) printf("Test. All fractions are correct. \n");                 // Удалить после тестирования ф-ции.
  30.     else             printf("Test. Not all fractions are correct !!! \n");          // Удалить после тестирования ф-ции.
  31. }
  32.  
  33.  
  34.  
  35. //  Возвращает:
  36. //  = 0, если дробь неправильная
  37. //  = 1, если правильная 
  38. ////////////////////////////////////////////////////  k - это числитель
  39. int checking_the_correct_fraction(int k, int p)   //  p -   знаменатель
  40. {
  41.    
  42.     if(k >= p)  return 0;
  43.    
  44.    
  45.     while (p > 0)
  46.     {
  47.         int c = k % p;
  48.         k = p;
  49.         p = c;
  50.     }
  51.    
  52.     if(k != 1) return 0;    
  53.    
  54. return 1;    
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. //  Тестирует функцию "checking_the_correct_fraction()"
  65. //  несколькими предопределёнными дробями.
  66. //  Если дроби все правильные, то возвращает  1  (один)
  67. //  Если хотя бы одна неправильная, то вернёт 0  (ноль)
  68. //////////////////////////////////////////////////////////////////////
  69. int test(int n)                                                     //
  70. {
  71.    
  72.     switch(n)
  73.     {
  74.    
  75.        case  1:  //  Есть неправильные дроби
  76.    
  77.         if(checking_the_correct_fraction( 2,  5) == 0) return 0;          
  78.         if(checking_the_correct_fraction( 3,  7) == 0) return 0;
  79.         if(checking_the_correct_fraction(13, 14) == 0) return 0;
  80.         if(checking_the_correct_fraction(19, 20) == 0) return 0;    
  81.         if(checking_the_correct_fraction( 2,  6) == 0) return 0;
  82.         if(checking_the_correct_fraction( 3,  9) == 0) return 0;
  83.         if(checking_the_correct_fraction(13, 26) == 0) return 0;
  84.         if(checking_the_correct_fraction( 5, 20) == 0) return 0;    
  85.         return 1;
  86.        
  87.        case  2:  //  Все дроби правильные
  88.    
  89.         if(checking_the_correct_fraction( 2,  5) == 0) return 0;        
  90.         if(checking_the_correct_fraction( 3,  7) == 0) return 0;
  91.         if(checking_the_correct_fraction(13, 14) == 0) return 0;
  92.         if(checking_the_correct_fraction(19, 20) == 0) return 0;    
  93.         if(checking_the_correct_fraction( 5,  7) == 0) return 0;
  94.         if(checking_the_correct_fraction(13, 14) == 0) return 0;
  95.         if(checking_the_correct_fraction(15, 26) == 0) return 0;
  96.         if(checking_the_correct_fraction( 5, 19) == 0) return 0;    
  97.         return 1;  
  98.     }      
  99. }
  100.  
  101.  
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement