Ejemplo n.º 1
0
        public VeryLong Subtract(VeryLong subtrahend)
        {
            int minuendDecimalPlaces    = GetDecimalPlaces();
            int subtrahendDecimalPlaces = subtrahend.GetDecimalPlaces();
            int decimalPlaces           = Math.Max(minuendDecimalPlaces, subtrahendDecimalPlaces);

            FillUpDecimalPlacesWithZero(decimalPlaces);
            subtrahend.FillUpDecimalPlacesWithZero(decimalPlaces);

            string result                     = "";
            int    diff                       = 0;
            int    borrow                     = 0;
            int    currentLastDigit           = 0;
            int    currentLastDigitSubtrahend = 0;
            int    i = 0;

            if (!IsLargerOrEqual(subtrahend))
            {
                // swap: minuend is smaller than subtrahend --> result is negative
                VeryLong newMinuend    = subtrahend;
                VeryLong newSubtrahend = new VeryLong(ToString());
                VeryLong difference    = newMinuend.Subtract(newSubtrahend);
                return(new VeryLong("-" + difference.ToString()));
            }

            while (Length() > i || subtrahend.Length() > i || borrow != 0)
            {
                if (GetLastDigit(i) == ".")
                {
                    result = result.Insert(0, ".");
                    i++;
                    continue;
                }

                currentLastDigit           = int.Parse(GetLastDigit(i));
                currentLastDigitSubtrahend = int.Parse(subtrahend.GetLastDigit(i));
                diff = currentLastDigit - currentLastDigitSubtrahend - borrow;
                if (diff < 0)
                {
                    borrow = 1;
                    diff  += 10;
                }
                else
                {
                    borrow = 0;
                }
                result = result.Insert(0, diff.ToString());
                i++;
            }

            result = RemoveLeadingZeros(result);
            return(new VeryLong(result));
        }
Ejemplo n.º 2
0
        public VeryLong Multiply(VeryLong other)
        {
            int numberDecimalPlaces      = GetDecimalPlaces();
            int numberDecimalPlacesOther = other.GetDecimalPlaces();
            int resultDecialPlaces       = numberDecimalPlaces + numberDecimalPlacesOther;

            other = RemoveDecimalPlaces(other);
            string integerStringCopy = _integerString;

            _integerString = _integerString.Replace(".", "");

            List <string> multiplicationResults = new List <string>();
            string        multiplicationResult  = GetZeros(1);
            int           currentLastDigit      = 0;
            int           currentLastDigitOther = 0;
            int           resultDigit           = 0;
            int           behalte    = 0;
            int           mult       = 0;
            int           indexOther = 0;
            int           index      = 0;

            while (Length() > index)
            {
                indexOther           = 0;
                behalte              = 0;
                multiplicationResult = GetZeros(index);

                while (other.Length() > indexOther || behalte != 0)
                {
                    currentLastDigit      = int.Parse(GetLastDigit(index));
                    currentLastDigitOther = int.Parse(other.GetLastDigit(indexOther));
                    mult                 = currentLastDigit * currentLastDigitOther + behalte;
                    behalte              = mult / 10;
                    resultDigit          = mult % 10;
                    multiplicationResult = multiplicationResult.Insert(0, resultDigit.ToString());
                    indexOther++;
                }

                index++;
                multiplicationResults.Add(multiplicationResult);
            }

            VeryLong mainResult = VeryLong.AddSummands(multiplicationResults);

            mainResult.SetDecimalPlaces(resultDecialPlaces);
            mainResult.RemoveLeadingZeros();

            _integerString = integerStringCopy;
            return(mainResult);
        }
Ejemplo n.º 3
0
        public VeryLong DivideBy(VeryLong divisor, int numberOfDigits)
        {
            int numberDecimalPlacesDivisor = divisor.GetDecimalPlaces();

            divisor = RemoveDecimalPlaces(divisor);

            FillUpDecimalPlacesWithZero(numberOfDigits + numberDecimalPlacesDivisor);

            string dividendString = "";
            string divisorString  = divisor.ToString();
            string quotientDigit  = "";
            string quotient       = "";
            string mult           = "";
            string diff           = "";

            for (int i = 0; i < Length(); i++)
            {
                if (GetFirstDigit(i) == ".")
                {
                    quotient += ".";
                    continue;
                }

                dividendString += GetFirstDigit(i);
                dividendString  = VeryLong.RemoveLeadingZeros(dividendString);
                quotientDigit   = DivideSimple(dividendString, divisorString);
                quotient       += quotientDigit;
                mult            = (new VeryLong(quotientDigit).Multiply(new VeryLong(divisorString))).ToString();
                diff            = (new VeryLong(dividendString).Subtract(new VeryLong(mult))).ToString();
                dividendString  = diff;
            }

            VeryLong quotientResult      = new VeryLong(quotient);
            int      resultDecimalPlaces = quotientResult.GetDecimalPlaces();

            quotientResult = RemoveDecimalPlaces(quotientResult);
            quotientResult.SetDecimalPlaces(resultDecimalPlaces - numberDecimalPlacesDivisor);

            quotientResult.RemoveLeadingZeros();
            quotientResult.LimitPrecisionTo(numberOfDigits);
            return(quotientResult);
        }
Ejemplo n.º 4
0
        public VeryLong Add(VeryLong other)
        {
            int summand1DecimalPlaces = GetDecimalPlaces();
            int summand2DecimalPlaces = other.GetDecimalPlaces();
            int decimalPlaces         = Math.Max(summand1DecimalPlaces, summand2DecimalPlaces);

            FillUpDecimalPlacesWithZero(decimalPlaces);
            other.FillUpDecimalPlacesWithZero(decimalPlaces);

            string result                = "";
            int    sum                   = 0;
            int    behalte               = 0;
            int    resultDigit           = 0;
            int    currentLastDigit      = 0;
            int    currentLastDigitOther = 0;
            int    i = 0;

            while (Length() > i || other.Length() > i || behalte != 0)
            {
                if (GetLastDigit(i) == ".")
                {
                    result = result.Insert(0, ".");
                    i++;
                    continue;
                }

                currentLastDigit      = int.Parse(GetLastDigit(i));
                currentLastDigitOther = int.Parse(other.GetLastDigit(i));
                sum         = currentLastDigit + currentLastDigitOther + behalte;
                behalte     = sum / 10;
                resultDigit = sum % 10;
                result      = result.Insert(0, resultDigit.ToString());
                i++;
            }

            return(new VeryLong(result));
        }