-
Notifications
You must be signed in to change notification settings - Fork 0
Поиск подстроки #27
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
Open
kamendov-maxim
wants to merge
2
commits into
main
Choose a base branch
from
Knuth_Morris_Pratt
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
Поиск подстроки #27
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 @@ | ||
| jfgdas;vksgcdlfvahb`e/rbasjgffbddfcv,b gb rbaef lyjfvdfl;g`bdvfjlg`vdljc`gxdfkzgsxvfgdzsxzkcdhfrjgahkshxvcbx,d,zevgafukyrgfjzbc |
This file contains hidden or 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,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); |
This file contains hidden or 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,3 @@ | ||
| #pragma once | ||
|
|
||
| char *readFile(const char *const fileName); |
This file contains hidden or 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,9 @@ | ||
| #pragma once | ||
|
|
||
| typedef enum ErrorCode | ||
| { | ||
| ok, | ||
| memoryError | ||
| } ErrorCode; | ||
|
|
||
| long findSubstring(char const *const pattern, char const *const text, ErrorCode *const errorCode); |
This file contains hidden or 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,4 @@ | ||
| #pragma once | ||
| #include <stdbool.h> | ||
|
|
||
| const bool test(void); |
This file contains hidden or 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,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; | ||
| } |
This file contains hidden or 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,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); | ||
| 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; | ||
| } | ||
This file contains hidden or 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,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; | ||
| } |
This file contains hidden or 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,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; | ||
|
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. size_t - unsigned long -> переделать size_t на long |
||
| } | ||
This file contains hidden or 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,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"); | ||
|
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. проверить на 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; | ||
| } | ||
This file contains hidden or 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 @@ | ||
| 1 |
This file contains hidden or 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 @@ | ||
| 18 |
This file contains hidden or 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 @@ | ||
| 0 |
This file contains hidden or 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,2 @@ | ||
| a | ||
| aaa |
This file contains hidden or 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,2 @@ | ||
| dhgfaqasd | ||
| sbblkbv;bdfgbsdfkdhgfaqasdfsds |
This file contains hidden or 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,2 @@ | ||
|
|
||
| gfjbsdvdg |
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.
проверить на NULL