git.iko.soy/ikoHSE/SolveAvdoshin
on master help sign in
Initial commit

Solves equations (#1) w/o solution outputt

commitdb408326dfc1f18febec9ba8c5523411bd790e8f authorIlya Gulkov <ifranked@gmail.com> dateWed 23 Sep 2015, 13:34 (11 years ago)
6 files changed+405
.gitignore+40 SolveAvdoshin.sln+17 SolveAvdoshin/AndXorEquation.cs+192 SolveAvdoshin/Properties/AssemblyInfo.cs+27 SolveAvdoshin/SolveAvdoshin.cs+88 SolveAvdoshin/SolveAvdoshin.csproj+41
.gitignorenew file+40
@@ -0,0 +1,40 @@
1+#Autosave files
2+*~
3+
4+#build
5+[Oo]bj/
6+[Bb]in/
7+packages/
8+TestResults/
9+
10+# globs
11+Makefile.in
12+*.DS_Store
13+*.sln.cache
14+*.suo
15+*.cache
16+*.pidb
17+*.userprefs
18+*.usertasks
19+config.log
20+config.make
21+config.status
22+aclocal.m4
23+install-sh
24+autom4te.cache/
25+*.user
26+*.tar.gz
27+tarballs/
28+test-results/
29+Thumbs.db
30+
31+#Mac bundle stuff
32+*.dmg
33+*.app
34+
35+#resharper
36+*_Resharper.*
37+*.Resharper
38+
39+#dotCover
40+*.dotCover
SolveAvdoshin.slnnew file+17
@@ -0,0 +1,17 @@
1+
2+Microsoft Visual Studio Solution File, Format Version 12.00
3+# Visual Studio 2012
4+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SolveAvdoshin", "SolveAvdoshin\SolveAvdoshin.csproj", "{CD281751-5A98-4654-A7DB-1D5E1651B3D3}"
5+EndProject
6+Global
7+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+ Debug|x86 = Debug|x86
9+ Release|x86 = Release|x86
10+ EndGlobalSection
11+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+ {CD281751-5A98-4654-A7DB-1D5E1651B3D3}.Debug|x86.ActiveCfg = Debug|x86
13+ {CD281751-5A98-4654-A7DB-1D5E1651B3D3}.Debug|x86.Build.0 = Debug|x86
14+ {CD281751-5A98-4654-A7DB-1D5E1651B3D3}.Release|x86.ActiveCfg = Release|x86
15+ {CD281751-5A98-4654-A7DB-1D5E1651B3D3}.Release|x86.Build.0 = Release|x86
16+ EndGlobalSection
17+EndGlobal
SolveAvdoshin/AndXorEquation.csnew file+192
@@ -0,0 +1,192 @@
1+using System;
2+
3+namespace SolveAvdoshin
4+{
5+ public class SystemNonConsistentException : Exception
6+ {
7+
8+ }
9+
10+ public class AndXorEquation
11+ {
12+ public static int[] ConvertToBinary(int n)
13+ {
14+ int[] result = new int[(int)Math.Log(n, 2) + 1];
15+ int lastIndex = 0;
16+
17+ while(n != 0) {
18+ result[lastIndex++] = n % 2;
19+ n /= 2;
20+ }
21+
22+ return result;
23+ }
24+
25+ public static int SolveEq(int[] coefs)
26+ {
27+ if(coefs.Length != 9)
28+ throw new ArgumentException("Input array shold be 9 elements long");
29+
30+ int[,] binaryCoefs = new int[8, 9];
31+
32+ for(int i = 0; i <= 8; i++) {
33+ int[] binary = ConvertToBinary(coefs[i]);
34+
35+ for(int j = 0; j < binary.Length; j++) {
36+ binaryCoefs[7 - j, i] = binary[j];
37+ }
38+ }
39+
40+ var matrix = new AndXorSystem(binaryCoefs);
41+
42+ try {
43+ return matrix.Solve();
44+ }
45+ catch(SystemNonConsistentException) {
46+ return -1;
47+ }
48+ }
49+ }
50+
51+ public class AndXorSystem
52+ {
53+ int NumRows;
54+ int[,] Matrix;
55+
56+ public AndXorSystem(int[,] matrix)
57+ {
58+ if(matrix.GetLength(0) + 1 != matrix.GetLength(1))
59+ throw new ArgumentException("The array supplied isn't an Nx(N+1) matrix");
60+
61+ NumRows = matrix.GetLength(0);
62+
63+ for(int i = 0; i < NumRows; i++) {
64+ for(int j = 0; j < NumRows + 1; j++) {
65+ if(matrix[i, j] != 0 && matrix[i, j] != 1)
66+ throw new ArgumentException("The array supplied contains non-binary elements");
67+ }
68+ }
69+
70+ Matrix = matrix;
71+ }
72+
73+ public override string ToString()
74+ {
75+ string result = "";
76+
77+ for(int i = 0; i < NumRows; i++) {
78+ result += "( ";
79+
80+ for(int j = 0; j < NumRows + 1; j++) {
81+ result += Matrix[i, j].ToString("D") + (j == 7 ? " | " : " ");
82+ }
83+
84+ result += ")\n";
85+ }
86+
87+ return result;
88+ }
89+
90+ void SwapRows(int a, int b)
91+ {
92+ if(a < 0 || a >= NumRows || b < 0 || b >= NumRows)
93+ throw new ArgumentOutOfRangeException();
94+
95+ for(int i = 0; i < NumRows + 1; i++) {
96+ int t = Matrix[a, i];
97+ Matrix[a, i] = Matrix[b, i];
98+ Matrix[b, i] = t;
99+ }
100+ }
101+
102+ void XorRowIntoAnother(int a, int b)
103+ {
104+ if(a < 0 || a >= NumRows || b < 0 || b >= NumRows)
105+ throw new ArgumentOutOfRangeException();
106+
107+ for(int i = 0; i < NumRows + 1; i++) {
108+ Matrix[b, i] ^= Matrix[a, i];
109+ }
110+ }
111+
112+ void MakeCanonical()
113+ {
114+ // Реализуется алгоритм Гаусса, как Чернышев завещал
115+
116+ int curRow = 0, curCol = 0;
117+
118+ while(curRow < NumRows && curCol < NumRows) {
119+ if(Matrix[curRow, curCol] != 0) {
120+ for(int i = 0; i < NumRows; i++) {
121+ if(i == curRow)
122+ continue;
123+
124+ if(Matrix[i, curCol] == 1) {
125+ XorRowIntoAnother(curRow, i);
126+ }
127+ }
128+
129+ curRow++;
130+ curCol++;
131+ }
132+ else {
133+ int nonZeroRow = curRow;
134+
135+ for(int i = curRow + 1; i < NumRows; i++) {
136+ if(Matrix[i, curCol] != 0) {
137+ nonZeroRow = i;
138+ break;
139+ }
140+ }
141+
142+ if(nonZeroRow != curRow) {
143+ SwapRows(curRow, nonZeroRow);
144+
145+ continue;
146+ }
147+ else {
148+ curCol++;
149+ }
150+ }
151+ }
152+
153+ return;
154+ }
155+
156+ bool CheckConsistency()
157+ {
158+ for(int i = 0; i < NumRows; i++) {
159+ bool nonZeroRow = false;
160+
161+ for(int j = 0; j < NumRows; j++) {
162+ if(Matrix[i, j] != 0) {
163+ nonZeroRow = true;
164+ break;
165+ }
166+ }
167+
168+ if(!nonZeroRow && (Matrix[i, 8] != 0))
169+ return false;
170+ }
171+
172+ return true;
173+ }
174+
175+ public int Solve()
176+ {
177+ MakeCanonical();
178+
179+ if(!CheckConsistency())
180+ throw new SystemNonConsistentException();
181+
182+ int result = 0, power = 1;
183+ for(int i = 0; i < 8; i++) {
184+ result += Matrix[7 - i, 8] * power;
185+ power *= 2;
186+ }
187+
188+ return result;
189+ }
190+ }
191+}
192+
SolveAvdoshin/Properties/AssemblyInfo.csnew file+27
@@ -0,0 +1,27 @@
1+using System.Reflection;
2+using System.Runtime.CompilerServices;
3+
4+// Information about this assembly is defined by the following attributes.
5+// Change them to the values specific to your project.
6+
7+[assembly: AssemblyTitle("SolveAvdoshin")]
8+[assembly: AssemblyDescription("Программа для решения домашки Авдошина за первый модуль")]
9+[assembly: AssemblyConfiguration("")]
10+[assembly: AssemblyCompany("")]
11+[assembly: AssemblyProduct("")]
12+[assembly: AssemblyCopyright("frnkymac")]
13+[assembly: AssemblyTrademark("")]
14+[assembly: AssemblyCulture("")]
15+
16+// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
17+// The form "{Major}.{Minor}.*" will automatically update the build and revision,
18+// and "{Major}.{Minor}.{Build}.*" will update just the revision.
19+
20+[assembly: AssemblyVersion("1.0.*")]
21+
22+// The following attributes are used to specify the signing key for the assembly,
23+// if desired. See the Mono documentation for more information about signing.
24+
25+//[assembly: AssemblyDelaySign(false)]
26+//[assembly: AssemblyKeyFile("")]
27+
SolveAvdoshin/SolveAvdoshin.csnew file+88
@@ -0,0 +1,88 @@
1+using System;
2+
3+namespace SolveAvdoshin
4+{
5+ public class SolveAvdoshin
6+ {
7+ static string PrintEquation(int[] coefs)
8+ {
9+ string result = "";
10+
11+ for(int i = 0; i < 9; i++) {
12+ result += i == 8 ? " = " : i == 0 ? "" : " ⨁ ";
13+
14+ if(coefs[i] == -1)
15+ result += "___";
16+ else
17+ result += coefs[i].ToString("D");
18+
19+ result += i == 8 ? "" : " x" + (7 - i).ToString("D");
20+ }
21+
22+ return result;
23+ }
24+
25+ static int[] ConsoleInput()
26+ {
27+ return new int[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, }; // TODO: Sdielat
28+ }
29+
30+ static int[] ReadArgs(string[] args)
31+ {
32+ int[] coefs = new int[9];
33+
34+ if(args.Length == 0) {
35+ throw new ArgumentNullException();
36+ }
37+ else if(args.Length == 9) {
38+
39+ for(int i = 0; i < 9; i++) {
40+ coefs[i] = int.Parse(args[i]);
41+
42+ if(coefs[i] < 0 || coefs[i] > 255)
43+ throw new FormatException("Все аргументы должны быть целыми числами от 0 до 255");
44+ }
45+ }
46+ else {
47+ throw new FormatException("Аргументов то ли многовато, то ли маловато. Надо ровно 9");
48+ }
49+
50+ return coefs;
51+ }
52+
53+ public static void Solve(int[] coefs)
54+ {
55+ Console.WriteLine(PrintEquation(coefs));
56+
57+ int eqAnswer = AndXorEquation.SolveEq(coefs);
58+
59+ Console.WriteLine("\nОтвет: " + eqAnswer);
60+ }
61+
62+ public static void Main(string[] args)
63+ {
64+ int[] coefs = { -1, -1, -1, -1, -1, -1, -1, -1, -1, };
65+
66+ try {
67+ coefs = ReadArgs(args);
68+
69+ Solve(coefs);
70+
71+ throw new DivideByZeroException("Матибал");
72+ }
73+ catch(ArgumentNullException) {
74+ coefs = ConsoleInput();
75+
76+ Solve(coefs);
77+ }
78+ catch(FormatException e) {
79+ Console.WriteLine("Osheebka: " + e.Message);
80+ }
81+ catch(Exception e) { // TODO: Разобраться с экссепшенами
82+ Console.WriteLine("Непойманное исключение: " + e.Message);
83+ Console.WriteLine("\n" + e.StackTrace);
84+ }
85+ }
86+ }
87+}
88+
SolveAvdoshin/SolveAvdoshin.csprojnew file+41
@@ -0,0 +1,41 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+ <PropertyGroup>
4+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+ <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+ <ProjectGuid>{CD281751-5A98-4654-A7DB-1D5E1651B3D3}</ProjectGuid>
7+ <OutputType>Exe</OutputType>
8+ <RootNamespace>SolveAvdoshin</RootNamespace>
9+ <AssemblyName>SolveAvdoshin</AssemblyName>
10+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
11+ </PropertyGroup>
12+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
13+ <DebugSymbols>true</DebugSymbols>
14+ <DebugType>full</DebugType>
15+ <Optimize>false</Optimize>
16+ <OutputPath>bin\Debug</OutputPath>
17+ <DefineConstants>DEBUG;</DefineConstants>
18+ <ErrorReport>prompt</ErrorReport>
19+ <WarningLevel>4</WarningLevel>
20+ <Externalconsole>true</Externalconsole>
21+ <PlatformTarget>x86</PlatformTarget>
22+ </PropertyGroup>
23+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
24+ <DebugType>full</DebugType>
25+ <Optimize>true</Optimize>
26+ <OutputPath>bin\Release</OutputPath>
27+ <ErrorReport>prompt</ErrorReport>
28+ <WarningLevel>4</WarningLevel>
29+ <Externalconsole>true</Externalconsole>
30+ <PlatformTarget>x86</PlatformTarget>
31+ </PropertyGroup>
32+ <ItemGroup>
33+ <Reference Include="System" />
34+ </ItemGroup>
35+ <ItemGroup>
36+ <Compile Include="Properties\AssemblyInfo.cs" />
37+ <Compile Include="AndXorEquation.cs" />
38+ <Compile Include="SolveAvdoshin.cs" />
39+ </ItemGroup>
40+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
41+</Project>