Ejemplo n.º 1
0
        static void Main()
        {
            Fraction num1 = null, num2 = null;
            bool     flag = false;

            while (!flag)
            {
                do
                {
                    Console.WriteLine("Enter first fraction\nExamples: 1/2; -0,5; 54");
                    flag = Fraction.TryParse(Console.ReadLine(), out num1);
                } while (!flag);

                do
                {
                    Console.WriteLine("Enter second fraction");
                    flag = Fraction.TryParse(Console.ReadLine(), out num2);
                } while (!flag);
                flag = true;
            }


            Console.WriteLine($"{num1} - {num2} = {num1 - num2} = {(double)(num1 - num2)}");

            Console.WriteLine($"{num1} + {num2} = {num1 + num2} = {(double)(num1 + num2)}");

            Console.WriteLine($"{num1} * {num2} = {num1 * num2} = {(double)(num1 * num2)}");

            Console.WriteLine($"{num1} / {num2} = {num1 / num2} = {(double)(num1 / num2)}");

            Console.WriteLine($"{num1} == {num2} ? {num1 == num2}");

            Console.WriteLine($"{num1} != {num2} ? {num1 != num2}");

            Console.WriteLine($"{num1} < {num2} ? {num1 < num2}");

            Console.WriteLine($"{num1} > {num2} ? {num1 > num2}");

            Console.WriteLine($"{num1} <= {num2} ? {num1 <= num2}");

            Console.WriteLine($"{num1} >= {num2} ? {num1 >= num2}");
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Fraction fract1 = new Fraction(3, 20);

            Console.WriteLine("The first fraction:");
            Console.WriteLine(fract1.ToString());
            Console.WriteLine(fract1.ToString("D"));
            Console.WriteLine(fract1.ToString("M"));
            Console.WriteLine(fract1.ToString("I"));

            Fraction fract2 = new Fraction(100, 7);

            Console.WriteLine("\nThe second fraction:");
            Console.WriteLine(fract2.ToString());
            Console.WriteLine(fract2.ToString("D"));
            Console.WriteLine(fract2.ToString("M"));
            Console.WriteLine(fract2.ToString("I"));

            Fraction fract3;

            fract3 = fract1 + fract2;
            Console.WriteLine($"\nSum of fractions is: " + fract3.ToString("M"));
            fract3 = fract1 - fract2;
            Console.WriteLine($"Subtraction of fractions is':  " + fract3.ToString("M"));
            fract3 = fract1 * fract2;
            Console.WriteLine($"Multiplication of fractions is:  " + fract3.ToString("M"));
            fract3 = fract1 / fract2;
            Console.WriteLine($"Division of fractions is: " + fract3.ToString("M"));

            Fraction fract4;

            if (Fraction.TryParse("-3(2/15)", out fract4))
            {
                Console.WriteLine($"\nResult of parsing -3(2/15): {fract4.ToString()}");
            }

            double number    = fract4;
            int    intNumber = (int)fract4;

            Console.WriteLine($"Cast of fr4 to double: {number}");
            Console.WriteLine($"Cast of fr4 to int: {intNumber}");
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Fraction fr = new Fraction(5, 15);

            fr.Show();
            Fraction fr1 = new Fraction(10, 30);

            fr1.Show();
            if (fr.CompareTo(fr1) == 0)
            {
                Console.WriteLine("== 0");
            }
            fr *= 3;
            fr.Show();
            fr /= 3;
            fr.Show();
            fr = -fr;
            fr.Show();
            fr = -fr;
            fr.Show();
            fr += fr1;
            fr.Show();
            Fraction.TryParse("55/66", out fr);
            fr.Show();
            Fraction.TryParse("55.77", out fr);
            fr.Show();
            Fraction.TryParse("55,88", out fr);
            fr.Show();
            Fraction.TryParse("55", out fr);
            fr.Show();
            Console.WriteLine(fr1.ToString("F", null));
            Console.WriteLine(fr1.ToString("F4", null));
            Fraction f1 = new Fraction(15, 65);
            Fraction f2 = new Fraction(44, 12);
            Fraction temp;

            temp = f1 * f2;
            Console.WriteLine($"f1 * f2 = {temp.GetDoubleType()}");
            temp = f1 / f2;
            Console.WriteLine($"f1 / f2 = {temp.GetDoubleType()}");
            temp = f1 + f2;
            Console.WriteLine($"f1 + f2 = {temp.GetDoubleType()}");
            temp = f1 - f2;
            Console.WriteLine($"f1 - f2 = {temp.GetDoubleType()}");
            Console.WriteLine($"f1 > f2 = {f1 > f2}");
            Console.WriteLine($"f1 < f2 = {f1 < f2}");
            Console.WriteLine($"f1 >= f2 = {f1 >= f2}");
            Console.WriteLine($"f1 <= f2 = {f1 <= f2}");
            Console.WriteLine($"f1 == f2 = {f1 == f2}");
            Console.WriteLine($"f1 != f2 = {f1 != f2}");
            Console.WriteLine("Testing string to object");
            temp = Fraction.Parse("350/100");
            Console.WriteLine($"testF = 350/150 = {temp.GetDoubleType()}");
            temp = Fraction.Parse("1000");
            Console.WriteLine($"testF = 1000 = {temp.GetDoubleType()}");
            temp = Fraction.Parse("350.100");
            Console.WriteLine($"testF = 350.150 = {temp.GetDoubleType()}");
            temp = Fraction.Parse("350,100");
            Console.WriteLine($"testF = 350,150 = {temp.GetDoubleType()}");
            Console.WriteLine("Testing conversion operators and IConvertible");
            Console.WriteLine($"f1.ToBoolean = {f1.ToBoolean(null)}");
            Console.WriteLine($"f1.GetTypeCode = {f1.GetTypeCode()}");
            Console.WriteLine($"f1.ToDouble = {f1.ToDouble(null)}");
            Console.WriteLine($"f1.ToInt32 = {f1.ToInt32(null)}");
            Console.WriteLine((short)fr);
            Console.WriteLine((int)fr);
            Console.WriteLine((long)fr);
        }