Beispiel #1
0
        /// <summary>
        /// Check error for input
        /// </summary>
        /// <param name="_bigInteger1"></param>
        /// <param name="_bigInteger2"></param>
        /// <returns></returns>
        public bool IsHaveError(ref BigInteger _bigInteger1, ref BigInteger _bigInteger2)
        {
            // Check syntax
            if (m_string.Length != 2)
            {
                Console.WriteLine("Sai cu phap, Cu phap la : Add2N <SoThu1> <SoThu2>");
                Console.WriteLine("*-----------------------------*");

                return true; // Return having error
            }
            else
            {
                bool error = false; // Check error
                Regex myRegex = new Regex(@"^\d{1,}$"); // Condition

                // Check error for number 1
                if (!myRegex.IsMatch(m_string[0]))
                {
                    error = true; // Having error
                    Console.Write(m_string[0] + " ");
                }

                // Check error for number 1
                if (!myRegex.IsMatch(m_string[1]))
                {
                    error = true; // Having error
                    Console.Write(m_string[1] + " ");
                }

                if (error)
                {
                    Console.WriteLine("Chua hop le");
                    Console.WriteLine("******************");

                    return true; // Return having error
                }
            }

            Delete0InString(ref m_string[0]);
            _bigInteger1.BInteger = ReverseString(m_string[0]);
            Delete0InString(ref m_string[1]);
            _bigInteger2.BInteger = ReverseString(m_string[1]);

            return false; // None having error
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            View view = new View(args);
            BigInteger bigInteger1 = new BigInteger();
            BigInteger bigInteger2 = new BigInteger();

            Console.WriteLine("*++++++++++++++++++++*");

            // Continue if have error
            if (view.IsHaveError(ref bigInteger1, ref bigInteger2))
            {
                return; // Exit
            }
            else
            {
                //Print result
                view.PrintResult(bigInteger1.BInteger, bigInteger2.BInteger, bigInteger1.Add2BigInteger(bigInteger2));
            }
        }
        /// <summary>
        /// Add 2 big integer number 
        /// </summary>
        /// <param name="_bigInteger"> Number will be added</param>
        /// <returns> result </returns>
        public string Add2BigInteger(BigInteger _bigInteger)
        {
            StringBuilder result = new StringBuilder(); // Result of addition
            // Return string is most shortest
            int length = (BInteger.Length > _bigInteger.BInteger.Length)
                         ? _bigInteger.BInteger.Length
                         : BInteger.Length;
            // Save temp for addition
            int temp = 0;

            for (int i = 0; i < length; ++i)
            {
                // sum  2 number
                int sum = Int32.Parse(m_bInteger[i].ToString()) +
                            Int32.Parse(_bigInteger.BInteger[i].ToString()) +
                            temp;

                temp = sum / 10; // temp of addition

                result.Insert(i, (sum % 10).ToString()); // Insert sum into result string
            }

            // Continue insert remain of longer string
            if (BInteger.Length > length)
            {
                for (int i = length; i < BInteger.Length; ++i)
                {
                    int sum = Int32.Parse(m_bInteger[i].ToString()) + temp;
                    temp = sum / 10;
                    result.Insert(i, (sum % 10).ToString());
                }
                // Add temp into result string
                if (temp > 0)
                {
                    result.Insert(BInteger.Length, temp.ToString());
                }
            }
            else
            {
                // Continue insert remain of longer string
                if (_bigInteger.BInteger.Length > length)
                {
                    for (int i = length; i < _bigInteger.BInteger.Length; ++i)
                    {
                        int sum = Int32.Parse(_bigInteger.BInteger[i].ToString()) + temp;
                        temp = sum / 10;
                        result.Insert(i, (sum % 10).ToString());
                    }
                    // Add temp into result string
                    if (temp > 0)
                    {
                        result.Insert(_bigInteger.BInteger.Length, temp.ToString());
                    }
                }
                else // 2 string's length equal
                {
                    // Add temp into result string
                    if (temp > 0)
                    {
                        result.Insert(length, temp.ToString());
                    }
                }
            }

            return result.ToString(); // return string result
        }