Advertisement
Kostiggig

Untitled

Nov 8th, 2022
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.50 KB | None | 0 0
  1. // lab 10
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include <stdbool.h>
  7. #include <locale.h>
  8.  
  9. void printOnNewLine(char src[]) {
  10.     printf("\n%s", src);
  11. }
  12.  
  13. void task1() {
  14.     printf("%d", 12 % 18);
  15.     int start, end, countOfSimpleNumber = 0;
  16.  
  17.     printOnNewLine("Введите интервал");
  18.  
  19.     printOnNewLine("start: ");
  20.     scanf("%d", &start);
  21.     printOnNewLine("end: ");
  22.     scanf("%d", &end);
  23.  
  24.     if (start <= end) {
  25.         for (int i = start; i <= end; i++)
  26.         {
  27.             bool isSimple = true;
  28.             int cached = sqrt(i);
  29.             for(int j = 2; j <= cached; j++) {
  30.                 if(i % j == 0) {
  31.                     isSimple = false;
  32.                     break;
  33.                 }
  34.             }
  35.             if(isSimple) {
  36.                 countOfSimpleNumber++;
  37.                 printf("\nnumber is %d", i);
  38.             }
  39.         }
  40.        
  41.     } else {
  42.         printOnNewLine("Error! Start must be less or equal to end");
  43.     }
  44.  
  45.     printf("\nCount of simple numbers is %d", countOfSimpleNumber);
  46. }
  47.  
  48. void requestRangeWithStep(int *start, int *end, int *step) {
  49.     printOnNewLine("Введите начало range: ");
  50.     scanf("%d", start);
  51.  
  52.     printOnNewLine("Введите конец range: ");
  53.     scanf("%d", end);
  54.  
  55.     printOnNewLine("Введите шаг: ");
  56.     scanf("%d", step);
  57. }
  58.  
  59. bool srcDataIsCorrected(int start, int end, int step) {
  60.     bool correted = true;
  61.    
  62.     if (start > end) {
  63.         printOnNewLine("Error! Start must be less or equal to end");
  64.         correted = false;
  65.     }
  66.  
  67.     if (step < 1) {
  68.         printOnNewLine("Error! Step must be more or equal to 1");
  69.         correted = false;
  70.     }
  71.     return correted;
  72. }
  73.  
  74. void task2() {
  75.     int h1, h2;
  76.     int A, B, C, D;
  77.     double f;
  78.     char charOfFunc;
  79.  
  80.     printOnNewLine("a - f(x) = (x * ln^2(y) / y) + (6 * y^2), y > 0");
  81.     printOnNewLine("b - f(x) = sin(x) / cos(y/x), x != 0, cos(y/x) != 10");
  82.  
  83.     printOnNewLine("Введите номер функции: ");
  84.     scanf(" %c", &charOfFunc);
  85.  
  86.     switch (charOfFunc) {
  87.         case 'a':
  88.         requestRangeWithStep(&A, &B, &h1);
  89.         if (srcDataIsCorrected(A, B, h1)) {
  90.  
  91.             requestRangeWithStep(&C, &D, &h2);
  92.  
  93.             if (srcDataIsCorrected(C, D, h2)) {
  94.                 printOnNewLine("|\t        x          \t|\t        y        \t|\t        f        \t|");
  95.  
  96.                 for(int x = A; x <= B; x += h1) {
  97.                     for(int y = C; y <= D; y += h2) {
  98.                         // (x * ln^2(y) / y) + (6 * y^2)
  99.                         if(y > 0) {
  100.                             f = (x * log((y)) * log((y)) / y) + (6 * y^2);
  101.                             printf("\n|\t        %d      \t|\t        %d      \t|\t     %.3lf      \t|",x, y, f);
  102.                         } else {
  103.                             printf("\n|\t        %d      \t|\t        %d      \t|\t         -        \t|", x, y);
  104.                         }
  105.                     }
  106.                 }
  107.  
  108.             }
  109.  
  110.         }
  111.         break;
  112.  
  113.         case 'b':
  114.         requestRangeWithStep(&A, &B, &h1);
  115.         if (srcDataIsCorrected(A, B, h1)) {
  116.  
  117.             requestRangeWithStep(&C, &D, &h2);
  118.  
  119.             if (srcDataIsCorrected(C, D, h2)) {
  120.                 printOnNewLine("|\t        x          \t|\t        y        \t|\t        f        \t|");
  121.  
  122.                 for(int x = A; x <= B; x += h1) {
  123.                     for(int y = C; y <= D; y += h2) {
  124.                         // sin(x) / cos(y/x)
  125.                         double divider = cos((int) (y/x));
  126.                         if(x == 0 || divider == 0) {
  127.                             printf("\n|\t        %d      \t|\t        %d      \t|\t         -        \t|", x, y);
  128.                         } else {
  129.                             f = sin(x) / divider;
  130.                             printf("\n|\t        %d      \t|\t        %d      \t|\t     %.3lf      \t|",x, y, f);
  131.                         }
  132.                     }
  133.                 }
  134.                
  135.             }
  136.  
  137.         }
  138.         break;
  139.         default:  printf("\nfunc by char %c doesn't exist!", charOfFunc);
  140.     }
  141.  
  142. }
  143.  
  144. void task3() {
  145.  
  146.     int current;
  147.     int prev;
  148.     int lastMin;
  149.     int maxDistanceBetweenMin = 0;
  150.     int currentDistanceBetweenMin = 0;
  151.     bool isOrdered = true;
  152.     bool sequenceIsBuilding = false;
  153.     bool isFirstEnter = true;
  154.  
  155.     do {
  156.         scanf("%d", &current);
  157.         if (isFirstEnter) {
  158.             prev = current;
  159.             isFirstEnter = false;
  160.         }
  161.        
  162.         if (current != -1)
  163.         {
  164.  
  165.             if(isOrdered) {
  166.                 if (prev > current)
  167.                 {
  168.                     isOrdered = false;
  169.                 }
  170.                
  171.             }
  172.            
  173.            
  174.            if (!sequenceIsBuilding)
  175.            {
  176.                 printf("current %d", current);
  177.                 sequenceIsBuilding = true;
  178.                 lastMin = current;
  179.            } else {
  180.                 if(lastMin <= current) {
  181.                     currentDistanceBetweenMin++;
  182.                 } else {
  183.                     if(currentDistanceBetweenMin > maxDistanceBetweenMin) {
  184.                         maxDistanceBetweenMin = currentDistanceBetweenMin;
  185.                     }
  186.                     lastMin = current;
  187.                     currentDistanceBetweenMin = 0;
  188.                 }
  189.            }
  190.  
  191.            prev = current;
  192.         }
  193.        
  194.     } while (current != -1);
  195.  
  196.     if (isOrdered)
  197.     {
  198.         printOnNewLine("Последовательнось являетеся упорядоченной");
  199.     } else {
  200.         printOnNewLine("Последовательнось не являетеся упорядоченной");
  201.         printf("\nМаксимальное расстояние между локальными минимумами последовательности %d", maxDistanceBetweenMin);
  202.     }
  203.    
  204.    
  205. }
  206.  
  207. int main() {
  208.     int numberOfTask = -1;
  209.     do
  210.     {
  211.         printf("\nВведите номер задачи(1, 2 или 3, 0 - для выхода): ");
  212.         scanf("%d", &numberOfTask);
  213.  
  214.         switch(numberOfTask) {
  215.             case 1: task1(); break;
  216.             case 2: task2(); break;
  217.             case 3: task3(); break;
  218.             default: {
  219.                 if (numberOfTask != 0) {
  220.                     printf("\nNumber of task %d doesn't exist!", numberOfTask);
  221.                 }
  222.             }
  223.         }
  224.     } while (numberOfTask != 0);
  225.    
  226.     return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement