-3

助けが必要です。基本的に、テキストファイルにテキストの段落があり、テキストファイル(文字列として保存されている)を文字のリストに読み込んで、文字列内に表示される時間を保存する必要があります。したがって、(AZ)の間のリストを作成し、文字が表示される回数に応じて並べ替えます。LINQを使用せずにこれを行う方法はありますか?

ありがとうございました :)

4

2 に答える 2

3

これは、ハッシュテーブルを使用して行うことができます。

Hashtable listofChars = new Hashtable();

for () { // your loop for the chars in the file
        Char c ; // your char
        if (!listofChars.ContainsKey(c))
        {
            listofChars[c] = 1;
        }
        else {
            listofChars[c] = ((int)listofChars["c"]) + 1;
        } 
}

- 編集 -

var listofChars = new SortedDictionary<char, int>();
foreach(char c in File.ReadAllText(fileName))
{
    if (!listofChars.ContainsKey(c))
    {
        listofChars[c] = 1;
    }
    else
    {
        listofChars[c] += 1;
    } 
}
于 2012-08-19T19:38:39.730 に答える
1

効率的で簡単な方法はConcurrentDictionary、charをキーとして、数値を値として表示するaを作成することです。これには、優れたAddOrUpdate(upsert)メソッドがあります。

string text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
var chars = new System.Collections.Concurrent.ConcurrentDictionary<char, int>();
foreach (char c in text)
{
   chars.AddOrUpdate(c, 1, (chr, count) => count + 1);
}

Linq(Lambda!= Linq)なしの番号で注文する場合は、次のコードを使用できます。

List<KeyValuePair<char, int>> charList = chars.ToList();
charList.Sort((firstPair, nextPair) =>
{
    return firstPair.Value.CompareTo(nextPair.Value);
});

編集:少し上に降順の変更を注文する場合:

charList.Sort((firstPair, nextPair) =>
{
    return -(firstPair.Value.CompareTo(nextPair.Value));
});

結果:

Console.Write(string.Join(Environment.NewLine, charList.Select(kv => string.Format("Char={0} Num={1}", kv.Key, kv.Value))));

Char=  Num=90
Char=e Num=59
Char=t Num=43
Char=s Num=39
Char=n Num=38
Char=i Num=32
Char=a Num=28
Char=o Num=25
Char=r Num=24
Char=p Num=18
Char=m Num=18
Char=l Num=17
Char=u Num=17
Char=d Num=16
Char=h Num=14
Char=y Num=13
Char=g Num=11
Char=c Num=10
Char=k Num=7
Char=w Num=6
Char=f Num=6
Char=I Num=6
Char=v Num=5
Char=b Num=5
Char=L Num=5
Char=, Num=4
Char=. Num=4
Char=0 Num=3
Char=x Num=2
Char=1 Num=2
Char=' Num=1
Char=5 Num=1
Char=M Num=1
Char=A Num=1
Char=P Num=1
Char=6 Num=1
Char=9 Num=1
于 2012-08-19T19:55:53.333 に答える