Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guslik5 patch 5 #221

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions lab2/laba2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#include <stdio.h>
#include <string.h>

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;
}
215 changes: 215 additions & 0 deletions lab3/laba_3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#include <malloc.h>
#include <locale.h>

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;
}
Loading