Example #1
0
        /// <summary>
        /// Performs the subtraction and prints a detailed solution.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="stepByStep">Indicates whether the operation should be performed in step-by-step mode controlled by user.</param>
        public static void Subtraction(double x, double y, bool stepByStep)
        {
            // Make the numbers whole.
            int  degree    = NumberWork.GetMostRoundingDegree(x, y);
            long xArg      = (long)(x * NumberWork.Pow(10, degree));
            long yArg      = (long)(y * NumberWork.Pow(10, degree));
            int  maxLength = NumberWork.GetMaxLength(xArg, yArg);

            if (NumberWork.NumLength(xArg) < NumberWork.NumLength(yArg))
            {
                Swap(ref xArg, ref yArg);
            }

            Console.Write(String.Join("", Enumerable.Repeat(' ', maxLength - NumberWork.NumLength(xArg))));
            Console.WriteLine($"   {xArg}");
            Console.Write(String.Join("", Enumerable.Repeat(' ', maxLength - NumberWork.NumLength(yArg))));
            Console.WriteLine($"   {yArg}");
            Console.WriteLine("   " + String.Join("", Enumerable.Repeat("-", maxLength)));

            long[] yComps = NumberWork.GetNumberComponents(yArg);

            Console.Write(String.Join("", Enumerable.Repeat(' ', 3 + maxLength - NumberWork.NumLength(xArg))));
            Console.WriteLine(xArg);

            // Subtract each digit (component) from x and write interim results.
            foreach (var comp in yComps)
            {
                if (stepByStep)
                {
                    Control.WaitForUserKey();
                }

                Console.Write(String.Join("", Enumerable.Repeat(' ', 3 + maxLength - NumberWork.NumLength(comp))));
                Console.WriteLine(comp);
                Console.WriteLine("   " + String.Join("", Enumerable.Repeat("-", maxLength)));
                Console.Write(String.Join("", Enumerable.Repeat(' ', 3 + maxLength - NumberWork.NumLength(xArg - comp))));
                Console.WriteLine(xArg - comp);

                xArg -= comp;
            }

            Console.WriteLine("   " + String.Join("", Enumerable.Repeat("-", maxLength)));
            Console.Write(String.Join("", Enumerable.Repeat(' ', 3 + maxLength - NumberWork.NumLength(xArg) - 1)));
            Console.WriteLine(xArg * Math.Pow(10, -degree));
        }
Example #2
0
        /// <summary>
        /// Performs the addition and prints a detailed solution.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="stepByStep">Indicates whether the operation should be performed in step-by-step mode controlled by user.</param>
        public static void Addition(double x, double y, bool stepByStep)
        {
            // Make the arguments whole.
            int  degree    = NumberWork.GetMostRoundingDegree(x, y);
            long xArg      = (long)(x * NumberWork.Pow(10, degree));
            long yArg      = (long)(y * NumberWork.Pow(10, degree));
            int  maxLength = NumberWork.GetMaxLength(xArg, yArg);

            if (NumberWork.NumLength(xArg) < NumberWork.NumLength(yArg))
            {
                Swap(ref xArg, ref yArg);
            }

            Console.WriteLine($"   {xArg}");
            Console.WriteLine($"   " + String.Join("", Enumerable.Repeat(" ", maxLength - NumberWork.NumLength(yArg))) + yArg);
            Console.WriteLine("   " + String.Join("", Enumerable.Repeat("-", maxLength)));

            // Get components of the digits (see GetNumberComponents documentstion).
            long[] xComps = NumberWork.GetNumberComponents(xArg);
            long[] yComps = NumberWork.GetNumberComponents(yArg);

            for (int i = 0; i < maxLength; i++)
            {
                if (stepByStep)
                {
                    Control.WaitForUserKey();
                }

                // Add two digits (components).
                var result    = xComps[i] + (i < yComps.Length ? yComps[i] : 0);
                var strResult = result.ToString();

                // Write the result of the addition.
                Console.Write(String.Join("", Enumerable.Repeat(' ', 3 + maxLength - strResult.Length)));
                Console.WriteLine(strResult);
            }

            // Write the shrinked result.
            Console.WriteLine("   " + String.Join("", Enumerable.Repeat("-", maxLength)));
            Console.WriteLine("  " + (xArg + yArg) * Math.Pow(10, -degree));
        }