-
Notifications
You must be signed in to change notification settings - Fork 0
task1 #28
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?
task1 #28
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,4 @@ | ||
| * | ||
| !*.* | ||
|
|
||
|
|
||
| # Prerequisites | ||
| *.d | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| cmake_minimum_required(VERSION 3.8.0) | ||
| project(task1 VERSION 0.1.0 LANGUAGES C) | ||
|
|
||
| include(CTest) | ||
| enable_testing() | ||
|
|
||
| add_executable(task1 main.c | ||
| read/read.c | ||
| getMinimalNumber/getMinimalNumber.c | ||
| tests/test.c) | ||
|
|
||
| set(CPACK_PROJECT_NAME ${PROJECT_NAME}) | ||
| set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) | ||
| include(CPack) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
|
|
||
| #include <string.h> | ||
| #include <stdlib.h> | ||
|
|
||
| #include "getMinimalNumber.h" | ||
|
|
||
| #include "../read/read.h" | ||
|
|
||
| static int compare(const void *a, const void *b) | ||
| { | ||
| return *(const char *)a - *(const char *)b; | ||
| } | ||
|
|
||
| char * getMinimalNumber(char const * const number) | ||
| { | ||
| char *newNumber = copyString(number); | ||
| if (newNumber == NULL) | ||
| { | ||
| return NULL; | ||
| } | ||
|
|
||
| qsort(newNumber, strlen(newNumber), sizeof(char), compare); | ||
|
|
||
| return newNumber; | ||
|
Comment on lines
+22
to
+24
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. Не всё так просто. На тесте 10 правильный ответ был бы 10, а у Вас получится 01, что не является корректной записью числа в десятичной системе счисления. Причём, в тестах числа с нулём даже не рассматривались, так что ладно бы Вы неправильно поняли условие и не спросили, но тут, кажется, про это вообще не подумали :) |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #pragma once | ||
|
|
||
| char * getMinimalNumber(char const * const number); | ||
|
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.
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,91 @@ | ||||||
| #include <stdio.h> | ||||||
| #include <stdlib.h> | ||||||
| #include <locale.h> | ||||||
|
|
||||||
| #include "read/read.h" | ||||||
| #include "getMinimalNumber/getMinimalNumber.h" | ||||||
| #include "tests/test.h" | ||||||
|
|
||||||
| #define PROGRAM_FINISHED_CORRECTLY 0 | ||||||
| #define PROGRAM_FAILED_TESTS 1 | ||||||
| #define MEMORY_ERROR 2 | ||||||
|
|
||||||
| int compare(const void *a, const void *b) | ||||||
| { | ||||||
| return *(const char *)a - *(const char *)b; | ||||||
| } | ||||||
| // int comp (const char element1, const char element2) | ||||||
| // { | ||||||
| // if (element1 == element2) | ||||||
| // { | ||||||
| // return 0; | ||||||
| // } | ||||||
|
|
||||||
| // return (element1 < element2 ? -1 : 1); | ||||||
| // } | ||||||
|
Comment on lines
+17
to
+25
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. Закомментированного кода в окончательной версии быть не должно. |
||||||
|
|
||||||
| static clear(void) | ||||||
|
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.
Suggested change
|
||||||
| { | ||||||
| while (getchar() != '\n') | ||||||
| ; | ||||||
| } | ||||||
|
|
||||||
| int main(void) | ||||||
| { | ||||||
| setlocale(LC_ALL, "Russian"); | ||||||
| if (!test()) | ||||||
| { | ||||||
| printf("Программа не работает\n"); | ||||||
| return PROGRAM_FAILED_TESTS; | ||||||
| } | ||||||
|
|
||||||
| size_t len = 2; | ||||||
| char *number = "00"; | ||||||
|
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. Тут объявляется указатель на строковый литерал как мутабельный |
||||||
| bool isNaturalNUmber = false; | ||||||
| while (!isNaturalNUmber) | ||||||
| { | ||||||
| printf("Ведите число: "); | ||||||
|
|
||||||
| number = getString(&len, stdin, '\n', &isNaturalNUmber); | ||||||
| if (number == NULL) | ||||||
| { | ||||||
| return MEMORY_ERROR; | ||||||
| } | ||||||
| if (!isNaturalNUmber) | ||||||
| { | ||||||
| printf("\nВведите натуральное число\n"); | ||||||
| clear(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| char *minimalNumber = getMinimalNumber(number); | ||||||
| free(number); | ||||||
| if (number == NULL) | ||||||
|
Comment on lines
+62
to
+63
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, потом free |
||||||
| { | ||||||
| return MEMORY_ERROR; | ||||||
| } | ||||||
|
|
||||||
| printf("Минимальное число, полученное перестановкой цифр: "); | ||||||
| printf("%s\n", minimalNumber); | ||||||
|
|
||||||
| // size_t l = 0; | ||||||
| // bool f= false; | ||||||
| // char * a= getString(&l, stdin, '\n', &f); | ||||||
| // printf("%d\n", f); | ||||||
| } | ||||||
|
|
||||||
| // #include <stdio.h> | ||||||
| // #include <stdlib.h> | ||||||
|
|
||||||
| // char values[] = { 5, 4, 7, 1, 2 }; | ||||||
|
|
||||||
| // int compare(const void *a, const void *b) | ||||||
| // { | ||||||
| // return *(const char *)a - *(const char *)b; | ||||||
| // } | ||||||
|
|
||||||
| // char arr[] = "dbaurjvgeofx"; | ||||||
|
|
||||||
| // printf("Unsorted: %s\n", arr); | ||||||
| // qsort(arr, strlen(arr), 1, compare); | ||||||
| // printf("Sorted: %s\n", arr); | ||||||
|
Comment on lines
+77
to
+91
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. :( |
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| #include "read.h" | ||
|
|
||
| const char * allowedCharacters = "0123456789"; | ||
| const size_t allowedCharactersLen = 10; | ||
|
|
||
| char *getString(size_t * const len, FILE * file, char const endOfLine, bool *isNaturalNumber) | ||
| { | ||
| *len = 0; | ||
| size_t capacity = 1; | ||
| char *s = (char *)malloc(sizeof(char)); | ||
| if (s == NULL) | ||
| { | ||
| return NULL; | ||
| } | ||
| *isNaturalNumber = true; | ||
|
|
||
| for (char c = fgetc(file); c != endOfLine && c != EOF; c = fgetc(file)) | ||
| { | ||
| bool checkChar = false; | ||
| for (size_t i = 0; i < allowedCharactersLen; ++i) | ||
| { | ||
| if (c == allowedCharacters[i]) | ||
| { | ||
| checkChar = true; | ||
|
|
||
| if (s[0] == '0' && *len + 1 > 1) | ||
| { | ||
| checkChar = false; | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| if (!checkChar) | ||
| { | ||
| *isNaturalNumber = false; | ||
| free(s); | ||
| return "0"; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| 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; | ||
| } | ||
|
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. strdup |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdbool.h> | ||
| #include <string.h> | ||
|
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. Это вроде как не нужно в самом .h-файле, так что можно перенести в .c |
||
| #include <stdio.h> | ||
|
|
||
| char *getString(size_t * const len, FILE * file, char const endOfLine, bool *isNaturalNumber); | ||
| char *copyString(char const * const string); | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||||||||||||
| #include <string.h> | ||||||||||||||||
| #include <stdlib.h> | ||||||||||||||||
|
|
||||||||||||||||
| #include "test.h" | ||||||||||||||||
| #include "../getMinimalNumber/getMinimalNumber.h" | ||||||||||||||||
|
|
||||||||||||||||
| static const bool testCase(char const *const input, char const *const answer) | ||||||||||||||||
| { | ||||||||||||||||
| char *newNumber = getMinimalNumber(input); | ||||||||||||||||
| if (newNumber == NULL) | ||||||||||||||||
| { | ||||||||||||||||
| return false; | ||||||||||||||||
| } | ||||||||||||||||
| bool result = strcmp(newNumber, answer) == 0; | ||||||||||||||||
| free(newNumber); | ||||||||||||||||
| return result; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| const bool test(void) | ||||||||||||||||
| { | ||||||||||||||||
| const char * const input1 = "564353456"; | ||||||||||||||||
| const char * const answer1 = "334455566"; | ||||||||||||||||
| const char * const input2 = "5435"; | ||||||||||||||||
| const char * const answer2 = "3455"; | ||||||||||||||||
|
|
||||||||||||||||
| return testCase(input1, answer1) && testCase(input2, answer2); | ||||||||||||||||
|
Comment on lines
+21
to
+26
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.
Suggested change
Какие-то лишние церемонии |
||||||||||||||||
| } | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
| const bool test(void); |
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.
Лишняя пустая строчка