static void Main(string[] args) { while (true) { string a = Console.ReadLine(); string b = Console.ReadLine(); StringInt strint = new StringInt(a); //strint.Add(b); //strint.Subtract(b); strint.Multiply(b); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(strint.ToString()); Console.ResetColor(); } Console.ReadKey(true); }
public void Multiply(string factor) { //Console.WriteLine("Called multiply with argument " + factor + " and value " + this.GetValue()); if (factor[0] == '-' ^ this.isNegative) { this.isNegative = true; } else { this.isNegative = false; } //Make sure the second factor (stored in variable factor) is shorter if (this.value.Length < factor.Length) { string temp = this.value; this.value = factor.TrimStart('-'); factor = temp; } factor = factor.TrimStart('-'); string num = this.value; int factorLength = factor.Length; short c = 0; string[] partResults = new string[factorLength]; for (int i = 1; i <= factorLength; i++) { short b = 0; string revertedResult = String.Empty; try { b = Convert.ToInt16(factor[factor.Length - i].ToString()); } catch (IndexOutOfRangeException) { } for (int j = 1; j <= num.Length + 1; j++) { short a = 0; try { a = Convert.ToInt16(num[num.Length - j].ToString()); } catch (IndexOutOfRangeException) { } int result = (Convert.ToInt16(a) * Convert.ToInt16(b) + c); c = 0; if (result >= 10) { c = (short)(result / 10); result %= 10; } revertedResult += result; } //Revert string char[] resultCharArr = revertedResult.ToCharArray(); Array.Reverse(resultCharArr); //Add extra zeros to the end and save it as part result string suffix = String.Empty; for (int j = 0; j < i - 1; j++) { suffix += "0"; } partResults[i - 1] = new string(resultCharArr).TrimStart('0') + suffix; } //Add all part results together StringInt sum = new StringInt("0"); for (int i = 0; i < factorLength; i++) { if (partResults[i].Length == 0) { partResults[i] = "0"; } sum.Add(partResults[i]); } this.value = sum.GetValue().TrimStart('-'); }
public bool SmallerThan(string compare) { StringInt reversed = new StringInt(compare); return(reversed.GreaterThan(this.value)); }
public void Subtract(string reducer) { //Console.WriteLine("Called subtract with argument " + reducer + " and value " + this.GetValue()); if (!this.isNegative) { if (reducer[0] != '-') { //(+) - (+) //Don't change anything } else { //(+) - (-) reducer = reducer.TrimStart('-'); this.Add(reducer); return; } } else { if (reducer[0] != '-') { //(-) - (+) this.isNegative = false; this.Add(reducer); this.isNegative = true; return; } else { //(-) - (-) //Don't change anything } } //Make sure the first number is greater than the reducer StringInt temp = new StringInt(reducer.TrimStart('-')); if (temp.GreaterThan(this.value.TrimStart('-'))) { string tempVal = this.GetValue(); this.Set(reducer); reducer = tempVal; this.isNegative = !this.isNegative; } reducer = reducer.TrimStart('-'); int numLength = (reducer.Length > this.value.Length) ? reducer.Length : this.value.Length; numLength++; string num = this.value; short c = 0; string revertedResult = String.Empty; for (int i = 1; i <= numLength; i++) { short a = 0, b = 0; try { a = Convert.ToInt16(num[num.Length - i].ToString()); } catch (IndexOutOfRangeException) { } try { b = Convert.ToInt16(reducer[reducer.Length - i].ToString()); } catch (IndexOutOfRangeException) { } int result = Convert.ToInt16(a) - (Convert.ToInt16(b) + c); c = 0; if (result < 0) { c++; result += 10; } revertedResult += result.ToString(); } //Revert string char[] resultCharArr = revertedResult.ToCharArray(); Array.Reverse(resultCharArr); this.value = new string(resultCharArr).TrimStart('0'); if (this.value.Length == 0) { this.Set("0"); } }