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

Lab2 #209

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Lab2 #209

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
147 changes: 147 additions & 0 deletions lab2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#define base 1000000000


typedef struct uint1024_t {
uint32_t *chunk;
size_t count;
size_t size;
} uint1024_t;

void str_reverse(uint8_t *arr, size_t size) {
uint8_t temp;
for (size_t i = 0, j = size-1; i < size / 2; i++, j--) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

void print_uint1024_t(uint1024_t *x) {
if (x->count == 0) {
printf("null\n");
return;
}
ssize_t i = x->count-1;
for (; i > 0; --i) {
if (x->chunk[i] != 0) {
break;
}
}
printf("%u", x->chunk[i]);
for (--i; i != -1; --i) {
printf("%.9u", x->chunk[i]);
}
putchar('\n');
}

uint1024_t *add_op(uint1024_t *x, uint1024_t *y) {

size_t max_count;

if (x->count > y->count)
max_count = x->count;
else
max_count = y->count;

uint8_t carry = 0;

uint1024_t *result;
result = (uint1024_t*)malloc(sizeof(uint1024_t));
memcpy(result, x, sizeof(uint1024_t));

for (size_t i = 0; i < max_count; ++i) {
uint64_t temp = carry + x->chunk[i] + y->chunk[i];
result->chunk[i] = temp % base;
carry = temp / base;
}

if (carry) {
result->chunk[result->count] = carry;
result->count++;
}


return result;
}

uint1024_t *init(uint8_t *str) {

const size_t buff_size = 9;

uint1024_t *new_uint = (uint1024_t*)malloc(sizeof(uint1024_t));
new_uint->size = 32;
new_uint->count = 0;
new_uint->chunk = (uint32_t*)malloc(new_uint->size * sizeof(uint32_t));


uint8_t num[buff_size + 1];
memset(num, 0, (buff_size + 1) * sizeof(uint8_t));

size_t next = 0;
size_t index = 0;
size_t length = strlen(str);

for (int i = length - 1; i >= 0; i--) {
num[index++] = str[i];
if (index == buff_size) {
index = 0;
str_reverse(num, buff_size);
new_uint->chunk[next++] = atoi(num);
new_uint->count++;
}
}
if (index != 0) {
num[index] = '\0';
str_reverse(num, index);
new_uint->chunk[next++] = atoi(num);
new_uint->count++;
}

return new_uint;
}

void _free(uint1024_t *x) {
for (int i = 0; i < 32; i++){
free(x->chunk[i]);
}
free(x->chunk);
free(x);
}

int main() {

char str1[309];
char str2[309];

printf ("enter first uint1024_t num ");
scanf("%309s", str1);

printf ("enter second uint1024_t num ");
scanf("%309s", str2);

uint1024_t *x = init(str1);
uint1024_t *y = init(str2);

printf ("\nfirst uint1024_t num ");
print_uint1024_t(x);
printf ("\nsecond uint1024_t num ");
print_uint1024_t(y);

printf ("\nsum uint1024_t num ");
print_uint1024_t(add_op(x, y));


_free(x);
_free(y);



return 0;
}


144 changes: 144 additions & 0 deletions lab3/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

const char *calendar[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

typedef struct data {
char *remote_addr;
char *local_time;
char *time_zone;
char *request;
char *status;
char *bytes_sent;
} data;

int get_time(char *request_time) {

char month[4];
int time_value;
struct tm local_time = {};

sscanf(request_time, "%d/%3s/%d:%d:%d:%d", &local_time.tm_mday, month, &local_time.tm_year, &local_time.tm_hour, &local_time.tm_min, &local_time.tm_sec);
local_time.tm_year -= 1900;

for (int i = 0; i < 12; i++){
if (strcmp(month, calendar[i])){
local_time.tm_mon = i;
break;
}
}

time_value = mktime(&local_time);

//printf("time value = %d\n", time_value);
return time_value;
}

data get_data(char *request){

data result;

result.remote_addr = strtok(request, "-");

strtok(NULL, "[");
result.local_time = strtok(NULL, " ");

result.time_zone = strtok(NULL, "]");

strtok(NULL, "\"");
result.request = strtok(NULL, "\"");

result.status = strtok(NULL, " ");
result.bytes_sent = strtok(NULL, "\n");

return result;

}

int data_correct(data request_data){

if(request_data.local_time == NULL || request_data.request == NULL || request_data.status == NULL || request_data.time_zone == NULL || request_data.remote_addr == NULL || request_data.bytes_sent == NULL){
return 0;
}
return 1;
}

int main(int argc, char** argv){

if (argc != 3){
printf("too many or too less arguments (file name ; time window) \n");
exit(-1);
}

char *file_name = argv[1];

int tm_window = atoi(argv[2]);

if (tm_window < 1){
printf("time window error");
exit(-1);
}

unsigned long string_counter = 0;
unsigned long error_counter = 0;
int max_string = 256;
char *cur_request = calloc(4096, sizeof(char));
int *time_array = calloc(max_string, sizeof(long long));

FILE *input = fopen(file_name, "r");

if (input == NULL) {
printf("file path error");
exit(-1);
}

while (!feof(input)) {

fgets(cur_request, 4096, input);
data request_data = get_data(cur_request);

if (data_correct(request_data)){

if (request_data.status[0] == '5'){
printf("%s\n", request_data.request);
error_counter = error_counter + 1;
}

if (string_counter == max_string){
max_string = max_string * 2;
time_array = realloc(time_array, max_string * sizeof(long long));
}

time_array[string_counter] = get_time(request_data.local_time);
string_counter++;
}
}

int left = 0;
int right = 0;
int max_request = 0;
int maxl, maxr;

for (int left = 0; left < string_counter; left++){
right = left;

while (right < string_counter && time_array[right] - time_array[left] <= tm_window) {
right++;
}

if ((right - left) > max_request){
max_request = right - left;
maxl = time_array[left];
maxr = time_array[right] - 1;
}
}

printf("======================================================\n");
printf("max request : [%d] in time window : [%d] from time gap : [%d] to : [%d]\n", max_request, tm_window, maxl, maxr);
printf("total [5xx] errors: [%d]\n", error_counter);

return 0;
}

Loading