如何在 C# 中以数组形式显示文件文本?

发布于 2025-01-19 07:44:52 字数 925 浏览 0 评论 0原文

任务:准备一个程序,该程序将形成文本文件中使用的一组字母(数组),并按字母顺序将其显示在屏幕上。

我想制作信件(文件); (文件中的文本)以一个字母显示。但它向我显示“TextFile1.txt”

给出了 ShowLetters 方法。我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2025-01-26 07:44:52

使此特定作业更容易的一件事是,C#对待char and int作为某些上下文和操作等效;字符具有等效的数字 - 字母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] to array [9];当您制作数组时,其长度比将使用的最大索引号高1。要存储一个数组,您可以通过“ z”索引'z'+1 long:

var counts = new int['Z'+1];  

现在怎么做:

counts['A']++; //or could be counts[65], remember that 'A' and 65 are roughly synonymous

1'a'


counts现在计数 从计数开始?

从文件!

foreach(char f in theFileTextAsString){
  counts[f]++;
}

无论chars进来什么顺序,它们都会增加正确的插槽。如果文件具有ABA,则在此结束时,counts ['a']为2,而counts ['b']是1


请记住,字符可以是也会增加,因为我们可以向它们添加数字:

for(char c = 'A'; c <= 'Z'; c++){
    Console.Write(counts[c]);
}

这将通过英语字母中的所有字符循环cc'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 and int 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 table

You can add a character and an integer together; 'A' + 1 is 'B', or 66 if youre looking at them numerically. You can subtract one character from another: 'B' - 'A' is 1

If 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 say array[0] to array[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:

var counts = new int['Z'+1];  

Now how about doing:

counts['A']++; //or could be counts[65], remember that 'A' and 65 are roughly synonymous

Counts has now counted 1 'A'


So where do you get your chars from to go into the counts?

From the file!

foreach(char f in theFileTextAsString){
  counts[f]++;
}

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, and counts['B'] is 1


Remember that chars can be incremented too, because we can add numbers to them:

for(char c = 'A'; c <= 'Z'; c++){
    Console.Write(counts[c]);
}

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 from 0 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文