From 923f5e8d2edbe4fa074db8eac523561405e2f180 Mon Sep 17 00:00:00 2001 From: MagicGiant <88337554+MagicGiant@users.noreply.github.com> Date: Fri, 14 Jan 2022 18:43:08 +0300 Subject: [PATCH 1/2] laba 4 --- 4.c | 321 +++++++++++++++++++++++++++++++++++++++++++++++++++++ notation.h | 25 +++++ str.h | 61 ++++++++++ 3 files changed, 407 insertions(+) create mode 100644 4.c create mode 100644 notation.h create mode 100644 str.h diff --git a/4.c b/4.c new file mode 100644 index 0000000..22bbd22 --- /dev/null +++ b/4.c @@ -0,0 +1,321 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include "notation.h" + +FILE* file; +int ID3_size; + +char APIC[] = "APIC"; +char TXXX[] = "TXXX"; + +void error(char comment[]) +{ + printf("%s\n", comment); + printf("--error--\n"); + printf("____________________\n"); + exit(1); +} + +bool is_frame(char* arr) +{ + if (strcmp(APIC, arr) == 0 || strcmp(TXXX, arr) == 0) + return 0; + for (int i = 0; i < 3; i++) + if (arr[i] < 'A' || arr[i]>'Z') + return 0; + if ((arr[3] < 'A' || arr[3]>'Z') && (arr[3] < '0' || arr[3]>'9')) + return 0; + return 1; +} +void shift_file(int shift_it, int n) +{ + FILE* save = fopen("saving.sus", "w+b"); + int size = 0; + fseek(file, shift_it, SEEK_SET); + fseek(save, 0, SEEK_SET); + while (!feof(file)) + { + int c = getc(file); + putc(c, save); + size++; + } + fseek(file, shift_it + n, SEEK_SET); + fseek(save, 0, SEEK_SET); + for (int i = 0; i < size; i++) + { + int c = getc(save); + putc(c, file); + } + fclose(save); + remove("saving.sus"); +} +int scan_size(int number) +{ + char* bits = (char*)malloc(number * 7 + 1); + bits[number * 7] = 0; + for (int i = 0; i < number; i++) + { + int data = 0; + fread(&data, 1, 1, file); + bits = get_10_2(data, 7, i, bits); + } + int data = get_2_10(bits); + free(bits); + return data; +} +void print_size(int data) +{ + char* bits = (char*)malloc(33); + bits[32] = 0; + bits = get_10_2(data, 32, 0, bits); + char new_bits[33]; + new_bits[32] = 0; + int j = 3; + for (int i = 28; i >= 0; i--) + { + if ((i + 3) % 8 == 0) + { + new_bits[i + j] = '0'; + j--; + } + new_bits[i + j] = bits[i + 3]; + } + free(bits); + for (int i = 0; i < 4; i++) + { + char arr[9]; + arr[8] = 0; + for (int k = i * 8; k < (i + 1) * 8; k++) + arr[k - i * 8] = new_bits[k]; + putc(get_2_10(arr), file); + } +} +void read_file(char* your_frame) +{ + fseek(file, 10, SEEK_SET); + bool code = 0; + if (strlen(your_frame) == 4) + code = 1; + + int it = 0; + while (1) + { + char frame[5]; + frame[4] = 0; + fread(frame, 4, 1, file); + + if (code == 1) + while (1) + { + if (!is_frame(frame)) + { + printf("*WARNING! NOT FOUND THIS FRAME\n"); + return; + } + if (strcmp(frame, your_frame) == 0) + break; + else + { + fseek(file, 2 + scan_size(4), SEEK_CUR); + fread(frame, 4, 1, file); + } + } + + if (!is_frame(frame)) + return; + int frame_size = 0; + frame_size = scan_size(4); + + fseek(file, 2, SEEK_CUR); + printf("%s:", frame); + for (int i = 0; i < frame_size; i++) + { + char data = 0; + fread(&data, 1, 1, file); + printf("%c", data); + } + if (code == 1) + { + printf("\n"); + return; + } + printf("\n"); + } +} +void write_file(char* your_frame, char* data) +{ + fseek(file, 6, SEEK_SET); + ID3_size = scan_size(4); + int cursor = 0; + if (strlen(your_frame) != 4) + error("*YOUR FRAME MUST BE HAVE SIZE FOUR"); + if (!is_frame(your_frame)) + { + printf("*YOUR FRAME MUST BE HAVE ONLY CAPITAL ENGLISH LETTERS,\n"); + printf("AND LAST CHAPTER CAN BE NUMBER\n"); + error(""); + } + + fseek(file, 10, SEEK_SET); + + char frame[5]; + frame[4] = 0; + + fread(frame, 4, 1, file); + cursor += 14; + + while (1) + { + + if (strcmp(frame, your_frame) == 0) + break; + else + { + if (!is_frame(frame)) + { + shift_file(10, 11 + strlen(data)); + fseek(file, 10, SEEK_SET); + for (int i = 0; i < 4; i++) + putc(your_frame[i], file); + print_size(strlen(data)+1); + fseek(file, 2, SEEK_CUR); + putc(32, file); + for (int i = 0; i < strlen(data); i++) + putc(data[i], file); + fseek(file, 6, SEEK_SET); + print_size(ID3_size + 11 + strlen(data)); + return; + } + int size = scan_size(4); + fseek(file, 2 + size, SEEK_CUR); + fread(frame, 4, 1, file); + cursor += 10 + size; + } + } + int size_data = scan_size(4); + int ur_size_data = strlen(data); + fseek(file, cursor, SEEK_SET); + print_size(ur_size_data+1); + shift_file(cursor + 6 + size_data, ur_size_data-size_data+1); + fseek(file, cursor + 6, SEEK_SET); + putc(32, file); + for (int i = 0; i < ur_size_data; i++) + putc(data[i], file); + fseek(file, 6, SEEK_SET); + print_size(ID3_size-size_data+ur_size_data+1); + +} +void del(char your_frame[]) +{ + fseek(file, 6, SEEK_SET); + ID3_size = scan_size(4); + if (strlen(your_frame) != 4) + error("*YOUR FRAME MUST BE HAVE SIZE FOUR"); + char frame[5]; + frame[4] = 0; + fread(frame, 4, 1, file); + int cursor = 14; + while (1) + { + + if (strcmp(frame, your_frame) == 0) + break; + else + { + if (!is_frame(frame)) + { + printf("*WARNING! NOT FOUND THIS FRAME"); + return; + } + int size = scan_size(4); + fseek(file, 2 + size, SEEK_CUR); + fread(frame, 4, 1, file); + cursor += 10 + size; + } + } + int frame_size = scan_size(4); + shift_file(cursor + 2 + frame_size, -(frame_size + 10)); + printf("FRAME '%s' was DELITE\n", your_frame); +} +int main(int argc, char** argv) +{ + bool filepath = false; + bool com = false; + int i = 1; + printf("____________________\n"); + while (i < argc) + { + char* command; + command = strtok(argv[i], "="); + if (strcmp(command, "--filepath") == 0) + { + filepath = true; + command = strtok(NULL, "="); + file = fopen(command, "r+b"); + if (!file) + error("*NOT FOUND FILE PATH"); + } + if (strcmp(command, "--show") == 0) + { + com = true; + char ch[] = "*"; + read_file(ch); + } + if (strcmp(command, "--get") == 0) + { + com = true; + command = strtok(NULL, "="); + read_file(command); + } + if (strcmp(command, "--set") == 0) + { + + if (filepath == false) + error("* NOT WAS ENTER FILE PATH"); + com = true; + command = strtok(NULL, "="); + char* your_frame = command; + i++; + command = strtok(argv[i], "="); + if (strcmp(command, "--value") == 0) + command = strtok(NULL, "="); + else + error("*AFTER '--set' MUST BE '--value'"); + if (command == NULL) + error("*PLEASE, DON'T WRITE SPACE TO FIRST CHAPTER"); + char* arr = (char*)malloc(strlen(command+1)); + arr[strlen(command)] = 0; + arr = command; + i++; + for (int j = i; j < argc; j++) + { + arr = merge(arr, merge(" ", argv[i])); + i++; + } + printf("%s = %s\n", your_frame, arr); + write_file(your_frame, arr); + } + if (strcmp(command, "--delite")==0) + { + com = true; + if (filepath == false) + error("* NOT WAS ENTER FILE PATH"); + command=strtok(NULL, "="); + if (command == NULL) + error("* NOT WAS ENTER FRAME FOR DELITE"); + del(command); + } + i++; + } + if (filepath == false) + error("* NOT WAS ENTER FILE PATH"); + if (com == false) + printf("\n*WARNING! NOT WAS ENTER COMMAND\n"); + + printf("--nice--\n"); + printf("____________________\n"); + return 0; +} \ No newline at end of file diff --git a/notation.h b/notation.h new file mode 100644 index 0000000..b21540b --- /dev/null +++ b/notation.h @@ -0,0 +1,25 @@ +#include +#include "str.h" +char *get_10_2 (int data, int size, int iterator, char *arr) +{ + iterator++; + for (int i=size*iterator-1; i>=size*iterator-size; i--) + { + arr[i]=data%2+'0'; + data/=2; + } + return arr; +} +int get_2_10 (char* arr) +{ + int data=0; + int j=1; + for (int i = strlen(arr)-1; i>=0; i--) + { + if(arr[i]=='1') + data+=j; + j*=2; + } + return data; +} + diff --git a/str.h b/str.h new file mode 100644 index 0000000..3abc975 --- /dev/null +++ b/str.h @@ -0,0 +1,61 @@ +#include +#include +#include +#include +char *merge (char *arr1, char *arr2) +{ + int size_str=strlen(arr1)+strlen(arr2); + char *str=(char*)malloc(size_str+1); + for (int i=0; i=znak; i--) + { + int ch=data%10; + if (ch<0) + ch*=-1; + str[i]=ch+'0'; + data/=10; + } + if (znak==1) + str[0]='-'; + str[size_str]=0; + return str; +} +int get_int (char *str) +{ + int data=0; + bool k; + if (str[0]=='-') + k=1; + else + k=0; + for(int i=k; i Date: Fri, 14 Jan 2022 18:45:13 +0300 Subject: [PATCH 2/2] laba 5 --- 5.c | 310 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ str.h | 35 +------ 2 files changed, 314 insertions(+), 31 deletions(-) create mode 100644 5.c diff --git a/5.c b/5.c new file mode 100644 index 0000000..d918fd2 --- /dev/null +++ b/5.c @@ -0,0 +1,310 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include "str.h" +char* arr; +int size = 8; +int x, y; +int image_path; +int bit_span; +int byte_span; + +FILE* fin; +char* path; + +int n=-1, inp=-1, out=-1, dumb_freg=-1; + +int get_O(int X, int Y) +{ + return (X - 1) + (Y - 1) * x; +} +void print() +{ + for (int j = y; j >= 1; j--) + { + for (int i = 1; i <= x; i++) + { + printf("%i ", arr[get_O(i, j)]); + } + printf("\n"); + } +} +void check_color() +{ + int color = 0; + fseek(fin, 59, SEEK_SET); + fread(&color, 1, 1, fin); + if (color != 0) + { + for (int j = 1; j <= y; j++) + for (int i = 1; i <= x - bit_span; i++) + arr[get_O(i, j)] = (arr[get_O(i, j)] - 1) * (-1); + } +} +void get_10_2(int data) +{ + for (int i = size - 1; i >= size - 8; i--) + { + arr[i] = data % 2; + data /= 2; + } + size += 8; + arr = realloc(arr, size); +} +int get_2_10(int k, int Y) +{ + int data = 0; + int j = 1; + for (int i = x - k * 8; i > x - 8 - 8 * k; i--) + { + if (arr[get_O(i, Y)] == 1) + data += j; + j *= 2; + } + // printf("%i ", data); + return data; +} +void scan_file() +{ + //Узнаём длину + fseek(fin, 18, SEEK_SET); + fread(&x, 1, 4, fin); + + //Узнаём высоту + fseek(fin, 22, SEEK_SET); + fread(&y, 1, 4, fin); + + //Узнаём количество лишних бит + bit_span = (8 - x % 8) % 8; + //узнаём настоящую длину строки + x += bit_span; + //узнаём кл-во промежуточных байтов + byte_span = (4 - (x / 8 % 4)) % 4; + // printf("\n|%i %i %i|\n",x, byte_span, bit_span); + //Узнаём место начала изображения + fseek(fin, 10, SEEK_SET); + fread(&image_path, 1, 1, fin); + //считываем изображение + fseek(fin, image_path, SEEK_SET); + for (int j = 0; j < y; j++) + { + for (int i = 0; i < x / 8; i++) + { + int data = 0; + fread(&data, 1, 1, fin); + get_10_2(data); + } + fseek(fin, byte_span, SEEK_CUR); + } + //проверяем палитру + check_color(); +} +void print_file() +{ + static int it_output=0; + it_output++; + FILE *fout; + fout=fopen(merge(path, merge("//", merge(get_str(it_output), ".bmp"))), "wb"); + fseek(fin, 0, SEEK_SET); + fseek(fout, 0, SEEK_SET); + + char ch; + while (!feof(fin)) + { + ch = getc(fin); + putc(ch, fout); + } + fseek(fout, image_path, SEEK_SET); + for (int i = 1; i <= y; i++) + { + for (int k = x / 8 - 1; k >= 0; k--) + { + putc(get_2_10(k, i), fout); + } + fseek(fout, byte_span, SEEK_CUR); + } + fclose(fout); +} +bool check(int X, int Y) +{ + int lifes = 0; + if (X > 1 && arr[get_O(X - 1, Y)] == 1) + lifes++; + if (X > 1 && Y < y && arr[get_O(X - 1, Y + 1)] == 1) + lifes++; + if (Y < y && arr[get_O(X, Y + 1)] == 1) + lifes++; + if (X < x - bit_span && Y < y && arr[get_O(X + 1, Y + 1)] == 1) + lifes++; + if (X < x - bit_span && arr[get_O(X + 1, Y)] == 1) + lifes++; + if (X < x - bit_span && Y>1 && arr[get_O(X + 1, Y - 1)] == 1) + lifes++; + if (Y > 1 && arr[get_O(X, Y - 1)] == 1) + lifes++; + if (X > 1 && Y > 1 && arr[get_O(X - 1, Y - 1)] == 1) + lifes++; + if (arr[get_O(X, Y)] == 1 && (lifes == 2 || lifes == 3)) + return 1; + else if (lifes == 3) + return 1; + else + return 0; +} +void go_life() +{ + if (n==-1) + n=1000000; + + int loading=n/20; + int load_dash=20; + if (n<20) + loading = 20/n; + printf("loading:\n"); + char a = 177, b = 219; + for (int i = 0; i < 20; i++) + printf("%c", a); + printf("\r"); + for (int k = 0; k < n; k++) + { + char* arr_clone = malloc(x * y); + for (int i = 0; i < x * y; i++) + arr_clone[i] = 0; + + for (int j = 1; j <= y; j++) + { + for (int i = 1; i <= x; i++) + { + arr_clone[get_O(i, j)] = check(i, j); + } + } + + + bool uniformity=true; + if (n==1000000) + { + for (int i = 0; i < x * y; i++) + if (arr[i] != arr_clone[i]) + { + uniformity=false; + break; + } + if (uniformity) + break; + } + + for (int i = 0; i < x * y; i++) + arr[i] = arr_clone[i]; + + if(k%loading==0 && load_dash>1 && n>=20) + { + printf("%c", b); + load_dash--; + } + if(n<20) + { + for (int i=0; i #include #include -#include char *merge (char *arr1, char *arr2) { int size_str=strlen(arr1)+strlen(arr2); - char *str=(char*)malloc(size_str+1); + char *str=malloc(size_str+1); for (int i=0; i=znak; i--) + for (int i=size_str-1; i>=0; i--) { - int ch=data%10; - if (ch<0) - ch*=-1; - str[i]=ch+'0'; + str[i]=data%10+'0'; data/=10; } - if (znak==1) - str[0]='-'; str[size_str]=0; return str; } -int get_int (char *str) -{ - int data=0; - bool k; - if (str[0]=='-') - k=1; - else - k=0; - for(int i=k; i