Ejemplo n.º 1
0
        /// <summary>
        /// operator + overloaded
        /// addtion of 2 bignumber can use + now
        /// write the oprands and result to the screen
        /// return a sum bignumber
        /// </summary>
        /// <param name="op1">LHS oprand</param>
        /// <param name="op2">RHS oprand</param>
        /// <returns>sum bignumber</returns>
        public static BigNumber operator +(BigNumber op1, BigNumber op2)
        {
            if (op1.IsEmpty() && op2.IsEmpty())
            {
                return(null);
            }
            else if (op1.IsEmpty())
            {
                return(op2);
            }
            else if (op2.IsEmpty())
            {
                return(op1);
            }
            else
            {
                int sum;
                int carry    = 0;
                var current1 = op1.First;
                var current2 = op2.First;
                var result   = new BigNumber();

                //if any of those 2 still have digits, looping
                while (current1 != null || current2 != null)
                {
                    //reset sum to 0 every turn of loop
                    //take the carry value from last turn
                    sum = 0;

                    //if available add current value to the sum
                    //Char.GetNumericValue returns int of char's literal value
                    //thus it does not need to go through ASCII conversion
                    if (current1 != null)
                    {
                        sum     += (int)Char.GetNumericValue(current1.Value);
                        current1 = current1.Next;
                    }

                    if (current2 != null)
                    {
                        sum     += (int)Char.GetNumericValue(current2.Value);
                        current2 = current2.Next;
                    }

                    //add carry to sum, last turn carry now expired
                    sum += carry;

                    //new carry and new sum
                    carry = sum / 10;
                    sum   = sum % 10;

                    //add sum to the list
                    //sum must add 48 according to ASCII table to make a char's literal is this sum
                    //AddLast because need to make sure lower digits are at LHS of the list
                    result.AddLast((char)(sum + 48));
                }

                //if carry is not zero, and a new node in the list represent 1
                if (carry == 1)
                {
                    result.AddLast((char)49);
                }

                //print result to screen
                op1.Write();
                Console.WriteLine("+");
                op2.Write();
                Console.WriteLine("=");
                result.Write();

                return(result);
            }
        }