Esempio n. 1
0
 public static Fraction operator *(Fraction ob1, Fraction ob2)
 {
     Fraction res = new Fraction();
     res.numerator = ob1.sign * ob2.sign * (ob1.intPart * ob1.denominator + ob1.numerator) * (ob2.intPart * ob2.denominator + ob2.numerator);
     res.denominator = ob1.denominator * ob2.denominator;
     if (res.numerator < 0)
     {
         res.numerator *= -1;
         res.sign = -1;
     }
     res.GetMixedView();
     return res;
 }
Esempio n. 2
0
 public static Fraction operator /(int a, Fraction ob1)
 {
     Fraction res = new Fraction();
     Fraction ob2 = new Fraction(0, 1, Math.Abs(a), a / Math.Abs(a));
     res = ob2 / ob1;
     res.GetMixedView();
     return res;
 }
Esempio n. 3
0
        public static Fraction Parse(string str)
        {
            int intPart, numerator, denominator, sign;
            // разделение строки на подстроки
            // с помощью разделителя-пробела
            string [] strs=str.Split(' ');
            string[] strs1;
            Fraction res;
            if (strs.Length == 1)
            {
                // в строке не было найдено пробелов
                // производим разделение строки по символу ‘/’
                strs1 = str.Split('/');
                if (strs1.Length == 1)
                {
                    // число задано в виде только целой части
                    // выделяем целую часть
                    intPart = int.Parse(strs1[0]);
                    // в зависимости от значения целой части,
                    // формируем новую дробь
                    if (intPart!=0)

                        res = new Fraction(0, 1, Math.Abs(intPart),
                        intPart / Math.Abs(intPart));
                    else
                        res = new Fraction(0, 1, Math.Abs(intPart), 1);
                    return res;
                }
                else
                {
                    // число задано в виде только дробной части
                    // выделяем отдельно числитель и знаменатель
                    numerator = int.Parse(strs1[0]);
                    denominator = int.Parse(strs1[1]);
                    sign = 1;
                    // определяем знак числа по знаку числителя
                    if (numerator < 0)
                    {
                        numerator = -numerator;
                        sign = -1;
                    }
                    // формируем новую дробь и приводим ее
                    // к несократимому виду
                    res = new Fraction(numerator, denominator, 0, sign);
                    res.GetMixedView();
                    return res;
                }
            }
            // дробь задана в смешанном виде
            // отделяем дробную часть по разделителю ‘/’
            strs1 = strs[1].Split('/');
            intPart = int.Parse(strs[0]);
            // определяем знак числа по знаку целой части
            if (intPart < 0)
            {
                intPart = -intPart;
                sign = -1;
            }
            else
                sign = 1;
            numerator = int.Parse(strs1[0]);
            denominator = int.Parse(strs1[1]);
            // формируем новую дробь и приводим ее
            // к несократимому виду
            res = new Fraction(numerator, denominator, intPart, sign);
            res.GetMixedView();
            return res;
        }
Esempio n. 4
0
 public static Fraction operator /(Fraction ob1, Fraction ob2)
 {
     Fraction res = new Fraction();
     int den = ob2.denominator;
     int num = (ob2.intPart * ob2.denominator + ob2.numerator);
     ob2.numerator = den;
     ob2.denominator = num;
     ob2.intPart = 0;
     res = ob1 * ob2;
     res.GetMixedView();
     return res;
 }
Esempio n. 5
0
 public static Fraction operator -(int a, Fraction ob1)
 {
     if (a == 0)
         return new Fraction(ob1.numerator, ob1.denominator, ob1.intPart, ob1.sign);
     Fraction ob2 = new Fraction(0, 1, Math.Abs(a), a / Math.Abs(a));
     Fraction res = ob2 - ob1;
     res.GetMixedView();
     return res;
 }
Esempio n. 6
0
 public static Fraction operator +(double a, Fraction ob1)
 {
     if (a == 0)
         return new Fraction(ob1.numerator, ob1.denominator, ob1.intPart, ob1.sign);
     int intPart = Convert.ToInt32(Math.Floor(Math.Abs(a)));
     double flPart = a - intPart;
     int den = 1;
     while ((flPart - Convert.ToInt32(Math.Floor(Math.Abs(flPart)))) > 0)
     {
         flPart *= 10;
         den *= 10;
     }
     Fraction ob2 = new Fraction(
             Convert.ToInt32(flPart),
             den,
             intPart,
             Convert.ToInt32(a / Math.Abs(a))
     );
     return ob1 + ob2;
 }
Esempio n. 7
0
 public static Fraction operator +(int a, Fraction ob1)
 {
     // если к дроби прибавляется число, равное 0,
     // результат совпадает с операндом-дробью
     if (a == 0)
         return new Fraction(ob1.numerator, ob1.denominator,
         ob1.intPart, ob1.sign);
     // создание новой дроби ob2 = a
     Fraction ob2 = new Fraction(0, 1, Math.Abs(a), a / Math.Abs(a));
     Fraction res = ob1 + ob2; //сложение двух дробей
     return res;
 }
Esempio n. 8
0
 public static Fraction operator *(int a, Fraction ob1)
 {
     Fraction res = new Fraction();
     Fraction ob2 = new Fraction(0, 1, Math.Abs(a), a / Math.Abs(a));
     res.numerator = ob1.sign * ob2.sign * (ob1.numerator * ob2.intPart);
     res.denominator = ob1.denominator;
     if (res.numerator < 0)
     {
         res.numerator *= -1;
         res.sign = -1;
     }
     res.GetMixedView();
     return res;
 }