git.iko.soy/ikoHSE/SolveAvdoshin
on master help sign in
Evals now return a byte truth table. Got rid of the BooleanFunction class
commitb055d81a9551545f10b1b6784d96634016afc821 authorIlya Gulkov <ifranked@gmail.com> dateWed 30 Sep 2015, 15:09 (11 years ago) parentd0187db
2 files changed+69−173
SolveAvdoshin/BooleanFunctions.cs+68−172 SolveAvdoshin/SolveAvdoshin.cs+1−1
SolveAvdoshin/BooleanFunctions.cs+68−172
@@ -66,9 +66,7 @@ namespace SolveAvdoshin
6666
6767 public static void PrintMinimaInAvdoshinBases(int n)
6868 {
69− BooleanFunction f = new BooleanFunction(26);
70−
71− Console.WriteLine("\nМинимальные представления " + f.ToString("S") + " в базисах (для рисования в винлогике):");
69+ Console.WriteLine("\nМинимальные представления f_" + n + " в базисах (для рисования в винлогике):");
7270
7371 Console.WriteLine(" ☻ \tБазис\t\tВыражение");
7472 Console.WriteLine();
@@ -88,7 +86,7 @@ namespace SolveAvdoshin
8886 Console.Write("\t\t");
8987
9088 try {
91− var ex = f.MininalExpressionInBasis(AvdoshinBases[i], AvdoshinVars[i]);
89+ var ex = FindMininalExpressionInBasis(n, AvdoshinBases[i], AvdoshinVars[i]);
9290
9391 Console.WriteLine(ex.ToString());
9492 }
@@ -98,146 +96,41 @@ namespace SolveAvdoshin
9896 }
9997 }
10098
101− public static void Main()
102− {
103− var ex = new OpExpression(BooleanOperation.And, BooleanVariable.A,
104− new OpExpression(BooleanOperation.CoImp, BooleanVariable.B, BooleanVariable.C));;
105−
106− var ex1 = ex.Clone();
107−
108− ex.SetIthOp(0, BooleanOperation.Eq);
109− ex.SetIthVar(1, BooleanVariable.One);
110−
111− Console.WriteLine(ex.ToString());
112− Console.WriteLine(ex1.ToString());
113− }
114− }
115−
116− class TooLongToSearchForExpressionException : Exception
117− {
118−
119− }
120−
121− class BooleanFunction // TODO: Make private ?
122− {
123− protected int TruthTable;
124−
125− public BooleanFunction(int n)
126− {
127− TruthTable = n;
128− }
129−
130− public bool Equals(BooleanFunction that)
99+ static BooleanExpression FindMininalExpressionInBasis(int n, BooleanOperation[] ops, BooleanVariable[] vars)
131100 {
132− return this.TruthTable == that.TruthTable;
133− }
134−
135− public bool Eval(bool a, bool b, bool c)
136− {
137− return (TruthTable >> (7 - (a ? 4 : 0) - (b ? 2 : 0) - (c ? 1 : 0)) & 1) == 1;
138− }
139−
140− public int Eval(int a, int b, int c)
141− {
142− return TruthTable >> (7 - a * 4 - b * 2 - c) & 1;
143− }
144−
145− public static string VarRows()
146− {
147− string[] lines = new string[] { "A ", "B ", "C ", };
148−
149− for(int i = 0; i < 8; i++) {
150− lines[0] += (i >> 2 & 1) + " ";
151− lines[1] += (i >> 1 & 1) + " ";
152− lines[2] += (i & 1) + " ";
153− }
154−
155− return String.Join("\n", lines) + "\n";
156− }
157−
158− public static string DelimRow()
159− {
160− string line = "--";
161−
162− for(int i = 0; i < 8; i++) {
163− line += "--";
164− }
165−
166− return line + "\n";
167− }
168−
169− public string FuncRow()
170− {
171− string line = "F ";
101+ int depthLimit = ops.Length == 1 ? 10 : ops.Length == 2 ? 7 : 5;
172102
173− for(int i = 0; i < 8; i++) {
174− line += (Eval((i >> 2 & 1) == 1, (i >> 1 & 1) == 1, (i & 1) == 1) ? 1 : 0) + " ";
103+ for(int i = 2; i <= depthLimit; i++) {
104+ foreach(var ex in BooleanExpression.AllExpressions(i, ops, vars)) {
105+ if(n == ex.Eval()) {
106+ return ex;
107+ }
108+ }
175109 }
176110
177− return line + "\n";
111+ throw new TooLongToSearchForExpressionException();
178112 }
179113
180− public string ToString(string format)
114+ public static void Main1()
181115 {
182− if(format == "L") {
183− string result = "";
184−
185− result += VarRows();
186− result += DelimRow();
187− result += FuncRow();
116+ foreach(var op in AvdoshinBases[0]) {
117+ var ex = new OpExpression(op, BooleanVariable.B, BooleanVariable.C);
188118
189− return result;
119+ Console.WriteLine(ex.Eval() % 16);
190120 }
191− else if(format == "S") {
192− return "F_" + TruthTable;
193− }
194− else
195− throw new ArgumentException("Invalid format string. Use L for long or S for short");
196− }
197121
198− public override string ToString()
199− {
200− return ToString("S");
122+// foreach(var variable in AvdoshinVars[0]) {
123+// Console.WriteLine((new VarExpression(variable)).Eval());
124+// }
201125 }
126+ }
202127
203− public BooleanExpression GetFDNF() // СДНФ
204− {
205− BooleanExpression ex = null, cons1;
206−
207− for(int i = 0; i < 6; i++) {
208− if(!Eval((i >> 2 & 1) == 1, (i >> 1 & 1) == 1, (i & 1) == 1))
209− continue;
210−
211− BooleanExpression varA, varB, varC;
212−
213− varA = new VarExpression(BooleanVariable.A, (i >> 2 & 1) != 1);
214− varB = new VarExpression(BooleanVariable.B, (i >> 1 & 1) != 1);
215− varC = new VarExpression(BooleanVariable.C, (i & 1) != 1);
216−
217− cons1 = new OpExpression(BooleanOperation.And,
218− new OpExpression(BooleanOperation.And, varA, varB), varC);
219−
220− ex = ex != null ? new OpExpression(BooleanOperation.Or, cons1, ex) : cons1;
221− }
222−
223− return ex;
224− }
128+ class TooLongToSearchForExpressionException : Exception
129+ {
225130
226− public BooleanExpression MininalExpressionInBasis(BooleanOperation[] ops, BooleanVariable[] vars)
227− {
228− int depthLimit = ops.Length == 1 ? 10 : ops.Length == 2 ? 7 : 5;
131+ }
229132
230− for(int i = 2; i <= depthLimit; i++) {
231− foreach(var ex in BooleanExpression.AllExpressions(i, ops, vars)) {
232− if(TruthTable == ex.GetTruthTable()) {
233− return ex;
234− }
235− }
236− }
237133
238− throw new TooLongToSearchForExpressionException();
239− }
240− }
241134
242135 enum BooleanOperation { Zero, NOR, NotCoImp, NotA, NotImp, NotB, Xor, NAND, And, Eq, B, Imp, A, CoImp, Or, One };
243136 enum BooleanVariable { A, B, C, Zero, One, };
@@ -246,30 +139,13 @@ namespace SolveAvdoshin
246139 {
247140 public static readonly string[] OpSymbols = new string[] { "0", "↓", "</=", "(!A)", "=/>", "(!B)", "⨁", "|", "&", "==", "(B)", "=>", "(A)", "<=", "|", "1", };
248141
249− abstract public bool Eval(bool a, bool b, bool c);
142+ abstract public byte Eval();
250143 abstract new public string ToString();
251144 abstract public int CountVariables();
252145 abstract public void SetIthVar(int i, BooleanVariable value);
253146 abstract public int SetIthOp(int i, BooleanOperation value);
254147 abstract public BooleanExpression Clone();
255148
256− public int Eval(int a, int b, int c)
257− {
258− return Eval(a == 1, b == 1, c == 1) ? 1 : 0;
259− }
260−
261− public int GetTruthTable()
262− {
263− int truthTable = 0;
264−
265− for(int i = 0; i < 8; i++) {
266− truthTable *= 2;
267− truthTable += Eval((i >> 2) & 1, (i >> 1) & 1, i & 1);
268− }
269−
270− return truthTable;
271− }
272−
273149 public static IEnumerable<BooleanExpression> AllTrees(int size)
274150 {
275151 if(size == 0) {
@@ -373,47 +249,67 @@ namespace SolveAvdoshin
373249 return new OpExpression(Op, Left.Clone(), Right.Clone());
374250 }
375251
376− public override bool Eval(bool a, bool b, bool c)
252+ public override byte Eval()
377253 {
378− bool aVal = Left.Eval(a, b, c);
379− bool bVal = Right.Eval(a, b, c);
254+ byte aVal = Left.Eval();
255+ byte bVal = Right.Eval();
256+
257+ int res;
380258
381259 switch(Op) {
382260 case BooleanOperation.Zero:
383− return false;
261+ res = 0x00;
262+ break;
384263 case BooleanOperation.NOR:
385− return !(aVal || bVal);
264+ res = ~(aVal | bVal);
265+ break;
386266 case BooleanOperation.NotCoImp:
387− return !aVal && bVal;
267+ res = ~aVal & bVal;
268+ break;
388269 case BooleanOperation.NotA:
389− return !aVal;
270+ res = ~aVal;
271+ break;
390272 case BooleanOperation.NotImp:
391− return aVal && !bVal;
273+ res = aVal & ~bVal;
274+ break;
392275 case BooleanOperation.NotB:
393− return !bVal;
276+ res = ~bVal;
277+ break;
394278 case BooleanOperation.Xor:
395− return aVal != bVal;
279+ res = aVal ^ bVal;
280+ break;
396281 case BooleanOperation.NAND:
397− return !(aVal && bVal);
282+ res = ~(aVal & bVal);
283+ break;
398284 case BooleanOperation.And:
399− return aVal && bVal;
285+ res = aVal & bVal;
286+ break;
400287 case BooleanOperation.Eq:
401− return aVal == bVal;
288+ res = aVal ^ (~bVal);
289+ break;
402290 case BooleanOperation.B:
403− return bVal;
291+ res = bVal;
292+ break;
404293 case BooleanOperation.Imp:
405− return !aVal || bVal;
294+ res = ~aVal | bVal;
295+ break;
406296 case BooleanOperation.A:
407− return aVal;
297+ res = aVal;
298+ break;
408299 case BooleanOperation.CoImp:
409− return aVal || !bVal;
300+ res = aVal | ~bVal;
301+ break;
410302 case BooleanOperation.Or:
411− return aVal || bVal;
303+ res = aVal | bVal;
304+ break;
412305 case BooleanOperation.One:
413− return true;
306+ res = 0xFF;
307+ break;
414308 default:
415309 throw new ArgumentException();
416310 }
311+
312+ return (byte)res;
417313 }
418314
419315 public override string ToString()
@@ -491,19 +387,19 @@ namespace SolveAvdoshin
491387 return new VarExpression(Var);
492388 }
493389
494− public override bool Eval(bool a, bool b, bool c)
390+ public override byte Eval()
495391 {
496392 switch(Var) {
497393 case BooleanVariable.A:
498− return a;
394+ return 0x0F;
499395 case BooleanVariable.B:
500− return b;
396+ return 0x33;
501397 case BooleanVariable.C:
502− return c;
398+ return 0x55;
503399 case BooleanVariable.Zero:
504− return false;
400+ return 0x00;
505401 case BooleanVariable.One:
506− return true;
402+ return 0xFF;
507403 default:
508404 throw new Exception("Nigga wat?");
509405 }
SolveAvdoshin/SolveAvdoshin.cs+1−1
@@ -61,7 +61,7 @@ namespace SolveAvdoshin
6161 return eqAnswer;
6262 }
6363
64− public static void Main1(string[] args)
64+ public static void Main(string[] args)
6565 {
6666 int[] coefs = { -1, -1, -1, -1, -1, -1, -1, -1, -1, };
6767