Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lab 10
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <locale.h>
- void printOnNewLine(char src[]) {
- printf("\n%s", src);
- }
- void task1() {
- printf("%d", 12 % 18);
- int start, end, countOfSimpleNumber = 0;
- printOnNewLine("Введите интервал");
- printOnNewLine("start: ");
- scanf("%d", &start);
- printOnNewLine("end: ");
- scanf("%d", &end);
- if (start <= end) {
- for (int i = start; i <= end; i++)
- {
- bool isSimple = true;
- int cached = sqrt(i);
- for(int j = 2; j <= cached; j++) {
- if(i % j == 0) {
- isSimple = false;
- break;
- }
- }
- if(isSimple) {
- countOfSimpleNumber++;
- printf("\nnumber is %d", i);
- }
- }
- } else {
- printOnNewLine("Error! Start must be less or equal to end");
- }
- printf("\nCount of simple numbers is %d", countOfSimpleNumber);
- }
- void requestRangeWithStep(int *start, int *end, int *step) {
- printOnNewLine("Введите начало range: ");
- scanf("%d", start);
- printOnNewLine("Введите конец range: ");
- scanf("%d", end);
- printOnNewLine("Введите шаг: ");
- scanf("%d", step);
- }
- bool srcDataIsCorrected(int start, int end, int step) {
- bool correted = true;
- if (start > end) {
- printOnNewLine("Error! Start must be less or equal to end");
- correted = false;
- }
- if (step < 1) {
- printOnNewLine("Error! Step must be more or equal to 1");
- correted = false;
- }
- return correted;
- }
- void task2() {
- int h1, h2;
- int A, B, C, D;
- double f;
- char charOfFunc;
- printOnNewLine("a - f(x) = (x * ln^2(y) / y) + (6 * y^2), y > 0");
- printOnNewLine("b - f(x) = sin(x) / cos(y/x), x != 0, cos(y/x) != 10");
- printOnNewLine("Введите номер функции: ");
- scanf(" %c", &charOfFunc);
- switch (charOfFunc) {
- case 'a':
- requestRangeWithStep(&A, &B, &h1);
- if (srcDataIsCorrected(A, B, h1)) {
- requestRangeWithStep(&C, &D, &h2);
- if (srcDataIsCorrected(C, D, h2)) {
- printOnNewLine("|\t x \t|\t y \t|\t f \t|");
- for(int x = A; x <= B; x += h1) {
- for(int y = C; y <= D; y += h2) {
- // (x * ln^2(y) / y) + (6 * y^2)
- if(y > 0) {
- f = (x * log((y)) * log((y)) / y) + (6 * y^2);
- printf("\n|\t %d \t|\t %d \t|\t %.3lf \t|",x, y, f);
- } else {
- printf("\n|\t %d \t|\t %d \t|\t - \t|", x, y);
- }
- }
- }
- }
- }
- break;
- case 'b':
- requestRangeWithStep(&A, &B, &h1);
- if (srcDataIsCorrected(A, B, h1)) {
- requestRangeWithStep(&C, &D, &h2);
- if (srcDataIsCorrected(C, D, h2)) {
- printOnNewLine("|\t x \t|\t y \t|\t f \t|");
- for(int x = A; x <= B; x += h1) {
- for(int y = C; y <= D; y += h2) {
- // sin(x) / cos(y/x)
- double divider = cos((int) (y/x));
- if(x == 0 || divider == 0) {
- printf("\n|\t %d \t|\t %d \t|\t - \t|", x, y);
- } else {
- f = sin(x) / divider;
- printf("\n|\t %d \t|\t %d \t|\t %.3lf \t|",x, y, f);
- }
- }
- }
- }
- }
- break;
- default: printf("\nfunc by char %c doesn't exist!", charOfFunc);
- }
- }
- void task3() {
- int current;
- int prev;
- int lastMin;
- int maxDistanceBetweenMin = 0;
- int currentDistanceBetweenMin = 0;
- bool isOrdered = true;
- bool sequenceIsBuilding = false;
- bool isFirstEnter = true;
- do {
- scanf("%d", ¤t);
- if (isFirstEnter) {
- prev = current;
- isFirstEnter = false;
- }
- if (current != -1)
- {
- if(isOrdered) {
- if (prev > current)
- {
- isOrdered = false;
- }
- }
- if (!sequenceIsBuilding)
- {
- printf("current %d", current);
- sequenceIsBuilding = true;
- lastMin = current;
- } else {
- if(lastMin <= current) {
- currentDistanceBetweenMin++;
- } else {
- if(currentDistanceBetweenMin > maxDistanceBetweenMin) {
- maxDistanceBetweenMin = currentDistanceBetweenMin;
- }
- lastMin = current;
- currentDistanceBetweenMin = 0;
- }
- }
- prev = current;
- }
- } while (current != -1);
- if (isOrdered)
- {
- printOnNewLine("Последовательнось являетеся упорядоченной");
- } else {
- printOnNewLine("Последовательнось не являетеся упорядоченной");
- printf("\nМаксимальное расстояние между локальными минимумами последовательности %d", maxDistanceBetweenMin);
- }
- }
- int main() {
- int numberOfTask = -1;
- do
- {
- printf("\nВведите номер задачи(1, 2 или 3, 0 - для выхода): ");
- scanf("%d", &numberOfTask);
- switch(numberOfTask) {
- case 1: task1(); break;
- case 2: task2(); break;
- case 3: task3(); break;
- default: {
- if (numberOfTask != 0) {
- printf("\nNumber of task %d doesn't exist!", numberOfTask);
- }
- }
- }
- } while (numberOfTask != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement