This repository was archived by the owner on Jul 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor Core Logic and Improve Code Maintainability #5
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
48e777f
feat(params): Centralize configuration with constants
b1sted f965630
fix(parameter): Correct calculation description to leading edge
b1sted 84bbaeb
style(signal_analysis): fix indentation in switch statement
b1sted 0b62e50
refactor(input): abstract user response handling to a dictionary module
b1sted 9fe204f
refactor(constants): encapsulate module-specific constants
b1sted 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,37 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025 Nikita Mandrykin. All rights reserved. | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a | ||
| * copy of this software and associated documentation files (the "Software"), | ||
| * to deal in the Software without restriction, including without limitation | ||
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| * and/or sell copies of the Software, and to permit persons to whom the | ||
| * Software is furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
| * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| * DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| #ifndef CONSTANTS_H | ||
| #define CONSTANTS_H | ||
|
|
||
| // Максимальный размер массивов для данных. | ||
| // Используется в main для выделения памяти и в других модулях для проверки границ. | ||
| #define ARRAY_SIZE 1500 | ||
|
|
||
| // Пути к внешним файлам данных | ||
| #define FILE_PATH_ARRAY_T "data/array_t.txt" | ||
| #define FILE_PATH_ARRAY_UVX "data/array_Uvx.txt" | ||
| #define FILE_PATH_ARRAY_UVIX "data/array_Uvix.txt" | ||
| #define FILE_PATH_ZAST "data/zast.txt" | ||
|
|
||
| #endif // CONSTANTS_H |
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
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,43 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025 Nikita Mandrykin. All rights reserved. | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a | ||
| * copy of this software and associated documentation files (the "Software"), | ||
| * to deal in the Software without restriction, including without limitation | ||
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| * and/or sell copies of the Software, and to permit persons to whom the | ||
| * Software is furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
| * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| * DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| #ifndef RESPONSE_DICTIONARY_H | ||
| #define RESPONSE_DICTIONARY_H | ||
|
|
||
| typedef enum { | ||
| RESPONSE_UNKNOWN, | ||
| RESPONSE_AFFIRMATIVE, | ||
| RESPONSE_NEGATIVE | ||
| } ResponseType; | ||
|
|
||
| typedef struct { | ||
| const char *keyword_text; | ||
| ResponseType type; | ||
| } KeywordMapping; | ||
|
|
||
| extern const KeywordMapping RESPONSE_DICTIONARY[]; | ||
| extern const int DICTIONARY_SIZE; | ||
|
|
||
| ResponseType get_response_type_by_keyword(const char* input); | ||
|
|
||
| #endif // RESPONSE_DICTIONARY_H |
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
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
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
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
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 |
|---|---|---|
|
|
@@ -26,8 +26,19 @@ | |
|
|
||
| #include "parameter.h" | ||
| #include "forming.h" | ||
| #include "constants.h" | ||
|
|
||
| #define N 1500 | ||
| // Требуемая относительная погрешность (1% в соотв. с README), при достижении которой расчет останавливается. | ||
| static const float EPSILON = 0.01f; | ||
|
|
||
| // Начальная погрешность для входа в цикл while. Просто значение > EPSILON. | ||
| static const float INITIAL_CURRENT_PRECISION = 1.0f; | ||
|
|
||
| // Стартовое значение для 'prev_parameter', чтобы избежать ложной точности на первой итерации. | ||
| static const float PREV_PARAMETER_INITIAL = 1e10f; | ||
|
|
||
| // Начальное количество точек 'n' для первого шага итерационного расчета. | ||
| static const int INITIAL_POINTS = 11; | ||
|
|
||
| // Функция расчета длительности переднего фронта импульса | ||
| float calc_leading_edge(int n, float *U, float dt) { | ||
|
|
@@ -42,7 +53,7 @@ float calc_leading_edge(int n, float *U, float dt) { | |
| float U1 = Umin + 0.9 * (Umax - Umin); | ||
| float U2 = Umin + 0.1 * (Umax - Umin); | ||
|
|
||
| // Считаем длительность заднего фронта | ||
| // Считаем длительность переднего фронта | ||
| float duration = 0; | ||
| for (int i = 0; i < n - 1; i++) { | ||
| if (U[i] < U1 && U[i] > U2 && U[i + 1] > U[i]) duration += dt; | ||
|
|
@@ -53,29 +64,26 @@ float calc_leading_edge(int n, float *U, float dt) { | |
|
|
||
| // Функция расчета параметра с заданной точностью | ||
| void calculate_with_precision() { | ||
| float epsilon = 0.01; // Требуемая точность (1%) | ||
| float current_precision = 1.0; // Текущая погрешность | ||
| float prev_parameter = 1e10; // Начальное (очень большое) значение | ||
| int n = 11; // Начальное количество точек | ||
| float current_precision = INITIAL_CURRENT_PRECISION; // Текущая погрешность | ||
| float prev_parameter = PREV_PARAMETER_INITIAL; // Начальное (очень большое) значение | ||
| int n = INITIAL_POINTS; // Начальное количество точек | ||
|
|
||
| float current_parameter, t[N], Uvx[N], Uvix[N], dt; | ||
| float current_parameter, t[ARRAY_SIZE], Uvx[ARRAY_SIZE], Uvix[ARRAY_SIZE], dt; | ||
|
|
||
| printf("\nЗаданный параметр: расчет длительности заднего фронта для Uвых\n"); | ||
| printf("\nЗаданный параметр: расчет длительности переднего фронта для Uвых\n"); | ||
|
|
||
| while (current_precision > epsilon) { | ||
| while (current_precision > EPSILON) { | ||
| // Формирование массивов | ||
| forming_time(n, t, &dt); | ||
|
|
||
| float t1 = 10, t2 = 15, t3 = 45, a = 20, b = 0.5, c = 17; | ||
| forming_Uvx(n, t, Uvx, t1, t2, t3, a, b, c); | ||
| float Uvx1 = 20, d = 2, e = -5; | ||
| forming_Uvix(n, Uvx, Uvix, Uvx1, d, e); | ||
| forming_Uvx(n, t, Uvx); | ||
| forming_Uvix(n, Uvx, Uvix); | ||
|
|
||
| // Расчет параметра | ||
| current_parameter = calc_leading_edge(n, Uvix, dt); | ||
|
|
||
| // Расчет погрешности | ||
| if (prev_parameter != 1e10) { | ||
| if (prev_parameter != PREV_PARAMETER_INITIAL) { | ||
|
||
| current_precision = fabs(prev_parameter - current_parameter) / current_parameter; | ||
| } | ||
|
|
||
|
|
@@ -86,7 +94,7 @@ void calculate_with_precision() { | |
| n *= 2; | ||
| } | ||
|
|
||
| if (n >= N) { | ||
| if (n >= ARRAY_SIZE) { | ||
| printf("Предупреждение: достигнут максимальный размер массива без достижения требуемой точности\n"); | ||
| } else { | ||
| printf("Итоговое значение параметра: %.6f (точность: %.2f%%)\n", current_parameter, current_precision * 100); | ||
|
|
||
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,54 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * SPDX-FileCopyrightText: Copyright (c) 2025 Nikita Mandrykin. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * SPDX-License-Identifier: MIT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Permission is hereby granted, free of charge, to any person obtaining a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * copy of this software and associated documentation files (the "Software"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * to deal in the Software without restriction, including without limitation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * and/or sell copies of the Software, and to permit persons to whom the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Software is furnished to do so, subject to the following conditions: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * The above copyright notice and this permission notice shall be included in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * all copies or substantial portions of the Software. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * DEALINGS IN THE SOFTWARE. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <string.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "response_dictionary.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const KeywordMapping RESPONSE_DICTIONARY[] = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Утвердительные ответы | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"да", RESPONSE_AFFIRMATIVE}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"Да", RESPONSE_AFFIRMATIVE}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"дА", RESPONSE_AFFIRMATIVE}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"ДА", RESPONSE_AFFIRMATIVE}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Отрицательные ответы | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"нет", RESPONSE_NEGATIVE}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"Нет", RESPONSE_NEGATIVE}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"нЕт", RESPONSE_NEGATIVE}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"неТ", RESPONSE_NEGATIVE}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"НЕт", RESPONSE_NEGATIVE}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"нЕТ", RESPONSE_NEGATIVE}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"НЕТ", RESPONSE_NEGATIVE} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const int DICTIONARY_SIZE = sizeof(RESPONSE_DICTIONARY) / sizeof(RESPONSE_DICTIONARY[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ResponseType get_response_type_by_keyword(const char *input) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < DICTIONARY_SIZE; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (strcmp(input, RESPONSE_DICTIONARY[i].keyword_text) == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return RESPONSE_DICTIONARY[i].type; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return RESPONSE_UNKNOWN; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+25
to
+54
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "response_dictionary.h" | |
| const KeywordMapping RESPONSE_DICTIONARY[] = { | |
| // Утвердительные ответы | |
| {"да", RESPONSE_AFFIRMATIVE}, | |
| {"Да", RESPONSE_AFFIRMATIVE}, | |
| {"дА", RESPONSE_AFFIRMATIVE}, | |
| {"ДА", RESPONSE_AFFIRMATIVE}, | |
| // Отрицательные ответы | |
| {"нет", RESPONSE_NEGATIVE}, | |
| {"Нет", RESPONSE_NEGATIVE}, | |
| {"нЕт", RESPONSE_NEGATIVE}, | |
| {"неТ", RESPONSE_NEGATIVE}, | |
| {"НЕт", RESPONSE_NEGATIVE}, | |
| {"нЕТ", RESPONSE_NEGATIVE}, | |
| {"НЕТ", RESPONSE_NEGATIVE} | |
| }; | |
| const int DICTIONARY_SIZE = sizeof(RESPONSE_DICTIONARY) / sizeof(RESPONSE_DICTIONARY[0]); | |
| ResponseType get_response_type_by_keyword(const char *input) { | |
| for (int i = 0; i < DICTIONARY_SIZE; i++) { | |
| if (strcmp(input, RESPONSE_DICTIONARY[i].keyword_text) == 0) { | |
| return RESPONSE_DICTIONARY[i].type; | |
| } | |
| } | |
| return RESPONSE_UNKNOWN; | |
| } | |
| #include <ctype.h> | |
| #include "response_dictionary.h" | |
| const KeywordMapping RESPONSE_DICTIONARY[] = { | |
| // Утвердительные ответы | |
| {"да", RESPONSE_AFFIRMATIVE}, | |
| // Отрицательные ответы | |
| {"нет", RESPONSE_NEGATIVE} | |
| }; | |
| const int DICTIONARY_SIZE = sizeof(RESPONSE_DICTIONARY) / sizeof(RESPONSE_DICTIONARY[0]); | |
| ResponseType get_response_type_by_keyword(const char *input) { | |
| char normalized_input[256]; | |
| normalize_to_lowercase(input, normalized_input); | |
| for (int i = 0; i < DICTIONARY_SIZE; i++) { | |
| if (strcmp(normalized_input, RESPONSE_DICTIONARY[i].keyword_text) == 0) { | |
| return RESPONSE_DICTIONARY[i].type; | |
| } | |
| } | |
| return RESPONSE_UNKNOWN; | |
| } | |
| void normalize_to_lowercase(const char *input, char *output) { | |
| for (int i = 0; input[i] != '\0'; i++) { | |
| output[i] = tolower((unsigned char)input[i]); | |
| } | |
| output[strlen(input)] = '\0'; | |
| } |
Oops, something went wrong.
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.
[nitpick] The
break;afterreturn true;is unreachable. You can remove thisbreak(and the similar one afterreturn false;) to simplify the code.