about summary refs log blame commit diff stats
path: root/numericx.c
blob: 87cde703ef30fb24d2d0681443c3810daeed78d9 (plain) (tree)
































































                                                                                  

































                                                         






                                                                      



















































                                                                            
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>

#ifdef DEBUG
#define DEBUG true
#else
#define DEBUG false
#endif

typedef struct NumeralPtr
{
	char const* symbol;
	struct NumeralPtr *next;
	struct NumeralPtr *previous;
} numeral_ptr;

char* program_name = NULL;

/*
 * Creates new digit for numeral_ptr
 * Return pointer to new numeral_ptr
 * Returns NULL in case of no memory
 */
numeral_ptr*
new_digit(numeral_ptr* last_numeral, char const* to_first)
{
	numeral_ptr* starting_point = malloc(sizeof(numeral_ptr));
	if(starting_point == NULL)
	{
		fprintf(stderr, "error: %s: %s!", program_name, strerror(ENOMEM));
		return NULL;
	}

	starting_point->symbol = to_first;
	starting_point->previous = last_numeral;
	starting_point->next = NULL;

	return starting_point;
}

void
increment(numeral_ptr* numeral, char* num_first, char* num_last)
{
	bool cycled = false;

	if( numeral->symbol == num_last )
	{
		if( numeral->next == NULL )
			numeral->next = new_digit(numeral, num_first);
		else
			increment(numeral->next, num_first, num_last);

		cycled = true;
	}

	if( cycled )
		numeral->symbol = num_first;
	else
		++(numeral->symbol);
}

bool
is_the_same(numeral_ptr* numeral, char* number_arg)
{
	while( !(numeral == NULL) )
	{
		if( *(numeral->symbol) == *(number_arg) )
		{
			numeral = numeral->next;
			++(number_arg);
			continue;
		}

		return false;
	}

	return (*number_arg == '\0') ? true : false;
}

void
print_numeral(numeral_ptr* numeral)
{
	while( !(numeral == NULL) )
	{
		printf("%c", *(numeral->symbol));
		numeral = numeral->next;
	}

	printf("\n");
}

int
main(int argc, char* argv[])
{
	program_name = argv[0];

	/* argument processing */
	if( !(argc == 2) )
	{
		fprintf(stderr, "usage: %s <number>\n", program_name);
		exit(EXIT_FAILURE);
	}

	/* Numeral System variables from MACROS */
	char* from = malloc( (strlen(FROM_NUMERICALS) + 1) * sizeof(char) );
	char* to = malloc( (strlen(TO_NUMERICALS) + 1) * sizeof(char) );

	strcpy(from, FROM_NUMERICALS);
	strcpy(to, TO_NUMERICALS);

	/* Number variables to be converted */
	char* number = argv[1];

	/* _first and _last variables */
	char* from_first = from;
	char* from_last = from + (strlen(from) - 1);

	char* to_first = to;
	char* to_last = to + (strlen(to) - 1);

	char* number_first = number;
	char* number_last = number + (strlen(number) - 1);

	/* initializing counting and result */
	numeral_ptr* counting = new_digit(NULL, from_first);
	numeral_ptr* result = new_digit(NULL, to_first);

	/* increments until it finishes */
	while( !is_the_same(counting, number_first) )
	{
		if(DEBUG)
		{
			puts("----");
			print_numeral(result);
		}
		increment(result, to_first, to_last);

		if(DEBUG)
			print_numeral(counting);
		increment(counting, from_first, from_last);
	}
	if(DEBUG)
		puts("----");

	/* print */
	if(DEBUG)
	{
		printf("counting: ");
		print_numeral(counting);
		printf("result: ");
	}
	print_numeral(result);

	return EXIT_SUCCESS;
}