コード例 #1
0
        static void Main(string[] args)
        {
            Fractions fraction = new Fractions(1, 4);
            Fractions fraction1 = new Fractions(1, 5);
            double doubleFraction = 0.5;

            Console.WriteLine(fraction.ToString());
            Console.WriteLine(fraction.Equals(fraction1));

            Fractions sum = fraction + fraction1;
            Fractions odd = fraction - fraction1;
            Fractions mult = fraction * fraction1;
            Fractions devide = fraction / fraction1;
            Console.WriteLine(sum);
            Console.WriteLine(odd);
            Console.WriteLine(mult);
            Console.WriteLine(devide);

            double dSum = fraction + doubleFraction;
            double dOdd = fraction - doubleFraction;
            double dMult = fraction * doubleFraction;
            double dDevide = fraction / doubleFraction;
            Console.WriteLine(dSum);
            Console.WriteLine(dOdd);
            Console.WriteLine(dMult);
            Console.WriteLine(dDevide);
        }
コード例 #2
0
        public static Fractions operator *(Fractions first, Fractions second)
        {
            Fractions mult = new Fractions();

            mult.numerator = first.numerator * second.numerator;
            mult.denominator = first.denominator * second.denominator;

            return mult;
        }
コード例 #3
0
        public static Fractions operator +(Fractions first, Fractions second)
        {
            Fractions sum = new Fractions();
            int xFirst = first.numerator;
            int yFirst = first.denominator;
            int xSecond = second.numerator;
            int ySecond = second.denominator;

            int differenceFirst = 0;
            int differenceSecond = 0;
            int NOK = 0;

            if (yFirst == ySecond)
            {
                sum.numerator = xFirst + xSecond;
                sum.denominator = yFirst;

                return sum;
            }

            else
            {
                NOK = yFirst * ySecond;
                differenceFirst = NOK / yFirst;
                differenceSecond = NOK / ySecond;

                xFirst *= differenceFirst;
                yFirst *= differenceFirst;

                xSecond *= differenceSecond;
                ySecond *= differenceSecond;

                sum.numerator = xFirst + xSecond;
                sum.denominator = ySecond;

                return sum;
            }
        }
コード例 #4
0
        public static Fractions operator /(Fractions first, Fractions second)
        {
            Fractions devide = new Fractions();

            devide.numerator = first.numerator * second.denominator;
            devide.denominator = first.denominator * second.numerator;

            return devide;
        }