1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-10-29 22:17:23 +01:00

feat(solutions): add convert-number-from-base-to-another/c/function

This commit is contained in:
Divlo 2021-10-20 15:31:49 +02:00
parent 42e6ade610
commit d97fe47377
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
6 changed files with 123 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# convert-number-from-base-to-another/c/function
Created by [@Divlo](https://github.com/Divlo) on 20 October 2021.

View File

@ -0,0 +1,10 @@
#include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) {
size_t length = strlen(string);
string[length] = character;
string[length + 1] = '\0';
}

View File

@ -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

View File

@ -0,0 +1,19 @@
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
#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;
}

View File

@ -0,0 +1,11 @@
#ifndef __INPUT__
#define __INPUT__
/**
* @brief Read a line from stdin.
*
* @return char*
*/
char* input();
#endif

View File

@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}