示例#1
0
        static void Main(string[] args)
        {
            RationalNumber number1          = new RationalNumber(3, 5);                // example for constructor
            RationalNumber numbGetByMethod1 = RationalNumber.GetObjectByString("2/5"); // examples for method that takes string and returns
            RationalNumber numbGetByMethod2 = RationalNumber.GetObjectByString("2.5"); // object (uses regular expressions)
            RationalNumber numbGetByMethod3 = RationalNumber.GetObjectByString("4/10");


            Console.WriteLine(numbGetByMethod1 > numbGetByMethod2); // examples for overloading operators
            Console.WriteLine(numbGetByMethod1 < numbGetByMethod2);
            Console.WriteLine(numbGetByMethod1 + numbGetByMethod2);


            Console.WriteLine(numbGetByMethod1.ToString("A")); // example for overloading ToString method.
            Console.WriteLine(numbGetByMethod2.ToString("B")); // it also takes parameter(A\B\C) to
            Console.WriteLine(numbGetByMethod1.ToString("C")); // display string in different formats


            if (numbGetByMethod1.Equals(numbGetByMethod3))      // example for overloading the interface of comparison
            {
                Console.WriteLine("numbers are equal");
            }


            int x = (int)numbGetByMethod2;                     // examples for explicit\implicit casting to other types

            Console.WriteLine(x);

            double y = (double)numbGetByMethod1;

            Console.WriteLine(y);

            RationalNumber number2 = 24;
            RationalNumber number5 = 2.4;

            Console.WriteLine(number5.ToString("A"));
        }
示例#2
0
        static void Main(string[] args)
        {
            RationalNumber a = new RationalNumber(15, 30);
            RationalNumber b = new RationalNumber(10, 8);
            RationalNumber c = new RationalNumber(8, 4);
            RationalNumber d = new RationalNumber(34, 17);
            RationalNumber[] rationalNumbers = new RationalNumber[] { a, b, c, d };

            foreach (RationalNumber i in rationalNumbers)
            {
                RationalNumber.reduceFraction(i);
            }

            Array.Sort(rationalNumbers);
            foreach (RationalNumber i in rationalNumbers)
            {
                RationalNumber.getType(i, "in decimal");
            }

            Console.WriteLine();

            Console.WriteLine(a > b);
            Console.WriteLine(d < c);
            Console.WriteLine(d == c);
            Console.WriteLine(b.Equals(d));
            Console.WriteLine(d.Equals(c));
            Console.WriteLine();

            RationalNumber rationalNumber = new RationalNumber();
            rationalNumber = a + b;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(rationalNumber, "with fractions");
            Console.WriteLine();
            rationalNumber = a - b;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(rationalNumber, "with fractions");
            Console.WriteLine();
            rationalNumber = a * b;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(rationalNumber, "with fractions");
            Console.WriteLine();
            rationalNumber = a / b;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(rationalNumber, "with fractions");
            Console.WriteLine();
            a++;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(a, "with fractions");
            Console.WriteLine();
            a--;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(a, "with fractions");
            Console.WriteLine();
            rationalNumber = a + 10;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(rationalNumber, "with fractions");
            Console.WriteLine();

            int buf1;
            double buf2;
            buf1 = a;
            Console.WriteLine(buf1);
            buf2 = (double)a;
            Console.WriteLine(buf2 + "\n");

            Console.WriteLine(a.ToString("in decimal"));
            Console.WriteLine(a.ToString("with fractions"));
            Console.WriteLine();

            RationalNumber num = new RationalNumber();
            string str = "99/17";
            num = (RationalNumber)str;
            RationalNumber.getType(num, "with fractions");
            Console.WriteLine();

            string inputStr = Console.ReadLine();
            string[] rationalNumbers1 = inputStr.Split(' ');
            List<RationalNumber> listNumbers = new List<RationalNumber>();
            RationalNumber.processStr(rationalNumbers1, listNumbers);
            foreach (RationalNumber i in listNumbers)
            {
                Console.WriteLine(i.ToString("in decimal"));
            }
        }