diff --git a/lab1/WordCont.c b/lab1/WordCont.c new file mode 100644 index 0000000..ea2f68b --- /dev/null +++ b/lab1/WordCont.c @@ -0,0 +1,81 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include + + +int main(int argc, char* argv[]) { + + + if (argc < 2) { + printf("Usage: WordCount.exe [OPTIONS] filename\n\nFor a list of options use: WordCount.exe -h\n\n\n\n\n\n\n "); + return 1; + } + char* option = argv[1]; + + + FILE* input; + char* name; + name = argv[argc - 1]; + + input = fopen(name, "r"); + + if (input == NULL) { + printf("File not be opened"); + return 1; + } + + + + for (int l = 1; l <= argc - 2; l++) { + char* options = argv[l]; + int ch; + + if ((strcmp(options, "-l") == 0) || (strcmp(options, "--lines") == 0)) { + int CountLines = 0; + int word = 0; + while ((ch = fgetc(input)) != EOF) { + if (ch == '\n') { + CountLines++; + } + fseek(input, 0, SEEK_SET); + printf("Opened file contains %d lines\n", CountLines + word); + + } + + else if ((strcmp(options, "-c") == 0) || (strcmp(options, "--bytes") == 0)) { + int size = -1; + do { + ch = fgetc(input); + size++; + } while (ch != EOF); + fseek(input, 0, SEEK_SET); + printf("File size is %d bytes\n", size); + } + + + else if ((strcmp(options, "-w") == 0) || (strcmp(options, "--words") == 0)) { + int ch = 0; + int isspace = 0; + int words = 0; + while (ch != EOF) { + ch = fgetc(input); + if ((ch == ' ') || (ch == '\n') || (ch == '\t') || (ch == '\v') || (ch == '\f') || (ch == EOF) || (ch == '.') || (ch == ',')) { + if (words == 1) { + words = 0; + isspace++; + } + } + else { + words = 1; + } + } + fseek(input, 0, SEEK_SET); + printf("The file has %d words\n", isspace); + } + else { + printf("Invalid command.\n"); + } + } + fclose(input); + return 0; +} \ No newline at end of file diff --git a/lab1/WordCont1.c b/lab1/WordCont1.c new file mode 100644 index 0000000..ea2f68b --- /dev/null +++ b/lab1/WordCont1.c @@ -0,0 +1,81 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include + + +int main(int argc, char* argv[]) { + + + if (argc < 2) { + printf("Usage: WordCount.exe [OPTIONS] filename\n\nFor a list of options use: WordCount.exe -h\n\n\n\n\n\n\n "); + return 1; + } + char* option = argv[1]; + + + FILE* input; + char* name; + name = argv[argc - 1]; + + input = fopen(name, "r"); + + if (input == NULL) { + printf("File not be opened"); + return 1; + } + + + + for (int l = 1; l <= argc - 2; l++) { + char* options = argv[l]; + int ch; + + if ((strcmp(options, "-l") == 0) || (strcmp(options, "--lines") == 0)) { + int CountLines = 0; + int word = 0; + while ((ch = fgetc(input)) != EOF) { + if (ch == '\n') { + CountLines++; + } + fseek(input, 0, SEEK_SET); + printf("Opened file contains %d lines\n", CountLines + word); + + } + + else if ((strcmp(options, "-c") == 0) || (strcmp(options, "--bytes") == 0)) { + int size = -1; + do { + ch = fgetc(input); + size++; + } while (ch != EOF); + fseek(input, 0, SEEK_SET); + printf("File size is %d bytes\n", size); + } + + + else if ((strcmp(options, "-w") == 0) || (strcmp(options, "--words") == 0)) { + int ch = 0; + int isspace = 0; + int words = 0; + while (ch != EOF) { + ch = fgetc(input); + if ((ch == ' ') || (ch == '\n') || (ch == '\t') || (ch == '\v') || (ch == '\f') || (ch == EOF) || (ch == '.') || (ch == ',')) { + if (words == 1) { + words = 0; + isspace++; + } + } + else { + words = 1; + } + } + fseek(input, 0, SEEK_SET); + printf("The file has %d words\n", isspace); + } + else { + printf("Invalid command.\n"); + } + } + fclose(input); + return 0; +} \ No newline at end of file diff --git a/lab2/lab2.1.c b/lab2/lab2.1.c new file mode 100644 index 0000000..6df8e55 --- /dev/null +++ b/lab2/lab2.1.c @@ -0,0 +1,130 @@ +define _CRT_SECURE_NO_WARNINGS + +#include +#include +#include +#include +#include +#define Findmax(a,b) (((a) > (b)) ? (a) : (b)) +const int cc = 1e9; //radix + +typedef struct +{ + int32_t* int32; + size_t size; +} uint1024_t; + +uint1024_t from_uint(unsigned int x) +{ + int size; + if (x >= cc) size = 2; + else size = 1; + uint1024_t t; + t.int32 = malloc(size * sizeof(int32_t));// size * sizeof(int32_t) + t.size = size; + t.int32[0] = x % cc; + if (size > 1) t.int32[1] = x / cc; + return t; +} + +void printf_value(uint1024_t x)//вывод +{ + int size = x.size; + printf("%d", !size ? 0 : x.int32[size - 1]); //display significant digits without 9 zeros + for (int index = size - 2; index >= 0; --index) //output the result: significant digits are printed at the end of the array + printf("%09d", x.int32[index]); +} + +void scanf_value(uint1024_t* x)//заполнение полного массива +{ + char str[1024]; + scanf("%s", str); + int len = strlen(str); + int size = len % 9 == 0 ? len / 9 : len / 9 + 1; + uint1024_t result; + result.int32 = malloc(size * sizeof(int32_t)); + result.size = size; + for (int index = len, j = 0; index > 0; index -= 9, j++) + { + str[index] = '\0'; //убираю нули + result.int32[j] = (atoi(index >= 9 ? str + index - 9 : str)); //I take 9 digits from the end, if the index is less than 9, I go to the rest of the line + } + *x = result; +} + + +uint1024_t add_op(uint1024_t x, uint1024_t y)//сложение +{ + uint1024_t result; + int size = Findmax(x.size, y.size); + result.int32 = calloc(size, sizeof(int32_t)); + result.size = size; + int32_t over = 0; + for (int index = 0; index < size || over; index++) + { + if (index == size) + { + result.int32 = realloc(result.int32, (size + 1) * sizeof(int32_t)); + result.int32[size] = 0; + result.size++; + } + result.int32[index] = over + (index < x.size ? x.int32[index] : 0) + (index < y.size ? y.int32[index] : 0); + if (result.int32[index] >= cc) over = 1, result.int32[index] -= cc; + else over = 0; + } + return result; +} + +uint1024_t subtr_op(uint1024_t x, uint1024_t y)//вычитание +{ + uint1024_t result; + int size = Findmax(x.size, y.size); + result.int32 = calloc(size, sizeof(int32_t)); + result.size = size; + int over = 0; + for (int index = 0; index < size || over; index++) + { + if (index == size) + break; + result.int32[index] = x.int32[index] - (index < y.size ? y.int32[index] : 0) - over; + if (result.int32[index] < 0) { + over = 1; + result.int32[index] += cc; + } + else over = 0; + } + while (result.int32[result.size - 1] == 0 && result.size > 1) + result.size--; + result.int32 = realloc(result.int32, result.size * sizeof(int32_t)); + return result; +} + +int main() +{ + setlocale(LC_ALL, "Rus"); + uint1024_t num1, num2; + char ArrayOfChar[310]; + memset(ArrayOfChar, 0, 310); + printf("Введите число\n"); + scanf_value(&num1); + printf("Введите число\n"); + scanf_value(&num2); + fseek(stdin, 0, SEEK_END); + while (1) + { + printf("Выберете: + - * \n"); + scanf("%c", ArrayOfChar); + if (!strcmp(ArrayOfChar, "-")) num1 = subtr_op(num1, num2); + else if (!strcmp(ArrayOfChar, "+")) num1 = add_op(num1, num2); + else break; + printf_value(num1); + fseek(stdin, 0, SEEK_END); + printf("\nВведите число\n"); + scanf_value(&num2); + fseek(stdin, 0, SEEK_END); + return 0; + } + } + + + diff --git a/lab3/main.c b/lab3/main.c new file mode 100644 index 0000000..230451a --- /dev/null +++ b/lab3/main.c @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#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; +} \ No newline at end of file diff --git a/lab4/main.c b/lab4/main.c new file mode 100644 index 0000000..46c06b2 --- /dev/null +++ b/lab4/main.c @@ -0,0 +1,173 @@ +#include +#include +#include +#include +#include + + +#define ID3_MAX_SIZE 128 + +typedef struct ID3TAG { + char signature[3]; // "TAG" + char name[30]; // имя песни + char artist[30]; // исполнитль + char album[30]; // альбом + char year[4]; // год + char description[30]; // описание + unsigned short number_in_album[1]; //номер трека в альбоме + unsigned short null_byte[1]; // нулевой байт, проверка номера в альбоме + char genre; // Жанр +} ID3TAG; + +/* Возвращает кол-во байт на 128 меньше от общего размера файла */ +long file_offset(FILE* fp) { + fseek(fp, 0, SEEK_END); // Устанавливает индикатор на 0 байт от конца + //ftell(fp) - кол-во байт от начала файла + return ftell(fp) - ID3_MAX_SIZE; // на 128 от конца +} + +int main(int argc, char* argv[]) { + setlocale(LC_ALL, "Rus"); + FILE* Filein = NULL; // Изначальный mp3 файл + FILE* Fileout = NULL; // Изменённый mp3 файл + char* mp3_name; // Название mp3 файла + char* tag; // Название тэга + char* val; // Испрльзуется в --value=val + ID3TAG* TAGS = NULL; + + /* Если 11 первых символов argv[1] == 11 первым символам '--filepath=', + то strncmp() вернёт 0 */ + if (!strncmp(argv[1],"--filepath=",11)) { + // Ссылка на следующий эл-т после '=' + mp3_name = strpbrk(argv[1], "=") + 1; + // Если файл не открывается то вывести сообщение об ошибке + if ((Filein = fopen(mp3_name, "rb")) == NULL) { + printf("Failed to open file %s for reading.\n", mp3_name); + return 0; + } + } + else { + printf("Команда --filepath= не распознана.\n"); + return 0; + } + + + // Здесь временно будет храниться вся информация из mp3 файла + char* temp = (char*) malloc(sizeof(char) * ID3_MAX_SIZE); + // Весь массив (все байты) заполняются 0 + memset(temp, 0, ID3_MAX_SIZE); + // Устанавливает индикатор на 0 байт от конца + fseek(Filein, file_offset(Filein), SEEK_SET); + // + fread(temp, 1, ID3_MAX_SIZE, Filein); + // Копируем тэги + TAGS = (ID3TAG*)(temp); + + + /* --show */ + if(!strcmp("--show", argv[2])) { + printf("\nSong Name: %s\n",TAGS->name); + printf("Artist: %s\n",TAGS->artist); + printf("Album: %s\n",TAGS->album); + printf("Year: %s\n",TAGS->year); + printf("Description: %s\n",TAGS->description); + printf("Number in album: %s\n",TAGS->number_in_album); + printf("null_byte: %s\n",TAGS->null_byte); + printf("Genre: %d\n\n",TAGS->genre); + } + /* --get */ + else if (!strncmp("--get=", argv[2], 6)) { + tag = strpbrk(argv[2], "=") + 1; + if ( !strcmp(tag, "name") ) + printf("Song Name: %s\n",TAGS->name); + + else if ( !strcmp(tag, "artist") ) + printf("Artist: %s\n",TAGS->artist); + + else if ( !strcmp("album", tag) ) + printf("Album: %s\n",TAGS->album); + + else if ( !strcmp("year", tag) ) + printf("Year: %s\n",TAGS->year); + + else if ( !strcmp("description", tag)) + printf("Description: %s\n",TAGS->description); + + else if ( !strcmp("number_in_album", tag) ) + printf("Number in album: %d\n\n",TAGS->number_in_album); + + else if ( !strcmp("null_byte", tag) ) + printf("null_byte: %d\n\n",TAGS->null_byte); + + else if ( !strcmp("genre", tag) ) + printf("Genre: %d\n\n",TAGS->genre); + + else { + printf("Unknown TAG.\n"); + return 0; + } + } + /* --set= */ + else if (!strncmp("--set=", argv[2], 6)) { + // Название тэга + tag = strpbrk(argv[2], "=") + 1; + if (!strncmp("--value=", argv[3], 8)) { + val = strpbrk(argv[3], "=") + 1; + if ( !strcmp(tag, "name") ) + memcpy(TAGS->name, val, 30); + + else if ( !strcmp(tag, "artist") ) + memcpy(TAGS->artist, val, 30); + + else if ( !strcmp(tag, "album") ) + memcpy(TAGS->album, val, 30); + + else if ( !strcmp(tag, "year") ) + memcpy(TAGS->year, val, 4); + + else if ( !strcmp(tag, "description") ) + memcpy(TAGS->description, val, 30); + + else if ( !strcmp("number_in_album", tag) ) + memcpy(TAGS->number_in_album, val, 1); + + else if ( !strcmp("null_byte", tag) ) + memcpy(TAGS->null_byte, val, 1); + + else if ( !strcmp("genre", tag) ) + memcpy(TAGS->genre, val, 1); + + } + else { + printf("Error with command --value=\n"); + return 0; + } + } + + else { + printf("Unknown command: %s\n", argv[2]); + return 0; + } + + // Закрытие изначального mp3 файла + fclose(Filein); + + if ((Fileout = fopen(mp3_name, "wb+")) == NULL) { + printf("Failed to open file %s for writing.\n", mp3_name); + return 0; + } + + // В temp2[] будут временно хранится изменённые тэги + char* temp2 =(char*) malloc(sizeof(char) * ID3_MAX_SIZE); + // Весь массив (все байты) заполняются 0 + memset(temp2, 0, ID3_MAX_SIZE); + // Записываем изменённые тэги + temp2 = (char*)TAGS; + fseek(Fileout, file_offset(Fileout), SEEK_SET); + // Записываем 128 байт из temp2[] 1 раз в Fileout + fwrite(temp2, 1, ID3_MAX_SIZE, Fileout); + free(temp2); + fclose(Fileout); + + return 0; +} \ No newline at end of file