示例#1
0
        public void testMultiply()
        {
            MyInt a = new MyInt(456);
            MyInt b = new MyInt(4356);
            MyInt c = new MyInt(-5493);
            MyInt d = new MyInt(-3205);

            Assert.AreEqual(a.multiply(b).longValue(), 1986336);
            Assert.AreEqual(b.multiply(c).longValue(), -23927508);
            Assert.AreEqual(c.multiply(d).longValue(), 17605065);
        }
示例#2
0
        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()));
        }