Ejemplo n.º 1
0
        public string ConvertToOtherSystem(NumericSystems otherNumSystem)
        {
            if (otherNumSystem == intitialNumericSystem)
            {
                return(Formatter.GetFormat(number));
            }
            else
            if (otherNumSystem == NumericSystems.Decimal)
            {
                var(numb, fraction) = ConvertToDecimalSystem();
                return(Formatter.GetFormat(numb, fraction));
            }
            else // (otherNumSystem != originalNumSystem)
            {
                BigInteger integer_part;
                double     fraction_part;

                if (intitialNumericSystem != NumericSystems.Decimal)
                {
                    (integer_part, fraction_part) = ConvertToDecimalSystem();
                }
                else
                {
                    var(str_int, str_frac)        = Formatter.SplitNumberByDot(number);
                    (integer_part, fraction_part) = (BigInteger.Parse(str_int), double.Parse("0." + str_frac));
                }

                StringBuilder
                    sb_integer  = new StringBuilder(),
                    sb_fraction = new StringBuilder();

                while (integer_part != 0)
                {
                    sb_integer.Append(SymbolOf((byte)(integer_part % (int)otherNumSystem)));

                    integer_part /= (int)otherNumSystem;
                }

                while (sb_fraction.Length < 40 && fraction_part != 0)
                {
                    fraction_part *= (int)otherNumSystem;
                    var integer_number = (byte)Math.Truncate(fraction_part);

                    sb_fraction.Append(SymbolOf(integer_number));
                    fraction_part -= integer_number;
                }


                return(Formatter.GetFormat(
                           string.Join("", sb_integer.ToString().Reverse()), sb_fraction.ToString()));
            }
        }
Ejemplo n.º 2
0
        public static string Minus(string op_1, string op_2, NumericSystems NumSystem)
        {
            string sign = "";

            // if second operator biggen than first - change their place & make sign minus
            if (Compare(op_1, op_2) < 0)
            {
                var temp = op_1;
                op_1 = op_2;
                op_2 = temp;
                sign = "-";
            }

            // convert numbers into integer & fraction parts
            var(op_1_int_part, op_1_frac_part) = Formatter.SplitNumberByDot(op_1);
            var(op_2_int_part, op_2_frac_part) = Formatter.SplitNumberByDot(op_2);
            Formatter.DigitsAfterDotAlignment(ref op_1_frac_part, ref op_2_frac_part);

            // add zeros if number's length are not equal
            if (op_1_int_part.Length > op_2_int_part.Length)
            {
                var difference = op_1_int_part.Length - op_2_int_part.Length;

                for (int i = 0; i < difference; i++)
                {
                    op_2_int_part = op_2_int_part.Insert(0, "0");
                }
            }

            // find integer part
            var integer = Plus(op_1_int_part, GetAdditionalCode(op_2_int_part, NumSystem), NumSystem).Remove(0, 1);

            string fraction;

            // find out if 1 fraction more than another
            if (Compare(op_1_frac_part, op_2_frac_part) < 0)
            {
                fraction = GetAdditionalCode(
                    Plus(op_2_frac_part, GetAdditionalCode(op_1_frac_part, NumSystem), NumSystem)
                    .Remove(0, 1), NumSystem);

                integer = Minus(integer, "1", NumSystem);
            }
            else
            {
                fraction = Plus(op_1_frac_part, GetAdditionalCode(op_2_frac_part, NumSystem), NumSystem).Remove(0, 1);
            }

            return(sign + Formatter.GetFormat(integer, fraction));
        }
        public static string Displace(string number, int count)
        {
            if (count == 0)
            {
                return(number);
            }
            var(integer_part, fraction_part) = Formatter.SplitNumberByDot(number);


            var isPositiveDisplace = count > 0;

            for (int i = 0; i < Math.Abs(count); i++)
            {
                if (isPositiveDisplace)
                {
                    string buffer;

                    if (Formatter.IsZero(fraction_part))
                    {
                        buffer = "0";
                    }
                    else
                    {
                        buffer        = fraction_part[0].ToString();
                        fraction_part = fraction_part.Remove(0, 1);
                    }

                    integer_part = integer_part.Insert(integer_part.Length, buffer);
                }
                else
                {
                    string buffer;

                    if (Formatter.IsZero(integer_part))
                    {
                        buffer = "0";
                    }
                    else
                    {
                        buffer       = integer_part[integer_part.Length - 1].ToString();
                        integer_part = integer_part.Remove(integer_part.Length - 1, 1);
                    }

                    fraction_part = fraction_part.Insert(0, buffer);
                }
            }

            return(Formatter.GetFormat(integer_part, fraction_part));
        }
Ejemplo n.º 4
0
        public static string Plus(string op_1, string op_2, NumericSystems NumSystem)
        {
            // factor numbers into integer & fraction parts
            var(op_1_int_part, op_1_frac_part) = Formatter.SplitNumberByDot(op_1);
            var(op_2_int_part, op_2_frac_part) = Formatter.SplitNumberByDot(op_2);
            var dotIndex = Formatter.DigitsAfterDotAlignment(ref op_1_frac_part, ref op_2_frac_part);

            var operand_1 = op_1_int_part.Concat(op_1_frac_part).Reverse().ToArray();
            var operand_2 = op_2_int_part.Concat(op_2_frac_part).Reverse().ToArray();
            var result    = new List <char>();

            // create buffer
            int num_sys = (int)NumSystem;
            int buffer  = 0;

            // going in cycle for max count between 1 & 2 operators times
            for (int i = 0; i < operand_1.Length || i < operand_2.Length; i++)
            {
                // add value of next elements in every operator if it's not end
                if (i < operand_1.Length)
                {
                    buffer += NumberOf(operand_1[i]);
                }
                if (i < operand_2.Length)
                {
                    buffer += NumberOf(operand_2[i]);
                }

                // add to result remainder of the division
                result.Add(SymbolOf((byte)(buffer % num_sys)));

                // save to buffer integer of the division
                buffer /= num_sys;
            }

            // throw off buffer to result if it's not zero
            if (buffer != 0)
            {
                result.Add(SymbolOf((byte)buffer));
            }

            result.Insert(dotIndex, '.');
            result.Reverse();
            return(Formatter.GetFormat(string.Join("", result)));
        }