@@ -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+