﻿/*
 * Problem 9.* Binary floating-point
 * Write a program that shows the internal binary representation of
 * given 32-bit signed floating-point number in IEEE 754 format (the C# type float).
 */
using System;
using System.Text.RegularExpressions;

class P09_BinaryFloatingPoint
{
    /// <summary>
    /// Converts a number from decimal to binary
    /// </summary>
    /// <param name="input"> The integer to be converted</param>
    /// <returns>Number in the requested base represented as string</returns>
    static string FromDecimalToBinary(int number)
    {
        string result = string.Empty;

        while (number / 2 != 0)
        {
            int toAdd = number % 2;
            result = toAdd + result;
            number /= 2;
        }

        result = number + result;
        return result;
    }
    /// <summary>
    /// Method used to calculate the binary representation of the floating point part 
    /// of single precision floating number.
    /// </summary>
    /// <param name="number"> The number to be converted, always between 0 and 1</param>
    /// <returns> Binary representation of the floating part of single precision floating point number </returns>
    static string TurnDecimalPartToBinary(decimal number)
    {
        int precision = 50;
        decimal subResult = number;
        string result = string.Empty;

        for (int i = 0; i < precision; i++)
        {
            subResult *= 2;

            if (subResult > 1)
            {
                result += 1;
                subResult -= 1;
            }
            else if (subResult < 1)
            {
                result += 0;
            }
            else
            {
                result += 1;
                break;
            }
        }

        return result.PadRight(50, '0');
    }

    static void Main()
    {
        //// input
        Console.Write("Type a float: ");
        string input = Console.ReadLine();
        string result = string.Empty;
        
        // populating the sign bit
        if (input.Substring(0, 1) == "-")
        {
            result += 1;
            input = input.Substring(1);
        }
        else
        {
            result += 0;
        }

        // splitting the number to integer and decimal part
        int dotIndex = input.IndexOf('.');
        string integerPart = string.Empty;
        string decimalPart = string.Empty;

        if (dotIndex == -1)
        {
            integerPart = input;
            decimalPart = ".0";           
        }
        else
        {
            integerPart = input.Substring(0, dotIndex);
            decimalPart = input.Substring(dotIndex);

            if (integerPart == string.Empty)
            {
                integerPart = "0";
            }
        }

        string integerBinary = FromDecimalToBinary(int.Parse(integerPart));
        string decimalBinary = TurnDecimalPartToBinary(decimal.Parse(decimalPart));

        // finding the exponent and mantissa binary representation
        string temp = integerBinary + "." + decimalBinary;
        dotIndex = temp.IndexOf('.');
        int oneIndex = temp.IndexOf('1');
        string mantissa = string.Empty;

        if (dotIndex > oneIndex)
        {
            result += FromDecimalToBinary((dotIndex - 1) + 127).PadLeft(8, '0');
            Console.WriteLine("sign: {0}", result.Substring(0, 1));
            Console.Write("exponent: ");
            Console.WriteLine(result.Substring(1));
            mantissa = integerBinary.Substring(1) + decimalBinary;
            mantissa = mantissa.Substring(0, 23);
            Console.Write("mantissa: ");
            Console.WriteLine(mantissa);
        }
        else
        {
            result += FromDecimalToBinary((dotIndex - oneIndex) + 127).PadLeft(8, '0');
            Console.WriteLine("sign: {0}", result.Substring(0, 1));
            Console.Write("exponent: ");
            Console.WriteLine(result.Substring(1));
            mantissa = decimalBinary.Substring(decimalBinary.IndexOf('1') + 1, 23);
            Console.Write("mantissa: ");
            Console.WriteLine(mantissa);
        }
       
        result += mantissa;

        // output
        Console.WriteLine("The binary representation of {0} is {1}", input, result);
    }
}