git.iko.soy/ikoHSE/SolveAvdoshin
on master help sign in
Boolean expression evaluation and ToString
commit96c4d1a945edd85c595bfb5896c82af4d002d152 authorIlya Gulkov <ifranked@gmail.com> dateThu 24 Sep 2015, 10:45 (11 years ago) parentdb40832
3 files changed+201−10
SolveAvdoshin/BooleanFunctions.cs+190 SolveAvdoshin/SolveAvdoshin.cs+10−10 SolveAvdoshin/SolveAvdoshin.csproj+1
SolveAvdoshin/BooleanFunctions.csnew file+190
@@ -0,0 +1,190 @@
1+using System;
2+
3+namespace SolveAvdoshin
4+{
5+ public static class BooleanFunctions
6+ {
7+ public static void Main()
8+ {
9+ var ex = new OpSubexpression(BooleanOperation.Imp, new NotSubexpression(BooleanVariable.A), new OpSubexpression(BooleanOperation.Xor, BooleanVariable.B, BooleanVariable.C));
10+
11+ Console.WriteLine(ex.ToString());
12+ Console.WriteLine(ex.Eval(false, true, true));
13+ }
14+ }
15+
16+ enum BooleanOperation { Zero, NOR, NotBImp, NotA, NotImp, NotB, Xor, NAND, And, Eq, B, Imp, A, BImp, Or, One };
17+ enum BooleanVariable { A, B, C };
18+
19+ abstract class BooleanExpression // TODO: Make private
20+ {
21+
22+ }
23+
24+ abstract class BooleanSubexpression
25+ {
26+ abstract public bool Eval(bool a, bool b, bool c);
27+ abstract new public string ToString();
28+ }
29+
30+ class NotSubexpression : BooleanSubexpression
31+ {
32+ BooleanSubexpression Arg;
33+
34+ public NotSubexpression(BooleanSubexpression arg)
35+ {
36+ Arg = arg;
37+ }
38+
39+ public NotSubexpression(BooleanVariable variable)
40+ {
41+ Arg = new VarSubexpression(variable);
42+ }
43+
44+ public override string ToString()
45+ {
46+ return "!" + Arg.ToString();
47+ }
48+
49+ public override bool Eval(bool a, bool b, bool c)
50+ {
51+ return !Arg.Eval(a, b, c);
52+ }
53+ }
54+
55+ class OpSubexpression : BooleanSubexpression
56+ {
57+ static readonly string[] OpSymbols = new string[] { "(Zero)", "(NOR)", "</=", "(NotA)", "=/>", "(NotB)", "⨁", "(NAND)", "&", "==", "(B)", "=>", "(A)", "<=", "|", "(One)", };
58+
59+ BooleanOperation Op;
60+ BooleanSubexpression Left, Right;
61+
62+ public OpSubexpression(BooleanOperation op, BooleanSubexpression left, BooleanSubexpression right)
63+ {
64+ Op = op;
65+ Left = left;
66+ Right = right;
67+ }
68+
69+ public OpSubexpression(BooleanOperation op, BooleanVariable left, BooleanSubexpression right)
70+ {
71+ Op = op;
72+ Left = new VarSubexpression(left);
73+ Right = right;
74+ }
75+
76+ public OpSubexpression(BooleanOperation op, BooleanSubexpression left, BooleanVariable right)
77+ {
78+ Op = op;
79+ Left = left;
80+ Right = new VarSubexpression(right);
81+ }
82+
83+ public OpSubexpression(BooleanOperation op, BooleanVariable left, BooleanVariable right)
84+ {
85+ Op = op;
86+ Left = new VarSubexpression(left);
87+ Right = new VarSubexpression(right);
88+ }
89+
90+ public override bool Eval(bool a, bool b, bool c)
91+ {
92+ bool aVal = Left.Eval(a, b, c);
93+ bool bVal = Right.Eval(a, b, c);
94+
95+ switch(Op) {
96+ case BooleanOperation.Zero:
97+ return false;
98+ case BooleanOperation.NOR:
99+ return !(aVal || bVal);
100+ case BooleanOperation.NotBImp:
101+ return !aVal && bVal;
102+ case BooleanOperation.NotA:
103+ return !aVal;
104+ case BooleanOperation.NotImp:
105+ return aVal && !bVal;
106+ case BooleanOperation.NotB:
107+ return !bVal;
108+ case BooleanOperation.Xor:
109+ return aVal != bVal;
110+ case BooleanOperation.NAND:
111+ return !(aVal && bVal);
112+ case BooleanOperation.And:
113+ return aVal && bVal;
114+ case BooleanOperation.Eq:
115+ return aVal == bVal;
116+ case BooleanOperation.B:
117+ return bVal;
118+ case BooleanOperation.Imp:
119+ return !aVal || bVal;
120+ case BooleanOperation.A:
121+ return aVal;
122+ case BooleanOperation.BImp:
123+ return aVal || !bVal;
124+ case BooleanOperation.Or:
125+ return aVal || bVal;
126+ case BooleanOperation.One:
127+ return true;
128+ default:
129+ throw new ArgumentException();
130+ }
131+ }
132+
133+ public override string ToString()
134+ {
135+ return "(" + Left.ToString() + " " + OpSymbols[(int)Op] + " " + Right.ToString() + ")";
136+ }
137+ }
138+
139+ class VarSubexpression : BooleanSubexpression
140+ {
141+ static readonly string[] VarSymbols = new string[] { "A", "B", "C", };
142+
143+ BooleanVariable Var;
144+
145+ public VarSubexpression(BooleanVariable var)
146+ {
147+ Var = var;
148+ }
149+
150+ public override bool Eval(bool a, bool b, bool c)
151+ {
152+ switch(Var) {
153+ case BooleanVariable.A:
154+ return a;
155+ case BooleanVariable.B:
156+ return b;
157+ case BooleanVariable.C:
158+ return c;
159+ default:
160+ throw new Exception("Nigga wat?");
161+ }
162+ }
163+
164+ public override string ToString()
165+ {
166+ return VarSymbols[(int)Var];
167+ }
168+ }
169+
170+ class BooleanFunction // TODO: Make private ?
171+ {
172+ int TruthTable;
173+
174+ public BooleanFunction(int n)
175+ {
176+ TruthTable = n;
177+ }
178+
179+ public int Eval(int a, int b, int c)
180+ {
181+ return TruthTable >> (7 - 4 * a - 2 * b - c) & 1;
182+ }
183+
184+ public BooleanExpression GetFDNF() // СДНФ
185+ {
186+ return null;
187+ }
188+ }
189+}
190+
SolveAvdoshin/SolveAvdoshin.cs+10−10
@@ -24,7 +24,7 @@ namespace SolveAvdoshin
2424
2525 static int[] ConsoleInput()
2626 {
27− return new int[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, }; // TODO: Sdielat
27+ return new int[] { 220, 160, 85, 253, 210, 159, 103, 101, 72, }; // TODO: Скопировать сюда нормальный инпут
2828 }
2929
3030 static int[] ReadArgs(string[] args)
@@ -57,21 +57,21 @@ namespace SolveAvdoshin
5757 int eqAnswer = AndXorEquation.SolveEq(coefs);
5858
5959 Console.WriteLine("\nОтвет: " + eqAnswer);
60+
61+// BooleanFunction bFun =
6062 }
6163
62− public static void Main(string[] args)
64+ public static void Main1(string[] args)
6365 {
6466 int[] coefs = { -1, -1, -1, -1, -1, -1, -1, -1, -1, };
6567
6668 try {
67− coefs = ReadArgs(args);
68−
69− Solve(coefs);
70−
71− throw new DivideByZeroException("Матибал");
72− }
73− catch(ArgumentNullException) {
74− coefs = ConsoleInput();
69+ try {
70+ coefs = ReadArgs(args);
71+ }
72+ catch(ArgumentNullException) {
73+ coefs = ConsoleInput();
74+ }
7575
7676 Solve(coefs);
7777 }
SolveAvdoshin/SolveAvdoshin.csproj+1
@@ -36,6 +36,7 @@
3636 <Compile Include="Properties\AssemblyInfo.cs" />
3737 <Compile Include="AndXorEquation.cs" />
3838 <Compile Include="SolveAvdoshin.cs" />
39+ <Compile Include="BooleanFunctions.cs" />
3940 </ItemGroup>
4041 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
4142</Project>