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.º 2
0
        public static string Divide(string op_1, string op_2, NumericSystems NumSystem)
        {
            string result_ = "";

            var numbers_to_Displace = Formatter.DigitsAfterDotAlignment(op_1, op_2);

            op_1 = Displace(op_1, numbers_to_Displace);
            op_2 = Displace(op_2, numbers_to_Displace);

            int  integer_number_count = 1;
            bool is_first_time = true;
            int  now_int_number, last_int_number = 0, base_displace = 0;


            while (!Formatter.IsZero(op_1) && result_.Length - integer_number_count < 20)
            {
                // if second OP bigger than first
                while (Compare(op_1, op_2) < 0)
                {
                    op_1 = Displace(op_1, 1);
                    base_displace++;
                }


                // create dividing part - number from <second operand>, that can be maximum divided by <first operand>
                string dividing_part = op_2;
                now_int_number = 1 - base_displace;
                while (Compare(Displace(dividing_part, 1), op_1) < 0)
                {
                    dividing_part = Displace(dividing_part, 1);
                    now_int_number++;
                }

                if (is_first_time)
                {
                    integer_number_count = now_int_number;
                    is_first_time        = false;
                }
                else
                {
                    var discharge = last_int_number - now_int_number;
                    for (int i = 0; i < discharge - 1; i++)
                    {
                        result_ += '0';
                    }
                }
                last_int_number = now_int_number;


                // <first operand> minus <dividing part> (toResult) times
                var toResult = 0;

                while (Compare(op_1, dividing_part) >= 0)
                {
                    op_1 = Minus(op_1, dividing_part, NumSystem);
                    toResult++;
                }

                if (toResult >= (int)NumSystem)
                {
                    result_ += Convertor.Convert(NumericSystems.Decimal, NumSystem, toResult.ToString());
                    integer_number_count++;
                }
                else
                {
                    result_ += SymbolOf((byte)toResult);
                }
            }


            // displace result by <integer number count>
            return(Displace(result_, integer_number_count - result_.Length));
        }