private static BigPointNumber Division(BigPointNumber A, BigPointNumber B)
        {
            BigPointNumber a = A;
            BigPointNumber b = B;

            RemoveFractionalParts(ref a, ref b);

            string         wholePart      = Div(a, b).ToString();
            BigPointNumber defractionPart = Mod(a, b);

            StringBuilder resStr = new StringBuilder(wholePart);

            if (defractionPart.digits.Count == 1 && defractionPart.digits[0] == 0)
            {
                return(new BigPointNumber(resStr.ToString()));
            }

            BigPointNumber defractionPart_tmp = defractionPart;
            int            i = 0;

            while (defractionPart_tmp.ToString() != "0" && i < 20)
            {
                i++;
                defractionPart_tmp = defractionPart_tmp * new BigPointNumber("10");
                BigPointNumber wholePart_tmp = Div(defractionPart_tmp, b);
                //if (wholePart_tmp.ToString() != "0")
                resStr.Append(wholePart_tmp.ToString());
                defractionPart_tmp = Mod(defractionPart_tmp, b);
            }


            BigPointNumber result = new BigPointNumber(resStr.ToString().Substring(0, wholePart.Length) + "," + resStr.ToString().Substring(wholePart.Length, resStr.ToString().Length - wholePart.Length));

            return(result);
        }
Beispiel #2
0
        //"Входной" метод класса
        static public BigPointNumber Calculate(string input)
        {
            string         output = GetExpression(input); //Преобразовываем выражение в постфиксную запись
            BigPointNumber result = Counting(output);     //Решаем полученное выражение

            return(result);                               //Возвращаем результат
        }
        private static BigPointNumber Multiply(BigPointNumber a, BigPointNumber b)
        {
            var retValue = Zero;

            for (var i = 0; i < a.Size; i++)
            {
                for (int j = 0, carry = 0; (j < b.Size) || (carry > 0); j++)
                {
                    var cur = retValue.GetByte(i + j) + a.GetByte(i) * b.GetByte(j) + carry;
                    retValue.SetByte(i + j, (byte)(cur % 10));
                    carry = cur / 10;
                }
            }

            retValue.Sign = a.Sign == b.Sign ? Sign.Plus : Sign.Minus;
            int delta = 0;

            delta += a.PointPosition != -1 ? a.SizeAfterPoint : 0;
            delta += b.PointPosition != -1 ? b.SizeAfterPoint : 0;
            retValue.PointPosition = retValue.digits.Count - delta;
            if (retValue.PointPosition == retValue.digits.Count)
            {
                retValue.PointPosition = -1;
            }
            retValue.RemoveNulls();
            return(retValue);
        }
        public static BigPointNumber operator -(BigPointNumber a)
        {
            BigPointNumber tmp = a;

            tmp.Sign = tmp.Sign == Sign.Plus ? Sign.Minus : Sign.Plus;
            return(tmp);
        }
        public static BigPointNumber Exp(byte val, int exp)
        {
            BigPointNumber bigInt = Zero;

            bigInt.SetByte(exp, val);
            bigInt.RemoveNulls();
            return(bigInt);
        }
        private static BigPointNumber Substract(BigPointNumber a, BigPointNumber b)
        {
            BigPointNumber tmp_a = a;
            BigPointNumber tmp_b = b;


            var digits = new List <byte>();

            BigPointNumber max = Zero;
            BigPointNumber min = Zero;

            //сравниваем числа игнорируя знак
            var compare = Comparison(tmp_a, tmp_b, ignoreSign: true);

            switch (compare)
            {
            case -1:
                min = tmp_a;
                max = tmp_b;
                break;

            case 0:
                //если числа равны возвращаем 0
                return(Zero);

            case 1:
                min = tmp_b;
                max = tmp_a;
                break;
            }

            //из большего вычитаем меньшее
            EqualizationDigitsCount(ref tmp_a, ref tmp_b);

            var t = 0;

            for (var i = 0; i < tmp_a.Size; i++)
            {
                var s = max.GetByte(i) - min.GetByte(i) - t;
                if (s < 0)
                {
                    s += 10;
                    t  = 1;
                }
                else
                {
                    t = 0;
                }

                digits.Add((byte)s);
            }
            int            tmp_PointPos = tmp_a.PointPosition > tmp_b.PointPosition ? tmp_a.PointPosition : tmp_b.PointPosition;
            BigPointNumber result       = new BigPointNumber(digits, max.Sign, tmp_PointPos);

            result.RemoveNulls();
            return(new BigPointNumber(digits, max.Sign, tmp_PointPos));
        }
        public static BigPointNumber Factorial(int x)
        {
            BigPointNumber result = new BigPointNumber("1");

            for (int i = 2; i <= x; i++)
            {
                result = result * new BigPointNumber(i);
            }
            return(result);
        }
        private static void EqualizationDigitsCount(ref BigPointNumber a, ref BigPointNumber b)
        {
            int deltaBefore = Math.Abs(a.SizeBeforePoint - b.SizeBeforePoint);
            int deltaAfter  = Math.Abs(a.SizeAfterPoint - b.SizeAfterPoint);

            if (a.SizeAfterPoint > b.SizeAfterPoint)
            {
                for (int i = 0; i < deltaAfter; i++)
                {
                    if (b.PointPosition == -1)
                    {
                        b.PointPosition = b.digits.Count;
                    }
                    b.addDigit(0, 0);
                }
            }
            else if (a.SizeAfterPoint < b.SizeAfterPoint)
            {
                for (int i = 0; i < deltaAfter; i++)
                {
                    if (a.PointPosition == -1)
                    {
                        a.PointPosition = a.digits.Count;
                    }
                    a.addDigit(0, 0);
                }
            }



            if (a.SizeBeforePoint > b.SizeBeforePoint)
            {
                for (int i = 0; i < deltaBefore; i++)
                {
                    b.addDigit(b.digits.Count, 0);
                    if (b.PointPosition != -1)
                    {
                        b.PointPosition++;
                    }
                }
            }

            else if (a.SizeBeforePoint < b.SizeBeforePoint)
            {
                for (int i = 0; i < deltaBefore; i++)
                {
                    a.addDigit(a.digits.Count, 0);
                    if (b.PointPosition != -1)
                    {
                        a.PointPosition++;
                    }
                }
            }
        }
        private static BigPointNumber Div(BigPointNumber A, BigPointNumber B)
        {
            BigPointNumber a = A;
            BigPointNumber b = B;

            RemoveFractionalParts(ref a, ref b);

            BigNumber a_ = new BigNumber(a.ToString());
            BigNumber b_ = new BigNumber(b.ToString());

            return(new BigPointNumber(BigNumber.Div(a_, b_).ToString()));
        }
        private static int CompareSize(BigPointNumber a, BigPointNumber b)
        {
            if (a.SizeBeforePoint < b.SizeBeforePoint)
            {
                return(-1);
            }
            else if (a.SizeBeforePoint > b.SizeBeforePoint)
            {
                return(1);
            }

            return(CompareDigits(a, b));
        }
        private static BigPointNumber Mod(BigPointNumber A, BigPointNumber B)
        {
            BigPointNumber a = A;
            BigPointNumber b = B;

            RemoveFractionalParts(ref a, ref b);

            BigPointNumber result = a - (Div(a, b) * b);

            return(result);

            //BigNumber a_ = new BigNumber(a.ToString());
            //BigNumber b_ = new BigNumber(b.ToString());

            //return new BigPointNumber(BigNumber.Mod(a_, b_).ToString());
        }
        private static int CompareSign(BigPointNumber a, BigPointNumber b, bool ignoreSign = false)
        {
            if (!ignoreSign)
            {
                if (a.Sign < b.Sign)
                {
                    return(-1);
                }
                else if (a.Sign > b.Sign)
                {
                    return(1);
                }
            }

            return(CompareSize(a, b));
        }
        private static void RemoveFractionalParts(ref BigPointNumber a, ref BigPointNumber b)
        {
            int aSizeAfterPoint    = a.SizeAfterPoint;
            int bSizeAfterPoint    = b.SizeAfterPoint;
            int MaxSizeAfterPoints = Math.Max(aSizeAfterPoint, bSizeAfterPoint);

            for (int i = 0; i < MaxSizeAfterPoints - aSizeAfterPoint; i++)
            {
                a.digits.Insert(0, 0);
            }
            for (int i = 0; i < MaxSizeAfterPoints - bSizeAfterPoint; i++)
            {
                b.digits.Insert(0, 0);
            }
            a.PointPosition = -1;
            b.PointPosition = -1;
        }
Beispiel #14
0
        static private BigPointNumber Counting(string input)
        {
            BigPointNumber         result = new BigPointNumber("0");      //Результат
            Stack <BigPointNumber> temp   = new Stack <BigPointNumber>(); //Dhtvtyysq стек для решения

            for (int i = 0; i < input.Length; i++)                        //Для каждого символа в строке
            {
                //Если символ - цифра, то читаем все число и записываем на вершину стека
                if (Char.IsDigit(input[i]))
                {
                    string a = string.Empty;

                    while (!IsDelimeter(input[i]) && !IsOperator(input[i])) //Пока не разделитель
                    {
                        a += input[i];                                      //Добавляем
                        i++;
                        if (i == input.Length)
                        {
                            break;
                        }
                    }
                    temp.Push(new BigPointNumber(a));  //Записываем в стек
                    i--;
                }
                else if (IsOperator(input[i])) //Если символ - оператор
                {
                    //Берем два последних значения из стека
                    BigPointNumber a = temp.Pop();
                    BigPointNumber b = temp.Pop();

                    switch (input[i]) //И производим над ними действие, согласно оператору
                    {
                    case '+': result = b + a; break;

                    case '-': result = b - a; break;

                    case '*': result = b * a; break;

                    case '/': result = b / a; break;
                    }
                    temp.Push(result); //Результат вычисления записываем обратно в стек
                }
            }
            return(temp.Peek()); //Забираем результат всех вычислений из стека и возвращаем его
        }
        public static BigPointNumber Pi()
        {
            BigPointNumber res = new BigPointNumber(0);

            for (int i = 0; i <= 100000; i++)
            {
                BigPointNumber fraction = new BigPointNumber(4) / new BigPointNumber(2 * i + 1);
                if (i % 2 == 0)
                {
                    res = res + fraction;
                }
                else
                {
                    res = res - fraction;
                }
            }
            return(res);
        }
        private static int CompareDigits(BigPointNumber a, BigPointNumber b)
        {
            BigPointNumber tmp_a = a;
            BigPointNumber tmp_b = b;

            EqualizationDigitsCount(ref tmp_a, ref tmp_b);

            for (var i = tmp_a.Size; i >= 0; i--)
            {
                if (tmp_a.GetByte(i) < tmp_b.GetByte(i))
                {
                    return(-1);
                }
                else if (tmp_a.GetByte(i) > tmp_b.GetByte(i))
                {
                    return(1);
                }
            }

            return(0);
        }
        private static BigPointNumber Add(BigPointNumber a, BigPointNumber b)
        {
            BigPointNumber tmp_a = a;
            BigPointNumber tmp_b = b;

            EqualizationDigitsCount(ref tmp_a, ref tmp_b);

            var digits = new List <byte>();

            byte t = 0;

            for (int i = 0; i < tmp_a.Size; i++)
            {
                byte sum = (byte)(a.GetByte(i) + b.GetByte(i) + t);
                if (sum > 10)
                {
                    sum -= 10;
                    t    = 1;
                }
                else
                {
                    t = 0;
                }

                digits.Add(sum);
            }

            if (t > 0)
            {
                digits.Add(t);
            }

            int tmp_PointPos = tmp_a.PointPosition > tmp_b.PointPosition ? tmp_a.PointPosition : tmp_b.PointPosition;

            return(new BigPointNumber(digits, tmp_a.Sign, tmp_PointPos));
        }
 //-------------------------------------------------------------------
 private static int Comparison(BigPointNumber a, BigPointNumber b, bool ignoreSign = false)
 {
     return(CompareSign(a, b, ignoreSign));
 }