#include <stdio.h>
#include <windows.h>
#include <math.h>
unsigned long long converter(int typeOfConvertion, unsigned long numberToConvert);
unsigned long long puissanceFunction(unsigned long x, unsigned long y);

int main(int argc, char argv[])
{
    // Title
    unsigned int i;
    for(i = 0; i <= 20; i++)
    {
        printf("#");
        Sleep(100);
    }
    printf(" binary converter ");
    for(i = 0; i <= 20; i++)
    {
        printf("#");
        Sleep(100);
    }
    printf("\n\n\n");

    // Home
    printf("What would you like to do ?\n\n");
    Sleep(150);
    printf("1.)Convert Decimal to Binary\n");
    printf("2.)Convert Decimal to Hexadecimal\n");
    printf("3.)Convert Binary to Decimal\n");
    printf("4.)Convert Hexadecimal to Decimal\n");

    unsigned int choice;
    printf("Your Choice: ");
    scanf("%u", &choice);

    switch(choice)
    {
        case 1:
        {
            printf("You have chosen \"Convert Decimal to Binary\"\n");
            Sleep(1500);
            system("cls");
            printf("enter a number ");
            unsigned long nombre;
            scanf("%lu", &nombre);
            if(nombre > 1048575)
            {
                printf("Too high, limit is 1048575 (integer overflow)\nexiting...");
                Sleep(2800);
                return 1;
            }
            printf("Number Converted: %llu\n\n", converter(1, nombre));
            system("pause");
        } break;
        case 2:
        {
            printf("You have chosen \"Convert Decimal to Hexadecimal\"\n");
            Sleep(1500);
            system("cls");
            printf("Coming soon !\n");
            system("pause");
        } break;
        case 3:
        {
            printf("You have chosen \"Convert Binary to Decimal\"\n");
            Sleep(1500);
            system("cls");
            printf("Coming soon !\n");
            system("pause");
        } break;
        case 4:
        {
            printf("You have chosen \"Convert Hexadecimal to Decimal\"\n");
            Sleep(1500);
            system("cls");
            printf("Coming soon !\n");
            system("pause");
        } break;
        case 5:
        {
            printf("Easter Egg !\n");
            system(".\\ressources\\pic\\picture\\meme.jpg");
            system("pause");
        } break;
        default:
        {
            printf("Invalid Choice\n");
        }
    }
    return 0;
}
unsigned long long converter(int typeOfConvertion, unsigned long numberToConvert)
{
    unsigned long long numberConverted = 0;
    unsigned long puissances = 0;
    unsigned long base = 2;
    unsigned long long converterTable = 0;

    if(typeOfConvertion == 1)
    {
        while(numberToConvert > 0)
        {
            converterTable = pow(base, puissances);

            if(converterTable == numberToConvert)
            {
                numberToConvert = numberToConvert - converterTable;
                numberConverted = numberConverted + puissanceFunction(10, puissances);
                puissances = 0;
            }else if(converterTable < numberToConvert)
            {
                puissances += 1;
            }else if(converterTable > numberToConvert)
            {
                puissances -= 1;
                converterTable = pow(base, puissances);
                numberToConvert = numberToConvert - converterTable;
                numberConverted = numberConverted + puissanceFunction(10, puissances);
                puissances = 0;
            }
        }
        return numberConverted;
    }
}
unsigned long long puissanceFunction(unsigned long x, unsigned long y)
{
    unsigned long long result;
    unsigned long puissancesResult;

    if(y == 0)
    {
        return 1;
    }else if(x == 0)
    {
        return 0;
    }else
    {
        return x*puissanceFunction(x, y-1);
    }

    return result;
}