@@ -4,7 +4,7 @@ namespace BooleanExpressionParser;
44
55class Formatter
66{
7- public static string FormatInfixTokens ( IEnumerable < Token > tokens )
7+ public static string FormatTokens ( IEnumerable < Token > tokens )
88 {
99 var sb = new StringBuilder ( ) ;
1010
@@ -16,36 +16,24 @@ public static string FormatInfixTokens(IEnumerable<Token> tokens)
1616 return sb . ToString ( ) ;
1717 }
1818
19- public static string FormatPrefixTokens ( IEnumerable < Token > tokens )
20- {
21- var sb = new StringBuilder ( ) ;
22-
23- foreach ( var token in tokens )
24- {
25- sb . Append ( token . ToString ( ) ) ;
26- sb . Append ( " " ) ;
27- }
28-
29- return sb . ToString ( ) ;
30- }
31-
32- public static string FormatTruthTable ( Ast ast , List < bool [ ] > table )
19+ public static string FormatTruthTable ( Ast ast , List < bool [ ] > table , string label = "Result" )
3320 {
3421 var sb = new StringBuilder ( ) ;
3522
3623 var variableLine = ast . Variables . Select ( v => Repeat ( '━' , v . Length + 2 ) ) . ToList ( ) ;
24+ var resultLine = Repeat ( '━' , label . Length + 4 ) ;
3725
3826 sb . Append ( "┏━" ) ;
39- variableLine . ForEach ( s => sb . Append ( s ) ) ;
40- sb . AppendLine ( "━┳━━━━━━━━ ┓" ) ;
27+ sb . AppendJoin ( null , variableLine ) ;
28+ sb . AppendLine ( $ "━┳{ resultLine } ┓") ;
4129
4230 sb . Append ( "┃ " ) ;
4331 ast . Variables . ForEach ( v => sb . Append ( $ " { v } ") ) ;
44- sb . AppendLine ( " ┃ Result ┃" ) ;
32+ sb . AppendLine ( $ " ┃ { label } ┃") ;
4533
4634 sb . Append ( "┣━" ) ;
47- variableLine . ForEach ( s => sb . Append ( s ) ) ;
48- sb . AppendLine ( "━╋━━━━━━━━ ┫" ) ;
35+ sb . AppendJoin ( null , variableLine ) ;
36+ sb . AppendLine ( $ "━╋{ resultLine } ┫") ;
4937
5038 foreach ( bool [ ] row in table )
5139 {
@@ -57,12 +45,14 @@ public static string FormatTruthTable(Ast ast, List<bool[]> table)
5745 sb . Append ( $ "{ pad1 } { ( row [ i ] ? '1' : '0' ) } { pad2 } ") ;
5846 }
5947
60- sb . AppendLine ( $ " ┃ { ( row [ ^ 1 ] ? '1' : '0' ) } ┃") ;
48+ string pad3 = Repeat ( ' ' , ( int ) Math . Ceiling ( label . Length / 2.0f ) ) ;
49+ string pad4 = Repeat ( ' ' , ( int ) Math . Floor ( label . Length / 2.0f ) ) ;
50+ sb . AppendLine ( $ " ┃ { pad3 } { ( row [ ^ 1 ] ? '1' : '0' ) } { pad4 } ┃") ;
6151 }
6252
6353 sb . Append ( "┗━" ) ;
64- variableLine . ForEach ( s => sb . Append ( s ) ) ;
65- sb . AppendLine ( "━┻━━━━━━━━ ┛" ) ;
54+ sb . AppendJoin ( null , variableLine ) ;
55+ sb . AppendLine ( $ "━┻{ resultLine } ┛") ;
6656
6757 return sb . ToString ( ) ;
6858 }
0 commit comments