gpt4 book ai didi

c# - 计算/排序文本文件中的字符

转载 作者:太空宇宙 更新时间:2023-11-03 23:37:00 25 4
gpt4 key购买 nike

我正在尝试编写一个程序来读取文本文件,按字符对其进行排序,并跟踪每个字符在文档中出现的次数。这是我到目前为止所拥有的。

class Program
{
static void Main(string[] args)
{
CharFrequency[] Charfreq = new CharFrequency[128];

try
{
string line;
System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\User\Documents\Visual Studio 2013\Projects\Array_Project\wap.txt");
while ((line = file.ReadLine()) != null)
{
int ch = file.Read();

if (Charfreq.Contains(ch))
{

}
}

file.Close();

Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}

我的问题是,这里的 if 语句中应该包含什么?

我还有一个 Charfrequency 类,我将在此处包含它,以防我包含它有帮助/必要(是的,我有必要使用数组而不是列表或数组列表)。

public class CharFrequency
{
private char m_character;
private long m_count;

public CharFrequency(char ch)
{
Character = ch;
Count = 0;
}

public CharFrequency(char ch, long charCount)
{
Character = ch;
Count = charCount;
}

public char Character
{
set
{
m_character = value;
}

get
{
return m_character;
}
}

public long Count
{
get
{
return m_count;
}
set
{
if (value < 0)
value = 0;

m_count = value;
}
}

public void Increment()
{
m_count++;

}

public override bool Equals(object obj)
{
bool equal = false;
CharFrequency cf = new CharFrequency('\0', 0);

cf = (CharFrequency)obj;

if (this.Character == cf.Character)
equal = true;

return equal;
}

public override int GetHashCode()
{
return m_character.GetHashCode();
}

public override string ToString()
{
String s = String.Format("'{0}' ({1}) = {2}", m_character, (byte)m_character, m_count);

return s;
}

}

最佳答案

关于c# - 计算/排序文本文件中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30291191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com