Пример #1
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);
        }
Пример #2
0
        static void Main(string[] args)
        {
            WriteLine("\t Test functions \n");

            Fraction fr1 = new Fraction(1, 5);
            Fraction fr2 = new Fraction(2, 8);
            Fraction fr3 = new Fraction(1, 4);
            bool     tmp = true;

            WriteLine("1. Math and comparison operations : \n");
            fr3 = fr1 + fr2;
            WriteLine("\t1/5 + 2/8 = {0}", fr3.ToString("div"));
            fr3 = fr2 - fr1;
            WriteLine("\t2/8 - 1/5 = {0}", fr3.ToString("div"));
            fr3 = fr2 * fr1;
            WriteLine("\t(2/8) * (1/5) = {0}", fr3.ToString("div"));
            fr3 = fr2 / fr1;
            WriteLine("\t(2/8) / (1/5) = {0}", fr3.ToString("div"));
            tmp = fr2 > fr1; // true
            WriteLine("\t(2/8) > (1/5) - {0}", tmp);
            tmp = fr2 < fr1; // false
            WriteLine("\t(2/8) < (1/5) - {0}", tmp);
            Fraction fr4 = new Fraction(1, 4);

            tmp = (fr4 == fr2); // true
            WriteLine("\t(1/4) == (2,8) = {0}", tmp);
            tmp = (fr4 != fr1); // true
            WriteLine("\t(1/4) != (1/5) = {0}", tmp);
            Fraction fr5 = new Fraction(4, 5);

            tmp = (fr5 >= fr1);
            WriteLine("\t(4/5) >= (1/5) = {0}", tmp);
            tmp = (fr5 <= fr1);
            WriteLine("\t(4/5) <= (1/5) = {0}", tmp);

            WriteLine("2. Object to string in diff. formats : \n");
            Fraction fr6 = new Fraction(10, 20);

            WriteLine("\t(10/20) = {0}", fr6.ToString());
            WriteLine("\t(10/20) = {0}", fr6.ToString("div"));
            Fraction fr7 = new Fraction(3, 8);

            WriteLine("\t(3/8) = {0}", fr7.ToString());
            WriteLine("\t(3/8) = {0}", fr7.ToString("div"));
            Fraction fr8 = new Fraction(1, 100);

            WriteLine("\t(1/100) = {0}", fr8.ToString());
            WriteLine("\t(1/100) = {0}", fr8.ToString("div"));

            WriteLine("3. String to object in diff. forms : \n");
            Fraction fr9  = Fraction.Parse("2.5");
            Fraction fr10 = Fraction.Parse("1/4");

            WriteLine("\tfrom 2.5 = {0}", fr9.ToString());
            WriteLine("\tfrom 1/4 = {0}", fr10.ToString("div"));

            WriteLine("4. compare and equal : \n");
            Random          rand    = new Random(DateTime.Now.Millisecond);
            List <Fraction> ourList = new List <Fraction>();

            for (int i = 0; i < 10; i++)
            {
                ourList.Add(new Fraction(rand.Next(5, 20), rand.Next(1, 20)));
            }

            WriteLine("\tBefore sort : ");
            for (int i = 0; i < 10; i++)
            {
                Write("({0}) ", ourList[i].ToString("div"));
            }
            WriteLine();

            ourList.Sort();
            WriteLine("\tAfter sort : ");
            for (int i = 0; i < 10; i++)
            {
                Write("({0}) ", ourList[i].ToString("div"));
            }

            Fraction fr11 = new Fraction(15, 6);
            Fraction fr12 = new Fraction(15, 6);

            tmp = fr11.Equals(fr12);
            WriteLine("\n(15/6) equals (15/6) = {0}\n", tmp);

            WriteLine("5. Try Fraction to (int) or (double) : \n");

            Fraction fr20 = new Fraction(4, 3);

            WriteLine("\t(int)4/3 = {0}", (int)fr20);
            WriteLine("\t(double)4/3 = {0}", (double)fr20);

            WriteLine("End.");

            ReadKey();
        }
Пример #3
0
        static void Main()
        {
            {
                var        rnd  = new Random();
                Fraction[] sort = new Fraction[13];
                Console.Write("Массив: ");
                for (int i = 0; i < 13; i++)
                {
                    try
                    {
                        sort[i] = new Fraction(rnd.Next(-42, 42), rnd.Next(-42, 42));
                    }
                    catch
                    {
                        sort[i] = new Fraction(rnd.Next(-42, 42));
                    }
                    Console.Write($"{sort[i]} ");
                }
                Console.WriteLine();
                Array.Sort(sort);
                Console.Write("Отсортированный массив: ");
                for (int i = 0; i < 13; i++)
                {
                    Console.Write($"{sort[i]} ");
                }
                Console.WriteLine();
                Console.WriteLine();
            }

            {
                Fraction a = new Fraction(3, 4);
                Fraction b = new Fraction(35, -42);
                Console.WriteLine($"3/4 = {a.Numerator}/{b.Denominator}");
                Console.WriteLine($"35/-42 = {b}");
                Console.WriteLine($"{a} + {b} = {a + b}");
                Console.WriteLine($"{a} - {b} = {a - b}");
                Console.WriteLine($"{a} * {b} = {a * b}");
                Console.WriteLine($"{a} / {b} = {a / b}");
                Console.WriteLine();
            }

            {
                decimal  t = 420.13M;
                Fraction x = t;
                Console.WriteLine($"{t} == {x.ToString()}");
                Console.WriteLine($"{t} == {x.ToString("float")}");
                Console.WriteLine($"{t} == {x.ToString("binary")}");
                Console.WriteLine($"{t} ~= {x.ToString("integer")}");
                Console.WriteLine();

                t = -13.42M;
                x = t;
                Console.WriteLine($"{t} == {x.ToString()}");
                Console.WriteLine($"{t} == {x.ToString("float")}");
                Console.WriteLine($"{t} == {x.ToString("binary")}");
                Console.WriteLine($"{t} ~= {x.ToString("integer")}");
                Console.WriteLine();

                t = 42M;
                x = t;
                Console.WriteLine($"{t} == {x.ToString()}");
                Console.WriteLine($"{t} == {x.ToString("float")}");
                Console.WriteLine($"{t} == {x.ToString("binary")}");
                Console.WriteLine($"{t} ~= {x.ToString("integer")}");
                Console.WriteLine();

                t = -13.5M;
                x = t;
                Console.WriteLine($"{t} == {x.ToString()}");
                Console.WriteLine($"{t} == {x.ToString("float")}");
                Console.WriteLine($"{t} == {x.ToString("binary")}");
                Console.WriteLine($"{t} ~= {x.ToString("integer")}");
                Console.WriteLine();
            }

            {
                Console.WriteLine($"-420/-7 = {Fraction.Parse("-420/-7")}");
                Console.WriteLine($"42 = {Fraction.Parse("42")}");
                Console.WriteLine($"13.42 = {Fraction.Parse("13.42")}");
                Console.WriteLine($"13,42 = {Fraction.Parse("13,42")}");
                Console.WriteLine();
            }

            {
                Fraction x = Fraction.Parse("1 / 7");
                Console.WriteLine($"x == {x} == {(int)x} == {(double)x} == {(decimal)x}");
                x++;
                Console.WriteLine($"x++ == {x} == {(int)x} == {(double)x} == {(decimal)x}");
            }
        }