Skip to content

Commit 1572b75

Browse files
committed
Updated table to accept dynamic length variable names
1 parent efeb918 commit 1572b75

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

v2/BooleanExpressionParser/.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
// "A.B",
1212
// "A+B",
1313
// "!(A+B+C)",
14-
"(((A.B&C) OR A) AND (NOT B + !C)) AND NOT D",
15-
// "!S.D_0 + D_1 AND S"
14+
// "(((A.B&C) OR A) AND (NOT B + !C)) AND NOT D",
15+
"!S.D_0 + D_1 AND S",
1616
],
1717
"cwd": "${workspaceFolder}",
1818
"console": "integratedTerminal", // enables input (instead of internalConsole)

v2/BooleanExpressionParser/Formatter.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,36 @@ public static string FormatTruthTable(Ast ast, List<bool[]> table)
3333
{
3434
var sb = new StringBuilder();
3535

36-
sb.AppendLine($"┏{Repeat('━', ast.Variables.Count * 3 + 2)}{Repeat('━', 8)}┓");
37-
sb.AppendLine($"┃ {String.Join(" ", ast.Variables)} ┃ Result ┃");
38-
sb.AppendLine($"┣{Repeat('━', ast.Variables.Count * 3 + 2)}{Repeat('━', 8)}┫");
36+
var variableLine = ast.Variables.Select(v => Repeat('━', v.Length + 2)).ToList();
3937

40-
foreach (var row in table)
38+
sb.Append("┏━");
39+
variableLine.ForEach(s => sb.Append(s));
40+
sb.AppendLine("━┳━━━━━━━━┓");
41+
42+
sb.Append("┃ ");
43+
ast.Variables.ForEach(v => sb.Append($" {v} "));
44+
sb.AppendLine(" ┃ Result ┃");
45+
46+
sb.Append("┣━");
47+
variableLine.ForEach(s => sb.Append(s));
48+
sb.AppendLine("━╋━━━━━━━━┫");
49+
50+
foreach (bool[] row in table)
4151
{
42-
sb.AppendLine($"┃ {String.Join(" ", row[0..^1].Select(b => b ? "1" : "0"))}{(row[^1] ? "1" : "0")} ┃");
52+
sb.Append("┃ ");
53+
for (int i = 0; i < row.Length - 1; i++)
54+
{
55+
string pad1 = Repeat(' ', (int)Math.Ceiling(ast.Variables[i].Length / 2.0f));
56+
string pad2 = Repeat(' ', (int)Math.Floor(ast.Variables[i].Length / 2.0f));
57+
sb.Append($"{pad1}{(row[i] ? '1' : '0')}{pad2} ");
58+
}
59+
60+
sb.AppendLine($" ┃ {(row[^1] ? '1' : '0')} ┃");
4361
}
4462

45-
sb.AppendLine($"┗{Repeat('━', ast.Variables.Count * 3 + 2)}{Repeat('━', 8)}┛");
63+
sb.Append("┗━");
64+
variableLine.ForEach(s => sb.Append(s));
65+
sb.AppendLine("━┻━━━━━━━━┛");
4666

4767
return sb.ToString();
4868
}

v2/BooleanExpressionParser/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ static void Main(params string[] args)
1818
var parser = new Parser();
1919
var prefixTokens = parser.ParseTokens(infixTokens);
2020

21-
Console.WriteLine($"Expression: {expression}");
22-
Console.WriteLine($"Prefix: {Formatter.FormatPrefixTokens(prefixTokens)}");
2321
Console.WriteLine($"Infix: {Formatter.FormatInfixTokens(infixTokens)}");
22+
Console.WriteLine($"Prefix: {Formatter.FormatPrefixTokens(prefixTokens)}");
2423

2524
var ast = parser.GrowAst(prefixTokens);
2625

0 commit comments

Comments
 (0)