-
Notifications
You must be signed in to change notification settings - Fork 8
Update main.cpp #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,133 @@ | ||
| #include <iostream> | ||
| #include <fstream> | ||
| #include <cstring> | ||
|
|
||
| int main(int, char**){ | ||
| const int MAX_LINE_LENGTH = 1024; | ||
| const int MAX_FILTER = 128; | ||
| const int MAX_CONTEXT_LENGTH = 128; | ||
| const int MAX_WORDS = 10000; | ||
|
|
||
| void parseArgs(int argc, char* argv[], char* inputPath, char* filterdb, char* outputPath) { | ||
| for (int i = 1; i < argc; ++i) { | ||
| if (strcmp(argv[i], "--input") == 0 && i + 1 < argc) std::strcpy(inputPath, argv[++i]); | ||
| else if (strcmp(argv[i], "--filterdb") == 0 && i + 1 < argc) std::strcpy(filterdb, argv[++i]); | ||
| else if (strcmp(argv[i], "--output") == 0 && i + 1 < argc) std::strcpy(outputPath, argv[++i]); | ||
| } | ||
| } | ||
|
|
||
| char** readFilters(const char* filterdb, int& filterCount) { | ||
|
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. ссылка весит 8 байт, когда int-- 4. тут можно без ссылки |
||
| std::ifstream file(filterdb); | ||
| if (!file) return nullptr; | ||
|
|
||
| char** filters = new char*[MAX_FILTER]; | ||
| filterCount = 0; | ||
| char line[MAX_FILTER]; | ||
|
|
||
| while (file.getline(line, MAX_FILTER) && filterCount < MAX_FILTER) { | ||
| filters[filterCount] = new char[strlen(line) + 1]; | ||
| std::strcpy(filters[filterCount++], line); | ||
|
Comment on lines
+27
to
+28
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. вот, кстати, как будто не регисронезависимо |
||
| } | ||
|
|
||
| file.close(); | ||
|
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. можно не закрывать сам бы закрылся |
||
| return filters; | ||
| } | ||
|
|
||
| bool containsFilter(const char* word, char** filters, int filterCount, char*& matchedFilter) { | ||
| for (int i = 0; i < filterCount; ++i) { | ||
| if (strstr(word, filters[i])) { | ||
| matchedFilter = filters[i]; | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| void freeMemory(char** filters, int filterCount) { | ||
| for (int i = 0; i < filterCount; ++i) delete[] filters[i]; | ||
| delete[] filters; | ||
| } | ||
|
|
||
| void processFile(const char* inputPath, const char* outputPath, char** filters, int filterCount) { | ||
|
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. Хорошо, что вы решили разделить немноголо логику на функции, но конкретно эта слишком большая. Стоило бы разделить чтение, анализ файла и вывод |
||
| std::ifstream inputFile(inputPath); | ||
| std::ofstream outputFile(outputPath); | ||
| if (!inputFile || !outputFile) return; | ||
|
|
||
| char word[MAX_CONTEXT_LENGTH]; | ||
| int filterUsage[MAX_FILTER] = {0}; | ||
| int discardedSizes[MAX_WORDS]; | ||
| int discardedCount = 0; | ||
| char discardedWords[MAX_WORDS][MAX_CONTEXT_LENGTH]; | ||
| int usedFilterIndex[MAX_WORDS]; | ||
| int usedFilterCount = 0; | ||
|
|
||
| while (inputFile >> word) { | ||
| char* matchedFilter = nullptr; | ||
| if (containsFilter(word, filters, filterCount, matchedFilter)) { | ||
| int len = strlen(word); | ||
| discardedSizes[discardedCount] = len; | ||
| std::strcpy(discardedWords[discardedCount], word); | ||
|
|
||
| for (int i = 0; i < filterCount; ++i) { | ||
| if (strcmp(filters[i], matchedFilter) == 0) { | ||
| filterUsage[i]++; | ||
| usedFilterIndex[discardedCount] = i; | ||
| break; | ||
| } | ||
| } | ||
| discardedCount++; | ||
| } else { | ||
| outputFile << word << " "; | ||
| } | ||
|
Comment on lines
+65
to
+80
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. вот тут можно было бы проифать ваш else, сделать continue и избежать вложенности |
||
| } | ||
| inputFile.close(); | ||
| outputFile.close(); | ||
|
|
||
| int maxUsage = 0, mostUsedFilterIndex = -1; | ||
| for (int i = 0; i < filterCount; ++i) { | ||
| if (filterUsage[i] > maxUsage) { | ||
| maxUsage = filterUsage[i]; | ||
| mostUsedFilterIndex = i; | ||
| } | ||
| } | ||
|
|
||
| if (mostUsedFilterIndex == -1) return; | ||
|
|
||
| int relevantSizes[MAX_WORDS]; | ||
| int relevantCount = 0; | ||
| for (int i = 0; i < discardedCount; ++i) { | ||
| if (usedFilterIndex[i] == mostUsedFilterIndex) { | ||
| relevantSizes[relevantCount++] = discardedSizes[i]; | ||
| } | ||
| } | ||
|
|
||
| if (relevantCount > 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. инвертировать if |
||
| for (int i = 0; i < relevantCount - 1; ++i) | ||
| for (int j = i + 1; j < relevantCount; ++j) | ||
| if (relevantSizes[i] > relevantSizes[j]) | ||
| std::swap(relevantSizes[i], relevantSizes[j]); | ||
|
|
||
| int median = (relevantCount % 2 == 0) ? | ||
| (relevantSizes[relevantCount / 2 - 1] + relevantSizes[relevantCount / 2]) / 2 | ||
| : relevantSizes[relevantCount / 2]; | ||
|
|
||
| std::cout << filters[mostUsedFilterIndex] << " " << median << std::endl; | ||
| } | ||
| } | ||
|
|
||
| int main(int argc, char* argv[]) { | ||
| char inputPath[MAX_LINE_LENGTH] = ""; | ||
| char filterdb[MAX_LINE_LENGTH] = ""; | ||
| char outputPath[MAX_LINE_LENGTH] = ""; | ||
|
|
||
| parseArgs(argc, argv, inputPath, filterdb, outputPath); | ||
|
|
||
| int filterCount = 0; | ||
| char** filters = readFilters(filterdb, filterCount); | ||
|
|
||
| if (filters) { | ||
| processFile(inputPath, outputPath, filters, filterCount); | ||
| freeMemory(filters, filterCount); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
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.
конечно, это не портит работоспособность кода, но я бы советовала Вам все же придерживаться кодстайла, к которому мы привыкали целый семестр. Все же так код становится более читаемым. Не вижу смысла в данной работе править вам кодстайл, но оставлю как замечание в начале