private static LargeInteger MultiplyBy10(LargeInteger value) { char[] output = new char[value.ToString().Length + 1]; string str = ReverseStr(value.ToString()); for (int j = str.Length - 1; j >= 0; j--) { output[j + 1] = str[j]; } output[0] = '0'; return new LargeInteger(ReverseStr(new string(output))); }
private static LargeInteger positiveMultiplyPositive(LargeInteger int1, LargeInteger int2) { string str = ReverseStr(int2.ToString()); LargeInteger multiplicationCarry = null; LargeInteger result = new LargeInteger("0"); for (int j = 0; j < str.Length; j++) { int digit = Convert.ToInt32(str[j].ToString()); multiplicationCarry = MultiplyDigit(int1, digit); for (int k = 0; k < j; k++) { multiplicationCarry = MultiplyBy10(multiplicationCarry); } result = result + multiplicationCarry; } return result; }
private static LargeInteger bigMinusSmall(LargeInteger int1, LargeInteger int2) { char[] output = new char[Math.Max(int1.ToString().Length, int2.ToString().Length) + 1]; string str1 = ReverseStr(int1.ToString()); string str2 = ReverseStr(int2.ToString()); int length1 = str1.Length; int length2 = str2.Length; int maxLength = length1 > length2 ? length1 : length2; int carry = 0; for (int j = 0; j < maxLength; j++) { int digit1 = 0; if (j > 0 && j < length1 - 1) digit1 = Convert.ToInt32(str1[j].ToString()) + 10; else if (j > length1 - 1) digit1 = 0; else if (j == 0 || j == length1 - 1) digit1 = Convert.ToInt32(str1[j].ToString()); int digit2 = (j > length2 - 1) ? 0 : Convert.ToInt32(str2[j].ToString()); int digitsum = digit1 - digit2 + carry; if (digitsum >= 10) { digitsum -= 10; carry = 0; } else if (digitsum < 10 && digitsum >= 0) { carry = j > 0 ? -1 : 0; } else if (digitsum < 0) { digitsum = -digitsum; carry = -1; } output[j] = Convert.ToChar(Convert.ToString(digitsum)); } return new LargeInteger(ReverseStr(new string(output))); }
private static LargeInteger MultiplyDigit(LargeInteger number, int digit) { char[] output = new char[number.ToString().Length + 1]; int carry = 0; string str = ReverseStr(number.ToString()); int j = 0; for (j = 0; j < str.Length; j++) { int digit1 = Convert.ToInt32(str[j].ToString()); int digitProduct = digit1 * digit; digitProduct += carry; if (digitProduct >= 10) { carry = digitProduct / 10; digitProduct -= carry * 10; } else carry = 0; output[j] = Convert.ToChar(Convert.ToString(digitProduct)); } if (carry != 0) output[j++] = Convert.ToChar(Convert.ToString(carry)); return new LargeInteger(ReverseStr(new string(output))); }
private static LargeInteger positivePlusPositive(LargeInteger int1, LargeInteger int2) { char[] output = new char[Math.Max(int1.ToString().Length, int2.ToString().Length) + 1]; string str1 = ReverseStr(int1.ToString()); string str2 = ReverseStr(int2.ToString()); int length1 = str1.Length; int length2 = str2.Length; int maxLength = length1 > length2 ? length1 : length2; int carry = 0; int j = 0; for (j = 0; j < maxLength; j++) { int digit1 = (j > length1 - 1) ? 0 : Convert.ToInt32(str1[j].ToString()); int digit2 = (j > length2 - 1) ? 0 : Convert.ToInt32(str2[j].ToString()); int digitsum = digit1 + digit2 + carry; if (digitsum >= 10) { digitsum -= 10; carry = 1; } else carry = 0; output[j] = Convert.ToChar(Convert.ToString(digitsum)); } if (carry == 1) { output[j++] = '1'; } string str = ReverseStr(new string(output)); return new LargeInteger(str); }
public static string RunWithParameter(string option, string command) { Console.WriteLine("Now test input: " + command); string operation = string.Empty; if (option == "1") { operation = "addition"; } else if (option == "2") { operation = "subtraction"; } else if (option == "3") { operation = "multiplication"; } Console.WriteLine("Computation: " + operation); List <string> numbers = Utilities.ProcessCommand(command.Trim()); if (numbers.Count == 0) { Console.WriteLine("There is no invalid integers in the input command."); return(string.Empty); } else if (numbers.Count == 1) { Console.WriteLine("There is only one valid integer in the input command: " + numbers[0] + ", no calculation is needed."); return(numbers[0]); } else { string formula = string.Empty; LargeInteger result = new LargeInteger(numbers[0]); switch (option) { case "1": formula = string.Join(" + ", numbers.ToArray()); for (int i = 1; i < numbers.Count(); i++) { result += new LargeInteger(numbers[i]); } break; case "2": formula = string.Join(" - ", numbers.ToArray()); for (int i = 1; i < numbers.Count(); i++) { result -= new LargeInteger(numbers[i]); } break; case "3": formula = string.Join(" * ", numbers.ToArray()); for (int i = 1; i < numbers.Count(); i++) { result *= new LargeInteger(numbers[i]); } break; } Console.WriteLine("Formula: " + formula); Console.WriteLine("Result = " + result); Console.WriteLine("********* END OF THIS TEST CASE"); Console.WriteLine(); return(result.ToString()); } }
// Subtraction public static LargeInteger operator -(LargeInteger int1, LargeInteger int2) { // Step 1. positive/negative check for int1 and int2 string str1 = int1.ToString(); string str2 = int2.ToString(); LargeInteger trueInt1 = new LargeInteger(str1); LargeInteger trueInt2 = new LargeInteger(str2); // If the number is negative, remove "-" sign and handle the +/- sign manually if (str1[0] == '-' && str2[0] == '-') { trueInt1 = new LargeInteger(str2.Remove(0, 1).TrimStart('0')); trueInt2 = new LargeInteger(str1.Remove(0, 1).TrimStart('0')); if (string.IsNullOrEmpty(trueInt1.value)) trueInt1.value = "0"; if (string.IsNullOrEmpty(trueInt2.value)) trueInt2.value = "0"; } else if (str1[0] == '-' && str2[0] != '-') { trueInt1 = new LargeInteger(str1.Remove(0, 1).TrimStart('0')); LargeInteger intermidateResult = trueInt1 + trueInt2; return intermidateResult.value == "0" ? new LargeInteger("0") : new LargeInteger("-" + intermidateResult.ToString()); } else if (str1[0] != '-' && str2[0] == '-') { trueInt2 = new LargeInteger(str2.Remove(0, 1).TrimStart('0')); return trueInt1 + trueInt2; } // If it can reach here (both number are positive), proceed to step. 2 // Step 2. compare which is larger: int1 or int2, since method "bigMinusSmall" requries int1 to be larger, // switch order of subtraction and manually add negative sign if necessary bool int1Larger = true; int length1 = trueInt1.ToString().Length; int length2 = trueInt2.ToString().Length; // newInt1, newInt2 are used to compare which is larger LargeInteger newInt1 = trueInt1; LargeInteger newInt2 = trueInt2; if (length1 < length2) { string zeros = string.Empty; for (int i = 0; i < length2 - length1; i++) zeros += "0"; newInt1 = new LargeInteger(zeros + trueInt1.ToString()); } else if (length1 > length2) { string zeros = string.Empty; for (int i = 0; i < length1 - length2; i++) zeros += "0"; newInt2 = new LargeInteger(zeros + trueInt2.ToString()); } string strNewInt1 = newInt1.value; string strNewInt2 = newInt2.value; for (int i = 0; i < strNewInt1.Length; i++) { int number1 = Convert.ToInt32(strNewInt1[i].ToString()); int number2 = Convert.ToInt32(strNewInt2[i].ToString()); if (number2 < number1) { break; } else if (number2 > number1) { int1Larger = false; break; } } return int1Larger ? bigMinusSmall(trueInt1, trueInt2) : new LargeInteger("-" + bigMinusSmall(trueInt2, trueInt1)); }