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 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));
        }