public void testAdd() { MyInt a = new MyInt(549); MyInt b = new MyInt(475); Assert.AreEqual(a.add(b).longValue(), 1024); MyInt c = new MyInt("10000003454"); Assert.AreEqual(b.add(c).ToString(), "10000003929"); MyInt d = new MyInt(-500); Assert.AreEqual(d.add(a).longValue(), 49); Assert.AreEqual(d.add(b).longValue(), -25); Assert.AreEqual((new MyInt(0)).add(new MyInt(-0)).longValue(), 0); MyInt e = new MyInt(-3554); Assert.AreEqual(c.add(e).ToString(), "9999999900"); Assert.AreEqual(e.add(d).longValue(), -4054); }
public MyInt divide(MyInt b) { if (b.compareTo(new MyInt(0))) { return(new MyInt(0)); } byte[] valueA = getValue(); byte[] valueB = b.getValue(); List <byte> result = new List <byte>(); bool isPositiveA = valueA[0] == 0; bool isPositiveB = valueB[0] == 0; valueA = valueA.Skip(1).ToArray(); valueB = valueB.Skip(1).ToArray(); int start = 0; int end = 1; while (end <= valueA.Length) { List <byte> tempAValue = valueA.Skip(start).Take(end - start).ToList();; tempAValue.Insert(0, 0); List <byte> tempBValue = valueB.ToList(); tempBValue.Insert(0, 0); MyInt tempA = new MyInt(tempAValue.ToArray()); MyInt tempB = new MyInt(tempBValue.ToArray()); if (tempA.max(tempB).compareTo(tempA)) { MyInt resultDivide = new MyInt(1); while (!tempB.multiply(resultDivide).min(tempA).compareTo(tempA)) { resultDivide = resultDivide.add(new MyInt(1)); } if (!resultDivide.compareTo(new MyInt(1))) { resultDivide = resultDivide.substract(new MyInt(1)); } result.AddRange(resultDivide.getValue().Skip(1)); MyInt resultMultiply = resultDivide.multiply(tempB); MyInt resultDivideRest = tempA.substract(resultMultiply); start = end - resultDivideRest.getValue().Length + 1; for (int i = 0; i + start < end; i++) { valueA[i + start] = resultDivideRest.getValue()[i + 1]; } } end++; } if (result.Count == 0) { result.Insert(0, 0); } result.Insert(0, (byte)(isPositiveA == isPositiveB ? 0 : 1)); return(new MyInt(result.ToArray())); }