feat: implement numeral system converter with validation and error handling

Co-authored-by: aider (openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
baiobelfer 2025-09-03 09:36:12 +02:00
parent 14373b1ad4
commit fbc5308a31

View File

@ -0,0 +1,128 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to convert a character to a digit
int char_to_digit(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
return -1; // Invalid character
}
// Function to convert a digit to a character
char digit_to_char(int d) {
if (d >= 0 && d <= 9) {
return '0' + d;
} else if (d >= 10 && d <= 15) {
return 'A' + (d - 10);
}
return '\0'; // Invalid digit
}
// Function to convert a number from given base to decimal
long long to_decimal(const char *num, int base) {
long long result = 0;
int len = strlen(num);
for (int i = 0; i < len; i++) {
int digit = char_to_digit(num[i]);
if (digit == -1 || digit >= base) {
return -1; // Invalid digit for the given base
}
result = result * base + digit;
}
return result;
}
// Function to convert a decimal number to given base
void from_decimal(long long num, int base, char *result) {
if (num == 0) {
result[0] = '0';
result[1] = '\0';
return;
}
char temp[65]; // Enough to hold a 64-bit number in binary
int index = 0;
while (num > 0) {
temp[index++] = digit_to_char(num % base);
num /= base;
}
// Reverse the string
for (int i = 0; i < index; i++) {
result[i] = temp[index - 1 - i];
}
result[index] = '\0';
}
// Function to validate if a number is valid in the given base
int is_valid_number(const char *num, int base) {
int len = strlen(num);
if (len == 0) return 0;
for (int i = 0; i < len; i++) {
int digit = char_to_digit(num[i]);
if (digit == -1 || digit >= base) {
return 0;
}
}
return 1;
}
int main(int argc, char *argv[]) {
// Check the number of arguments
if (argc != 4) {
fprintf(stderr, "Błąd: Nieprawidłowa liczba argumentów.\n");
fprintf(stderr, "Użycie: %s <liczba> <system_z> <system_na>\n", argv[0]);
return 1;
}
// Parse the bases
int base_from, base_to;
if (sscanf(argv[2], "%d", &base_from) != 1 ||
sscanf(argv[3], "%d", &base_to) != 1) {
fprintf(stderr, "Błąd: Podstawy systemów muszą być liczbami całkowitymi.\n");
return 1;
}
// Check if bases are in valid range
if (base_from < 2 || base_from > 16) {
fprintf(stderr, "Błąd: System źródłowy musi być w zakresie 2-16.\n");
return 1;
}
if (base_to < 2 || base_to > 16) {
fprintf(stderr, "Błąd: System docelowy musi być w zakresie 2-16.\n");
return 1;
}
// Check if the input number is valid in the source base
if (!is_valid_number(argv[1], base_from)) {
fprintf(stderr, "Błąd: Liczba '%s' jest nieprawidłowa w systemie o podstawie %d.\n", argv[1], base_from);
return 1;
}
// Convert to decimal
long long decimal = to_decimal(argv[1], base_from);
if (decimal == -1) {
fprintf(stderr, "Błąd: Nie udało się przekonwertować liczby do systemu dziesiętnego.\n");
return 1;
}
// Convert from decimal to target base
char result[65]; // Enough to hold a 64-bit number in binary
from_decimal(decimal, base_to, result);
// Print the result
printf("%s\n", result);
return 0;
}