Exemple #1
0
        /// <summary>
        /// Разность
        /// </summary>
        public SimpleFractions Difference(SimpleFractions A, SimpleFractions B)
        {
            if (A.Numerator == 0)
            {
                return(Multiplication(Norm(B), new SimpleFractions(-1, 1)));
            }
            else if (B.Numerator == 0)
            {
                return(Norm(A));
            }
            SimpleFractions otv = new SimpleFractions();
            SimpleFractions a = Norm(A), b = Norm(B);

            if (a.Denominator == b.Denominator)
            {
                otv.Denominator = a.Denominator;
                otv.Numerator   = a.Numerator - b.Numerator;
            }
            else
            {
                int nok = NOK(Convert.ToInt32(a.Denominator), Convert.ToInt32(b.Denominator));
                otv.Denominator = nok;
                otv.Numerator   = a.Numerator * (nok / a.Denominator) - b.Numerator * (nok / b.Denominator);
            }
            return(Norm(otv));
        }
Exemple #2
0
        /// <summary>
        /// Cокращение дроби
        /// </summary>
        public SimpleFractions Reduction(SimpleFractions simpleFractions)
        {
            SimpleFractions a = simpleFractions;

            if ((simpleFractions.Denominator < 0 && simpleFractions.Numerator >= 0) || (simpleFractions.Numerator < 0 && simpleFractions.Denominator < 0))
            {
                simpleFractions.Numerator   *= -1;
                simpleFractions.Denominator *= -1;
            }
            var nod = NOD(new List <int> {
                a.Numerator, a.Denominator
            });

            if (nod != 1)
            {
                a.Denominator /= nod;
                a.Numerator   /= nod;
            }
            //if (nod != 0)
            //{
            //    a.Denominator /= nod;
            //    a.Numerator /= nod;
            //    nod = NOD(a.Numerator, a.Denominator);
            //}
            return(a);
        }
Exemple #3
0
        /// <summary>
        /// Вывод ответа
        /// </summary>
        private List <SimpleFractions> PrintAnswer(List <List <SimpleFractions> > table, List <int> basis, List <SimpleFractions> F)
        {
            List <SimpleFractions> answer = new List <SimpleFractions>();
            string str = "";

            for (int j = 1; j < table[0].Count; j++)
            {
                str += "x" + j + " = ";
                var id = basis.FindIndex(a => a == j - 1);
                if (id >= 0)
                {
                    str += table[id][0].toString() + "\n";
                    answer.Add(table[id][0]);
                }
                else
                {
                    str += "0\n";
                    answer.Add(new SimpleFractions(0, 1));
                }
            }
            SimpleFractions fAnswer = new SimpleFractions(0, 1);

            for (int i = 0; i < answer.Count; i++)
            {
                fAnswer = sFM.Sum(fAnswer, sFM.Multiplication(answer[i], F[i]));
            }
            fAnswer = sFM.Sum(fAnswer, F[F.Count - 1]);
            answer.Add(fAnswer);
            str += "Z = " + fAnswer.toString();
            if (Notify != null)
            {
                Notify(str);
            }
            return(answer);
        }
Exemple #4
0
        /// <summary>
        /// Приведение дроби к нормальному виду (сокращение)
        /// </summary>
        public SimpleFractions Norm(SimpleFractions simpleFractions)
        {
            SimpleFractions fractions = simpleFractions;

            if (fractions.Numerator == 0)
            {
                fractions.Denominator = 1; return(fractions);
            }
            fractions = Reduction(fractions);
            if (NOD(new List <int> {
                fractions.Numerator, fractions.Denominator
            }) != 0)
            {
                int nod = NOD(new List <int> {
                    fractions.Numerator, fractions.Denominator
                });
                fractions.Numerator   /= nod;
                fractions.Denominator /= nod;
            }
            if (fractions.Denominator < 0)
            {
                fractions.Numerator   *= -1;
                fractions.Denominator *= -1;
            }
            return(fractions);
        }
Exemple #5
0
        /// <summary>
        /// Деление
        /// </summary>
        public SimpleFractions Division(SimpleFractions a, SimpleFractions b)
        {
            SimpleFractions otv = new SimpleFractions();

            otv.Denominator = a.Denominator * b.Numerator;
            otv.Numerator   = a.Numerator * b.Denominator;
            return(Norm(otv));
        }
Exemple #6
0
        /// <summary>
        /// Произведение
        /// </summary>
        public SimpleFractions Multiplication(SimpleFractions a, SimpleFractions b)
        {
            SimpleFractions otv = new SimpleFractions();

            otv.Denominator = a.Denominator * b.Denominator;
            otv.Numerator   = a.Numerator * b.Numerator;
            return(otv);
        }
Exemple #7
0
        /// <summary>
        /// Если a < b, то true, иначе false
        /// </summary>
        public bool Comparisons(SimpleFractions a, SimpleFractions b)
        {
            var c = Difference(b, a);

            if ((c.Numerator > 0 && c.Denominator > 0) || (c.Denominator < 0 && c.Numerator < 0))
            {
                return(true);
            }
            return(false);
        }
Exemple #8
0
        /// <summary>
        /// Модуль
        /// </summary>
        public SimpleFractions Mod(SimpleFractions fractions)
        {
            SimpleFractions f = new SimpleFractions();

            if (fractions.Numerator < 0)
            {
                f.Numerator = fractions.Numerator * -1;
            }
            if (fractions.Denominator < 0)
            {
                f.Denominator = fractions.Denominator * -1;
            }
            return(f);
        }
Exemple #9
0
        /// <summary>
        /// Максимальный элемент (первый попавшийся)
        /// </summary>
        public SimpleFractions Max(SimpleFractions a, SimpleFractions b)
        {
            SimpleFractions a1 = Norm(a);
            SimpleFractions b1 = Norm(b);

            if (Difference(a1, b1).Numerator < 0)
            {
                return(b1);
            }
            else
            {
                return(a1);
            }
        }
Exemple #10
0
        /// <summary>
        /// Модуль
        /// </summary>
        public SimpleFractions Abs(SimpleFractions fractions)
        {
            SimpleFractions simpleFractions = Norm(fractions);

            if (simpleFractions.Denominator < 0)
            {
                simpleFractions.Denominator *= -1;
            }
            if (simpleFractions.Numerator < 0)
            {
                simpleFractions.Numerator *= -1;
            }
            return(simpleFractions);
        }
Exemple #11
0
 public MatrixFractions(int[,] matrInt)
 {
     N      = matrInt.GetLength(0);
     M      = matrInt.GetLength(1);
     Matrix = new SimpleFractions[N, M];
     for (int i = 0; i < N; i++)
     {
         for (int j = 0; j < M; j++)
         {
             Matrix[i, j]             = new SimpleFractions();
             Matrix[i, j].Numerator   = matrInt[i, j];
             Matrix[i, j].Denominator = 1;
         }
     }
 }
Exemple #12
0
        private bool ObmenStrok(MatrixFractions matrix, int a, int b)
        {
            if (a >= matrix.N || b >= matrix.N)
            {
                return(false);
            }
            SimpleFractions tmp = new SimpleFractions();

            for (int i = 0; i < matrix.M; i++)
            {
                tmp = matrix.Matrix[a, i];
                matrix.Matrix[a, i] = matrix.Matrix[b, i];
                matrix.Matrix[b, i] = tmp;
            }
            return(true);
        }
Exemple #13
0
        /// <summary>
        /// Сумма
        /// </summary>
        public SimpleFractions Sum(SimpleFractions a, SimpleFractions b)
        {
            SimpleFractions otv = new SimpleFractions();

            if (a.Denominator == b.Denominator)
            {
                otv.Denominator = a.Denominator;
                otv.Numerator   = a.Numerator + b.Numerator;
            }
            else
            {
                int nok = NOK(Convert.ToInt32(a.Denominator), Convert.ToInt32(b.Denominator));
                otv.Denominator = nok;
                otv.Numerator   = a.Numerator * (nok / a.Denominator) + b.Numerator * (nok / b.Denominator);
            }
            return(Norm(otv));
        }
        /// <summary>
        /// Сложение строк. True - удачно, False - невозможно
        /// </summary>
        /// <param name="num1">Строка, к которой прибалять и в которую записывается ответ</param>
        /// <returns></returns>
        public bool LineDifference(MatrixFractions matrix, int num1, int num2, SimpleFractions koeff)
        {
            MatrixFractions matrixFractions = matrix;

            if (num1 > matrixFractions.N || num2 > matrixFractions.N || num1 < 0 || num2 < 0)
            {
                return(false);
            }
            SimpleFractionsMeneger sFM = new SimpleFractionsMeneger();

            for (int j = 0; j < matrixFractions.M; j++)
            {
                matrixFractions.Matrix[num1, j] = sFM.Sum(matrixFractions.Matrix[num1, j],
                                                          sFM.Multiplication(koeff, matrixFractions.Matrix[num2, j]));
            }
            matrix = matrixFractions;
            return(true);
        }
Exemple #15
0
        public bool RectangleMetod(MatrixFractions matrix)
        {
            List <int> banIndexStrList = new List <int>();

            if (matrix.M < matrix.N)
            {
                if (Notify != null)
                {
                    Notify($"Строк больше столбцов! Невозможно решить! (Код 1.0)\n");
                }
                return(false);
            }
            for (int nowStr = 0, nowCol = 0; nowStr < matrix.N || nowCol < matrix.N; nowStr++, nowCol++)
            {
                if (_mFM.Norm(matrix))
                {
                    if (Notify != null)
                    {
                        Notify("Сокращение:\n" + matrix.toString() + "\n");
                    }
                }
                //блок проверки
                var check = CheckExceptions(matrix);
                if (check == null)
                {
                    if (Notify != null)
                    {
                        Notify($"Ошибка! \n");
                    }
                    return(false);
                }
                if (check.SingleOrDefault() == -1)
                {
                    if (Notify != null)
                    {
                        Notify($"Противоречие! \n");
                    }
                    return(false);
                }
                if (check.Count > 0)
                {
                    foreach (var ch in check)
                    {
                        banIndexStrList.Add(ch);
                    }
                }
                if (Notify != null)
                {
                    Notify($"Пока что все идет по плану... наверное! \n");
                }
                //проверка банлиста
                int strNullCount = CheckBanlist(check, nowStr, matrix.N);
                if (strNullCount > 0)
                {
                    nowStr += strNullCount - 1;
                    if (nowStr >= matrix.N)
                    {
                        if (Notify != null)
                        {
                            Notify("Зануленная строка!\n" + matrix.toString() + "\n");
                        }
                    }
                }
                if (nowStr >= matrix.N)
                {
                    return(true);
                }
                //проверка на 0-вой элемент
                if (matrix.Matrix[nowStr, nowCol].Numerator == 0)
                {
                    int Str = nowStr;
                    Str++;
                    if (Str >= matrix.N)
                    {
                        if (Notify != null)
                        {
                            Notify($"Ведущий элемент 0, а строки закончились. Упс... \n");
                        }
                        return(true);
                    }
                    while (matrix.Matrix[Str, nowCol].Numerator == 0)
                    {
                        Str++;
                        if (Str >= matrix.N)
                        {
                            if (Notify != null)
                            {
                                Notify($"Ведущий элемент 0, а строки закончились. Упс... \n");
                            }
                            return(true);
                        }
                    }
                    ObmenStrok(matrix, Str, nowStr);
                    if (Notify != null)
                    {
                        Notify($"Обмен: ({nowStr} и {Str})\n"); Notify(matrix.toString() + "\n");
                    }
                }
                //приводим к 1
                if (matrix.Matrix[nowStr, nowCol].Numerator != matrix.Matrix[nowStr, nowCol].Denominator)
                {
                    SimpleFractions kof = matrix.Matrix[nowStr, nowCol];
                    for (int j1 = 0; j1 < matrix.M; j1++)
                    {
                        matrix.Matrix[nowStr, j1] = _sFM.Division(matrix.Matrix[nowStr, j1], kof);
                    }
                    if (Notify != null)
                    {
                        Notify($"Приведение: ({nowStr + 1}) : {kof.toString()} \n"); Notify(matrix.toString() + "\n");
                    }
                }
                //прямоугольники
                for (int j1 = nowStr + 1; j1 < matrix.M; j1++)
                {
                    for (int i1 = 0; i1 < matrix.N; i1++)
                    {
                        if (i1 != nowCol)
                        {
                            matrix.Matrix[i1, j1] =
                                _sFM.Difference(matrix.Matrix[i1, j1],
                                                _sFM.Multiplication(matrix.Matrix[nowStr, j1], matrix.Matrix[i1, nowCol]));
                        }
                    }
                }
                for (int i = 0; i < matrix.N; i++)
                {
                    if (i != nowStr)
                    {
                        matrix.Matrix[i, nowCol].Denominator = 1;
                        matrix.Matrix[i, nowCol].Numerator   = 0;
                    }
                }
                if (Notify != null)
                {
                    Notify("После прямоугольников: \n" + matrix.toString() + "\n");
                }
            }
            return(true);
        }