diff --git a/SolveAvdoshin/AndXorEquation.cs b/SolveAvdoshin/AndXorEquation.cs index 6894af4..1ff779b 100644 --- a/SolveAvdoshin/AndXorEquation.cs +++ b/SolveAvdoshin/AndXorEquation.cs @@ -22,16 +22,28 @@ namespace SolveAvdoshin return result; } - public static int SolveEq(int[] coefs) + public static int SolveEq(int[] coefs, bool verbose = false) { if(coefs.Length != 9) throw new ArgumentException("Input array shold be 9 elements long"); int[,] binaryCoefs = new int[8, 9]; + if(verbose) + Console.WriteLine(""); + for(int i = 0; i <= 8; i++) { int[] binary = ConvertToBinary(coefs[i]); + if(verbose) { + Console.Write(coefs[i] + "_10 = "); + + for(int j = 7 - binary.Length + 1; j < 8; j++) + Console.Write(binary[7 - j]); + + Console.WriteLine("_2"); + } + for(int j = 0; j < binary.Length; j++) { binaryCoefs[7 - j, i] = binary[j]; } @@ -40,7 +52,7 @@ namespace SolveAvdoshin var matrix = new AndXorSystem(binaryCoefs); try { - return matrix.Solve(); + return matrix.Solve(verbose); } catch(SystemNonConsistentException) { return -1; @@ -109,12 +121,15 @@ namespace SolveAvdoshin } } - void MakeCanonical() + void MakeCanonical(bool verbose) { // Реализуется алгоритм Гаусса, как Чернышев завещал int curRow = 0, curCol = 0; + if(verbose) + Console.WriteLine("\n" + this); + while(curRow < NumRows && curCol < NumRows) { if(Matrix[curRow, curCol] != 0) { for(int i = 0; i < NumRows; i++) { @@ -123,9 +138,15 @@ namespace SolveAvdoshin if(Matrix[i, curCol] == 1) { XorRowIntoAnother(curRow, i); + + if(verbose) + Console.WriteLine("({0}) ⨁= ({1})", i, curRow); } } + if(verbose) + Console.WriteLine("\n" + this); + curRow++; curCol++; } @@ -142,6 +163,11 @@ namespace SolveAvdoshin if(nonZeroRow != curRow) { SwapRows(curRow, nonZeroRow); + if(verbose) { + Console.WriteLine("меняем местами {0} и {1} столбцы\n", curRow, nonZeroRow); + Console.WriteLine(this); + } + continue; } else { @@ -172,9 +198,9 @@ namespace SolveAvdoshin return true; } - public int Solve() + public int Solve(bool verbose = false) { - MakeCanonical(); + MakeCanonical(verbose); if(!CheckConsistency()) throw new SystemNonConsistentException(); diff --git a/SolveAvdoshin/SolveAvdoshin.cs b/SolveAvdoshin/SolveAvdoshin.cs index 4c9913d..3402b91 100644 --- a/SolveAvdoshin/SolveAvdoshin.cs +++ b/SolveAvdoshin/SolveAvdoshin.cs @@ -79,11 +79,11 @@ namespace SolveAvdoshin return coefs; } - static int Solve(int[] coefs) + static int Solve(int[] coefs, bool verbose) { Console.WriteLine(PrintEquation(coefs)); - int eqAnswer = AndXorEquation.SolveEq(coefs); + int eqAnswer = AndXorEquation.SolveEq(coefs, verbose); return eqAnswer; } @@ -228,6 +228,9 @@ namespace SolveAvdoshin static void PrintAnswers(int n, int a, int b, bool minOps, bool showBlocks, bool noLookBack) { + if(b == 1) + return; + var functions = new Action[] { BooleanFunctions.PrintDerivatives, BooleanFunctions.PrintExpressionsForDerivatives, @@ -282,9 +285,11 @@ namespace SolveAvdoshin goto case ExecutionMode.CommandLine; case ExecutionMode.CommandLine: - n = Solve(coefs); + Console.WriteLine("\n1.\n"); - Console.WriteLine("\n1.\n\nОтвет: " + n + "\n"); + n = Solve(coefs, verboseSystem); + + Console.WriteLine("\nОтвет: " + n + "\n"); break; case ExecutionMode.NoEquation: