@@ -6,39 +6,120 @@ namespace SolveAvdoshin
66 {
77 public static void Main()
88 {
9− var ex = new OpSubexpression(BooleanOperation.Imp, new NotSubexpression(BooleanVariable.A), new OpSubexpression(BooleanOperation.Xor, BooleanVariable.B, BooleanVariable.C));
9+// Console.WriteLine(ex.ToString());
10+// Console.WriteLine(ex.Eval(true, true, true));
11+
12+// var f = new BooleanFunction(134);
13+//
14+// Console.WriteLine(f.ToString());
15+// Console.WriteLine(f.ToString("L"));
16+
17+ var ex = (new BooleanFunction(26)).GetFDNF();
1018
1119 Console.WriteLine(ex.ToString());
12− Console.WriteLine(ex.Eval(false, true, true));
1320 }
1421 }
1522
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
23+ class BooleanFunction // TODO: Make private ?
2024 {
21−
25+ int TruthTable;
26+
27+ public BooleanFunction(int n)
28+ {
29+ TruthTable = n;
30+ }
31+
32+ public bool Eval(bool a, bool b, bool c)
33+ {
34+ return (TruthTable >> (7 - (a ? 4 : 0) - (b ? 2 : 0) - (c ? 1 : 0)) & 1) == 1;
35+ }
36+
37+ public string ToString(string format)
38+ {
39+ if(format == "L") {
40+ string[] lines = new string[] { "A ", "B ", "C ", "--", "F ", };
41+
42+ for(int i = 0; i < 8; i++) {
43+ lines[0] += (i >> 2 & 1) + " ";
44+ lines[1] += (i >> 1 & 1) + " ";
45+ lines[2] += (i & 1) + " ";
46+ lines[3] += "--";
47+ lines[4] += (Eval((i >> 2 & 1) == 1, (i >> 1 & 1) == 1, (i & 1) == 1) ? 1 : 0) + " ";
48+ }
49+
50+ string result = "";
51+
52+ foreach(string line in lines) result += line + "\n";
53+
54+ return result;
55+ }
56+ else if(format == "S") {
57+ return "BooleanFunction(" + TruthTable + ")";
58+ }
59+ else
60+ throw new ArgumentException("Invalid format string. Use L for long or S for short");
61+ }
62+
63+ public override string ToString()
64+ {
65+ return ToString("S");
66+ }
67+
68+ public BooleanExpression GetFDNF() // СДНФ
69+ {
70+ BooleanExpression ex = null, cons1;
71+
72+ for(int i = 0; i < 8; i++) {
73+ if(!Eval((i >> 2 & 1) == 1, (i >> 1 & 1) == 1, (i & 1) == 1))
74+ continue;
75+
76+ BooleanExpression varA, varB, varC;
77+
78+ if((i >> 2 & 1) == 1)
79+ varA = new VarExpression(BooleanVariable.A);
80+ else
81+ varA = new NotExpression(BooleanVariable.A);
82+
83+ if((i >> 1 & 1) == 1)
84+ varB = new VarExpression(BooleanVariable.B);
85+ else
86+ varB = new NotExpression(BooleanVariable.B);
87+
88+ if((i & 1) == 1)
89+ varC = new VarExpression(BooleanVariable.C);
90+ else
91+ varC = new NotExpression(BooleanVariable.C);
92+
93+ cons1 = new OpExpression(BooleanOperation.And, new OpExpression(BooleanOperation.And, varA, varB), varC);
94+
95+ ex = ex != null ? new OpExpression(BooleanOperation.Or, cons1, ex) : cons1;
96+ }
97+
98+ return ex;
99+ }
22100 }
23101
24− abstract class BooleanSubexpression
102+ enum BooleanOperation { Zero, NOR, NotBImp, NotA, NotImp, NotB, Xor, NAND, And, Eq, B, Imp, A, BImp, Or, One };
103+ enum BooleanVariable { A, B, C };
104+
105+ abstract class BooleanExpression
25106 {
26107 abstract public bool Eval(bool a, bool b, bool c);
27108 abstract new public string ToString();
28109 }
29110
30− class NotSubexpression : BooleanSubexpression
111+ class NotExpression : BooleanExpression
31112 {
32− BooleanSubexpression Arg;
113+ BooleanExpression Arg;
33114
34− public NotSubexpression(BooleanSubexpression arg)
115+ public NotExpression(BooleanExpression arg)
35116 {
36117 Arg = arg;
37118 }
38119
39− public NotSubexpression(BooleanVariable variable)
120+ public NotExpression(BooleanVariable variable)
40121 {
41− Arg = new VarSubexpression(variable);
122+ Arg = new VarExpression(variable);
42123 }
43124
44125 public override string ToString()
@@ -52,39 +133,39 @@ namespace SolveAvdoshin
52133 }
53134 }
54135
55− class OpSubexpression : BooleanSubexpression
136+ class OpExpression : BooleanExpression
56137 {
57138 static readonly string[] OpSymbols = new string[] { "(Zero)", "(NOR)", "</=", "(NotA)", "=/>", "(NotB)", "⨁", "(NAND)", "&", "==", "(B)", "=>", "(A)", "<=", "|", "(One)", };
58139
59140 BooleanOperation Op;
60− BooleanSubexpression Left, Right;
141+ BooleanExpression Left, Right;
61142
62− public OpSubexpression(BooleanOperation op, BooleanSubexpression left, BooleanSubexpression right)
143+ public OpExpression(BooleanOperation op, BooleanExpression left, BooleanExpression right)
63144 {
64145 Op = op;
65146 Left = left;
66147 Right = right;
67148 }
68149
69− public OpSubexpression(BooleanOperation op, BooleanVariable left, BooleanSubexpression right)
150+ public OpExpression(BooleanOperation op, BooleanVariable left, BooleanExpression right)
70151 {
71152 Op = op;
72− Left = new VarSubexpression(left);
153+ Left = new VarExpression(left);
73154 Right = right;
74155 }
75156
76− public OpSubexpression(BooleanOperation op, BooleanSubexpression left, BooleanVariable right)
157+ public OpExpression(BooleanOperation op, BooleanExpression left, BooleanVariable right)
77158 {
78159 Op = op;
79160 Left = left;
80− Right = new VarSubexpression(right);
161+ Right = new VarExpression(right);
81162 }
82163
83− public OpSubexpression(BooleanOperation op, BooleanVariable left, BooleanVariable right)
164+ public OpExpression(BooleanOperation op, BooleanVariable left, BooleanVariable right)
84165 {
85166 Op = op;
86− Left = new VarSubexpression(left);
87− Right = new VarSubexpression(right);
167+ Left = new VarExpression(left);
168+ Right = new VarExpression(right);
88169 }
89170
90171 public override bool Eval(bool a, bool b, bool c)
@@ -136,13 +217,13 @@ namespace SolveAvdoshin
136217 }
137218 }
138219
139− class VarSubexpression : BooleanSubexpression
220+ class VarExpression : BooleanExpression
140221 {
141222 static readonly string[] VarSymbols = new string[] { "A", "B", "C", };
142223
143224 BooleanVariable Var;
144225
145− public VarSubexpression(BooleanVariable var)
226+ public VarExpression(BooleanVariable var)
146227 {
147228 Var = var;
148229 }
@@ -166,25 +247,5 @@ namespace SolveAvdoshin
166247 return VarSymbols[(int)Var];
167248 }
168249 }
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− }
189250}
190251