Skip to content

Commit 85799a1

Browse files
committed
Added ability to join multiple tables horizontally
1 parent e00237c commit 85799a1

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

BooleanExpressionParser/Formatter.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static string FormatTokens(IEnumerable<Token> tokens)
1818
return sb.ToString();
1919
}
2020

21-
public static string FormatTruthTable(Ast ast, List<bool[]> table, string label="Result")
21+
public static string FormatTruthTable(Ast ast, List<bool[]> table, string label = "Result")
2222
{
2323
var sb = new StringBuilder();
2424

@@ -60,4 +60,19 @@ public static string FormatTruthTable(Ast ast, List<bool[]> table, string label=
6060
}
6161

6262
static string Repeat(char c, int count) => new string(c, count);
63+
64+
public static String JoinTruthTables(params string[] tables)
65+
{
66+
var sb = new StringBuilder();
67+
68+
var lines = tables.Select(t => t.Split(Environment.NewLine)).ToList();
69+
70+
for (int i = 0; i < lines.Max(l => l.Length); i++)
71+
{
72+
sb.AppendJoin(" ", lines.Select(l => i < l.Length ? l[i] : Repeat(' ', l[0].Length)));
73+
sb.AppendLine();
74+
}
75+
76+
return sb.ToString();
77+
}
6378
}

BooleanExpressionParser/Program.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ static void Main(params string[] args)
1414
args = QueryExpressions();
1515
}
1616

17+
var tables = new List<string>();
18+
1719
foreach (var expression in args)
1820
{
1921
var tokeniser = new Tokeniser(expression);
@@ -22,17 +24,22 @@ static void Main(params string[] args)
2224
var parser = new Parser();
2325
var prefixTokens = parser.ParseTokens(infixTokens);
2426

25-
Console.WriteLine(expression);
26-
Console.WriteLine("Infix: " + Formatter.FormatTokens(infixTokens));
27-
Console.WriteLine("Prefix: " + Formatter.FormatTokens(prefixTokens));
28-
2927
var ast = parser.GrowAst(prefixTokens);
3028

3129
var evaluator = new Evaluator(ast);
3230
var table = evaluator.EvaluateAll();
3331

3432
var tableString = Formatter.FormatTruthTable(ast, table, label: Formatter.FormatTokens(infixTokens));
35-
Console.WriteLine(tableString);
33+
tables.Add(tableString);
34+
}
35+
36+
if (tables.Count > 1)
37+
{
38+
Console.WriteLine(Formatter.JoinTruthTables(tables.ToArray()));
39+
}
40+
else
41+
{
42+
Console.WriteLine(tables[0]);
3643
}
3744
}
3845

0 commit comments

Comments
 (0)