Ejemplo n.º 1
0
        public string Compute(IList <string> numbers)
        {
            if (numbers.Count != 2)
            {
                throw new Exception("Division needs 2 numbers. First divided by the Second. Numbers found:" + numbers.Count);
            }

            NumercStringUtils nsu = new NumercStringUtils();

            string n = numbers[0];
            string d = numbers[1];

            if (nsu.IsZeroString(n))
            {
                return("0");
            }
            else if (nsu.IsZeroString(d))
            {
                throw new DivideByZeroException();
            }

            if (n == d)
            {
                return("1");
            }

            // assumption n > d
            return(DoDivision(n, d));
        }
Ejemplo n.º 2
0
        private string DoDivision(string n, string d)
        {
            NumercStringUtils nsu = new NumercStringUtils();
            int    i  = d.Length;
            string ni = n.Substring(0, i);

            if (!nsu.OneGreaterThanTwo(ni, d))
            {
                ni = n.Substring(i, ++i);
            }

            for (int j = 1; j < 10; j++)
            {
            }

            if (nsu.OneGreaterThanTwo(n, d))
            {
                //answer > 1

                //if(n.Length - d.Length > 1)
                //{ Find}
            }
            else
            {
                //answer < 1
                // lets deal with it later
            }

            return("");
        }
Ejemplo n.º 3
0
        private bool ReorderLargerFirst(ref IList <string> numbers)
        {
            bool negateAnswer     = false;
            NumercStringUtils nsu = new NumercStringUtils();

            if (numbers[0].Length < numbers[1].Length ||
                !nsu.OneGreaterThanTwo(numbers[0], numbers[1]))
            {
                negateAnswer = true;
                // swap
                string t = numbers[0];
                numbers[0] = numbers[1];
                numbers[1] = t;
            }
            return(negateAnswer);
        }
Ejemplo n.º 4
0
        public string Compute(IList <string> numbers)
        {
            if (numbers.Count > 2)
            {
                throw new Exception("Incorrect count of numbers to subtract, expecting 2, found: " + numbers.Count.ToString());
            }

            StringMatrixTransformer smt = new StringMatrixTransformer();
            NumercStringUtils       nsu = new NumercStringUtils();

            bool negateAnswer           = ReorderLargerFirst(ref numbers);
            IList <IList <int> > matrix = smt.TransformStringListToReversedIntMatrix(numbers);

            StringBuilder sb = new StringBuilder();

            int i = 0;

            for (; i < matrix[1].Count; i++)
            {
                if (matrix[0][i] < matrix[1][i])
                {
                    SetupCarryAt(matrix[0], i);
                }
                sb.Append(matrix[0][i] - matrix[1][i]);
            }

            // get the rest if there's any left in the larger number
            for (; i < matrix[0].Count; i++)
            {
                sb.Append(matrix[0][i]);
            }

            return(nsu.TrimLeadingZeros(
                       negateAnswer ?
                       "-" + smt.ReverseString(sb.ToString()) :
                       smt.ReverseString(sb.ToString())));
        }