-
Notifications
You must be signed in to change notification settings - Fork 32
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.v2 #219
Open
Var-S
wants to merge
22
commits into
somov7:main
Choose a base branch
from
Var-S:lab2.2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lab2.v2 #219
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
30f4a6d
Add files via upload
Var-S e50f264
2
Var-S 5908b0f
2
Var-S b8cee83
3
Var-S b9d06f5
4
Var-S a2ffc56
4
Var-S 85b1786
4
Var-S c1de0ee
4
Var-S 04ec29e
4
Var-S 4924882
Add files via upload
Var-S 16d29fb
Update lab2.1.c
Var-S d735271
Add files via upload
Var-S abcd0d2
lab3
Var-S f583eed
Delete WordCont1.c
Var-S dd9c8c8
Delete WordCont.c
Var-S a8d0972
Delete lab2.1.c
Var-S 9484dd4
Add files via upload
Var-S 7092189
Merge pull request #3 from Var-S/lab2
Var-S 843500c
Delete LongArifm.c
Var-S 63de156
Last chance
Var-S 61dcf09
Again
Var-S c6b50bd
YES
Var-S File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
#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 carry = 0; | ||
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] + carry); | ||
z.array[i] = value % overflow; | ||
carry = value / overflow; | ||
} | ||
return z; | ||
} | ||
|
||
uint1024_t mult_op(uint1024_t x, uint1024_t y) { | ||
uint1024_t mlt = from_uint(0); | ||
uint64_t overflow = MAX_UNSIGNED_INT; | ||
for (int i = 0; i <= 31; i++) { | ||
uint64_t carry = 0; | ||
uint1024_t z = from_uint(0); | ||
for (int j = 0; j <= 31; j++) { | ||
uint64_t value = ((uint64_t) x.array[j] * (uint64_t) y.array[i] + carry); | ||
z.array[j] = value % overflow; | ||
carry = value / overflow; | ||
} | ||
if (carry > 0) { | ||
printf("overflow!\n"); | ||
return mlt; | ||
} | ||
|
||
mlt = add_op(mlt, z); | ||
} | ||
return mlt; | ||
} | ||
|
||
|
||
uint1024_t subtr_op(uint1024_t x, uint1024_t y) { | ||
int carry = 0; | ||
uint64_t overflow = MAX_UNSIGNED_INT; | ||
uint1024_t sum_mass; | ||
for (int i = 0; i <= 31; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. magic number |
||
sum_mass.array[i] = 0; | ||
} | ||
int count = sizeof(x.array)/sizeof(x.array[0]); | ||
for (size_t i=0; i<count || carry; ++i) { | ||
if (i == count){ | ||
break; | ||
} | ||
x.array[i] -= carry + (i < count ? y.array[i] : 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если будет carry на последнем индексе, то случится выход за границы массива, вероятно, с сегфолтом |
||
carry = x.array[i] < 0; | ||
if (carry != 0) { | ||
x.array[i] += overflow; | ||
} | ||
} | ||
return x; | ||
|
||
} | ||
|
||
uint1024_t segmentation(uint1024_t x, long long y){ | ||
|
||
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 carry = 0; | ||
for (int i = 31; i >= 0; i--) { | ||
uint64_t value = (x.array[i] + carry * overflow); | ||
z.array[i] = value / 10; | ||
carry = value % 10; | ||
} | ||
line[strlen(line)] = carry + '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"); | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Умножение не работает
-2