我怎樣才能得到用棍棒編碼的ascii藝術的數字?如何從文本文件像ASCII碼藝術與C#Tryparse數字?
我得被淹死用棍子nubmers。
第一步是獲得4行,而不是用字母來控制它。
我得到的文本字符串中的[]
string[] lines = File.ReadAllLines("SourceFile.txt");
第一4線均達到線[3]。 我怎樣才能控制不同的線在同一個位置? 它就像一個二維數組或我必須做其他事情?
我怎樣才能得到用棍棒編碼的ascii藝術的數字?如何從文本文件像ASCII碼藝術與C#Tryparse數字?
我得被淹死用棍子nubmers。
第一步是獲得4行,而不是用字母來控制它。
我得到的文本字符串中的[]
string[] lines = File.ReadAllLines("SourceFile.txt");
第一4線均達到線[3]。 我怎樣才能控制不同的線在同一個位置? 它就像一個二維數組或我必須做其他事情?
首先,您需要一個存儲符號的模式和指標的對象(在您的案例中是數字)。也此對象具有對於給定的陣列中識別它的圖案的方法:
public class AsciiNumber
{
private readonly char[][] _data;
public AsciiNumber(char character, char[][] data)
{
this._data = data;
this.Character = character;
}
public char Character
{
get;
private set;
}
public int Width
{
get
{
return this._data[0].Length;
}
}
public int Height
{
get
{
return this._data.Length;
}
}
public bool Match(string[] source, int startRow, int startColumn)
{
if (startRow + this.Height > source.Length)
{
return false;
}
for (var i = startRow; i < startRow + this.Height; i++)
{
var row = source[i];
if (startColumn + this.Width > row.Length)
{
return false;
}
for (var j = startColumn; j < startColumn + this.Width; j++)
{
if (this._data[i - startRow][j - startColumn] != row[j])
{
return false;
}
}
}
return true;
}
}
然後可以創建你處理類似字母(I只用數字1和3):
public static class Alphabet
{
private static readonly AsciiNumber Number1 = new AsciiNumber('1', new[]{
new []{'|'},
new []{'|'},
new []{'|'},
new []{'|'},
});
private static readonly AsciiNumber Number3 = new AsciiNumber('3', new[]{
new []{'-','-','-'},
new []{' ',' ','/'},
new []{' ',' ','\\'},
new []{'-','-','-'},
});
public static readonly IEnumerable<AsciiNumber> All = new[] { Number1, Number3 };
}
假設在你的源文件編號已permament和平等的高度,你可以嘗試這樣的代碼:
string[] lines = File.ReadAllLines("SourceFile.txt");
var lineHeight = 4;
var text = new StringBuilder();
for (var i = 0; i < lines.Length; i += lineHeight)
{
var j = 0;
while (j < lines[i].Length)
{
var match = Alphabet.All.FirstOrDefault(character => character.Match(lines, i, j));
if (match != null)
{
text.Append(match.Character);
j += match.Width;
}
else
{
j++;
}
}
}
Console.WriteLine("Recognized numbers: {0}", text.ToString());
NB如果行高改變了文件,你必須改進上面的代碼。
不知何故,我只能得到1的代碼 –
你有沒有在字母表中加入其他數字?你是否理解我的代碼的作用?雖然它與我的測試輸入完全一致,但它可能會因輸入而失敗。使用調試器來了解錯誤。 –
讓我們假設我們有這樣的文字,我們希望它來解析數字:
--- --- | | | -----
/ _| | |__| |___
\ | | | |
-- --- | | ____|
首先,我們應該去掉任何不必要的空格(或製表符,如果存在的話),並把一個分離器字符(如$
)的數字之間,獲得類似這樣的東西:
$---$---$|$| |$-----$
$/$ _|$|$|__|$|___ $
$ \ $| $|$ |$ |$
$-- $---$|$ |$____|$
現在我們應該調用Split
功能上的文本的每一行,使用$
作爲分隔符,然後比較resul與字母表。
讓我們看看如何用代碼來做到這一點。鑑於解析string[] lines
文本,我們將創建一個擴展方法來去除不必要的空格,把一個separator
字符/字符串,而不是:
public static class StringHelperClass
{
// Extension method to remove any unnecessary white-space and put a separator char instead.
public static string[] ReplaceSpacesWithSeparator(this string[] text, string separator)
{
// Create an array of StringBuilder, one for every line in the text.
StringBuilder[] stringBuilders = new StringBuilder[text.Length];
// Initialize stringBuilders.
for (int n = 0; n < text.Length; n++)
stringBuilders[n] = new StringBuilder().Append(separator);
// Get shortest line in the text, in order to avoid Out Of Range Exception.
int shorterstLine = text.Min(line => line.Length);
// Temporary variables.
int lastSeparatorIndex = 0;
bool previousCharWasSpace = false;
// Start processing the text, char after char.
for (int n = 0; n < shorterstLine; ++n)
{
// Look for white-spaces on the same position on
// all the lines of the text.
if (text.All(line => line[n] == ' '))
{
// Go to next char if previous char was also a white-space,
// or if this is the first white-space char of the text.
if (previousCharWasSpace || n == 0)
{
previousCharWasSpace = true;
lastSeparatorIndex = n + 1;
continue;
}
previousCharWasSpace = true;
// Append non white-space chars to the StringBuilder
// of each line, for later use.
for (int i = lastSeparatorIndex; i < n; ++i)
{
for (int j = 0; j < text.Length; j++)
stringBuilders[j].Append(text[j][i]);
}
// Append separator char.
for (int j = 0; j < text.Length; j++)
stringBuilders[j].Append(separator);
lastSeparatorIndex = n + 1;
}
else
previousCharWasSpace = false;
}
for (int j = 0; j < text.Length; j++)
text[j] = stringBuilders[j].ToString();
// Return formatted text.
return text;
}
}
然後,在Main
方法,我們將使用:
lines = lines.ReplaceSpacesWithSeparator("$");
和
ASCIINumbersParser parser = new ASCIINumbersParser(lines, "$");
其中ASCIINumbersParser
是這個類:
public class ASCIINumbersParser
{
// Will store a list of all the possible numbers
// found in the text.
public List<string[]> CandidatesList { get; }
public ASCIINumbersParser(string[] text, string separator)
{
CandidatesList = new List<string[]>();
string[][] candidates = new string[text.Length][];
for (int n = 0; n < text.Length; ++n)
{
// Split each line in the text, using the separator char/string.
candidates[n] =
text[n].Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);
}
// Put the strings in such a way that each CandidateList item
// contains only one possible number found in the text.
for (int i = 0; i < candidates[0].Length; ++i)
CandidatesList.Add(candidates.Select(c => c[i]).ToArray());
}
}
在這一點上,我們將使用下面的類來生成一些數字的ASCII藝術表現,並與我們的原始字符串比較的結果(在Main
):
public static class ASCIINumberHelper
{
// Get an ASCII art representation of a number.
public static string[] GetASCIIRepresentationForNumber(int number)
{
switch (number)
{
case 1:
return new[]
{
"|",
"|",
"|",
"|"
};
case 2:
return new[]
{
"---",
" _|",
"| ",
"---"
};
case 3:
return new[]
{
"---",
"/",
@" \ ",
"-- "
};
case 4:
return new[]
{
"| |",
"|__|",
" |",
" |"
};
case 5:
return new[]
{
"-----",
"|___ ",
" |",
"____|"
};
default:
return null;
}
}
// See if two numbers represented as ASCII art are equal.
public static bool ASCIIRepresentationMatch(string[] number1, string[] number2)
{
// Return false if the any of the two numbers is null
// or their lenght is different.
// if (number1 == null || number2 == null)
// return false;
// if (number1.Length != number2.Length)
// return false;
if (number1?.Length != number2?.Length)
return false;
try
{
for (int n = 0; n < number1.Length; ++n)
{
if (number1[n].CompareTo(number2[n]) != 0)
return false;
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex);
return false;
}
return true;
}
}
最後,該Main
功能看起來就像這樣:
static void Main()
{
string ASCIIString = @"
--- --- | | | -----
/ _| | |__| |___
\ | | | |
-- --- | | ____| ";
string[] lines =
ASCIIString.Split(new[] {"\n","\r\n"}, StringSplitOptions.RemoveEmptyEntries);
lines = lines.ReplaceSpacesWithSeparator("$");
ASCIINumbersParser parser = new ASCIINumbersParser(lines, "$");
// Try to find all numbers contained in the ASCII string
foreach (string[] candidate in parser.CandidatesList)
{
for (int i = 1; i < 10; ++i)
{
string[] num = ASCIINumberHelper.GetASCIIRepresentationForNumber(i);
if (ASCIINumberHelper.ASCIIRepresentationMatch(num, candidate))
Console.WriteLine("Number {0} was found in the string.", i);
}
}
}
// Expected output:
// Number 3 was found in the string.
// Number 2 was found in the string.
// Number 1 was found in the string.
// Number 4 was found in the string.
// Number 5 was found in the string.
Here你可以找到完整的代碼。
每個數字都有其獨特的模式不是?你可以計算行數[0]中有多少個空格,並且看到第一行中有多少個數字。接下來的0到4行來形成數字,然後再計數5 – SamY
,因爲你知道有多少數字,你可以檢查每個模式 – SamY
@SamY是的,每個數字都有其獨特的模式。我如何在Code中做你的logik? (要計算和控制模式?) –