From b713b8928f4e2889fbafda88721ce2ce5f621b21 Mon Sep 17 00:00:00 2001 From: Guslik5 <91369419+Guslik5@users.noreply.github.com> Date: Sat, 18 Dec 2021 14:00:51 +0300 Subject: [PATCH 1/5] create laba_6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit от Гусаченко Дмитрия --- lab6/laba_6.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 lab6/laba_6.c diff --git a/lab6/laba_6.c b/lab6/laba_6.c new file mode 100644 index 00000000..09a4bd09 --- /dev/null +++ b/lab6/laba_6.c @@ -0,0 +1,206 @@ +#include +#include +#include + +unsigned int Back_Change_Size(unsigned char size[4]) +{ + unsigned int res = 0; + for (int i = 0; i < 4; i++) + res += size[3 - i] << (8 * i); + return res; +} + +void Change_Size(unsigned int size, unsigned char *res) +{ + // printf("%d", size); + for (int i = 0; i < 4; i++) + { + res[i] = (size << (8 * i)) >> 24; + } +} + +void extract(FILE *arhive) +{ + FILE *file; + char sim; + sim = fgetc(arhive); + fseek(arhive, 0, SEEK_SET); + while (!feof(arhive)) + { + if (sim == EOF) + break; + + char arr_size_fileName[4]; + fread(arr_size_fileName, sizeof(char), sizeof(arr_size_fileName), arhive); + int size_fileName_int = Back_Change_Size(arr_size_fileName); + char arr_fileName[size_fileName_int + 1]; + fread(arr_fileName, sizeof(char), size_fileName_int, arhive); + arr_fileName[size_fileName_int] = '\0'; + file = fopen(arr_fileName, "wb"); + char arr_size_file[4]; + fread(arr_size_file, sizeof(char), sizeof(arr_size_file), arhive); + unsigned long long size_file_int = Back_Change_Size(arr_size_file); + for (int i = 0; i < size_file_int; i++) + { + sim = fgetc(arhive); + fputc(sim, file); + } + sim = fgetc(arhive); + fseek(arhive, -1, SEEK_CUR); + printf("\nextract: "); + for (int i = 0; i < size_fileName_int; i++) + { + printf("%c", arr_fileName[i]); + } + } + fclose(arhive); +} + +void list(FILE *arhive) +{ + int counter_files = 0; + char sim = fgetc(arhive); + fseek(arhive, 0, SEEK_SET); + // fseek(arhive, 0, SEEK_END); + // long long size_file = ftell(arhive); + // fseek(arhive, 0, SEEK_SET); + while (!feof(arhive)) + { + if (sim == EOF) + break; + char arr_size_fileName[4]; + fread(arr_size_fileName, sizeof(char), sizeof(arr_size_fileName), arhive); + int size_fileName_int = Back_Change_Size(arr_size_fileName); + char arr_fileName[size_fileName_int]; + fread(arr_fileName, sizeof(char), size_fileName_int, arhive); + char arr_size_file[4]; + fread(arr_size_file, sizeof(char), sizeof(arr_size_file), arhive); + unsigned long long size_file_int = Back_Change_Size(arr_size_file); + fseek(arhive, size_file_int, SEEK_CUR); + counter_files++; + printf("%d)",counter_files); + for (int i = 0; i < size_fileName_int; i++) + { + printf("%c", arr_fileName[i]); + } + printf(" size: %lli\n", size_file_int); + sim = fgetc(arhive); + fseek(arhive, -1, SEEK_CUR); + + } + printf(" <>", counter_files); +} + +void creat(FILE *arhive, char *file_name) +{ + char sim; + FILE *file; + file = fopen(file_name, "rb"); + if (file == NULL) + { + printf("\nerror: file "); + for (int i = 0; i < strlen(file_name); i++) + { + printf("%c", file_name[i]); + } + + printf(" not found"); + } + else + { + fseek(file, 0, SEEK_END); + unsigned long long size_file_int = ftell(file); + fseek(file, 0, SEEK_SET); + char arr_size_file[4]; + Change_Size(size_file_int, arr_size_file); + + char arr_size_fileName[4]; + int size_fileName_int = strlen(file_name); + Change_Size(size_fileName_int, arr_size_fileName); + + fwrite(arr_size_fileName, sizeof(char), sizeof(arr_size_fileName), arhive); + fwrite(file_name, sizeof(char), size_fileName_int, arhive); + fwrite(arr_size_file, sizeof(char), sizeof(arr_size_file), arhive); + for (int i = 0; i < size_file_int; i++) + { + sim = fgetc(file); + fputc(sim, arhive); + } + + printf("\nadd: "); + for (int i = 0; i < size_fileName_int; i++) + { + printf("%c", file_name[i]); + } + fclose(file); + remove(file_name); + } +} +// 1085176 12048461 + + +int main(int argc, char **argv) +{ + FILE *file; + FILE *arhive ;//= fopen("arhive.arc", "rb"); + // char file_name[] = "tib.mp3"; + // char file_name2[] = "file2.txt"; + // char file_name3[] = "bitok.mp3"; + char *arhive_name; + if (argc < 4) + { + printf("error\n invalid numbers arguments"); + return 1; + } + for (int i = 0; i < argc; i++) + { + if (!strcmp(argv[i], "--file")) + { + arhive_name = argv[i + 1]; + } + + if (!strcmp(argv[i], "--create")) + { + printf("\n============================== create ==============================\n"); + arhive = fopen(arhive_name, "ab"); + for (int i = 4; i < argc; i++) + { + creat(arhive, argv[i]); + } + fclose(arhive); + printf("\n====================================================================\n"); + } + if (!strcmp(argv[i], "--list")) + { + printf("=============================== list ===============================\n"); + arhive = fopen(arhive_name, "rb"); + list(arhive); + fclose(arhive); + printf("\n====================================================================\n"); + } + if (!strcmp(argv[i], "--extract")) + { + printf("\n============================== extract ==============================\n"); + arhive = fopen(arhive_name, "rb"); + extract(arhive); + fclose(arhive); + remove(arhive_name); + printf("\n=====================================================================\n"); + // if (remove(arhive_name) != 0) + // { + // printf("no") ; + // } + // else + // printf("yes"); + } + } + + // arc --file data.arc --create a.txt b.bin c.bmp + + // creat(arhive, file_name); + // creat(arhive,file_name2); + // creat(arhive,file_name3); + // list(arhive); + // extract(arhive); + return 0; +} From 199c37391956cece02ee82afe49c6bbd6b96ebe6 Mon Sep 17 00:00:00 2001 From: Guslik5 <91369419+Guslik5@users.noreply.github.com> Date: Sat, 18 Dec 2021 15:47:48 +0300 Subject: [PATCH 2/5] Create laba_3.c --- lab3/laba_3.c | 215 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 lab3/laba_3.c diff --git a/lab3/laba_3.c b/lab3/laba_3.c new file mode 100644 index 00000000..e6131cd9 --- /dev/null +++ b/lab3/laba_3.c @@ -0,0 +1,215 @@ +#include +#include +#include +#include +#include +#include + +int size = 0; +typedef struct queue +{ + long long value; + struct queue *next; +} queue_c; + +queue_c *head = NULL; +queue_c *tail = NULL; + +queue_c *push(long long x) +{ + queue_c *ptr = (queue_c *)malloc(sizeof(queue_c)); + if (head != NULL) + { + tail->next = ptr; + ptr->value = x; + ptr->next = NULL; + tail = ptr; + } + else + { + ptr->value = x; + ptr->next = NULL; + tail = ptr; + head = ptr; + } + size++; +} + +void pop() +{ + if (head != NULL) + { + queue_c *temp = head->next; + free(head); + head = temp; + } + size--; +} + +int main(int argc, char **argv) +{ + if (argc != 3) + { + printf("ERROR: invalid number of arguments"); + return 1; + } + FILE *fin; + fin = fopen(argv[1], "r"); + if (fin == NULL) + { + printf("ERROR: the file does not exist"); + return 1; + } + long long counter = 0; + char sim; + long long second = atoi(argv[2]); + long long right_max; + long long left_max; + long long count_request = 0; + char request[1000]; + char month[4]; + long long time_1 = 0; + long long time_2 = 0; + int max = 0; + int a = 0; + struct tm *time = (struct tm *)malloc(sizeof(struct tm)); + while (sim != EOF) + { + while ((sim != '[') && (sim !=EOF)) + { + sim = fgetc(fin); + } + + if (sim == '[') + { + time -> tm_mday = (fgetc(fin) - 48) * 10 + fgetc(fin) - 48; + + sim = fgetc(fin); + month[0] = fgetc(fin); + month[1] = fgetc(fin); + month[2] = fgetc(fin); + if (strcmp(month, "Jan") == 0) + time->tm_mon = 0; + if (strcmp(month, "Feb") == 0) + time->tm_mon = 1; + if (strcmp(month, "Mar") == 0) + time->tm_mon = 2; + if (strcmp(month, "Apr") == 0) + time->tm_mon = 3; + if (strcmp(month, "May") == 0) + time->tm_mon = 4; + if (strcmp(month, "Jun") == 0) + time->tm_mon = 5; + if (strcmp(month, "Jul") == 0) + time->tm_mon = 6; + if (strcmp(month, "Aug") == 0) + time->tm_mon = 7; + if (strcmp(month, "Sep") == 0) + time->tm_mon = 8; + if (strcmp(month, "Oct") == 0) + time->tm_mon = 9; + if (strcmp(month, "Nov") == 0) + time->tm_mon = 10; + if (strcmp(month, "Dec") == 0) + time->tm_mon = 11; + + sim = fgetc(fin); + time->tm_year = (fgetc(fin) - 48) * 1000 + (fgetc(fin) - 48) * 100 + (fgetc(fin) - 48) * 10 + (fgetc(fin) - 48) - 1900;; + sim = fgetc(fin); + time->tm_hour = (fgetc(fin) - 48) * 10 + (fgetc(fin) - 48); + sim = fgetc(fin); + time->tm_min = (fgetc(fin) - 48) * 10 + (fgetc(fin) - 48); + sim = fgetc(fin); + time->tm_sec = (fgetc(fin) - 48) * 10 + (fgetc(fin) - 48); + sim = fgetc(fin); + + time_1 = mktime(time); + if (max == 0) + { + push(time_1); + time_2 = time_1; + count_request++; + } + else + { + if (time_1 - time_2 <= second) + { + push(time_1); + count_request++; + } + else + { + while ((time_1 - time_2 > second) && (size != 0)) + { + pop(); + count_request--; + if (size != 0) + time_2 = head->value; + } + push(time_1); + count_request++; + if (size == 1) + time_2 = time_1; + } + } + } + + while ((sim != '\"') && (sim != EOF)) + { + sim = fgetc(fin); + } + + if (max < count_request) + { + max = count_request; + left_max = time_2; + right_max = time_1; + } + + //поиск количества ошибок и сами реквесты + if (sim == '\"') + { + sim = fgetc(fin); + long long i = 0; + while (sim != '\"') + { + request[i] = sim; + i++; + sim = fgetc(fin); + } + sim = fgetc(fin); + sim = fgetc(fin); + //if ((sim != '1') && (sim != '2')) + //{ + //counter++; + if (sim == '5') + { + counter++; + int j; + for (j = 0; j < i; j++) + { + printf("%c", request[j]); + } + printf("\n"); + } + //while (sim != '\n') + //{ + // sim = fgetc(fin); + //} + //} + } + while ((sim != '\n') && (sim != EOF)) + { + sim = fgetc(fin); + } + } + printf("count of error %i\n", counter); + printf("maximum number of requests per % lli seconds:", second); + printf("% lli \n", max); + time = localtime(&left_max); + // time = &left_max; + printf("from:\n %s", asctime(time)); + time = localtime(&right_max); + printf("until:\n %s", asctime(time)); + return 0; +} From fd7d18c2e079df7189052bc2e4f99fe8b5effcc8 Mon Sep 17 00:00:00 2001 From: Guslik5 <91369419+Guslik5@users.noreply.github.com> Date: Sat, 18 Dec 2021 16:23:18 +0300 Subject: [PATCH 3/5] Create laba_3_final --- lab3/laba_3_final | 215 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 lab3/laba_3_final diff --git a/lab3/laba_3_final b/lab3/laba_3_final new file mode 100644 index 00000000..e6131cd9 --- /dev/null +++ b/lab3/laba_3_final @@ -0,0 +1,215 @@ +#include +#include +#include +#include +#include +#include + +int size = 0; +typedef struct queue +{ + long long value; + struct queue *next; +} queue_c; + +queue_c *head = NULL; +queue_c *tail = NULL; + +queue_c *push(long long x) +{ + queue_c *ptr = (queue_c *)malloc(sizeof(queue_c)); + if (head != NULL) + { + tail->next = ptr; + ptr->value = x; + ptr->next = NULL; + tail = ptr; + } + else + { + ptr->value = x; + ptr->next = NULL; + tail = ptr; + head = ptr; + } + size++; +} + +void pop() +{ + if (head != NULL) + { + queue_c *temp = head->next; + free(head); + head = temp; + } + size--; +} + +int main(int argc, char **argv) +{ + if (argc != 3) + { + printf("ERROR: invalid number of arguments"); + return 1; + } + FILE *fin; + fin = fopen(argv[1], "r"); + if (fin == NULL) + { + printf("ERROR: the file does not exist"); + return 1; + } + long long counter = 0; + char sim; + long long second = atoi(argv[2]); + long long right_max; + long long left_max; + long long count_request = 0; + char request[1000]; + char month[4]; + long long time_1 = 0; + long long time_2 = 0; + int max = 0; + int a = 0; + struct tm *time = (struct tm *)malloc(sizeof(struct tm)); + while (sim != EOF) + { + while ((sim != '[') && (sim !=EOF)) + { + sim = fgetc(fin); + } + + if (sim == '[') + { + time -> tm_mday = (fgetc(fin) - 48) * 10 + fgetc(fin) - 48; + + sim = fgetc(fin); + month[0] = fgetc(fin); + month[1] = fgetc(fin); + month[2] = fgetc(fin); + if (strcmp(month, "Jan") == 0) + time->tm_mon = 0; + if (strcmp(month, "Feb") == 0) + time->tm_mon = 1; + if (strcmp(month, "Mar") == 0) + time->tm_mon = 2; + if (strcmp(month, "Apr") == 0) + time->tm_mon = 3; + if (strcmp(month, "May") == 0) + time->tm_mon = 4; + if (strcmp(month, "Jun") == 0) + time->tm_mon = 5; + if (strcmp(month, "Jul") == 0) + time->tm_mon = 6; + if (strcmp(month, "Aug") == 0) + time->tm_mon = 7; + if (strcmp(month, "Sep") == 0) + time->tm_mon = 8; + if (strcmp(month, "Oct") == 0) + time->tm_mon = 9; + if (strcmp(month, "Nov") == 0) + time->tm_mon = 10; + if (strcmp(month, "Dec") == 0) + time->tm_mon = 11; + + sim = fgetc(fin); + time->tm_year = (fgetc(fin) - 48) * 1000 + (fgetc(fin) - 48) * 100 + (fgetc(fin) - 48) * 10 + (fgetc(fin) - 48) - 1900;; + sim = fgetc(fin); + time->tm_hour = (fgetc(fin) - 48) * 10 + (fgetc(fin) - 48); + sim = fgetc(fin); + time->tm_min = (fgetc(fin) - 48) * 10 + (fgetc(fin) - 48); + sim = fgetc(fin); + time->tm_sec = (fgetc(fin) - 48) * 10 + (fgetc(fin) - 48); + sim = fgetc(fin); + + time_1 = mktime(time); + if (max == 0) + { + push(time_1); + time_2 = time_1; + count_request++; + } + else + { + if (time_1 - time_2 <= second) + { + push(time_1); + count_request++; + } + else + { + while ((time_1 - time_2 > second) && (size != 0)) + { + pop(); + count_request--; + if (size != 0) + time_2 = head->value; + } + push(time_1); + count_request++; + if (size == 1) + time_2 = time_1; + } + } + } + + while ((sim != '\"') && (sim != EOF)) + { + sim = fgetc(fin); + } + + if (max < count_request) + { + max = count_request; + left_max = time_2; + right_max = time_1; + } + + //поиск количества ошибок и сами реквесты + if (sim == '\"') + { + sim = fgetc(fin); + long long i = 0; + while (sim != '\"') + { + request[i] = sim; + i++; + sim = fgetc(fin); + } + sim = fgetc(fin); + sim = fgetc(fin); + //if ((sim != '1') && (sim != '2')) + //{ + //counter++; + if (sim == '5') + { + counter++; + int j; + for (j = 0; j < i; j++) + { + printf("%c", request[j]); + } + printf("\n"); + } + //while (sim != '\n') + //{ + // sim = fgetc(fin); + //} + //} + } + while ((sim != '\n') && (sim != EOF)) + { + sim = fgetc(fin); + } + } + printf("count of error %i\n", counter); + printf("maximum number of requests per % lli seconds:", second); + printf("% lli \n", max); + time = localtime(&left_max); + // time = &left_max; + printf("from:\n %s", asctime(time)); + time = localtime(&right_max); + printf("until:\n %s", asctime(time)); + return 0; +} From 9d6a051a86475be281023d6f3aefe5b77fc7d938 Mon Sep 17 00:00:00 2001 From: Guslik5 <91369419+Guslik5@users.noreply.github.com> Date: Sat, 15 Jan 2022 13:17:53 +0300 Subject: [PATCH 4/5] Create laba2.c --- lab2/laba2.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 lab2/laba2.c diff --git a/lab2/laba2.c b/lab2/laba2.c new file mode 100644 index 00000000..2228b557 --- /dev/null +++ b/lab2/laba2.c @@ -0,0 +1,172 @@ +#include +#include + +typedef struct Num +{ + unsigned int arr[32]; +} Num; + +Num uint1024_t_from_uint(unsigned int value) // перевод числа в uint1024 +{ + Num res; + for (int i = 0; i < 31; i++) + res.arr[i] = 0; + res.arr[31] = value; + return res; +} +Num add_op(Num value1, Num value2) // cложение uint1024 +{ + Num res; + for (int i = 0; i < 32; i++) // зануляем + res.arr[i] = 0; + res.arr[0] += value1.arr[0] + value2.arr[0]; // мы не можем проверить на переполнение, поэтому делаем это сразу(отрежем если переполнение) + for (int i = 31; i > 0; i--) + { + res.arr[i] += value1.arr[i] + value2.arr[i]; + if ((value1.arr[i] > res.arr[i]) && (value2.arr[i] > res.arr[i])) // проверяем на переполнение + res.arr[i - 1]++; // тк при сложении может быть переполнение только на 1 + } + + return res; +} +Num subtr_op(Num value, Num y) //разность +{ + Num res; + for (int i = 0; i < 32; i++) // зануляем + res.arr[i] = 0; + res.arr[0] += value.arr[0] - y.arr[0]; + for (int i = 31; i >= 0; i--) + { + res.arr[i] += value.arr[i] - y.arr[i]; + if (res.arr[i] > value.arr[i]) // проверяем переполнение + res.arr[i - 1]--; + } + + return res; +} +Num multiplication(Num value, unsigned int value_int) // Произведение с unsigned int +{ + Num res; + for (int i = 0; i < 32; i++) // зануляем + res.arr[i] = 0; + unsigned long long a = value_int; + for (int i = 31; i >= 0; i--) + { + unsigned long long composition; + unsigned long long b = value.arr[i]; + composition = a * b; // перемножение интов не даст больше лонга, переполнения тут не будет, но нужно учесть, что в лонге как бы другая система счисления + res.arr[i] += composition % 4294967296; // делим на систему(как бы обратный перевод) + if (i != 0) // переполнение + res.arr[i - 1] += composition / 4294967296; + } + return res; +} +Num mult_op(Num value, Num value2) //Произведение uint1024 +{ + Num res; + for (int i = 0; i < 32; i++) // зануляем + res.arr[i] = 0; + for (int i = 31; i >= 0; i--) + { + if (value2.arr[i] != 0) + { + Num temp = multiplication(value, value2.arr[i]); + int k = 31 - i; + for (int j = k; j < 32; j++) // сдвигаем на разряд при умножении + { + temp.arr[j - k] = temp.arr[j]; + } + for (int j = 32 - k; j < 32; j++) + { + temp.arr[j] = 0; + } + res = add_op(res, temp); + } + } + return res; +} +void printf_value(Num value) //вывод +{ + char buf[1024] = {0}; + for (int i = 0; i < 1024; i++) + { + + Num res; + for (int i = 0; i < 32; i++) + { + res.arr[i] = 0; + } + unsigned long long remains = 0; + for (int i = 0; i < 32; i++) // находим остаток от деления на 10 и делим на 10 + { + res.arr[i] = (remains * 4294967296 + value.arr[i]) / 10; + remains = (remains * 4294967296 + value.arr[i]) % 10; + } + unsigned mod = remains; + value = res; // после деления на 10 + + buf[1023 - i] = mod + '0'; // записываем чарами с конца + } + + unsigned begin = 0; + while (buf[begin] == '0') //пропускаем ненужные 0 в начале + begin++; + if (begin != 1024) //если все 0, значит число 0 + for (int i = begin; i < 1024; i++) // выписываем начиная с момента, когда встретим первую цифру + { + printf("%c", buf[i]); + } + else + { + printf("0"); + } +} +void scanf_value(Num *value) //ввод +{ + char buffer[1024]; + scanf("%s", buffer); + int counter = 0; + for (int i = 0; i < 32; i++) // зануляем + value->arr[i] = 0; + while (buffer[counter] != '\0' && counter < 1024) // пока не дойдем до конца или не выйдем за 1024 + { + Num templete; + for (int i = 0; i < 32; i++) // зануляем + templete.arr[i] = 0; + for (int i = 31; i >= 0; i--) + { + unsigned long long composition; + unsigned long long a = value->arr[i]; + composition = a * 10; + templete.arr[i] += composition % 4294967296; + if (i != 0) // переполнение + templete.arr[i - 1] += composition / 4294967296; + } + *value = templete; + Num temp = uint1024_t_from_uint(buffer[counter] - '0'); //перевод символа в uint1024 + *value = add_op(*value, temp); + counter++; + } +} + +int main() +{ + Num a; + Num b; + Num temp; + printf("Введите первое число: "); + scanf_value(&a); + printf("\n"); + printf("Введите второе число: "); + scanf_value(&b); + printf("\nСумма: "); + temp = add_op(a, b); + printf_value(temp); + printf("\nРазность: "); + temp = subtr_op(a, b); + printf_value(temp); + printf("\nПроизведение: "); + temp = mult_op(a, b); + printf_value(temp); + return 0; +} From 7a490367ebe681a5ef55f78439f6dcc1a63f1711 Mon Sep 17 00:00:00 2001 From: Guslik5 <91369419+Guslik5@users.noreply.github.com> Date: Sat, 15 Jan 2022 13:31:44 +0300 Subject: [PATCH 5/5] Create last laba 2 --- lab2/last_laba2.c | 172 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 lab2/last_laba2.c diff --git a/lab2/last_laba2.c b/lab2/last_laba2.c new file mode 100644 index 00000000..2228b557 --- /dev/null +++ b/lab2/last_laba2.c @@ -0,0 +1,172 @@ +#include +#include + +typedef struct Num +{ + unsigned int arr[32]; +} Num; + +Num uint1024_t_from_uint(unsigned int value) // перевод числа в uint1024 +{ + Num res; + for (int i = 0; i < 31; i++) + res.arr[i] = 0; + res.arr[31] = value; + return res; +} +Num add_op(Num value1, Num value2) // cложение uint1024 +{ + Num res; + for (int i = 0; i < 32; i++) // зануляем + res.arr[i] = 0; + res.arr[0] += value1.arr[0] + value2.arr[0]; // мы не можем проверить на переполнение, поэтому делаем это сразу(отрежем если переполнение) + for (int i = 31; i > 0; i--) + { + res.arr[i] += value1.arr[i] + value2.arr[i]; + if ((value1.arr[i] > res.arr[i]) && (value2.arr[i] > res.arr[i])) // проверяем на переполнение + res.arr[i - 1]++; // тк при сложении может быть переполнение только на 1 + } + + return res; +} +Num subtr_op(Num value, Num y) //разность +{ + Num res; + for (int i = 0; i < 32; i++) // зануляем + res.arr[i] = 0; + res.arr[0] += value.arr[0] - y.arr[0]; + for (int i = 31; i >= 0; i--) + { + res.arr[i] += value.arr[i] - y.arr[i]; + if (res.arr[i] > value.arr[i]) // проверяем переполнение + res.arr[i - 1]--; + } + + return res; +} +Num multiplication(Num value, unsigned int value_int) // Произведение с unsigned int +{ + Num res; + for (int i = 0; i < 32; i++) // зануляем + res.arr[i] = 0; + unsigned long long a = value_int; + for (int i = 31; i >= 0; i--) + { + unsigned long long composition; + unsigned long long b = value.arr[i]; + composition = a * b; // перемножение интов не даст больше лонга, переполнения тут не будет, но нужно учесть, что в лонге как бы другая система счисления + res.arr[i] += composition % 4294967296; // делим на систему(как бы обратный перевод) + if (i != 0) // переполнение + res.arr[i - 1] += composition / 4294967296; + } + return res; +} +Num mult_op(Num value, Num value2) //Произведение uint1024 +{ + Num res; + for (int i = 0; i < 32; i++) // зануляем + res.arr[i] = 0; + for (int i = 31; i >= 0; i--) + { + if (value2.arr[i] != 0) + { + Num temp = multiplication(value, value2.arr[i]); + int k = 31 - i; + for (int j = k; j < 32; j++) // сдвигаем на разряд при умножении + { + temp.arr[j - k] = temp.arr[j]; + } + for (int j = 32 - k; j < 32; j++) + { + temp.arr[j] = 0; + } + res = add_op(res, temp); + } + } + return res; +} +void printf_value(Num value) //вывод +{ + char buf[1024] = {0}; + for (int i = 0; i < 1024; i++) + { + + Num res; + for (int i = 0; i < 32; i++) + { + res.arr[i] = 0; + } + unsigned long long remains = 0; + for (int i = 0; i < 32; i++) // находим остаток от деления на 10 и делим на 10 + { + res.arr[i] = (remains * 4294967296 + value.arr[i]) / 10; + remains = (remains * 4294967296 + value.arr[i]) % 10; + } + unsigned mod = remains; + value = res; // после деления на 10 + + buf[1023 - i] = mod + '0'; // записываем чарами с конца + } + + unsigned begin = 0; + while (buf[begin] == '0') //пропускаем ненужные 0 в начале + begin++; + if (begin != 1024) //если все 0, значит число 0 + for (int i = begin; i < 1024; i++) // выписываем начиная с момента, когда встретим первую цифру + { + printf("%c", buf[i]); + } + else + { + printf("0"); + } +} +void scanf_value(Num *value) //ввод +{ + char buffer[1024]; + scanf("%s", buffer); + int counter = 0; + for (int i = 0; i < 32; i++) // зануляем + value->arr[i] = 0; + while (buffer[counter] != '\0' && counter < 1024) // пока не дойдем до конца или не выйдем за 1024 + { + Num templete; + for (int i = 0; i < 32; i++) // зануляем + templete.arr[i] = 0; + for (int i = 31; i >= 0; i--) + { + unsigned long long composition; + unsigned long long a = value->arr[i]; + composition = a * 10; + templete.arr[i] += composition % 4294967296; + if (i != 0) // переполнение + templete.arr[i - 1] += composition / 4294967296; + } + *value = templete; + Num temp = uint1024_t_from_uint(buffer[counter] - '0'); //перевод символа в uint1024 + *value = add_op(*value, temp); + counter++; + } +} + +int main() +{ + Num a; + Num b; + Num temp; + printf("Введите первое число: "); + scanf_value(&a); + printf("\n"); + printf("Введите второе число: "); + scanf_value(&b); + printf("\nСумма: "); + temp = add_op(a, b); + printf_value(temp); + printf("\nРазность: "); + temp = subtr_op(a, b); + printf_value(temp); + printf("\nПроизведение: "); + temp = mult_op(a, b); + printf_value(temp); + return 0; +}