/// <summary> /// This method multiplys two values given by user (this, other) /// </summary> /// <param name="other">Second value to multiply</param> /// <returns>Multiply object</returns> public override Number Calculate(Number num1, Number num2) { char[] a = num1.GetNumber(num1).ToCharArray(); char[] b = num2.GetNumber(num2).ToCharArray(); int[] c = new int[a.Length + b.Length]; int[] d = new int[a.Length + b.Length]; //Null object or empty array checking if (num2 is null || this is null || a.Length == 0 || b.Length == 0) { num2.AssignInputValue(""); return(num2); } for (int i = 0; i < b.Length; i++) { int bIndex = b.Length - 1 - i; char bitA, bitB; bitB = bIndex >= 0 ? b[bIndex] : '0'; if (bitB != '0' && i == 0) { for (int j = 0; j < c.Length - i; j++) { int aIndex = a.Length - 1 - j; bitA = aIndex >= 0 ? a[aIndex] : '0'; c[c.Length - j - 1 - i] = bitA == '0' ? 0 : 1; } } if (bitB == '0' && i != 0 && (a.Length > 1 || b.Length > 1)) { for (int j = 0; j < d.Length; j++) { d[j] = 0; } } if (bitB != '0' && i != 0 && (a.Length > 1 || b.Length > 1)) { for (int j = 0; j < i; j++) { d[d.Length - 1 - j] = 0; } for (int j = 0; j < d.Length - i; j++) { int aIndex = a.Length - 1 - j; bitA = aIndex >= 0 ? a[aIndex] : '0'; d[d.Length - j - 1 - i] = bitA == '0' ? 0 : 1; } } string s1 = string.Join("", c); string s2 = string.Join("", d); Number m1 = new Binary(s1); Number m2 = new Binary(s2); Operation ab = new AddOperation(); var m3 = ab.Calculate(m1, m2).ToString(); for (int k = 0; k < m3.Length; k++) { c[k] = m3[k] - '0'; } } bool first = true; var sb = new StringBuilder(); int countZeros = 0; for (int i = 0; i < c.Length; i++) { if (first) { if (c[i] != 0) { first = false; sb.Append((char)(c[i] + '0')); continue; } else { countZeros++; } } else { sb.Append((char)(c[i] + '0')); } if (countZeros == c.Length) { sb.Append((char)(c[i] + '0')); } } //Console.WriteLine("Multiply: " + sb.ToString()); if (((!num1.GetSign(num1) && !num2.GetSign(num2)) || (num1.GetSign(num1) && num2.GetSign(num2))) || (countZeros == c.Length)) { return(new Binary(sb.ToString())); } else { return(new Binary(sb.ToString(), true)); } }
/// <summary> /// This method subtract the two values given by user (this, other) /// </summary> /// <param name="other">Second value to Add</param> /// <returns>Add object</returns> public override Number Calculate(Number num1, Number num2) { char[] a = num1.GetNumber(num1).ToCharArray(); char[] b = num2.GetNumber(num2).ToCharArray(); //Null object or empty array checking if (num2 is null || this is null || a.Length == 0 || b.Length == 0) { num2.AssignInputValue(""); return(num2); } if ((num1.GetNumber(num1) == num2.GetNumber(num2)) && (num1.GetSign(num1) && num2.GetSign(num2))) { num2.AssignInputValue("0", false); return(num2); } bool flag = false; int len = a.Length > b.Length ? a.Length : b.Length; if (b.Length > a.Length) { flag = true; } else if (b.Length == a.Length) { for (int i = 0; i < a.Length; i++) { if (a[i] < b[i]) { flag = true; break; } else { flag = false; break; } } } else { flag = false; } a = num1.GetNumber(num1).PadLeft(b.Length, '0').ToCharArray(); b = num2.GetNumber(num2).PadLeft(a.Length, '0').ToCharArray(); if (flag) { for (int i = 0; i < len; i++) { char temp = a[i]; a[i] = b[i]; b[i] = temp; } } for (int i = 0; i < len; i++) { if (b[i] == '0') { b[i] = '1'; } else { b[i] = '0'; } } Number add1 = new Binary(new String(b)); Number add2 = new Binary("1"); Operation addSum1 = new AddOperation(); Number sum1 = addSum1.Calculate(add1, add2); Number add3 = new Binary(sum1.GetNumber(sum1)); Number add4 = new Binary(new String(a)); Operation addSum2 = new AddOperation(); Number sum2 = addSum2.Calculate(add3, add4); char[] c = sum2.GetNumber(sum2).ToCharArray(); if (c.Length > len) { for (int i = 0; i < len; i++) { c[i] = c[i + 1]; } c = c.Take(c.Length - 1).ToArray(); } String p = new String(c); //this is for when both are signed if (num1.GetSign(num1) && num2.GetSign(num2) && !flag) { num2.AssignInputValue(p, true); return(num2); } //this is for when this is signed and the other isn't if (num1.GetSign(num1) && !num2.GetSign(num2)) { Number c1 = new Binary(num1.GetNumber(num1)); Number c2 = new Binary(num2.GetNumber(num2)); Operation a1 = new AddOperation(); Number res = a1.Calculate(c1, c2); res.AssignInputValue(res.GetNumber(res), true); return(res); } //this is for when this is not signed and the other is signed if (!num1.GetSign(num1) && num2.GetSign(num2)) { Number c1 = new Binary(num1.GetNumber(num1)); Number c2 = new Binary(num2.GetNumber(num2)); Operation a1 = new AddOperation(); Number res = a1.Calculate(c1, c2); return(res); } if (flag && num1.GetSign(num1) && num2.GetSign(num2)) { return(new Binary(p)); } else if (flag) { return(new Binary(p, true)); } else { return(new Binary(p)); } }
/// <summary> /// This method divides the two values given by user (this, other) /// </summary> /// <param name="other">Second value to divide</param> /// <returns>Divide object</returns> public override Number Calculate(Number num1, Number num2) { char[] a = num1.GetNumber(num1).ToCharArray(); char[] b = num2.GetNumber(num2).ToCharArray(); bool flag1 = true, flag2 = true; int t = 0, countZeros = 0; //Null object or empty array checking if (num2 is null || this is null || a.Length == 0 || b.Length == 0) { throw new ArgumentNullException(); /*num2.AssignInputValue(""); * return num2;*/ } //Can't divide by zero for (int z = 0; z < b.Length; z++) { if (b[z] == '0') { countZeros++; } } if (countZeros == b.Length) { throw new DivideByZeroException(); } //Example for 2/7, return 0 if (a.Length < b.Length) { Console.Write("0"); return(new Binary("0")); } else { if (a.Length == b.Length) { for (int i = 0; i < a.Length; i++) { if (a[i] < b[i]) { flag1 = false; break; } } } //E.g, 5/7, return 0 if (!flag1) { Console.Write("0"); return(new Binary("0")); } char[] c = new char[a.Length]; string q1_first = "0", q = ""; //if a.length > b.length e.g., a= 1010101 b = 100, then create new array and add zeros in the beginning. if (a.Length > b.Length) { for (int i = 0; i < a.Length - b.Length; i++) { c[i] = '0'; } for (int i = a.Length - b.Length; i < a.Length; i++) { c[i] = b[t]; t++; } } else { for (int i = 0; i < b.Length; i++) { c[i] = b[i]; } } string s1 = new string(a); string s2 = new string(c); while (flag2) { Number n1 = new Binary(s1); Number n2 = new Binary(s2); Operation op = new SubOperation(); Number rem = op.Calculate(n1, n2); s1 = rem.GetNumber(rem); Number q1 = new Binary(q1_first); Number q2 = new Binary("1"); Operation op1 = new AddOperation(); Number quot = op1.Calculate(q1, q2); q1_first = quot.GetNumber(quot); char[] p1 = rem.GetNumber(rem).ToCharArray(); char[] p2 = s2.ToCharArray(); //check if num1 is greater than num2 for (int i = 0; i < a.Length; i++) { if (p1[i] > p2[i]) { flag2 = true; break; } else if (p1[i] < p2[i]) { flag2 = false; break; } } q = quot.GetNumber(quot); } Console.Write(q); if (((!num1.GetSign(num1) && !num2.GetSign(num2)) || (num1.GetSign(num1) && num2.GetSign(num2))) || (q == "0")) { return(new Binary(q)); } else { return(new Binary(q, true)); } } }