diff --git a/challenges/convert-number-from-base-to-another/solutions/c/function/README.md b/challenges/convert-number-from-base-to-another/solutions/c/function/README.md new file mode 100644 index 0000000..d9e9698 --- /dev/null +++ b/challenges/convert-number-from-base-to-another/solutions/c/function/README.md @@ -0,0 +1,3 @@ +# convert-number-from-base-to-another/c/function + +Created by [@Divlo](https://github.com/Divlo) on 20 October 2021. diff --git a/challenges/convert-number-from-base-to-another/solutions/c/function/character.c b/challenges/convert-number-from-base-to-another/solutions/c/function/character.c new file mode 100644 index 0000000..49d4f00 --- /dev/null +++ b/challenges/convert-number-from-base-to-another/solutions/c/function/character.c @@ -0,0 +1,10 @@ +#include "character.h" + +#include +#include + +void character_append(char* string, char character) { + size_t length = strlen(string); + string[length] = character; + string[length + 1] = '\0'; +} diff --git a/challenges/convert-number-from-base-to-another/solutions/c/function/character.h b/challenges/convert-number-from-base-to-another/solutions/c/function/character.h new file mode 100644 index 0000000..0fab88f --- /dev/null +++ b/challenges/convert-number-from-base-to-another/solutions/c/function/character.h @@ -0,0 +1,13 @@ +#ifndef __CHARACTER__ +#define __CHARACTER__ + +/** + * @brief Append a character to a string, assuming string points to an array + * with enough space. + * + * @param string + * @param character + */ +void character_append(char* string, char character); + +#endif diff --git a/challenges/convert-number-from-base-to-another/solutions/c/function/input.c b/challenges/convert-number-from-base-to-another/solutions/c/function/input.c new file mode 100644 index 0000000..c07bb6d --- /dev/null +++ b/challenges/convert-number-from-base-to-another/solutions/c/function/input.c @@ -0,0 +1,19 @@ +#include "input.h" + +#include +#include + +#include "character.h" + +char* input() { + char character; + size_t length = 1; + char* string = malloc(length * sizeof(char)); + *string = '\0'; + while ((character = getchar()) != '\n' && character != EOF) { + length++; + string = realloc(string, length * sizeof(char)); + character_append(string, character); + } + return string; +} diff --git a/challenges/convert-number-from-base-to-another/solutions/c/function/input.h b/challenges/convert-number-from-base-to-another/solutions/c/function/input.h new file mode 100644 index 0000000..d432398 --- /dev/null +++ b/challenges/convert-number-from-base-to-another/solutions/c/function/input.h @@ -0,0 +1,11 @@ +#ifndef __INPUT__ +#define __INPUT__ + +/** + * @brief Read a line from stdin. + * + * @return char* + */ +char* input(); + +#endif diff --git a/challenges/convert-number-from-base-to-another/solutions/c/function/solution.c b/challenges/convert-number-from-base-to-another/solutions/c/function/solution.c new file mode 100644 index 0000000..ce7336d --- /dev/null +++ b/challenges/convert-number-from-base-to-another/solutions/c/function/solution.c @@ -0,0 +1,67 @@ +#include +#include +#include + +#include "input.h" + +int math_power(int base, int power) { + int result = 1; + for (int iteration = 0; iteration < power; iteration++) { + result = result * base; + } + return result; +} + +char* convert_from_base_10_to_base(unsigned long number, unsigned int base) { + if (number == 0) { + return "0"; + } + int remainders[64] = {}; + int index = 0; + while (number > 0) { + remainders[index] = number % base; + number = number / base; + index++; + } + char* result = malloc(sizeof(char) * (index + 1)); + int index_result = 0; + for (int iteration = index - 1; iteration >= 0; iteration--) { + int remainder = remainders[iteration]; + if (remainder >= 10) { + result[index_result] = (char)((remainder - 10) + 'A'); + } else { + result[index_result] = (char)(remainder + '0'); + } + index_result++; + } + return result; +} + +int convert_from_base_to_base_10(char* number, unsigned int base) { + int length = strlen(number); + int exponent = length - 1; + int result = 0; + int index = 0; + while (exponent >= 0) { + int current_number = (int)(number[index] - '0'); + if (current_number >= 10) { + current_number = (int)(number[index] - 'A') + 10; + } else { + current_number = (int)(number[index] - '0'); + } + result = result + current_number * math_power(base, exponent); + exponent--; + index++; + } + return result; +} + +int main() { + char* number = input(); + int base_from; + int base_target; + scanf("%d", &base_from); + scanf("%d", &base_target); + printf("%s\n", convert_from_base_10_to_base(convert_from_base_to_base_10(number, base_from), base_target)); + return EXIT_SUCCESS; +}