public static HugeInteger operator /(HugeInteger firstInteger, HugeInteger secondInteger) { if (secondInteger.ToString() == "0") { return(null); } HugeInteger quotient = new HugeInteger("0"); HugeInteger firstIntegerTemp = new HugeInteger(firstInteger); HugeInteger secondIntegerTemp = new HugeInteger(secondInteger); firstIntegerTemp.NumberSign = '+'; secondIntegerTemp.NumberSign = '+'; HugeInteger difference = new HugeInteger(firstIntegerTemp); HugeInteger zero = new HugeInteger("0"); bool condition = difference >= secondIntegerTemp; while (condition == true) { quotient += new HugeInteger("1"); difference -= secondIntegerTemp; condition = (difference - secondIntegerTemp) >= zero; } if (firstInteger.NumberSign != secondInteger.NumberSign && quotient != new HugeInteger("0")) { quotient.NumberSign = '-'; } return(quotient); }
public static HugeInteger operator %(HugeInteger firstInteger, HugeInteger secondInteger) { HugeInteger quotient = firstInteger / secondInteger; HugeInteger remainder = firstInteger - (secondInteger * quotient); return(remainder); }
public static HugeInteger operator *(HugeInteger firstInteger, HugeInteger secondInteger) { if (firstInteger.IsZero() || secondInteger.IsZero()) { return(new HugeInteger("0")); } HugeInteger firstIntegerTemp = new HugeInteger(firstInteger); HugeInteger secondIntegerTemp = new HugeInteger(secondInteger); firstIntegerTemp.NumberSign = '+'; secondIntegerTemp.NumberSign = '+'; if (firstIntegerTemp < secondInteger) { return(secondInteger * firstInteger); } HugeInteger resultProduct = new HugeInteger("0"); int zeros = 0; for (int shortRow = secondInteger.HugeIntegerArray.Length - 1; shortRow >= 0; shortRow--) { int product = 0; int carry = 0; string productString = ""; for (int i = 0; i < zeros; i++) { productString += "0"; } for (int longRow = firstInteger.HugeIntegerArray.Length - 1; longRow >= 0; longRow--) { product = secondInteger.HugeIntegerArray[shortRow] * firstInteger.HugeIntegerArray[longRow] + carry; if (longRow == 0) { productString = productString.Insert(0, product.ToString()); break; } if (product >= 10) { carry = product / 10; product %= 10; } else { carry = 0; } productString = productString.Insert(0, product.ToString()); } resultProduct += new HugeInteger(productString); zeros++; } if (firstInteger.NumberSign != secondInteger.NumberSign) { resultProduct.NumberSign = '-'; } return(resultProduct); }
public static HugeInteger operator -(HugeInteger firstInteger, HugeInteger secondInteger) { if (firstInteger.NumberSign == '+' && secondInteger.NumberSign == '+') { if (firstInteger >= secondInteger) { string differenceString = ""; int smallLength = Math.Min(firstInteger.HugeIntegerArray.GetLength(0), secondInteger.HugeIntegerArray.GetLength(0)); int longLength = Math.Max(firstInteger.HugeIntegerArray.GetLength(0), secondInteger.HugeIntegerArray.GetLength(0)); int borrow = 0; for (int counter = 1; counter <= smallLength; counter++) { int difference = firstInteger.HugeIntegerArray[firstInteger.HugeIntegerArray.GetLength(0) - counter] - secondInteger.HugeIntegerArray[secondInteger.HugeIntegerArray.GetLength(0) - counter] - borrow; if (difference < 0) { difference += 10; borrow = 1; } else { borrow = 0; } differenceString = differenceString.Insert(0, "" + difference); } for (int counter = smallLength + 1; counter <= longLength; counter++) { int subtractionColumn; try { subtractionColumn = firstInteger.HugeIntegerArray[firstInteger.HugeIntegerArray.GetLength(0) - counter] - borrow; } catch { subtractionColumn = secondInteger.HugeIntegerArray[secondInteger.HugeIntegerArray.GetLength(0) - counter] - borrow; } if (subtractionColumn < 0) { subtractionColumn += 10; borrow = 1; } else { borrow = 0; } differenceString = differenceString.Insert(0, "" + subtractionColumn); } return(new HugeInteger(differenceString)); } else { HugeInteger difference = secondInteger - firstInteger; difference.NumberSign = '-'; return(difference); } } else if (firstInteger.NumberSign == '+' && secondInteger.NumberSign == '-') { HugeInteger temp = new HugeInteger(secondInteger); temp.NumberSign = '+'; return(new HugeInteger(firstInteger + temp)); } else if (firstInteger.NumberSign == '-' && secondInteger.NumberSign == '+') { HugeInteger temp = new HugeInteger(firstInteger); temp.NumberSign = '-'; return(new HugeInteger(secondInteger + temp)); } else { HugeInteger firstIntegerTemp = new HugeInteger(firstInteger); HugeInteger secondIntegerTemp = new HugeInteger(secondInteger); firstIntegerTemp.NumberSign = '+'; secondIntegerTemp.NumberSign = '+'; HugeInteger difference = secondIntegerTemp - firstIntegerTemp; return(new HugeInteger(difference)); } }
public HugeInteger(HugeInteger calculatedNumber) { HugeIntegerArray = calculatedNumber.HugeIntegerArray; NumberSign = calculatedNumber.NumberSign; }
public static HugeInteger operator +(HugeInteger firstInteger, HugeInteger secondInteger) { if (firstInteger.NumberSign == '+' && secondInteger.NumberSign == '+') { string sumString = ""; int smallLength = Math.Min(firstInteger.HugeIntegerArray.GetLength(0), secondInteger.HugeIntegerArray.GetLength(0)); int longLength = Math.Max(firstInteger.HugeIntegerArray.GetLength(0), secondInteger.HugeIntegerArray.GetLength(0)); int carry = 0; for (int counter = 1; counter <= smallLength; counter++) { int sum = firstInteger.HugeIntegerArray[firstInteger.HugeIntegerArray.GetLength(0) - counter] + secondInteger.HugeIntegerArray[secondInteger.HugeIntegerArray.GetLength(0) - counter] + carry; if (firstInteger.HugeIntegerArray.GetLength(0) == secondInteger.HugeIntegerArray.GetLength(0) && counter == smallLength) { sumString = sumString.Insert(0, "" + sum); break; } else { sumString = sumString.Insert(0, "" + sum % 10); } if (sum >= 10) { carry = 1; } else { carry = 0; } } for (int counter = smallLength + 1; counter <= longLength; counter++) { int additionColumn; try { additionColumn = firstInteger.HugeIntegerArray[firstInteger.HugeIntegerArray.GetLength(0) - counter] + carry; } catch { additionColumn = secondInteger.HugeIntegerArray[secondInteger.HugeIntegerArray.GetLength(0) - counter] + carry; } sumString = sumString.Insert(0, "" + additionColumn % 10); if (additionColumn >= 10) { carry = 1; if (counter == longLength) { sumString = sumString.Insert(0, "" + carry); } } else { carry = 0; } } return(new HugeInteger(sumString)); } else if (firstInteger.NumberSign == '+' && secondInteger.NumberSign == '-') { HugeInteger temp = new HugeInteger(secondInteger); temp.NumberSign = '+'; return(firstInteger - temp); } else if (firstInteger.NumberSign == '-' && secondInteger.NumberSign == '+') { HugeInteger temp = new HugeInteger(firstInteger); temp.NumberSign = '+'; return(secondInteger - temp); } else { HugeInteger firstIntegerTemp = new HugeInteger(firstInteger); HugeInteger secondIntegerTemp = new HugeInteger(secondInteger); firstIntegerTemp.NumberSign = '+'; secondIntegerTemp.NumberSign = '+'; HugeInteger sum = firstIntegerTemp + secondIntegerTemp; sum.NumberSign = '-'; return(sum); } }
static void Main(string[] args) { bool agreeToInputAgain = true; do { HugeInteger firstInteger = Input(); HugeInteger secondInteger = Input(); Console.WriteLine(); Console.WriteLine($"The value of the first integer: {firstInteger.ToString(), 4}"); Console.WriteLine($"The value of the second integer: {secondInteger.ToString(), 4}"); Console.WriteLine(); Console.WriteLine($"The value of the first integer input is zero: {firstInteger.IsZero(), 4}"); Console.WriteLine($"The value of the second integer input is zero: {secondInteger.IsZero(), 4}"); Console.WriteLine(); Console.WriteLine($"The sum of the first integer and the second integer: {firstInteger + secondInteger, 4}"); Console.WriteLine($"The difference of the first integer and the second integer: {firstInteger - secondInteger, 4}"); Console.WriteLine($"The product of the first integer and the second integer: {firstInteger * secondInteger, 4}"); Console.WriteLine($"The quotient of the first integer and the second integer: {firstInteger / secondInteger, 4}"); Console.WriteLine($"The remainder of the first integer and the second integer: {firstInteger % secondInteger,4}"); Console.WriteLine(); Console.WriteLine($"The sum of the second integer and the first integer: {secondInteger + firstInteger, 4}"); Console.WriteLine($"The difference of the second integer and the first integer: {secondInteger - firstInteger, 4}"); Console.WriteLine($"The product of the second integer and the first integer: {secondInteger * firstInteger, 4}"); Console.WriteLine($"The quotient of the second integer and the first integer: {secondInteger / firstInteger, 4}"); Console.WriteLine($"The remainder of the first integer and the second integer: {firstInteger % secondInteger,4}"); Console.WriteLine(); Console.WriteLine($"The first integer is equal to the second integer: {firstInteger == secondInteger,4}"); Console.WriteLine($"The first integer is not equal to the second integer: {firstInteger != secondInteger,4}"); Console.WriteLine($"The first integer is greater than the second integer: {firstInteger > secondInteger,4}"); Console.WriteLine($"The first integer is less than the second integer: {firstInteger < secondInteger,4}"); Console.WriteLine($"The first integer is greater than or equal to the second integer: {firstInteger >= secondInteger,4}"); Console.WriteLine($"The first integer is less than or equal to the second integer: {firstInteger <= secondInteger,4}"); Console.WriteLine(); Console.WriteLine($"The second integer is equal to the first integer: {secondInteger == firstInteger,4}"); Console.WriteLine($"The second integer is not equal to the first integer: {secondInteger != firstInteger,4}"); Console.WriteLine($"The second integer is greater than the first integer: {secondInteger > firstInteger,4}"); Console.WriteLine($"The second integer is less than the first integer: {secondInteger < firstInteger,4}"); Console.WriteLine($"The second integer is greater than or equal to the first integer: {secondInteger >= firstInteger,4}"); Console.WriteLine($"The second integer is less than or equal to the first integer: {secondInteger <= firstInteger,4}"); Console.WriteLine(); Console.Write($"Would you like to input values again? Yes(YES) or End(END). "); string decision = Console.ReadLine(); Console.WriteLine(); if (decision == "YES") { agreeToInputAgain = true; Console.WriteLine(); } else if (decision == "END") { agreeToInputAgain = false; Console.WriteLine(); } }while (agreeToInputAgain == true); }