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