diff --git a/13_Maksimova/contr1.c b/13_Maksimova/contr1.c new file mode 100644 index 0000000..8ef2d8c --- /dev/null +++ b/13_Maksimova/contr1.c @@ -0,0 +1,53 @@ +#include "stdio.h" +#include"stdlib.h" +#include"locale.h" +int unic(int* a, int s, int x) { + for (int i = 0; i < s; i++) { + if (a[i] == x) { + return 0; + } + } + return 1; +}int main() { + setlocale(LC_ALL, "Rus"); + int* a1, * a2, * ob; + int s1, s2, obs = 0; + printf("Введите размер первого массива:"); + scanf_s("%d", &s1); + a1 = (int*)malloc(s1 * sizeof(int)); + printf("Введите элементы первого массива:\n"); + for (int i = 0; i < s1; i++) { + scanf_s("%d", &a1[i]); + } + printf("Введите размер второго массива:"); + scanf_s("%d", &s2); + a2 = (int*)malloc(s2 * sizeof(int)); + printf("Введите элементы второго массива:\n"); + for (int i = 0; i < s2; i++) + { + scanf_s("%d", &a2[i]); + } + ob = (int*)malloc((s1 + 2) * sizeof(int)); + for (int i = 0; i < s1; i++) { + if (unic(ob, obs, a1[i])) + { + ob[obs++] = a1[i]; + } + } + for (int i = 0; i < s2; i++) { + if (unic(ob, obs, a2[i])) + { + ob[obs++] = a2[i]; + } + } + printf("Массив объединения:\n"); + for (int i = 0; i < obs; i++) + { + printf("%d", ob[i]); + } + printf("\n"); + free(a1); + free(a2); + free(ob); + return 0; +} \ No newline at end of file diff --git a/13_Maksimova/contr2.c b/13_Maksimova/contr2.c new file mode 100644 index 0000000..fc13f5e --- /dev/null +++ b/13_Maksimova/contr2.c @@ -0,0 +1,88 @@ +#include +#include +#include +#include + + +void fillMatrix(double** matrix, int m, int n) { + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + matrix[i][j] = (double)(rand() % 100) / 10; + } + } +} + +void printMatrix(double** matrix, int m, int n) { + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + printf("%.2f ", matrix[i][j]); + } + printf("\n"); + } +} + +double calculateAverage(double** matrix, int m, int n) { + double sum = 0.0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + sum += matrix[i][j]; + } + } + return sum / (m * n); +} + +int findColumnWithMostAboveAverage(double** matrix, int m, int n, double average, int* columnIndex) { + int maxCount = 0; + *columnIndex = -1; + + for (int j = 0; j < n; j++) { + int count = 0; + for (int i = 0; i < m; i++) { + if (matrix[i][j] > average) { + count++; + } + } + if (count > maxCount) { + maxCount = count; + *columnIndex = j; + } + } + + return maxCount; +} + +int main() { + int m, n; + setlocale(LC_ALL, "Rus"); + srand(time(NULL)); + printf("Введите количество строк: "); + scanf_s("%d", &m); + printf("Введите количество столбцов: "); + scanf_s("%d", &n); + double** matrix = (double**)malloc(m * sizeof(double*)); + for (int i = 0; i < m; i++) { + matrix[i] = (double*)malloc(n * sizeof(double)); + } + fillMatrix(matrix, m, n); + printf("Матрица:\n"); + printMatrix(matrix, m, n); + double average = calculateAverage(matrix, m, n); + printf("Среднее значение в матрице: %.2f\n", average); + int columnIndex; + int countAboveAverage = findColumnWithMostAboveAverage(matrix, m, n, average, &columnIndex); + + if (columnIndex != -1) { + printf("Столбец с наибольшим количеством значений больше среднего: %d\n", columnIndex + 1); + printf("Количество значений в этом столбце больше среднего: %d\n", countAboveAverage); + } + else { + printf("Нет значений больше среднего.\n"); + } + + for (int i = 0; i < m; i++) { + free(matrix[i]); + } + free(matrix); + + return 0; +} diff --git a/13_Maksimova/dv14.c b/13_Maksimova/dv14.c new file mode 100644 index 0000000..7d53526 --- /dev/null +++ b/13_Maksimova/dv14.c @@ -0,0 +1,60 @@ +#include +#include + +void printMatrix(int matrix[100][100], int a, int b) { + for (int i = 0; i < a; i++) { + for (int j = 0; j < b; j++) { + printf("%d ", matrix[i][j]); + } + printf("\n"); + } +} + +void removeColumn(int matrix[100][100], int* a, int* b, int colToRemove) { + for (int i = 0; i < *a; i++) { + for (int j = colToRemove; j < *b - 1; j++) { + matrix[i][j] = matrix[i][j + 1]; + } + } + (*b)--; +} + +int main() { + int matrix[100][100]; + int a, b; + setlocale(LC_ALL, "Rus"); + printf("Введите количество строк и столбцов матрицы: "); + scanf_s("%d %d", &a, &b); + + printf("Введите элементы матрицы:\n"); + for (int i = 0; i < a; i++) { + for (int j = 0; j < b; j++) { + scanf_s("%d", &matrix[i][j]); + } + } + int minElement; + int minColIndex = -1; + + minElement = matrix[0][0]; + minColIndex = 0; + + for (int j = 0; j < b; j++) { + for (int i = 0; i < a; i++) { + if (matrix[i][j] < minElement) { + minElement = matrix[i][j]; + minColIndex = j; + } + } + } + + if (minColIndex != -1) { + removeColumn(matrix, &a, &b, minColIndex); + printf("\nМатрица после удаления столбца с минимальным элементом (%d):\n", minElement); + printMatrix(matrix, a, b); + } + else { + printf("Не удалось найти минимальный элемент.\n"); + } + + return 0; +} \ No newline at end of file diff --git a/13_Maksimova/dv22.c b/13_Maksimova/dv22.c new file mode 100644 index 0000000..c33a0fb --- /dev/null +++ b/13_Maksimova/dv22.c @@ -0,0 +1,60 @@ +#include +#include + +// Функция для нахождения максимального количества подряд идущих свободных мест +void findMaxFreeSeats(int train[100][100], int wagons, int seats, int* maxFree, int* wagonNumber) { + *maxFree = 0; + *wagonNumber = -1; + + for (int i = 0; i < wagons; i++) { + int currentFree = 0; + + for (int j = 0; j < seats; j++) { + if (train[i][j] == 0) { + currentFree++; + } + else { + if (currentFree > *maxFree) { + *maxFree = currentFree; + *wagonNumber = i + 1; + } + currentFree = 0; + } + } + + if (currentFree > *maxFree) { + *maxFree = currentFree; + *wagonNumber = i + 1; + } + } +} + +int main() { + setlocale(LC_ALL, "Rus"); + int train[100][100]; + int wagons, seats; + + printf("Введите количество вагонов: "); + scanf_s("%d", &wagons); + printf("Введите количество мест в каждом вагоне: "); + scanf_s("%d", &seats); + + for (int i = 0; i < wagons; i++) { + printf("Введите данные для вагона %d (0 - свободно, 1 - занято):\n", i + 1); + for (int j = 0; j < seats; j++) { + scanf_s("%d", &train[i][j]); + } + } + int maxFree, wagonNumber; + findMaxFreeSeats(train, wagons, seats, &maxFree, &wagonNumber); + + if (wagonNumber != -1) { + printf("Максимальное количество подряд идущих свободных мест: %d\n", maxFree); + printf("Номер вагона с этим количеством свободных мест: %d\n", wagonNumber); + } + else { + printf("Все места заняты.\n"); + } + + return 0; +} diff --git a/13_Maksimova/kassa.c b/13_Maksimova/kassa.c new file mode 100644 index 0000000..4fb7937 --- /dev/null +++ b/13_Maksimova/kassa.c @@ -0,0 +1,117 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include + + +typedef struct { + char name[20]; + double price_per_kg; +} Product; + +// Функция для приведения строки к нижнему регистру +void to_lower_case(char* str) { + for (int i = 0; str[i]; i++) { + str[i] = tolower(str[i]); + } +} + +// Функция для чтения списка товаров с консоли +void read_purchase_list(char purchases[100][256], int* count) { + setlocale(LC_ALL, "Rus"); + printf("Введите список покупок (введите 'end' для завершения ввода):\n"); + *count = 0; + while (*count < 100) { + fgets(purchases[*count], 256, stdin); + purchases[*count][strcspn(purchases[*count], "\n")] = 0; + if (strcmp(purchases[*count], "end") == 0) { + break; + } + (*count)++; + } +} + +// Функция для разбора строки на количество и название товара +void parse_purchase(const char* line, double* quantity, char* name) { + char* token; + char line_copy[256]; + strcpy(line_copy, line); + + *quantity = 0.0; + name[0] = '\0'; + + token = strtok(line_copy, " "); + while (token != NULL) { + char* endptr; + double value = strtod(token, &endptr); + if (endptr != token) { + *quantity += value; + } + else { + strcat(name, token); + strcat(name, " "); + } + token = strtok(NULL, " "); + } + name[strlen(name) - 1] = '\0'; +} + +// Функция для формирования чека +void generate_check(Product stock[], int stock_count, char purchases[100][256], int purchase_count) { + FILE* check_file = fopen("check.txt", "w"); + if (check_file == NULL) { + printf("Ошибка создания файла чека.\n"); + return; + } + + double total_sum = 0.0; + fprintf(check_file, "Чек:\n"); + fprintf(check_file, "-----------------------------------\n"); + for (int i = 0; i < purchase_count; i++) { + double quantity = 0.0; + char name[256] = ""; + parse_purchase(purchases[i], &quantity, name); + to_lower_case(name); + + for (int j = 0; j < stock_count; j++) { + char stock_name[256]; + strcpy(stock_name, stock[j].name); + to_lower_case(stock_name); + + if (strcmp(stock_name, name) == 0) { + double cost = quantity * stock[j].price_per_kg; + fprintf(check_file, "%-15s %2.1f кг * %-10.2f = %.2f\n", stock[j].name, quantity, stock[j].price_per_kg, cost); + total_sum += cost; + break; + } + } + } + + fprintf(check_file, "-----------------------------------\n"); + fprintf(check_file, "Общая сумма: %.2f\n", total_sum); + fclose(check_file); + printf("Чек успешно сформирован в файл check.txt\n"); +} + +int main() { + Product stock[] = { + {"banana", 105.0}, + {"tomato", 300.0}, + {"kiwi", 400.0}, + {"apple", 150.0}, + {"cucumber", 200.0}, + {"potato", 50.0} + }; + int stock_count = sizeof(stock) / sizeof(stock[0]); + + char purchases[100][256]; + int purchase_count = 0; + + read_purchase_list(purchases, &purchase_count); + generate_check(stock, stock_count, purchases, purchase_count); + + return 0; +} + diff --git a/13_Maksimova/lab1.c b/13_Maksimova/lab1.c new file mode 100644 index 0000000..891cd56 --- /dev/null +++ b/13_Maksimova/lab1.c @@ -0,0 +1,280 @@ +#include +#include +#include +#include // Для QueryPerformanceCounter и QueryPerformanceFrequency + +#define MAX_SIZE 10000 + +void bubbleSort(int mass[], int size, int* iterations) { + int flag = 1; + *iterations = 0; + while (flag) { + flag = 0; + for (int i = 0; i < size - 1; i++) { + (*iterations)++; + if (mass[i] > mass[i + 1]) { + int t = mass[i]; + mass[i] = mass[i + 1]; + mass[i + 1] = t; + flag = 1; + } + } + } +} + +void merge(int mass[], int left, int mid, int right, int* iterations) { + int n1 = mid - left + 1; + int n2 = right - mid; + int leftArr[10000], rightArr[10000]; + + for (int i = 0; i < n1; i++) + leftArr[i] = mass[left + i]; + for (int j = 0; j < n2; j++) + rightArr[j] = mass[mid + 1 + j]; + + int i = 0, j = 0, k = left; + + while (i < n1 && j < n2) { + (*iterations)++; + if (leftArr[i] <= rightArr[j]) { + mass[k] = leftArr[i]; + i++; + } + else { + mass[k] = rightArr[j]; + j++; + } + k++; + } + + while (i < n1) { + mass[k] = leftArr[i]; + i++; + k++; + } + + while (j < n2) { + mass[k] = rightArr[j]; + j++; + k++; + } +} + +void mergeSort(int mass[], int left, int right, int* iterations) { + if (left < right) { + int mid = left + (right - left) / 2; + mergeSort(mass, left, mid, iterations); + mergeSort(mass, mid + 1, right, iterations); + merge(mass, left, mid, right, iterations); + } +} + +void quickSort(int mass[], int left, int right, int* iterations) { + if (left < right) { + int pivot = mass[(left + right) / 2]; + int i = left, j = right; + while (i <= j) { + while (mass[i] < pivot) i++; + while (mass[j] > pivot) j--; + if (i <= j) { + int temp = mass[i]; + mass[i] = mass[j]; + mass[j] = temp; + i++; + j--; + } + (*iterations)++; + } + quickSort(mass, left, j, iterations); + quickSort(mass, i, right, iterations); + } +} + +void insertionSort(int mass[], int size, int* iterations) { + *iterations = 0; + for (int i = 1; i < size; i++) { + int el = mass[i]; + int loc = i - 1; + while (loc >= 0 && mass[loc] > el) { + mass[loc + 1] = mass[loc]; + loc--; + (*iterations)++; + } + mass[loc + 1] = el; + (*iterations)++; + } +} + +void selectionSort(int mass[], int size, int* iterations) { + *iterations = 0; + for (int i = 0; i < size; i++) { + int min_idx = i; + for (int j = i + 1; j < size; j++) { + (*iterations)++; + if (mass[j] < mass[min_idx]) { + min_idx = j; + } + } + int temp = mass[min_idx]; + mass[min_idx] = mass[i]; + mass[i] = temp; + } +} + +int binSearch(int mass[], int min, int max, int target) { + while (min <= max) { + int mid = (min + max) / 2; + if (target < mass[mid]) { + max = mid - 1; + } + else if (target > mass[mid]) { + min = mid + 1; + } + else { + return mid; + } + } + return -1; +} + +int linSearch(int mass[], int size, int target) { + for (int i = 0; i < size; i++) { + if (mass[i] == target) { + return i; + } + } + return -1; +} + +int isSorted(int mass[], int size) { + for (int i = 1; i < size; i++) { + if (mass[i - 1] > mass[i]) { + return 0; + } + } + return 1; +} + +void copyArray(int source[], int destination[], int size) { + for (int i = 0; i < size; i++) { + destination[i] = source[i]; + } +} + +int main() { + setlocale(LC_ALL, "Rus"); + int a, b, size; + int mass[MAX_SIZE]; + + printf("Введите количество элементов массива (максимум %d): ", MAX_SIZE); + scanf_s("%d", &size); + if (size > MAX_SIZE) { + printf("Ошибка: количество элементов превышает максимальное значение.\n"); + return 1; + } + + printf("Выберите способ ввода массива:\n1. Ввод вручную\n2. Случайные числа\n"); + scanf_s("%d", &a); + + if (a == 1) { + printf("Задайте массив длиной в %d значений:\n", size); + for (int i = 0; i < size; i++) { + scanf_s("%d", &mass[i]); + } + } + else if (a == 2) { + srand(time(NULL)); + for (int i = 0; i < size; i++) { + mass[i] = rand() % 100; + printf("%d\n", mass[i]); + } + } + else { + printf("Ошибка: неверный выбор.\n"); + return 1; + } + + int temp[MAX_SIZE]; + int iterations = 0; + LARGE_INTEGER start, end, frequency; + + while (1) { + printf("Выберите метод сортировки:\n1. Пузырьковая сортировка\n2. Сортировка слиянием\n3. Быстрая сортировка\n4. Сортировка вставками\n5. Сортировка выбором\n6. Выход\n"); + scanf_s("%d", &b); + + if (b == 6) { + break; // Выход из программы + } + + copyArray(mass, temp, size); + + // Начало отсчета времени + QueryPerformanceFrequency(&frequency); // Получаем частоту таймера + QueryPerformanceCounter(&start); // Запоминаем начальное время + + switch (b) { + case 1: + bubbleSort(temp, size, &iterations); + break; + case 2: + mergeSort(temp, 0, size - 1, &iterations); + break; + case 3: + quickSort(temp, 0, size - 1, &iterations); + break; + case 4: + insertionSort(temp, size, &iterations); + break; + case 5: + selectionSort(temp, size, &iterations); + break; + default: + printf("Ошибка: неверный выбор.\n"); + continue; + } + + // Конец отсчета времени + QueryPerformanceCounter(&end); // Запоминаем конечное время + + double time_spent = (double)(end.QuadPart - start.QuadPart) / frequency.QuadPart; // Время в секундах + + printf("Отсортированный массив:\n"); + for (int i = 0; i < size; i++) { + printf("%d ", temp[i]); + } + printf("\n"); + + printf("Проверка, что массив отсортирован: %s\n", isSorted(temp, size) ? "Да" : "Нет"); + printf("Количество итераций: %d\n", iterations); + printf("Время работы: %f секунд\n", time_spent); + + int target; + printf("Выберите метод поиска:\n1. Бинарный поиск\n2. Линейный поиск\n"); + int searchMethod; + scanf_s("%d", &searchMethod); + + printf("Введите элемент для поиска: "); + scanf_s("%d", &target); + + int result; + if (searchMethod == 1) { + result = binSearch(temp, 0, size - 1, target); + } + else if (searchMethod == 2) { + result = linSearch(temp, size, target); + } + else { + printf("Ошибка: неверный выбор метода поиска.\n"); + continue; + } + + if (result != -1) { + printf("Элемент %d найден на позиции %d.\n", target, result); + } + else { + printf("Элемент %d не найден.\n", target); + } + } + + return 0; +} diff --git a/13_Maksimova/summa3chisel.c b/13_Maksimova/summa3chisel.c new file mode 100644 index 0000000..3f8f792 --- /dev/null +++ b/13_Maksimova/summa3chisel.c @@ -0,0 +1,25 @@ +#include +#include + +int main() +{ + + int sum; + for (sum = 0; sum <= 9 * 3; sum++) + { + int a, b; + for (a = 1; a <= 9; a++) + { + if (a > sum) break; + for (b = 0; b <= 9; b++) + { + int c = sum - a - b; + if (c < 0) break; + if (c <= 9) + printf("%d%d%d ", a, b, c); + } + } + } + + return 0; +} \ No newline at end of file diff --git a/13_Maksimova/teylor.c b/13_Maksimova/teylor.c new file mode 100644 index 0000000..fb4d38c --- /dev/null +++ b/13_Maksimova/teylor.c @@ -0,0 +1,260 @@ +#define _USE_MATH_DEFINES +#include +#include +#include +#include + + +void changefunction() { + printf("\nВыберите функцию:\n"); + printf("1. Sin\n"); + printf("2. Cos\n"); + printf("3. Exp\n"); + printf("4. Tanh\n"); + printf(">>> "); +} + + +double FirstSin(double x) { + return x; +} + +double NextSin(double x, int i) { + return (-x * x / ((i - 1) * i)); +} + + +double FirstCos(double x) { + return 1; +} + +double NextCos(double x, int i) { + i--; + return -x * x / ((i - 1) * i); +} + + +double FirstExp(double x) { + return 1; +} + +double NextExp(double x, int i) { + i--; + return x / i; +} + + +double FirstTanh(double x) { + return x; +} + +int factorial(int n) { + if (n == 0) + return 1; + return n * factorial(n - 1); +} + +double bernulli(int n) { // bernulli number B(n) + int k; + double result = 0.0; + int C; + + if (n == 0) + return 1; + + else { + for (k = 1; k <= n; k++) { + C = factorial(n + 1) / (factorial(n - k) * factorial(k + 1)); + result += C * bernulli(n - k); + } + result = result * (-1.0 / (n + 1)); + } + return result; +} + +double NextTanh(double x, int i) { + double th1, th2; + + if (i == 2) { + th1 = ((pow(2, 2 * i) * bernulli(2 * i) * (pow(2, 2 * i) - 1)) / factorial(2 * i)) * (pow(x, 2 * i - 1)); + return (th1 / x); + } + + th1 = ((pow(2, 2 * i) * bernulli(2 * i) * (pow(2, 2 * i) - 1)) / factorial(2 * i)) * (pow(x, 2 * i - 1)); + th2 = ((pow(2, 2 * (i - 2)) * bernulli(2 * (i - 2)) * (pow(2, 2 * (i - 2)) - 1)) / factorial(2 * (i - 2))) * (pow(x, 2 * (i - 2) - 1)); + + return (th1 / th2); +} + + +typedef double(*First)(double); +typedef double(*Next)(double, int); + +double Teylor(double x, int N, First f, Next g, int h, double eps, double res) { + double prev, next, sum; + int i = 1, shag; + sum = 0.0; + prev = f(x); + next = 0; + shag = h; + + while ((fabs(sum - res) > eps) && (i < N)) { + if (i == 1) + sum += prev; + else { + next = prev * g(x, i); + sum += next; + prev = next; + } + i += shag; + } + + printf("Эталонное значение: %lf \n", res); + printf("Вычисленная оценка значения: %lf \n", sum); + printf("Разница между оценкой и эталонным значением: %lf \n", fabs(res - sum)); + printf("Количество слагаемых,которое было вычислено: %d \n\n", i - 1); + return sum; +} + + +double Teylor2(double x, int N, First f, Next g, int h, double res) { + double prev, next, sum; + int i = 1, shag; + sum = 0.0; + prev = f(x); + next = 0; + shag = h; + + printf("\nЭталонное значение: %lf \n\n", res); + + while (N >= i) { + if (i == 1) + sum += prev; + else { + next = prev * g(x, i); + sum += next; + prev = next; + } + printf("(N = %d) Вычисленная оценка значения: %lf \n", i, sum); + printf("(N = %d) Разница между оценкой и эталонным значением: %lf \n\n", i, fabs(res - sum)); + i += shag; + } + return sum; +} + + +void main() { + setlocale(LC_ALL, "Rus"); + int N; + double eps; + double x; + double y; + int choice; + int mode = -1; + + while (mode != 3) { + printf("CВыберите режим:\n"); + printf("1. Однократный расчет\n"); + printf("2. Серийный расчет\n"); + printf("3. Выход\n"); + scanf_s("%d", &mode); + + if (mode == 3) { + printf("Пока!\n"); + break; + } + + do { + changefunction(); + scanf_s("%d", &choice); + printf("\n"); + } while ((choice < 1) || (choice > 4)); + + + if (choice == 4) { + printf("\n(!) tanh(x): -0.5 <= x <= 0.5\n"); + do { + printf("Введите x: \n"); + scanf_s("%lf", &x); + } while ((x < -0.5) || (x > 0.5)); + } + + else { + printf("Введите x: \n"); + scanf_s("%lf", &x); + } + + + if (choice == 4) { + printf("\n(!) tanh(x): N <= 10\n"); + do { + printf("Введите N: \n"); + scanf_s("%d", &N); + } while (N > 10); + } + + else { + printf("Введите N(<= 50):\n"); + scanf_s("%d", &N); + } + + + if (mode == 1) { + printf("Введите точность вычислений: \n"); + scanf_s("%lf", &eps); + } + + + if ((choice == 1) || (choice == 2)) { + if (fabs(x) == x) + while (fabs(x) > 2 * M_PI) + x = x - 2 * M_PI; + else + while (fabs(x) > 2 * M_PI) + x = x + 2 * M_PI; + } + + printf("\n"); + + if (mode == 1) { + switch (choice) { + case 1: + y = Teylor(x, N, FirstSin, NextSin, 2, eps, sin(x)); + break; + case 2: + y = Teylor(x, N, FirstCos, NextCos, 2, eps, cos(x)); + break; + case 3: + y = Teylor(x, N, FirstExp, NextExp, 1, eps, exp(x)); + break; + case 4: + y = Teylor(x, N, FirstTanh, NextTanh, 1, eps, tanh(x)); + break; + default: + printf("Неверный выбор\n"); + continue; + } + } + + + if (mode == 2) { + switch (choice) { + case 1: + y = Teylor2(x, N, FirstSin, NextSin, 2, sin(x)); + break; + case 2: + y = Teylor2(x, N, FirstCos, NextCos, 2, cos(x)); + break; + case 3: + y = Teylor2(x, N, FirstExp, NextExp, 1, exp(x)); + break; + case 4: + y = Teylor2(x, N, FirstTanh, NextTanh, 1, tanh(x)); + break; + default: + printf("Неверный выбор\n"); + continue; + } + } + } +}