コード例 #1
0
ファイル: Simplex_method.cs プロジェクト: Aleksandra06/AaCOM
        /// <summary>
        /// Поиск ведущего элемента
        /// </summary>
        private List <List <SimpleFractions> > NextTable(List <List <SimpleFractions> > table, List <int> basis, Tuple <int, int> search, List <SimpleFractions> co)
        {
            List <List <SimpleFractions> > newTable = new List <List <SimpleFractions> >();

            for (int i = 0; i < table.Count; i++)
            {
                newTable.Add(new List <SimpleFractions>());
                if (i == search.Item2)
                {
                    for (int j = 0; j < table[i].Count; j++)
                    {
                        newTable.LastOrDefault().Add(sFM.Division(table[i][j], table[search.Item2][search.Item1]));
                    }
                }
                else
                {
                    for (int j = 0; j < table[i].Count; j++)
                    {
                        newTable.LastOrDefault().Add(sFM.Difference(table[i][j], sFM.Division(sFM.Multiplication(table[search.Item2][j], table[i][search.Item1]), table[search.Item2][search.Item1])));
                    }
                }
            }
            basis[search.Item2] = search.Item1 - 1;
            return(newTable);
        }
コード例 #2
0
        private void PrintAnswer(/*List<Tuple<string, SimpleFractions>> answer*/ MatrixFractions matrix)
        {
            if (Notify == null)
            {
                return;
            }
            SimpleFractionsMeneger sfm = new SimpleFractionsMeneger();
            bool checkX;
            int  countX;

            for (int strNum = 0, colNum = 0; strNum < matrix.N || colNum < matrix.N; strNum++, colNum++)
            {
                checkX = false;
                countX = 0;
                for (int i = 0; i < matrix.M - 1; i++)
                {
                    if (matrix.Matrix[strNum, i].Numerator != 0)
                    {
                        if (colNum == i)
                        {
                            checkX = true; // обычный случай
                        }
                        countX++;
                    }
                }
                if (countX == 1 && checkX == true) // обычный случай
                {
                    SimpleFractions simpleAnswer = new SimpleFractions();
                    simpleAnswer = sfm.Division(matrix.Matrix[strNum, matrix.M - 1], matrix.Matrix[strNum, colNum]);
                    Notify($"x{colNum + 1} = {simpleAnswer.toString()}\n");
                }
                else if (countX == 0) // зануленная строка
                {
                    if (matrix.Matrix[strNum, matrix.M - 1].Numerator == 0)
                    {
                        Notify($"x{colNum + 1} - любое\n");
                    }
                    else
                    {
                        Notify($"Что-то пошло не так. Противоречие. Система не имеет решений\n");
                    }
                }
                else if (countX >= 1)//когда в строке остались еще не зануленные х
                {
                    int col = 0;
                    while (matrix.Matrix[strNum, col].Numerator == 0)
                    {
                        col++;
                        if (col >= matrix.M)
                        {
                            Notify($"Что-то пошло не так\n"); break;
                        }
                    }
                    string strAnswer = $"x{col + 1} = ";
                    if (matrix.Matrix[strNum, col].Numerator != matrix.Matrix[strNum, col].Denominator)
                    {
                        strAnswer += "( ";
                    }
                    if (matrix.Matrix[strNum, matrix.M - 1].Numerator != 0)
                    {
                        strAnswer += $" {matrix.Matrix[strNum, matrix.M - 1].toString()}";
                    }
                    int idCol = col + 1;
                    while (idCol < matrix.M - 1)
                    {
                        if (matrix.Matrix[strNum, idCol].Numerator != 0)
                        {
                            if (matrix.Matrix[strNum, idCol].Numerator == matrix.Matrix[strNum, idCol].Denominator)
                            {
                                strAnswer += $" - x{idCol + 1}";
                            }
                            else
                            {
                                strAnswer += $" - ({matrix.Matrix[strNum, idCol].toString()})*x{idCol + 1}";
                            }
                        }
                        idCol++;
                    }
                    if (matrix.Matrix[strNum, col].Numerator != matrix.Matrix[strNum, col].Denominator)
                    {
                        strAnswer += $" ) / ( {matrix.Matrix[strNum, col].toString()} )";
                    }
                    Notify($"{strAnswer}\n");
                    //string strAnswer = $"x{colNum + 1} = ";
                    //if (matrix.Matrix[strNum, colNum].Numerator != matrix.Matrix[strNum, colNum].Denominator)
                    //    strAnswer += "( ";
                    //if (matrix.Matrix[strNum, matrix.M - 1].Numerator != 0)
                    //    strAnswer += $" {matrix.Matrix[strNum, matrix.M - 1].toString()}";
                    //int idCol = matrix.N;
                    //while (idCol < matrix.M - 1)
                    //{
                    //    if (matrix.Matrix[strNum, idCol].Numerator != 0)
                    //    {
                    //        if (matrix.Matrix[strNum, idCol].Numerator == matrix.Matrix[strNum, idCol].Denominator)
                    //        {
                    //            strAnswer += $" - x{colNum + 1}";
                    //        }
                    //        else
                    //        {
                    //            strAnswer += $" - ({matrix.Matrix[strNum, idCol].toString()})*x{idCol + 1}";
                    //        }
                    //    }
                    //    idCol++;
                    //}

                    /*SimpleFractions simpleAnswer = new SimpleFractions();
                     * simpleAnswer = sfm.Division(matrix.Matrix[strNum, matrix.M - 1], matrix.Matrix[strNum, idCol]);
                     * strAnswer += $"{simpleAnswer.toString()}";*/

                    //if (matrix.Matrix[strNum, colNum].Numerator != matrix.Matrix[strNum, colNum].Denominator)
                    //    strAnswer += $" ) / ( {matrix.Matrix[strNum, colNum].toString()} )";
                    //Notify($"{strAnswer}\n");
                }
                else
                {
                    Notify($"Это не было предусмотрено(\n");
                }
            }
            //if (Notify == null) return;
            //foreach (var a in answer)
            //{
            //    if (a.Item2 != null)
            //        Notify($"{a.Item1} = {a.Item2.toString()}\n");
            //    else
            //        Notify($"{a.Item1} - любое\n");
            //}
        }