public NumberString Add(NumberString anotherNumber) { char[] x, y; if (this.number.Length > anotherNumber.Number.Length) { x = this.number.ToCharArray(); y = anotherNumber.Number.ToCharArray(); } else { y = this.number.ToCharArray(); x = anotherNumber.Number.ToCharArray(); } char[] sum = new char[x.Length]; int carry = 0; for (int iterX = x.Length - 1, iterY = y.Length - 1; iterX >= 0; iterX--, iterY--) { if (x[iterX] < 48 || x[iterX] > 57) { throw new Exception("The input number is not in number format! Please enter valid digits from 0 - 9"); } if (iterY >= 0 && (y[iterY] > 57 || y[iterY] < 48)) { throw new Exception("The input number is not in number format! Please enter valid digits from 0 - 9"); } int miniSum = x[iterX] + carry - 48; // Removing Ascii if (iterY >= 0) { miniSum += y[iterY] - 48; // Removing Ascii } carry = miniSum / 10; sum[iterX] = Convert.ToChar((miniSum % 10) + 48); } if (carry > 0) { return(new NumberString(carry.ToString() + new string(sum))); } else { return(new NumberString(sum)); } }
public NumberString Multiply(NumberString anotherNumber) { NumberString product = null, a = null, b = null, c = null, d = null; try { string x, y = anotherNumber.Number; short negativeCount = 0; if (this.IsNegative()) { x = this.number.Substring(1); negativeCount++; } else { x = this.number; } if (anotherNumber.IsNegative()) { y = anotherNumber.Number.Substring(1); negativeCount++; } else { y = anotherNumber.Number; } if (x.Equals(ZERO) || y.Equals(ZERO)) { return(new NumberString(ZERO)); } if (x.Length == 1 && y.Length == 1) { int miniProduct = Convert.ToInt32(x) * Convert.ToInt32(y); if (negativeCount % 2 == 1) { return(new NumberString("-" + miniProduct)); } return(new NumberString(miniProduct)); } bool applyEfficiency = false; if (x.Length == y.Length && this.isTwoMultiple(x.Length)) { applyEfficiency = true; } int xFirstHalfLength = x.Length / 2; int xSecondHalfLength = (x.Length + 1) / 2; int yFirstHalfLength = y.Length / 2; int ySecondHalfLength = (y.Length + 1) / 2; a = new NumberString(x.Substring(0, xFirstHalfLength), false); b = new NumberString(x.Substring(xFirstHalfLength), false); c = new NumberString(y.Substring(0, yFirstHalfLength), false); d = new NumberString(y.Substring(yFirstHalfLength), false); NumberString firstProduct = a.Multiply(c); NumberString acProduct = firstProduct.Clone(); firstProduct.PadRight(xSecondHalfLength + ySecondHalfLength, '0'); NumberString fourthProduct = b.Multiply(d); if (applyEfficiency) { NumberString secondProduct = a.Add(b).Multiply(c.Add(d)); secondProduct = secondProduct.Subtract(acProduct).Subtract(fourthProduct); secondProduct.PadRight(xSecondHalfLength, '0'); product = firstProduct.Add(secondProduct).Add(fourthProduct); } else { NumberString secondProduct = a.Multiply(d); secondProduct.PadRight(xSecondHalfLength, '0'); NumberString thirdProduct = b.Multiply(c); thirdProduct.PadRight(ySecondHalfLength, '0'); product = firstProduct.Add(secondProduct).Add(thirdProduct).Add(fourthProduct); } if (negativeCount % 2 == 1) { return(new NumberString("-" + product.Number)); } } catch (Exception ex) { Console.WriteLine(a.number); Console.WriteLine(b.number); Console.WriteLine(c.number); Console.WriteLine(d.number); throw ex; } return(product); }
public NumberString Subtract(NumberString anotherNumber) { char[] x, y; bool swap = true; if (this.number.Length > anotherNumber.Number.Length) { swap = false; } if (this.number.Length == anotherNumber.Number.Length) { for (int iter = 0; iter < this.number.Length; iter++) { if (this.number[iter] > anotherNumber.Number[iter]) { swap = false; break; } } } x = swap ? anotherNumber.Number.ToCharArray() : this.number.ToCharArray(); y = swap ? this.number.ToCharArray() : anotherNumber.Number.ToCharArray(); char[] difference = new char[x.Length]; int borrow = 0; bool stillBorrow = false; for (int iterX = x.Length - 1, iterY = y.Length - 1; iterX >= 0; iterX--, iterY--) { if (x[iterX] < 48 || x[iterX] > 57) { throw new Exception("The input number is not in number format! Please enter valid digits from 0 - 9"); } if (iterY >= 0 && (y[iterY] > 57 || y[iterY] < 48)) { throw new Exception("The input number is not in number format! Please enter valid digits from 0 - 9"); } int firstDigit, secondDigit; if (x[iterX] == 48 && borrow > 0) { firstDigit = 9; // Assiging 9 directly in case of borrow stillBorrow = true; } else { firstDigit = x[iterX] - borrow - 48; // Removing Ascii borrow = 0; } int miniDifference = firstDigit; if (iterY >= 0) { secondDigit = y[iterY] - 48; if (firstDigit >= secondDigit) { miniDifference -= secondDigit; borrow = stillBorrow ? borrow : 0; } else { miniDifference = miniDifference + 10 - secondDigit; borrow = 1; } } difference[iterX] = Convert.ToChar(miniDifference + 48); } string finalDiff = new string(difference); if (finalDiff.Equals("0")) { return(new NumberString(finalDiff)); } int indexOfNumber = finalDiff.IndexOfAny(NUMBERS); if (indexOfNumber == -1 && finalDiff[finalDiff.Length - 1] == '0') // To handle the situations like "0" or "000" etc. { return(new NumberString("0")); } finalDiff = finalDiff.Substring(indexOfNumber); if (swap) { return(new NumberString("-" + finalDiff)); } else { return(new NumberString(finalDiff)); } }