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 #217

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
148 changes: 148 additions & 0 deletions lab2/LongArifm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define MAX_UNSIGNED_INT 4294967296

typedef struct UINT1024 {
unsigned int array[32];
} uint1024_t;

uint1024_t from_uint(unsigned int x) {
uint1024_t a;
for (int i = 0; i <= 31; i++) {
a.array[i] = 0;
}
a.array[0] = x;
return a;
}

uint1024_t add_op(uint1024_t x, uint1024_t y) {
int trans_reg = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Правильное слово carry

uint64_t overflow = MAX_UNSIGNED_INT;
uint1024_t z = from_uint(0);
for (int i = 0; i <= 31; i++) {
uint64_t value = ((uint64_t) x.array[i] + (uint64_t) y.array[i] + trans_reg);
z.array[i] = value % overflow;
trans_reg = value / overflow;
}
return z;
}

uint1024_t mult_op(uint1024_t x, uint1024_t y) {
uint64_t overflow = MAX_UNSIGNED_INT;
uint1024_t mlt = from_uint(0);
uint1024_t c = from_uint(0);
int count = sizeof(x.array)/sizeof(x.array[0]);
int count2 = sizeof(y.array)/sizeof(y.array[0]);
for (size_t i=0; i<count; ++i)
for (int j=0, carry=0; j<count2 || carry; ++j) {
long long cur = c.array[i+j] + (long long)x.array[i] *(j < count2 ? y.array[j] : 0) + carry;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если будут большие числа, то будет выход за границы массива
-1

c.array[i+j] = (int)(cur % overflow);
carry = (int)(cur / overflow);
}
return c;
}


uint1024_t subtr_op(uint1024_t x, uint1024_t y) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не работает
-2

int carry = 0;
uint64_t overflow = MAX_UNSIGNED_INT;
uint1024_t sum_mass;
for (int i = 0; i <= 31; i++) {
sum_mass.array[i] = 0;
}
int count = sizeof(x.array)/sizeof(x.array[0]);
for (size_t i=0; i<count || carry; ++i) {
x.array[i] -= carry + (i < count ? y.array[i] : 0);
carry = x.array[i] < 0;
if (carry != 0) {
x.array[i] += overflow;
}
}
return x;

}

uint1024_t segmentation(uint1024_t x, long long y){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

division


long long carry = 0;
uint64_t overflow = MAX_UNSIGNED_INT;
uint1024_t sum_mass;
for (int i = 0; i <= 31; i++) {
sum_mass.array[i] = 0;
}
int count = sizeof(x.array)/sizeof(x.array[0]);
for (int i=(int)count-1; i>=0; --i) {
long long cur = x.array[i] + carry * overflow;
x.array[i] = (int)(cur / y);
carry = (int)(cur % y);
}
return x;
}

void scanf_value(uint1024_t *x) {
char line[310];
uint1024_t y;
y = from_uint(0);
scanf("%s", line);
for (int i = 0; i < strlen(line); i++) {
y = mult_op(y, from_uint(10));
y = add_op(y, from_uint(line[i] - '0'));
}
for (int i = 0; i <= 31; i++) {
x->array[i] = y.array[i];
}
}

void printf_value(uint1024_t x) {
uint64_t overflow = MAX_UNSIGNED_INT;
uint1024_t z = from_uint(0);
uint1024_t zero = from_uint(0);
char line[310];
memset(line, '\0', 310);
do {
uint64_t trans_reg = 0;
for (int i = 31; i >= 0; i--) {
uint64_t value = (x.array[i] + trans_reg * overflow);
z.array[i] = value / 10;
trans_reg = value % 10;
}
line[strlen(line)] = trans_reg + '0';
x = z;
} while (memcmp(&x, &zero, sizeof(int) * 32) != 0);
for (int i = strlen(line); i >= 0; i--) {
printf("%c", line[i]);
}
}

int main() {
uint1024_t a = from_uint(0), b = from_uint(0);
long long y = 0;
printf("Multiplication test\n");
scanf_value(&a);
scanf_value(&b);
printf_value(mult_op(a, b));
printf("\n");

printf("Segmentation test\n");
scanf_value(&a);
scanf("%llu",&y);
printf_value(segmentation(a, y));
printf("\n");

printf("Addition test\n");
scanf_value(&a);
scanf_value(&b);
printf_value(add_op(a, b));
printf("\n");


printf("Subtraction test\n");
scanf_value(&a);
scanf_value(&b);
printf_value(subtr_op(a, b));
printf("\n");
}



134 changes: 134 additions & 0 deletions lab3/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#define REQ_LEN 2500
//=======================================ERROR
void error_5xx(FILE* file1, FILE* file2) {

char string[REQ_LEN];
unsigned int size_of_string = 0;
int count_errors = 0;
int first_element = 0;
int last_element = 0;
while (!feof(file1)) {
char *check = fgets(string, REQ_LEN, file1);
if (check == NULL) {

}
size_of_string = strlen(string);
for (int i = 0; i < size_of_string; i++) {
if (string[i] == '"') {
first_element = i;
break;
}
}
for (int j = first_element + 1; j < size_of_string; j++) {
if (string[j] == '"') {
last_element = j;
break;
}
}
if (string[last_element + 2] == '5') {
fprintf(file2, "%s", string + first_element + 1);
count_errors = count_errors + 1;
}
}
printf("%d", count_errors);

}
//=======================================TIME

/*char Get_TimeStr(FILE *file){
int start_pos;
char string[REQ_LEN];
char sub_str[20];
unsigned int size_of_string = 0;
char *check = fgets(string, REQ_LEN , file);
if (check == NULL) {
return 1 ;
}
size_of_string = strlen(string);
for (int i = 0; i < size_of_string; i++) {
if (string[i] == '[') {
start_pos = i;
break;
}
}
strncpy(sub_str,string[start_pos + 1], 20);
return sub_str;
}*/


time_t Get_time(char *string) {
char month[12][4]= {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};
struct tm time;
time.tm_mday = atoi(string);
for (int i = 0; i < 11; i++)
if (!strcmp(month[i], string)) {
time.tm_mon = i;
break;
}
time.tm_year = atoi(string + 7) - 1900;
time.tm_hour = atoi(string + 12);
time.tm_min = atoi(string + 15);
time.tm_sec = atoi(string + 18);
return mktime(&time);
}

char *Time_str(char *string){

char *str = malloc(20);
int i = 0;
while (*(string+i) != '[' ){
i++;
}
strncpy(str, string + i + 1, 20);

return str;
}


void Time_Window(FILE *file, long long sec_period) {
long int first_pos = 0, sec_pos = 0, count = 0;
fseek(file, 0, SEEK_END);
char first_string[REQ_LEN], second_string[REQ_LEN];
int from_end = ftell(file);

while (sec_pos < from_end) {
fseek(file, first_pos, SEEK_SET);
fgets(first_string, REQ_LEN, file);
char *data = Time_str(first_string);
int firstTime = Get_time(data);
first_pos = ftell(file);
if (sec_pos > first_pos) {
fseek(file, sec_pos, SEEK_SET);
}
while (sec_pos < from_end) {
fgets(second_string, REQ_LEN, file);
sec_pos = ftell(file);
char *data1 = Time_str(second_string);
int lastTime = Get_time(data1);
if (difftime(lastTime, firstTime) > sec_period){
break;
}
count++;
}
}
printf("\n");
printf("window : %d count : %d \n", sec_period, count - 2);
}
//=======================================Main_Program

int main(int argc, char** argv) {
int period;
FILE *log_in = fopen("123.txt", "r");
error_5xx(log_in, stdout);
printf(" \n ");
printf("Write time period in seconds: ");
scanf("%d", &period);
Time_Window(log_in, period);
return 0;
}
Loading