From 1f94e689a193a65eb39bc21cdd0d3512079bfdaa Mon Sep 17 00:00:00 2001 From: Nikita-Popov-3824B1PM2 Date: Thu, 14 Nov 2024 07:48:47 +0300 Subject: [PATCH 1/7] Create 17_popov --- 17_popov | 1 + 1 file changed, 1 insertion(+) create mode 100644 17_popov diff --git a/17_popov b/17_popov new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/17_popov @@ -0,0 +1 @@ + From 5c7972e6d05f30e260fa680f8f1e4d051d4bd7b3 Mon Sep 17 00:00:00 2001 From: Nikita-Popov-3824B1PM2 Date: Thu, 14 Nov 2024 07:56:35 +0300 Subject: [PATCH 2/7] Delete 17_popov --- 17_popov | 1 - 1 file changed, 1 deletion(-) delete mode 100644 17_popov diff --git a/17_popov b/17_popov deleted file mode 100644 index 8b13789..0000000 --- a/17_popov +++ /dev/null @@ -1 +0,0 @@ - From a5825c7db80c068a7e750ba59e5ae30630b4ec4a Mon Sep 17 00:00:00 2001 From: Popov Nikita Date: Thu, 14 Nov 2024 08:03:42 +0300 Subject: [PATCH 3/7] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 17_popov/Lab1/lab1.c | 420 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 420 insertions(+) create mode 100644 17_popov/Lab1/lab1.c diff --git a/17_popov/Lab1/lab1.c b/17_popov/Lab1/lab1.c new file mode 100644 index 0000000..523a7f0 --- /dev/null +++ b/17_popov/Lab1/lab1.c @@ -0,0 +1,420 @@ +#include +#include +#include +#include +#include "windows.h" + +#define C 15000 + +void merge(int mass[], int left, int m, int right) { + int n1 = m - left + 1; + int n2 = right - m; + + int massleft[C], massright[C]; + + for (int i = 0; i < n1; i++) { + massleft[i] = mass[left + i]; + } + for (int i = 0; i < n2; i++) { + massright[i] = mass[m + 1 + i]; + } + + int i = 0, j = 0, k = left; + + for (; i < n1 && j < n2; k++) { + if (massleft[i] <= massright[j]) { + mass[k] = massleft[i++]; + } + else { + mass[k] = massright[j++]; + } + } + + for (; i < n1; i++, k++) { + mass[k] = massleft[i]; + } + + // Копирование оставшихся элементов из massright + for (; j < n2; j++, k++) { + mass[k] = massright[j]; + } +} + +double ShakerSort(int mass[], int n, int *p) +{ + LARGE_INTEGER freq, start, finish; + + QueryPerformanceFrequency(&freq); // 1 раз на программу + QueryPerformanceCounter(&start); + + int f = 1; + int left = 0; int right = n - 1; + int i, j; + double tmp; + + for (i = 0; (i < n) && (f); i++) + { + f = 0; + if (i % 2 == 1) + { + for (j = left; j < right - 1; j++) + { + (*p)++; + if (mass[j] > mass[j + 1]) + { + tmp = mass[j]; + mass[j] = mass[j + 1]; + mass[j + 1] = tmp; + f = 1; + } + + } + right--; + } + else + for (j = right - 1; j > left - 1; j--) + { + (*p)++; + if (mass[j] < mass[j - 1]) + { + tmp = mass[j]; + mass[j] = mass[j - 1]; + mass[j - 1] = tmp; + f = 1; + } + + + } + left++; + + } + QueryPerformanceCounter(&finish); // конец замера + return (double)(finish.QuadPart - start.QuadPart) / (double)freq.QuadPart; +} + +double InsertionSort(int mass[], int n, int *p) +{ + LARGE_INTEGER freq, start, finish; + + QueryPerformanceFrequency(&freq); // 1 раз на программу + QueryPerformanceCounter(&start); + + int tmp = 0; + int k, x; + + for (x = 1; x < n; x++) { + tmp = mass[x]; + k = x; + + while (k > 0 && mass[k - 1] > tmp) { + mass[k] = mass[k - 1]; + k--; + } + + mass[k] = tmp; + } + QueryPerformanceCounter(&finish); // конец замера + return (double)(finish.QuadPart - start.QuadPart) / (double)freq.QuadPart; +} + +double MergeSort(int mass[], int left, int right, int* p) +{ + LARGE_INTEGER freq, start, finish; + + QueryPerformanceFrequency(&freq); // 1 раз на программу + QueryPerformanceCounter(&start); + + if (left < right) + { + int m = (left + right) / 2; + MergeSort(mass, left, m, p); + MergeSort(mass, m + 1, right, p); + merge(mass, left, m, right); + (*p)++; + } + + QueryPerformanceCounter(&finish); // конец замера + return (double)(finish.QuadPart - start.QuadPart) / (double)freq.QuadPart; +} + +double QS(int mass[], int left, int right, int* p) +{ + LARGE_INTEGER freq, start, finish; + + QueryPerformanceFrequency(&freq); // 1 раз на программу + QueryPerformanceCounter(&start); + + int m = (right + left) / 2; + int val = mass[m]; + int i, j, tmp; + + i = left; + j = right; + + while (i <= j) + { + while (mass[i] < val) + i++; + while (mass[j] > val) + j--; + if (i <= j) + { + tmp = mass[i]; + mass[i] = mass[j]; + mass[j] = tmp; + i++; + j--; + } + (*p)++; + } + + if (i > left) + QS(mass, left, j, p); + if (j < right) + QS(mass, i, right, p); + QueryPerformanceCounter(&finish); // конец замера + return (double)(finish.QuadPart - start.QuadPart) / (double)freq.QuadPart; +} + +double SortShell(int mass[], int n, int* p) +{ + LARGE_INTEGER freq, start, finish; + + QueryPerformanceFrequency(&freq); // 1 раз на программу + QueryPerformanceCounter(&start); + + int step, i, j, tmp; + + for (step = n / 2; step > 0; step = step / 2) + { + for (i = step; i < n; i++) + { + tmp = mass[i]; + for (j = i; j >= step; j = j - step) + { + if (tmp < mass[j - step]) + { + mass[j] = mass[j - step]; + (*p)++; + } + else + break; + } + mass[j] = tmp; + } + } + + QueryPerformanceCounter(&finish); // конец замера + return (double)(finish.QuadPart - start.QuadPart) / (double)freq.QuadPart; +} + +int BinarySearch(int mass[], int n, int val, int *s) +{ + int left = 0; + int right = n - 1; + int m; + + while (left <= right) + { + (*s)++; + m = left + (right - left) / 2; + + if (mass[m] == val) + return m; + + if (mass[m] < val) + left = m + 1; + else + right = m - 1; + (*s)++; + } + + return -1; +} + +int LinearSearch_2(int mass[], int n, int val, int *s) +{ + int i = 0; + while (i < n) + { + (*s)++; + if (mass[i] == val) + return i; + else + i++; + + } + return -1; +} + +int random(int mass[], int n) +{ + int i; + for (i = 0; i < n; i++) + mass[i] = rand() % 1000; +} + + +// Добавить сортировки быструю + +int main() +{ + setlocale(LC_ALL, "Rus"); + srand(time(NULL)); + + int p = 0, s = 0; + double time; + double time1, time2, time3, time4, time5; + int a, M; + int N, n, i; + int mass[C]; + char answer = 'Y'; +Start: + printf("Выберите каким способом Вы хотите задать массив:\n1 - задать случайный массив;\n2 - задать свой массив.\n\n"); + scanf_s("%d", &N); + + printf("\nВведите длинну массива (длинна массива не должна превышать %d):\t", C); + scanf_s("%d", &n); + if (n > C) + goto Start; + + + switch (N) + { + case 1: + + random(mass, n); + break; + + case 2: + printf("Введите элементы массива:"); + for (i = 0; i < n; i++) + scanf_s("%d", &mass[i]); + break; + default: + printf("Выбран не существующий варинат. Попытайтесь снова.\n\n\n"); + goto Start; + break; + } + printf("\n"); + /*for (i = 0; i < n; i++) + printf("%d\t", mass[i]);*/ + printf("\n\n\n"); + +Choice: + + printf("\nВыберете способ сортировки массива:\n1 - двунаправленная сортировка пузырьком;\n2 - сортировка вставками;\n3 - сортировка слияния;\n4 - сортировка Хара;\n5 - сортировка Шелла.\n\n"); + scanf_s("%d", &N); + printf("\n"); + + switch (N) + { + case 1: + time = ShakerSort(mass, n, &p); + + + break; + case 2: + time = InsertionSort(mass, n, &p); + + + break; + case 3: + time = MergeSort(mass, 0, n - 1, &p); + + + break; + case 4: + time = QS(mass, 0, n - 1, &p); + + + break; + case 5: + time = SortShell(mass, n, &p); + + + break; + default: + printf("Выбран не существующий варинат. Попытайтесь снова.\n\n\n"); + goto Choice; + + break; + } + for (i = 0; i < n; i++) + printf("%d\t", mass[i]); + + + printf("\n\n"); + printf("%d\t%f", p, time); + printf("\n\n\n"); + Choice2: + printf("\nЖелаете выбрать другую сортировку или ввести другой массив?\n1 - выбрать другую сортировку.\n2 - ввести другой массив.\n3 - далее\n\n"); + scanf_s("%d", &N); + + switch (N) + { + case 1: + goto Choice; + + case 2: + goto Start; + case 3: + break; + + default: + printf("Выбран не существующий варинат. Попытайтесь снова.\n\n\n"); + goto Choice2; + + break; + } + Choice3: + printf("\nВыберете метод поиска:\n1 - линейный поиск;\n2 - бинарный поиск.\n\n"); + scanf_s("%d", &N); + + Choice4: + printf("\nВведите число, которое хотите найти:\t"); + scanf_s("%d", &a); + + switch (N) + { + case 1: + M = LinearSearch_2(mass, n, a, &s); + break; + case 2: + M = BinarySearch(mass, n, a, &s); + break; + default: + printf("Выбран не существующий варинат. Попытайтесь снова.\n\n\n"); + goto Choice3; + break; + } + + if (M > -1) + printf("%d", M); + else + { + while (answer == 'Y' || answer == 'N') + { + printf("Число не найдено.\nНайти другое число? (Y/N)\t"); + scanf_s(" %c", &answer, 1); + + if (answer == 'Y' || answer == 'N') + break; + + else + printf("Некорректный ввод. Пожалуйста, введите Y или N.\n"); + } + + } + + if (answer == 'Y') + goto Choice4; + + + + printf("\n\n"); + printf("%d", s); + + + return 0; +} \ No newline at end of file From cd3ec20d2363ae1a2791c84947844446c831f124 Mon Sep 17 00:00:00 2001 From: Popov Nikita Date: Thu, 14 Nov 2024 08:03:42 +0300 Subject: [PATCH 4/7] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 17_popov/Lab1/lab1.c | 106 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 93 insertions(+), 13 deletions(-) diff --git a/17_popov/Lab1/lab1.c b/17_popov/Lab1/lab1.c index 523a7f0..3f82a36 100644 --- a/17_popov/Lab1/lab1.c +++ b/17_popov/Lab1/lab1.c @@ -6,6 +6,16 @@ #define C 15000 + + +double Times(double t) +{ + printf("\n\n"); + printf("%f" t); + printf("\n\n\n"); +} + + void merge(int mass[], int left, int m, int right) { int n1 = m - left + 1; int n2 = right - m; @@ -34,7 +44,9 @@ void merge(int mass[], int left, int m, int right) { mass[k] = massleft[i]; } - // Копирование оставшихся элементов из massright + + + for (; j < n2; j++, k++) { mass[k] = massright[j]; } @@ -66,6 +78,10 @@ double ShakerSort(int mass[], int n, int *p) mass[j] = mass[j + 1]; mass[j + 1] = tmp; f = 1; + + + (*k)++; + } } @@ -81,6 +97,10 @@ double ShakerSort(int mass[], int n, int *p) mass[j] = mass[j - 1]; mass[j - 1] = tmp; f = 1; + + + (*k)++; + } @@ -100,6 +120,7 @@ double InsertionSort(int mass[], int n, int *p) QueryPerformanceCounter(&start); int tmp = 0; + int k, x; for (x = 1; x < n; x++) { @@ -109,6 +130,20 @@ double InsertionSort(int mass[], int n, int *p) while (k > 0 && mass[k - 1] > tmp) { mass[k] = mass[k - 1]; k--; + + int c, i; + + for (i = 0; i < n; i++) + { + tmp = mass[i]; + c = i; + (*p)++; + while (c > 0 && mass[c - 1] > tmp) + { + mass[c] = mass[c - 1]; + k--; + (*k)++; + } mass[k] = tmp; @@ -255,19 +290,27 @@ int random(int mass[], int n) } -// Добавить сортировки быструю + + + int main() { setlocale(LC_ALL, "Rus"); srand(time(NULL)); - int p = 0, s = 0; + + int p = 0, s = 0, k = 0; + double time; double time1, time2, time3, time4, time5; int a, M; int N, n, i; int mass[C]; + + + + char answer = 'Y'; Start: printf("Выберите каким способом Вы хотите задать массив:\n1 - задать случайный массив;\n2 - задать свой массив.\n\n"); @@ -279,17 +322,26 @@ int main() goto Start; + + int array = (int*) malloc(sizeof(int) * n)) + + switch (N) { case 1: - random(mass, n); + + random(array, n); + break; case 2: printf("Введите элементы массива:"); for (i = 0; i < n; i++) - scanf_s("%d", &mass[i]); + + + scanf_s("%d", &array[i]); + break; default: printf("Выбран не существующий варинат. Попытайтесь снова.\n\n\n"); @@ -310,27 +362,45 @@ int main() switch (N) { case 1: - time = ShakerSort(mass, n, &p); + + + time1 = ShakerSort(array, n, &p); + break; case 2: - time = InsertionSort(mass, n, &p); + + + time2 = InsertionSort(array, n, &p); + break; case 3: - time = MergeSort(mass, 0, n - 1, &p); + + + + time3 = MergeSort(array, 0, n - 1, &p); + break; case 4: - time = QS(mass, 0, n - 1, &p); + + + + time4 = QS(array, 0, n - 1, &p); + break; case 5: - time = SortShell(mass, n, &p); + + + + time5 = SortShell(array, n, &p); + break; @@ -341,12 +411,15 @@ int main() break; } for (i = 0; i < n; i++) - printf("%d\t", mass[i]); - printf("\n\n"); + printf("%d\t", array[i]); + + + /*printf("\n\n"); printf("%d\t%f", p, time); - printf("\n\n\n"); + printf("\n\n\n");*/ + Choice2: printf("\nЖелаете выбрать другую сортировку или ввести другой массив?\n1 - выбрать другую сортировку.\n2 - ввести другой массив.\n3 - далее\n\n"); scanf_s("%d", &N); @@ -378,10 +451,17 @@ int main() switch (N) { case 1: + M = LinearSearch_2(mass, n, a, &s); break; case 2: M = BinarySearch(mass, n, a, &s); + + M = LinearSearch_2(array, n, a, &s); + break; + case 2: + M = BinarySearch(array, n, a, &s); + break; default: printf("Выбран не существующий варинат. Попытайтесь снова.\n\n\n"); From 5888f76b0d4b710a720a70485ea600d93009d80b Mon Sep 17 00:00:00 2001 From: Popov Nikita Date: Thu, 12 Dec 2024 10:05:22 +0300 Subject: [PATCH 5/7] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 17_popov/Test/1D.c | 127 +++++++++++++++++++++++++++++++++++++++++++ 17_popov/Test/2D_1.c | 104 +++++++++++++++++++++++++++++++++++ 2 files changed, 231 insertions(+) create mode 100644 17_popov/Test/1D.c create mode 100644 17_popov/Test/2D_1.c diff --git a/17_popov/Test/1D.c b/17_popov/Test/1D.c new file mode 100644 index 0000000..23ab689 --- /dev/null +++ b/17_popov/Test/1D.c @@ -0,0 +1,127 @@ +#include "stdlib.h" +#include "stdio.h" +#include "time.h" + +void Fill(int arr[], int N) +{ + int i, j, buff, p; + j = 0; + while (j < N) + { + p = rand() % 20; + buff = 0; + for (i = 0; i < N; i++) + { + if (p == arr[i]) + { + buff++; + break; + } + + } + + if (buff == 0) + { + arr[j] = p; + j++; + } + } + +} + +int Union(int A[], int n, int B[], int m, int C[]) +{ + int i, j, k, buff; + //Копирование массива А в массив С + for (i = 0; i < n; i++) + C[i] = A[i]; + + k = n; + + for(j = 0; j < m; j++) + { + buff = 0; + for (i = 0; i < k; i++) + { + if (C[i] == B[j]) + { + buff++; + break; + } + + } + + if (buff == 0) + { + C[k] = B[j]; + k++; + } + } + return k; +} + +int Inter(int A[], int n, int B[], int m, int C[]) +{ + int i, j, k, buff; + + + k = 0; + + for (j = 0; j < m; j++) + { + for (i = 0; i < n; i++) + { + if (A[i] == B[j]) + { + C[k] = A[i]; + k++; + break; + } + + } + + } + return k; +} + +int Print(int arr[], int N) +{ + int i; + for (i = 0; i < N; i++) + printf("%d\t", arr[i]); +} + + +int main() +{ + //Ввод массивов + int n = 11, m = 8; + int l; + /*scanf_s("%d", n); + scanf_s("%d", m);*/ + int* A = (int*)malloc(sizeof(int) * n); + int* B = (int*)malloc(sizeof(int) * m); + int* C = (int*)malloc(sizeof(int) * (m + n)); + + + //Заполнение массивов + srand(time(NULL)); + Fill(A, n); + Fill(B, m); + + Print(A, n); + printf("\n\n"); + Print(B, m); + printf("\n\n"); + + l = Union(A, n, B, m, C); + Print(C, l); + printf("\n\n"); + l = Inter(A, n, B, m, C); + Print(C, l); + + free(C); + free(B); + free(A); + return 0; +} \ No newline at end of file diff --git a/17_popov/Test/2D_1.c b/17_popov/Test/2D_1.c new file mode 100644 index 0000000..c02abf3 --- /dev/null +++ b/17_popov/Test/2D_1.c @@ -0,0 +1,104 @@ +#include "stdlib.h" +#include "time.h" +#include "stdio.h" +#include "locale.h" + +void Fill(int** arr, int m, int n) +{ + int i, j; + for (i = 0; i < m; i++) + { + for (j = 0; j < n; j++) + { + scanf_s("%d", &arr[i][j]); + } + } +} + +int Pass(int** arr, int m, int n, int* max_x, int* max_y, int* a, int* b) +{ + int i, j, k, w, buff, x, y, S; + i = 0; + j = 0; + S = 0; + for (i = 0; i < m; i++) + { + for (j = 0; j < n; j++) + { + if (arr[i][j] == 0) + { + x = 0; + y = 0; + + + while ((j + x < n) && (arr[i][j + x] == 0)) + x++; + while ((i + y < m) && (arr[i + y][j] == 0)) + y++; + + buff = x * y; + + if (S < buff) + { + S = buff; + *max_x = i; + *max_y = j; + *a = i + y; + *b = j + x; + + } + + } + } + } + return S; +} + +void Print(int** arr, int m, int n) +{ + int i, j; + for (i = 0; i < m; i++) + { + printf("\n"); + for (j = 0; j < n; j++) + printf("%d\t", arr[i][j]); + } +} + +int main() +{ + setlocale(LC_ALL, "Rus"); + int max_x, max_y; + int a, b; + + int n = 8, m = 8; + int i; + scanf_s("%d %d", &m, &n); + + + int* Arr = (int*)malloc(sizeof(int) * m * n); + int** Matrix = (int**)malloc(sizeof(int*) * m); + for (i = 0; i < m; i++) + Matrix[i] = Arr + i * n; + if (Arr == NULL) + { + printf("Ошибка выделения памяти для массива Matrix.\n"); + free(Arr); + return 1; + } + + + + srand(time(NULL)); + Fill(Matrix, m, n); + Print(Matrix, m, n); + + printf("\n"); + printf("Площадь = %d\n", Pass(Matrix, m, n, &max_x, &max_y, &a, &b)); + printf("Верхний левый угол: (%d, %d)\n", max_x, max_y); + printf("Правый нижний угол: (%d, %d)\n", a - 1 , b - 1); + + free(Arr); + free(Matrix); + return 0; +} \ No newline at end of file From 1642a4740bf10983a445deb33e6c270e5ccf8b2d Mon Sep 17 00:00:00 2001 From: Popov Nikita Date: Thu, 12 Dec 2024 10:36:58 +0300 Subject: [PATCH 6/7] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB.?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 17_popov/Lab1/lab1.c | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/17_popov/Lab1/lab1.c b/17_popov/Lab1/lab1.c index 3f82a36..b5b81e0 100644 --- a/17_popov/Lab1/lab1.c +++ b/17_popov/Lab1/lab1.c @@ -123,27 +123,15 @@ double InsertionSort(int mass[], int n, int *p) int k, x; - for (x = 1; x < n; x++) { + for (x = 1; x < n; x++) + { tmp = mass[x]; k = x; - while (k > 0 && mass[k - 1] > tmp) { - mass[k] = mass[k - 1]; - k--; - - int c, i; - - for (i = 0; i < n; i++) - { - tmp = mass[i]; - c = i; - (*p)++; - while (c > 0 && mass[c - 1] > tmp) + while (k > 0 && mass[k - 1] > tmp) { - mass[c] = mass[c - 1]; - k--; - (*k)++; - + mass[k] = mass[k - 1]; + k-- } mass[k] = tmp; From 0c141b17b5ac7c66bbbe11c3b62973a872c8c04a Mon Sep 17 00:00:00 2001 From: Popov Nikita Date: Sat, 28 Dec 2024 18:15:29 +0300 Subject: [PATCH 7/7] =?UTF-8?q?=D0=9A=D0=B0=D1=81=D1=81=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавлен файл СЃ кассой СЃ суммированием --- 17_popov/Kassa/FileName.c | 192 ++++++++++++++++++++++++++++++++++++++ 17_popov/Test/1D.c | 127 ------------------------- 17_popov/Test/2D_1.c | 104 --------------------- 3 files changed, 192 insertions(+), 231 deletions(-) create mode 100644 17_popov/Kassa/FileName.c delete mode 100644 17_popov/Test/1D.c delete mode 100644 17_popov/Test/2D_1.c diff --git a/17_popov/Kassa/FileName.c b/17_popov/Kassa/FileName.c new file mode 100644 index 0000000..1638a1d --- /dev/null +++ b/17_popov/Kassa/FileName.c @@ -0,0 +1,192 @@ +#include "stdio.h" +#include "stdlib.h" +#include "string.h" +#include "ctype.h" +#include "locale.h" + +struct Product +{ + char* name; + double price; + double quantity; +}; + +typedef struct Product Product; + +Product create_product(char* name, double price, double quantity) +{ + Product product; + product.name = name; + product.price = price; + product.quantity = quantity; + return product; +} + +Product inventory[] = +{ + {"банан", 200.0, 10.0}, + {"яблоко", 202.0, 2.0}, + {"апельсин", 30.0, 15.0}, + {"картошка", 40.0, 100.0} +}; + +int string_length(char* s) +{ + int length = 0; + while (s[length] != '\0') + { + length++; + } + return length; +} + +int number_length(double n) +{ + int length = 3; + while (n > 1.0) + { + n /= 10; + length++; + } + if (n > 0.01) + { + return length; + } + else + { + return length + 1; + } +} + +Product find_product(Product* product) +{ + char* name = product->name; + double quantity = product->quantity; + Product result; + for (int i = 0; i < 4; i++) + { + if (strcmp(name, inventory[i].name) == 0) + { + if (quantity <= inventory[i].quantity) + { + result = create_product(name, inventory[i].price * quantity, quantity); + } + else + { + result = create_product(name, inventory[i].price * inventory[i].quantity, inventory[i].quantity); + } + return result; + } + } + + return create_product(name, 0.0, 0.0); +} + +// Функция для вывода информации о товаре и общей стоимости +void print_invoice(Product* products, Product* receipt, int count, double total) +{ + printf("\nНазвание товара\tЦена\tКоличество\n"); + for (int i = 0; i < count; i++) { + if (receipt[i].quantity > 0.0) { + printf("%s", receipt[i].name); + for (int j = 0; j < 13 - string_length(receipt[i].name); j++) + { + printf("."); + } + printf("%.2lf", receipt[i].price); + for (int j = 0; j < 14 - number_length(receipt[i].price); j++) + { + printf("."); + } + printf("%.2lf\n", receipt[i].quantity); + } + else { + printf("Товар %s не найден.\n", products[i].name); + } + } + printf("\nОбщая стоимость - %.2lf\n", total); +} +int main() +{ + system("chcp 1251"); + + setlocale(LC_ALL, "Russian"); + + int n; + char buffer[256]; + double total = 0.0; + + printf("Введите количество товаров, которые хотите купить:\n"); + scanf_s("%d", &n); + getchar(); + + Product products_list[100]; + Product receipt_list[100]; + int receipt_count = 0; + + for (int i = 0; i < n; i++) + { + printf("Введите товар %d:\n", i + 1); + fgets(buffer, sizeof(buffer), stdin); + + int product_quantity; + char product_name[100]; + + if (sscanf_s(buffer, "%d %99[^\n]", &product_quantity, product_name, (unsigned)_countof(product_name)) == 2) + { + + + for (int v = 0; product_name[v]; v++) + { + product_name[v] = tolower(product_name[v]); + } + + // Проверяем, есть ли товар уже в чеке + int found = 0; + for (int j = 0; j < receipt_count; j++) + { + if (strcmp(receipt_list[j].name, product_name) == 0) + { + receipt_list[j].quantity += product_quantity; + receipt_list[j] = find_product(&receipt_list[j]); + found = 1; + break; + } + } + + if (!found) + { + products_list[receipt_count].name = (char*)malloc(strlen(product_name) + 1); + strcpy_s(products_list[receipt_count].name, strlen(product_name) + 1, product_name); + products_list[receipt_count].quantity = product_quantity; + + receipt_list[receipt_count] = find_product(&products_list[receipt_count]); + receipt_count++; + + } + } + else + { + printf("Ошибка ввода. Попробуйте снова.\n"); + i--; + } + + } + + total = 0.0; + for (int i = 0; i < receipt_count; i++) + { + total += receipt_list[i].price; + } + + + print_invoice(products_list, receipt_list, receipt_count, total); + + + for (int i = 0; i < receipt_count; i++) + { + free(products_list[i].name); + } + + return 0; +} diff --git a/17_popov/Test/1D.c b/17_popov/Test/1D.c deleted file mode 100644 index 23ab689..0000000 --- a/17_popov/Test/1D.c +++ /dev/null @@ -1,127 +0,0 @@ -#include "stdlib.h" -#include "stdio.h" -#include "time.h" - -void Fill(int arr[], int N) -{ - int i, j, buff, p; - j = 0; - while (j < N) - { - p = rand() % 20; - buff = 0; - for (i = 0; i < N; i++) - { - if (p == arr[i]) - { - buff++; - break; - } - - } - - if (buff == 0) - { - arr[j] = p; - j++; - } - } - -} - -int Union(int A[], int n, int B[], int m, int C[]) -{ - int i, j, k, buff; - //Копирование массива А в массив С - for (i = 0; i < n; i++) - C[i] = A[i]; - - k = n; - - for(j = 0; j < m; j++) - { - buff = 0; - for (i = 0; i < k; i++) - { - if (C[i] == B[j]) - { - buff++; - break; - } - - } - - if (buff == 0) - { - C[k] = B[j]; - k++; - } - } - return k; -} - -int Inter(int A[], int n, int B[], int m, int C[]) -{ - int i, j, k, buff; - - - k = 0; - - for (j = 0; j < m; j++) - { - for (i = 0; i < n; i++) - { - if (A[i] == B[j]) - { - C[k] = A[i]; - k++; - break; - } - - } - - } - return k; -} - -int Print(int arr[], int N) -{ - int i; - for (i = 0; i < N; i++) - printf("%d\t", arr[i]); -} - - -int main() -{ - //Ввод массивов - int n = 11, m = 8; - int l; - /*scanf_s("%d", n); - scanf_s("%d", m);*/ - int* A = (int*)malloc(sizeof(int) * n); - int* B = (int*)malloc(sizeof(int) * m); - int* C = (int*)malloc(sizeof(int) * (m + n)); - - - //Заполнение массивов - srand(time(NULL)); - Fill(A, n); - Fill(B, m); - - Print(A, n); - printf("\n\n"); - Print(B, m); - printf("\n\n"); - - l = Union(A, n, B, m, C); - Print(C, l); - printf("\n\n"); - l = Inter(A, n, B, m, C); - Print(C, l); - - free(C); - free(B); - free(A); - return 0; -} \ No newline at end of file diff --git a/17_popov/Test/2D_1.c b/17_popov/Test/2D_1.c deleted file mode 100644 index c02abf3..0000000 --- a/17_popov/Test/2D_1.c +++ /dev/null @@ -1,104 +0,0 @@ -#include "stdlib.h" -#include "time.h" -#include "stdio.h" -#include "locale.h" - -void Fill(int** arr, int m, int n) -{ - int i, j; - for (i = 0; i < m; i++) - { - for (j = 0; j < n; j++) - { - scanf_s("%d", &arr[i][j]); - } - } -} - -int Pass(int** arr, int m, int n, int* max_x, int* max_y, int* a, int* b) -{ - int i, j, k, w, buff, x, y, S; - i = 0; - j = 0; - S = 0; - for (i = 0; i < m; i++) - { - for (j = 0; j < n; j++) - { - if (arr[i][j] == 0) - { - x = 0; - y = 0; - - - while ((j + x < n) && (arr[i][j + x] == 0)) - x++; - while ((i + y < m) && (arr[i + y][j] == 0)) - y++; - - buff = x * y; - - if (S < buff) - { - S = buff; - *max_x = i; - *max_y = j; - *a = i + y; - *b = j + x; - - } - - } - } - } - return S; -} - -void Print(int** arr, int m, int n) -{ - int i, j; - for (i = 0; i < m; i++) - { - printf("\n"); - for (j = 0; j < n; j++) - printf("%d\t", arr[i][j]); - } -} - -int main() -{ - setlocale(LC_ALL, "Rus"); - int max_x, max_y; - int a, b; - - int n = 8, m = 8; - int i; - scanf_s("%d %d", &m, &n); - - - int* Arr = (int*)malloc(sizeof(int) * m * n); - int** Matrix = (int**)malloc(sizeof(int*) * m); - for (i = 0; i < m; i++) - Matrix[i] = Arr + i * n; - if (Arr == NULL) - { - printf("Ошибка выделения памяти для массива Matrix.\n"); - free(Arr); - return 1; - } - - - - srand(time(NULL)); - Fill(Matrix, m, n); - Print(Matrix, m, n); - - printf("\n"); - printf("Площадь = %d\n", Pass(Matrix, m, n, &max_x, &max_y, &a, &b)); - printf("Верхний левый угол: (%d, %d)\n", max_x, max_y); - printf("Правый нижний угол: (%d, %d)\n", a - 1 , b - 1); - - free(Arr); - free(Matrix); - return 0; -} \ No newline at end of file