예제 #1
0
파일: Program.cs 프로젝트: morefun0302/WPC
        static void Main(string[] args)
        {
            while (true)
            {
                // Get the inputs from the user.
                Console.WriteLine("Calculate sum of fractions (Hit Return without entering to quit)");
                Console.WriteLine("e.g. for a=6 and b=7, output n will be 2, 3, 42");
                Console.WriteLine("i.e. 6/7 = 1/2 + 1/3 + 1/42");
                Console.WriteLine();

                Console.Write("Enter fraction numerator     a : ");
                string num = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(num)) break;

                Console.Write("Enter fraction denominator   b : ");
                string denom = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(denom)) break;
                Console.WriteLine();

                // Inputs are strings so we need to convert them into longs.
                long inNumerator;
                long inDenominator;

                if (long.TryParse(num, out inNumerator) && long.TryParse(denom, out inDenominator))
                {
                    if (inDenominator == 0)
                        Console.WriteLine("ERROR : Invalid denominator.");
                    else if (inNumerator >= inDenominator)
                        Console.WriteLine("ERROR : Fraction must be in the range 0 < fraction < 1.");
                    else
                    {
                        // These are used to test that the sum of the fractions found equals
                        // the input fraction.
                        double sumFractions = 0;
                        double inFraction = (double)inNumerator / (double)inDenominator;

                        // All set up so go get 'em Floyd!
                        Denominators denoms = new Denominators(inNumerator, inDenominator);
                        List<long> denominators = denoms.CalculateDenominators();

                        // Display them nicely formatted on one line.
                        Console.Write("Fractional denominators are  n : ");
                        for (int i = 0; i < denominators.Count; i++)
                        {
                            if (i + 1 != denominators.Count)
                                // All but the last value in the list.
                                Console.Write(denominators[i] + ", ");
                            else
                                // Last value in the list.
                                Console.WriteLine(denominators[i]);

                            // Add the fraction to the total so we can compare the final result against
                            // the input fraction.
                            sumFractions += 1 / (double)denominators[i];
                        }

                        // Display whether we've calculated the right fractions.
                        // The 2 values displayed should be the same.
                        Console.WriteLine();
                        Console.WriteLine("Input decimal value : {0}", inFraction);
                        Console.WriteLine("Sum of fractions    : {0}", sumFractions);
                    }
                }
                else
                {
                    Console.WriteLine("ERROR : Invalid values.");
                }
                Console.WriteLine();
                Console.WriteLine("----------------------------------------------------------------");
                Console.WriteLine();
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            while (true)
            {
                // Get the inputs from the user.
                Console.WriteLine("Calculate sum of fractions (Hit Return without entering to quit)");
                Console.WriteLine("e.g. for a=6 and b=7, output n will be 2, 3, 42");
                Console.WriteLine("i.e. 6/7 = 1/2 + 1/3 + 1/42");
                Console.WriteLine();

                Console.Write("Enter fraction numerator     a : ");
                string num = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(num))
                {
                    break;
                }

                Console.Write("Enter fraction denominator   b : ");
                string denom = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(denom))
                {
                    break;
                }
                Console.WriteLine();

                // Inputs are strings so we need to convert them into longs.
                long inNumerator;
                long inDenominator;

                if (long.TryParse(num, out inNumerator) && long.TryParse(denom, out inDenominator))
                {
                    if (inDenominator == 0)
                    {
                        Console.WriteLine("ERROR : Invalid denominator.");
                    }
                    else if (inNumerator >= inDenominator)
                    {
                        Console.WriteLine("ERROR : Fraction must be in the range 0 < fraction < 1.");
                    }
                    else
                    {
                        // These are used to test that the sum of the fractions found equals
                        // the input fraction.
                        double sumFractions = 0;
                        double inFraction   = (double)inNumerator / (double)inDenominator;

                        // All set up so go get 'em Floyd!
                        Denominators denoms       = new Denominators(inNumerator, inDenominator);
                        List <long>  denominators = denoms.CalculateDenominators();

                        // Display them nicely formatted on one line.
                        Console.Write("Fractional denominators are  n : ");
                        for (int i = 0; i < denominators.Count; i++)
                        {
                            if (i + 1 != denominators.Count)
                            {
                                // All but the last value in the list.
                                Console.Write(denominators[i] + ", ");
                            }
                            else
                            {
                                // Last value in the list.
                                Console.WriteLine(denominators[i]);
                            }

                            // Add the fraction to the total so we can compare the final result against
                            // the input fraction.
                            sumFractions += 1 / (double)denominators[i];
                        }

                        // Display whether we've calculated the right fractions.
                        // The 2 values displayed should be the same.
                        Console.WriteLine();
                        Console.WriteLine("Input decimal value : {0}", inFraction);
                        Console.WriteLine("Sum of fractions    : {0}", sumFractions);
                    }
                }
                else
                {
                    Console.WriteLine("ERROR : Invalid values.");
                }
                Console.WriteLine();
                Console.WriteLine("----------------------------------------------------------------");
                Console.WriteLine();
            }
        }