mirror of
https://github.com/ikoHSE/SolveAvdoshin.git
synced 2024-11-24 05:33:07 +03:00
System solution output
This commit is contained in:
parent
496dbe373e
commit
2f1685546d
@ -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();
|
||||
|
@ -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<int>[] {
|
||||
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:
|
||||
|
Loading…
Reference in New Issue
Block a user