mirror of
https://github.com/ikoHSE/SolveAvdoshin.git
synced 2024-10-03 23:28:13 +03:00
commit
db408326df
40
.gitignore
vendored
Normal file
40
.gitignore
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
#Autosave files
|
||||
*~
|
||||
|
||||
#build
|
||||
[Oo]bj/
|
||||
[Bb]in/
|
||||
packages/
|
||||
TestResults/
|
||||
|
||||
# globs
|
||||
Makefile.in
|
||||
*.DS_Store
|
||||
*.sln.cache
|
||||
*.suo
|
||||
*.cache
|
||||
*.pidb
|
||||
*.userprefs
|
||||
*.usertasks
|
||||
config.log
|
||||
config.make
|
||||
config.status
|
||||
aclocal.m4
|
||||
install-sh
|
||||
autom4te.cache/
|
||||
*.user
|
||||
*.tar.gz
|
||||
tarballs/
|
||||
test-results/
|
||||
Thumbs.db
|
||||
|
||||
#Mac bundle stuff
|
||||
*.dmg
|
||||
*.app
|
||||
|
||||
#resharper
|
||||
*_Resharper.*
|
||||
*.Resharper
|
||||
|
||||
#dotCover
|
||||
*.dotCover
|
17
SolveAvdoshin.sln
Normal file
17
SolveAvdoshin.sln
Normal file
@ -0,0 +1,17 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2012
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SolveAvdoshin", "SolveAvdoshin\SolveAvdoshin.csproj", "{CD281751-5A98-4654-A7DB-1D5E1651B3D3}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{CD281751-5A98-4654-A7DB-1D5E1651B3D3}.Debug|x86.ActiveCfg = Debug|x86
|
||||
{CD281751-5A98-4654-A7DB-1D5E1651B3D3}.Debug|x86.Build.0 = Debug|x86
|
||||
{CD281751-5A98-4654-A7DB-1D5E1651B3D3}.Release|x86.ActiveCfg = Release|x86
|
||||
{CD281751-5A98-4654-A7DB-1D5E1651B3D3}.Release|x86.Build.0 = Release|x86
|
||||
EndGlobalSection
|
||||
EndGlobal
|
192
SolveAvdoshin/AndXorEquation.cs
Normal file
192
SolveAvdoshin/AndXorEquation.cs
Normal file
@ -0,0 +1,192 @@
|
||||
using System;
|
||||
|
||||
namespace SolveAvdoshin
|
||||
{
|
||||
public class SystemNonConsistentException : Exception
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class AndXorEquation
|
||||
{
|
||||
public static int[] ConvertToBinary(int n)
|
||||
{
|
||||
int[] result = new int[(int)Math.Log(n, 2) + 1];
|
||||
int lastIndex = 0;
|
||||
|
||||
while(n != 0) {
|
||||
result[lastIndex++] = n % 2;
|
||||
n /= 2;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int SolveEq(int[] coefs)
|
||||
{
|
||||
if(coefs.Length != 9)
|
||||
throw new ArgumentException("Input array shold be 9 elements long");
|
||||
|
||||
int[,] binaryCoefs = new int[8, 9];
|
||||
|
||||
for(int i = 0; i <= 8; i++) {
|
||||
int[] binary = ConvertToBinary(coefs[i]);
|
||||
|
||||
for(int j = 0; j < binary.Length; j++) {
|
||||
binaryCoefs[7 - j, i] = binary[j];
|
||||
}
|
||||
}
|
||||
|
||||
var matrix = new AndXorSystem(binaryCoefs);
|
||||
|
||||
try {
|
||||
return matrix.Solve();
|
||||
}
|
||||
catch(SystemNonConsistentException) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class AndXorSystem
|
||||
{
|
||||
int NumRows;
|
||||
int[,] Matrix;
|
||||
|
||||
public AndXorSystem(int[,] matrix)
|
||||
{
|
||||
if(matrix.GetLength(0) + 1 != matrix.GetLength(1))
|
||||
throw new ArgumentException("The array supplied isn't an Nx(N+1) matrix");
|
||||
|
||||
NumRows = matrix.GetLength(0);
|
||||
|
||||
for(int i = 0; i < NumRows; i++) {
|
||||
for(int j = 0; j < NumRows + 1; j++) {
|
||||
if(matrix[i, j] != 0 && matrix[i, j] != 1)
|
||||
throw new ArgumentException("The array supplied contains non-binary elements");
|
||||
}
|
||||
}
|
||||
|
||||
Matrix = matrix;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string result = "";
|
||||
|
||||
for(int i = 0; i < NumRows; i++) {
|
||||
result += "( ";
|
||||
|
||||
for(int j = 0; j < NumRows + 1; j++) {
|
||||
result += Matrix[i, j].ToString("D") + (j == 7 ? " | " : " ");
|
||||
}
|
||||
|
||||
result += ")\n";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void SwapRows(int a, int b)
|
||||
{
|
||||
if(a < 0 || a >= NumRows || b < 0 || b >= NumRows)
|
||||
throw new ArgumentOutOfRangeException();
|
||||
|
||||
for(int i = 0; i < NumRows + 1; i++) {
|
||||
int t = Matrix[a, i];
|
||||
Matrix[a, i] = Matrix[b, i];
|
||||
Matrix[b, i] = t;
|
||||
}
|
||||
}
|
||||
|
||||
void XorRowIntoAnother(int a, int b)
|
||||
{
|
||||
if(a < 0 || a >= NumRows || b < 0 || b >= NumRows)
|
||||
throw new ArgumentOutOfRangeException();
|
||||
|
||||
for(int i = 0; i < NumRows + 1; i++) {
|
||||
Matrix[b, i] ^= Matrix[a, i];
|
||||
}
|
||||
}
|
||||
|
||||
void MakeCanonical()
|
||||
{
|
||||
// Реализуется алгоритм Гаусса, как Чернышев завещал
|
||||
|
||||
int curRow = 0, curCol = 0;
|
||||
|
||||
while(curRow < NumRows && curCol < NumRows) {
|
||||
if(Matrix[curRow, curCol] != 0) {
|
||||
for(int i = 0; i < NumRows; i++) {
|
||||
if(i == curRow)
|
||||
continue;
|
||||
|
||||
if(Matrix[i, curCol] == 1) {
|
||||
XorRowIntoAnother(curRow, i);
|
||||
}
|
||||
}
|
||||
|
||||
curRow++;
|
||||
curCol++;
|
||||
}
|
||||
else {
|
||||
int nonZeroRow = curRow;
|
||||
|
||||
for(int i = curRow + 1; i < NumRows; i++) {
|
||||
if(Matrix[i, curCol] != 0) {
|
||||
nonZeroRow = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(nonZeroRow != curRow) {
|
||||
SwapRows(curRow, nonZeroRow);
|
||||
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
curCol++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool CheckConsistency()
|
||||
{
|
||||
for(int i = 0; i < NumRows; i++) {
|
||||
bool nonZeroRow = false;
|
||||
|
||||
for(int j = 0; j < NumRows; j++) {
|
||||
if(Matrix[i, j] != 0) {
|
||||
nonZeroRow = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!nonZeroRow && (Matrix[i, 8] != 0))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public int Solve()
|
||||
{
|
||||
MakeCanonical();
|
||||
|
||||
if(!CheckConsistency())
|
||||
throw new SystemNonConsistentException();
|
||||
|
||||
int result = 0, power = 1;
|
||||
for(int i = 0; i < 8; i++) {
|
||||
result += Matrix[7 - i, 8] * power;
|
||||
power *= 2;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
27
SolveAvdoshin/Properties/AssemblyInfo.cs
Normal file
27
SolveAvdoshin/Properties/AssemblyInfo.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
// Information about this assembly is defined by the following attributes.
|
||||
// Change them to the values specific to your project.
|
||||
|
||||
[assembly: AssemblyTitle("SolveAvdoshin")]
|
||||
[assembly: AssemblyDescription("Программа для решения домашки Авдошина за первый модуль")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("")]
|
||||
[assembly: AssemblyCopyright("frnkymac")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
|
||||
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
|
||||
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
|
||||
|
||||
[assembly: AssemblyVersion("1.0.*")]
|
||||
|
||||
// The following attributes are used to specify the signing key for the assembly,
|
||||
// if desired. See the Mono documentation for more information about signing.
|
||||
|
||||
//[assembly: AssemblyDelaySign(false)]
|
||||
//[assembly: AssemblyKeyFile("")]
|
||||
|
88
SolveAvdoshin/SolveAvdoshin.cs
Normal file
88
SolveAvdoshin/SolveAvdoshin.cs
Normal file
@ -0,0 +1,88 @@
|
||||
using System;
|
||||
|
||||
namespace SolveAvdoshin
|
||||
{
|
||||
public class SolveAvdoshin
|
||||
{
|
||||
static string PrintEquation(int[] coefs)
|
||||
{
|
||||
string result = "";
|
||||
|
||||
for(int i = 0; i < 9; i++) {
|
||||
result += i == 8 ? " = " : i == 0 ? "" : " ⨁ ";
|
||||
|
||||
if(coefs[i] == -1)
|
||||
result += "___";
|
||||
else
|
||||
result += coefs[i].ToString("D");
|
||||
|
||||
result += i == 8 ? "" : " x" + (7 - i).ToString("D");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int[] ConsoleInput()
|
||||
{
|
||||
return new int[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, }; // TODO: Sdielat
|
||||
}
|
||||
|
||||
static int[] ReadArgs(string[] args)
|
||||
{
|
||||
int[] coefs = new int[9];
|
||||
|
||||
if(args.Length == 0) {
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
else if(args.Length == 9) {
|
||||
|
||||
for(int i = 0; i < 9; i++) {
|
||||
coefs[i] = int.Parse(args[i]);
|
||||
|
||||
if(coefs[i] < 0 || coefs[i] > 255)
|
||||
throw new FormatException("Все аргументы должны быть целыми числами от 0 до 255");
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new FormatException("Аргументов то ли многовато, то ли маловато. Надо ровно 9");
|
||||
}
|
||||
|
||||
return coefs;
|
||||
}
|
||||
|
||||
public static void Solve(int[] coefs)
|
||||
{
|
||||
Console.WriteLine(PrintEquation(coefs));
|
||||
|
||||
int eqAnswer = AndXorEquation.SolveEq(coefs);
|
||||
|
||||
Console.WriteLine("\nОтвет: " + eqAnswer);
|
||||
}
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
int[] coefs = { -1, -1, -1, -1, -1, -1, -1, -1, -1, };
|
||||
|
||||
try {
|
||||
coefs = ReadArgs(args);
|
||||
|
||||
Solve(coefs);
|
||||
|
||||
throw new DivideByZeroException("Матибал");
|
||||
}
|
||||
catch(ArgumentNullException) {
|
||||
coefs = ConsoleInput();
|
||||
|
||||
Solve(coefs);
|
||||
}
|
||||
catch(FormatException e) {
|
||||
Console.WriteLine("Osheebka: " + e.Message);
|
||||
}
|
||||
catch(Exception e) { // TODO: Разобраться с экссепшенами
|
||||
Console.WriteLine("Непойманное исключение: " + e.Message);
|
||||
Console.WriteLine("\n" + e.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
41
SolveAvdoshin/SolveAvdoshin.csproj
Normal file
41
SolveAvdoshin/SolveAvdoshin.csproj
Normal file
@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
|
||||
<ProjectGuid>{CD281751-5A98-4654-A7DB-1D5E1651B3D3}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>SolveAvdoshin</RootNamespace>
|
||||
<AssemblyName>SolveAvdoshin</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug</OutputPath>
|
||||
<DefineConstants>DEBUG;</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Externalconsole>true</Externalconsole>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release</OutputPath>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Externalconsole>true</Externalconsole>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="AndXorEquation.cs" />
|
||||
<Compile Include="SolveAvdoshin.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user