Align columns: Difference between revisions
Content added Content deleted
(add Standard ML) |
(Added C#) |
||
Line 1,499: | Line 1,499: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
===Old version=== |
|||
Uses a delegate, which were added to the language in C# 2, to define left-, right-, or center-justified. |
Uses a delegate, which were added to the language in C# 2, to define left-, right-, or center-justified. |
||
Line 1,578: | Line 1,579: | ||
}</lang> |
}</lang> |
||
===Newer version=== |
|||
{{out}} (centered): |
|||
Uses features of newer versions, like LINQ, lambdas and switch expressions. |
|||
{{works with|C sharp|C#|8+}} |
|||
<pre> |
|||
<lang csharp>using System; |
|||
⚫ | |||
using System.Linq; |
|||
⚫ | |||
⚫ | |||
enum Justification { Left, Center, Right } |
|||
⚫ | |||
⚫ | |||
public class Program |
|||
⚫ | |||
{ |
|||
</pre> |
|||
static void Main() |
|||
{ |
|||
string text = |
|||
@"Given$a$text$file$of$many$lines,$where$fields$within$a$line$ |
|||
are$delineated$by$a$single$'dollar'$character,$write$a$program |
|||
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$ |
|||
column$are$separated$by$at$least$one$space. |
|||
Further,$allow$for$each$word$in$a$column$to$be$either$left$ |
|||
justified,$right$justified,$or$center$justified$within$its$column."; |
|||
AlignColumns(text, Justification.Left); |
|||
Console.WriteLine(); |
|||
AlignColumns(text, Justification.Center); |
|||
Console.WriteLine(); |
|||
AlignColumns(text, Justification.Right); |
|||
} |
|||
public static void AlignColumns(string text, Justification justification) => |
|||
AlignColumns(text.Split(Environment.NewLine), justification); |
|||
public static void AlignColumns(string[] lines, Justification justification) => |
|||
AlignColumns(lines.Select(line => line.Split('$')).ToArray(), justification); |
|||
public static void AlignColumns(string[][] table, Justification justification) |
|||
{ |
|||
Console.WriteLine(justification + ":"); |
|||
int columns = table.Max(line => line.Length); |
|||
var columnWidths = |
|||
Enumerable.Range(0, columns) |
|||
.Select(i => table.Max(line => i < line.Length ? line[i].Length : 0) |
|||
).ToArray(); |
|||
foreach (var line in table) { |
|||
Console.WriteLine(string.Join(" ", |
|||
Enumerable.Range(0, line.Length) |
|||
.Select(i => justification switch { |
|||
Justification.Left => line[i].PadRight(columnWidths[i]), |
|||
Justification.Right => line[i].PadLeft(columnWidths[i]), |
|||
_ => line[i].PadLeft(columnWidths[i] / 2).PadRight(columnWidths[i]) |
|||
}) |
|||
)); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre style="height:30ex;overflow:scroll"> |
|||
Left: |
|||
Given a text file of many lines, where fields within a line |
|||
are delineated by a single 'dollar' character, write a program |
|||
that aligns each column of fields by ensuring that words in each |
|||
column are separated by at least one space. |
|||
Further, allow for each word in a column to be either left |
|||
⚫ | |||
Center: |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
justified, right justified, or center justified within its column. |
|||
Right: |
|||
Given a text file of many lines, where fields within a line |
|||
are delineated by a single 'dollar' character, write a program |
|||
that aligns each column of fields by ensuring that words in each |
|||
column are separated by at least one space. |
|||
Further, allow for each word in a column to be either left |
|||
justified, right justified, or center justified within its column.</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |