如何在 C# 中以数组形式显示文件文本?
任务:准备一个程序,该程序将形成文本文件中使用的一组字母(数组),并按字母顺序将其显示在屏幕上。
我想制作信件(文件); (文件中的文本)以一个字母显示。但它向我显示“TextFile1.txt”。
给出了 Show 和 Letters 方法。我的代码:
static void Main()
{
string file = "TextFile1.txt";
Show(file);
char[] let = Letters(file);
foreach (char c in let)
{
Console.WriteLine(c);
}
Console.ReadLine();
}
static void Show(string name)
{
if (File.Exists(name))
{
string text = File.ReadAllText(name, Encoding.GetEncoding(1257));
Console.WriteLine(text);
}
else Console.WriteLine("File {0} is not in disc", name);
}
static char[] Letters(string e)
{
e = e.ToUpper();
char[] mas = new char[32];
int n = 0;
foreach (char r in e)
if (Array.IndexOf(mas, r) < 0)
if (Char.IsLetter(r))
mas[n++] = r;
Array.Resize(ref mas, n);
return mas;
}
}
Task: Prepare a program that will form a set (array) of letters used in a text file and display it in alphabetical order on the screen.
I want to make Letters(file); (text in file) to show by one letter. But It shows me 'TextFile1.txt' instead.
Show and Letters methods are given. My code:
static void Main()
{
string file = "TextFile1.txt";
Show(file);
char[] let = Letters(file);
foreach (char c in let)
{
Console.WriteLine(c);
}
Console.ReadLine();
}
static void Show(string name)
{
if (File.Exists(name))
{
string text = File.ReadAllText(name, Encoding.GetEncoding(1257));
Console.WriteLine(text);
}
else Console.WriteLine("File {0} is not in disc", name);
}
static char[] Letters(string e)
{
e = e.ToUpper();
char[] mas = new char[32];
int n = 0;
foreach (char r in e)
if (Array.IndexOf(mas, r) < 0)
if (Char.IsLetter(r))
mas[n++] = r;
Array.Resize(ref mas, n);
return mas;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使此特定作业更容易的一件事是,C#对待
char
andint
作为某些上下文和操作等效;字符具有等效的数字 - 字母A的小数为65,这要归功于历史上位于ASCII表中的65位置,您可以将角色和整数添加在一起;
'a' + 1
是'b'
,或66
如果您以数字查看它们。您可以从另一个字符中减去一个字符:'b' - 'a'
是1,如果我们仅处理一个装满字母的文件,a到z,则为26个字符,它们都顺序运行。这听起来像一个阵列,对吗?
A是65。Z是90。这意味着我们实际上只需要一个大于91个元素的数组,才能捕获任何A到Z。91是
'Z' + 1
。无论您是用数字还是符号来思考,这并不重要。请记住,您必须制作
array [10]
才能说array [0]
toarray [9]
;当您制作数组时,其长度比将使用的最大索引号高1。要存储一个数组,您可以通过“ z”索引'z'+1
long:现在怎么做:
1'a'
counts现在计数 从计数开始?
从文件!
无论chars进来什么顺序,它们都会增加正确的插槽。如果文件具有ABA,则在此结束时,
counts ['a']
为2,而counts ['b']
是1请记住,字符可以是也会增加,因为我们可以向它们添加数字:
这将通过英语字母中的所有字符循环
c
;c
以'a'
开始,然后转到'b'
等等,等等,到/包括'z'< /code>
对此有一个考虑,然后将它们放在一起。也许在考虑了它之后,可以通过将数组从
'a''a'a'
(即0)运行到'z' - 'a'(即25),而不是从
0
到'z'
运行。它可以节省四分之一的千字节,以摆脱少于“ a”的每个炭存储计数的需求 - 对于我来说,用无关紧要的数学添加了混乱,但是一旦您掌握了这个概念您可能想尝试添加它并与字符作为数字等One of the things that makes this particular assignment easier is that C# treats
char
andint
as equivalent for some contexts and operations; characters have a numerical equivalent - the letter A is 65 in decimal numbers, thanks to historically being located at position 65 in the ASCII tableYou can add a character and an integer together;
'A' + 1
is'B'
, or66
if youre looking at them numerically. You can subtract one character from another:'B' - 'A'
is 1If we are dealing with just a file full of letters, A to Z then that's 26 characters, and they all run sequentially. This sounds like an array, right?
A is 65. Z is 90. This means we actually only need an array that is 91 elements big, to be able to capture any A to Z. 91 is
'Z' + 1
. It doesn't really matter whether you think in numbers or chars for this.Remember, you have to make an
array[10]
to be able to sayarray[0]
toarray[9]
; when you make the array its length is 1 more than the largest index number you will use. To store an array you can index by 'Z' you have to make it'Z'+1
long:Now how about doing:
Counts has now counted 1 'A'
So where do you get your chars from to go into the counts?
From the file!
Whatever order the chars come in, they increment the correct slot. If the file has ABA, then at the end of this,
counts['A']
is 2, andcounts['B']
is 1Remember that chars can be incremented too, because we can add numbers to them:
That will loop
c
through all the chars in the English alphabet;c
starts out as'A'
and then goes to'B'
and so on, up to/including'Z'
Have a think about this, and put it all together. Maybe after you've thought about it you can save some memory by having your array run from
'A'-'A'
(i.e. 0) to'Z'-'A'
(i.e. 25) instead of having it run from0
to'Z'
. It would saving a quarter of a kilobyte to get rid of the need to store counts for every char less than 'A' - it wasn't significant enough for me to add confusion to the answer with extraneous math, but once you grok the concept you might want to try adding it in and having a play with chars as numbers etc