Skip to content
Open
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
1 change: 1 addition & 0 deletions semester_1/Homework11/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jfgdas;vksgcdlfvahb`e/rbasjgffbddfcv,b gb rbaef lyjfvdfl;g`bdvfjlg`vdljc`gxdfkzgsxvfgdzsxzkcdhfrjgahkshxvcbx,d,zevgafukyrgfjzbc
8 changes: 8 additions & 0 deletions semester_1/Homework11/include/String.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <stdbool.h>
#include <string.h>
#include <stdio.h>

char *getString(size_t * const len, FILE * file, char const endOfLine);
char *copyString(char const * const string);
3 changes: 3 additions & 0 deletions semester_1/Homework11/include/readFile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

char *readFile(const char *const fileName);
9 changes: 9 additions & 0 deletions semester_1/Homework11/include/subString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

typedef enum ErrorCode
{
ok,
memoryError
} ErrorCode;

long findSubstring(char const *const pattern, char const *const text, ErrorCode *const errorCode);
4 changes: 4 additions & 0 deletions semester_1/Homework11/include/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <stdbool.h>

const bool test(void);
48 changes: 48 additions & 0 deletions semester_1/Homework11/src/String.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../include/String.h"

char *getString(size_t * const len, FILE * file, char const endOfLine)
{
*len = 0;
size_t capacity = 1;
char *s = (char *)malloc(sizeof(char));
if (s == NULL)
{
return NULL;
}

for (char c = fgetc(file); c != endOfLine && c != EOF; c = fgetc(file))
{
s[(*len)++] = c;

if (*len >= capacity)
{
capacity *= 2;
char *tmp = (char *)realloc(s, capacity * sizeof(char));
if (tmp == NULL)
{
free(s);
return NULL;
}
s = tmp;
}
}

s[*len] = '\0';

return s;
}

char *copyString(char const * const string)
{
const size_t len = strlen(string);
char *copy = (char *)malloc(len * sizeof(char));
if (copy != NULL)
{
strcpy(copy, string);
}
return copy;
}
62 changes: 62 additions & 0 deletions semester_1/Homework11/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>

#include "../include/subString.h"
#include "../include/String.h"
#include "../include/readFile.h"
#include "../include/test.h"

#define PROGRAM_FINISHED_CORRECTLY 0
#define PROGRAM_FAILED_TESTS 1
#define MEMORY_ERROR 2
#define FILE_ERROR 3

#define FILE_NAME "../file.txt"

int main(void)
{
setlocale(LC_ALL, "utf-8");

if (!test())
{
printf("Программа сейчас не работает\n");
return PROGRAM_FAILED_TESTS;
}

size_t len = 0;
printf("Введите подстроку: ");
char *pattern = getString(&len, stdin, '\n');
if (pattern == NULL)
{
return MEMORY_ERROR;
}
char *text = readFile(FILE_NAME);

Choose a reason for hiding this comment

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

проверить на NULL

if (text == NULL)
{
free(pattern);
return MEMORY_ERROR;
}

ErrorCode errorCode = ok;
long answer = findSubstring(pattern, text, &errorCode);
free(text);

if (errorCode != ok)
{
free(pattern);
printf("Программа не может получить доступ к памяти\n");
return MEMORY_ERROR;
}

if (answer == -1)
{
printf("Подстрока %s не встречается в тексте ни разу\n", pattern);
free(pattern);
return PROGRAM_FINISHED_CORRECTLY;
}

printf("Первое вхождение подстроки %s в тексте начинается на %ld символе\n", pattern, answer);
free(pattern);
return PROGRAM_FINISHED_CORRECTLY;
}
21 changes: 21 additions & 0 deletions semester_1/Homework11/src/readFile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>

#include "../include/String.h"

#define MEMORY_ERROR 2
#define FILE_ERROR 3

char *readFile(const char *const fileName)
{
FILE *file = fopen(fileName, "r");
if (file == NULL)
{
return NULL;
}
long len = 0;
char *str = getString(&len, file, EOF);
fclose(file);

return str;
}
91 changes: 91 additions & 0 deletions semester_1/Homework11/src/subString.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>

#include "../include/subString.h"

#define MEMORY_ERROR 2

static long *createLPSArray(char const *const pattern, const long pLength)
{
long len = 0;
long *lps = (long *)malloc(pLength * sizeof(long));
if (lps == NULL)
{
return NULL;
}

lps[0] = 0;
long i = 1;

while (i < pLength)
{
if (pattern[i] == pattern[len])
{
++len;
lps[i] = len;
++i;
}
else if (len != 0)
{
len = lps[len - 1];
}
else
{
lps[i] = 0;
++i;
}
}

return lps;
}

long findSubstring(char const *const pattern, char const *const text, ErrorCode *const errorCode)
{
long pLength = strlen(pattern);
long tLength = strlen(text);

if (pLength == 0)
{
return 0;
}

if (pLength > tLength)
{
return -1;
}

long *lps = createLPSArray(pattern, pLength);
if (lps == NULL)
{
*errorCode = memoryError;
}
*errorCode = ok;
long j = 0;
long i = 0;
while (i < tLength)
{
if (pattern[j] == text[i])
{
++j;
++i;
}

if (j == pLength)
{
free(lps);
return i - j + 1;
}

else if (i < tLength && pattern[j] != text[i])
{
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
free(lps);
return -1;

Choose a reason for hiding this comment

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

size_t - unsigned long -> переделать size_t на long

}
70 changes: 70 additions & 0 deletions semester_1/Homework11/src/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>
#include <stdlib.h>

#include "../include/test.h"
#include "../include/String.h"
#include "../include/subString.h"

static const bool testCase(const char *const testFileName, const char *const answerFileName)
{
FILE *testFile = fopen(testFileName, "r");
if (testFile == NULL)
{
return false;
}

size_t len = 0;
char *pattern = getString(&len, testFile, '\n');
if (pattern == NULL)
{
fclose(testFile);
return false;
}

char *text = getString(&len, testFile, EOF);
fclose(testFile);
if (text == NULL)
{
free(pattern);
return false;
}
ErrorCode errorCode = ok;
long position = findSubstring(pattern, text, &errorCode);
free(pattern);
free(text);
if (errorCode != ok)
{
return false;
}

FILE *answerFile = fopen(answerFileName, "r");

Choose a reason for hiding this comment

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

проверить на NULL

if (answerFile == NULL)
{
return false;
}

long answer = 0;
int scanfCheck = fscanf(answerFile, "%ld", &answer);
fclose(answerFile);
return scanfCheck == 1 && position == answer;
}

const bool test(void)
{
const size_t testNumber = 3;
const char *const testFileNames[testNumber] = {"../testFiles/test1.txt",
"../testFiles/test2.txt",
"../testFiles/test3.txt"};
const char *const answerFileNames[testNumber] = {"../testFiles/answer1.txt",
"../testFiles/answer2.txt",
"../testFiles/answer3.txt"};
for (size_t i = 0; i < testNumber; ++i)
{
if (!testCase(testFileNames[i], answerFileNames[i]))
{
return false;
}
}

return true;
}
1 change: 1 addition & 0 deletions semester_1/Homework11/testFiles/answer1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions semester_1/Homework11/testFiles/answer2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18
1 change: 1 addition & 0 deletions semester_1/Homework11/testFiles/answer3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
2 changes: 2 additions & 0 deletions semester_1/Homework11/testFiles/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a
aaa
2 changes: 2 additions & 0 deletions semester_1/Homework11/testFiles/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dhgfaqasd
sbblkbv;bdfgbsdfkdhgfaqasdfsds
2 changes: 2 additions & 0 deletions semester_1/Homework11/testFiles/test3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

gfjbsdvdg