计算类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CalCount
{
class CalUtil
{
public static void outputDict(Dictionary<char, int> Dict)
{
foreach(KeyValuePair<char,int> pair in Dict)
{
Console.WriteLine(new string(new char[]{pair.Key}) + ": "+ pair.Value);
}
}
public static Dictionary<char, int> getCount(string calStr)
{
if (calStr == null || calStr.Length == 0)
{
throw new Exception("Can't cal empty string!!");
}
Dictionary<char, int> retDict = new Dictionary<char, int>();
foreach (char c in calStr)
{
int oldValue;
retDict.TryGetValue(c,out oldValue);
retDict[c] = ++oldValue;
}
return retDict;
}
}
}
主程序类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CalCount
{
class Program
{
static void Main(string[] args)
{
CalUtil.outputDict(CalUtil.getCount("hello,World!!!"));
Console.ReadKey();
}
}
}
程序输出: