Пример #1
0
        static void Main(string[] args)
        {
            // Declare variants
            View view = new View(args);
            BigInteger bigInteger1 = new BigInteger();
            BigInteger bigInteger2 = new BigInteger();

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

            // Continue if having error
            if (view.IsHaveError(ref bigInteger1, ref bigInteger2))
            {
                // Do nothing
            }
            else
            {
                //Export result for user
                view.ExportResult(bigInteger1.StringInteger, bigInteger2.StringInteger, bigInteger1.Add2BigInteger(bigInteger2));
            }

            // Pause for debugging
            Console.Read();
        }
Пример #2
0
        /// <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 = (StringInteger.Length > bigInteger.StringInteger.Length)
                         ? bigInteger.StringInteger.Length
                         : StringInteger.Length;

            // Save temp for addition
            int temp = 0;

            for (int i = 0; i < length; ++i)
            {
                // sum of 2 number
                int sum = Int32.Parse(strInteger[i].ToString()) +
                            Int32.Parse(bigInteger.StringInteger[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 (StringInteger.Length > length)
            {
                for (int i = length; i < StringInteger.Length; ++i)
                {
                    int sum = Int32.Parse(strInteger[i].ToString()) + temp;
                    temp = sum / 10;
                    result.Insert(i, (sum % 10).ToString());
                }

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

                    // Add temp into result string
                    if (temp > 0)
                    {
                        result.Insert(bigInteger.StringInteger.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 result string
        }
Пример #3
0
        /// <summary>
        /// Check error for input
        /// </summary>
        /// <param name="bigInteger1">The first big number</param>
        /// <param name="bigInteger2">The second big number</param>
        /// <returns>The value number1 + number2</returns>
        public bool IsHaveError(ref BigInteger bigInteger1, ref BigInteger bigInteger2)
        {
            // Check syntax
            if (strInput.Length != 2)
            {
                Console.WriteLine("Sai cu phap! Cu phap: 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(strInput[0]))
                {
                    error = true; // Having error
                    Console.Write(strInput[0] + " ");
                }

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

                if (error)
                {
                    Console.WriteLine("Khong hop le!");
                    Console.WriteLine("*-----------------------------*");

                    return true; // Return having error
                }
            }
            Delete0InString(ref strInput[0]);
            bigInteger1.StringInteger = ReverseString(strInput[0]);
            Delete0InString(ref strInput[1]);
            bigInteger2.StringInteger = ReverseString(strInput[1]);

            return false; // None having error
        }