天天看點

C# 統計文章中字元的種類和個數 哈希表和字典的使用

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;

namespace Dictionary測試

{

    class Program

    {

        static void Main(string[] args)

        {

           //Hashtable的使用

           Hashtable ht=new Hashtable();

            ht.Add(10141303, "丁小未");

            ht.Add(10141301, "陳立");

            Console.Write("10141301學号對應的同學"+ht[10141301]+"\n");

            //周遊Hashtable

            foreach(DictionaryEntry de in ht) //fileht為一個Hashtable執行個體

            {

                Console.WriteLine(de.Key);//de.Key對應于keyvalue鍵值對key

                Console.WriteLine(de.Value);//de.Key對應于keyvalue鍵值對value

            }

            //Dictionary<char, char> dict = new Dictionary<char, char>();

            Console.WriteLine("請輸入英文:");

            string S = Console.ReadLine();

            string s=S.ToLower();

            Dictionary<char, int> countn = new Dictionary<char, int>();

            //countn.Add('a', 0);

            //countn.Add('b', 0);

            //countn.Add('c', 0);

            //countn.Add('d', 0);

            //countn.Add('e', 0);

            //countn.Add('f', 0);

            //countn.Add('g', 0);

            //countn.Add('h', 0);

            //countn.Add('i', 0);

            //countn.Add('j', 0);

            //countn.Add('k', 0);

            //countn.Add('l', 0);

            //countn.Add('m', 0);

            //countn.Add('n', 0);

            //countn.Add('o', 0);

            //countn.Add('p', 0);

            //countn.Add('q', 0);

            //countn.Add('r', 0);

            //countn.Add('s', 0);

            //countn.Add('t', 0);

            //countn.Add('u', 0);

            //countn.Add('v', 0);

            //countn.Add('w', 0);

            //countn.Add('x', 0);

            //countn.Add('y', 0);

            //countn.Add('z', 0);

            List<char> list = new List<char>();

            foreach (char a in s)

                list.Add(a);

            foreach (char v in list)

                //處理标點或者意外字元

                if (countn.ContainsKey(v))

                {

                    countn[v]++;

                }

                else

                    countn.Add(v, 1);

            計算運作時間的函數

            Stopwatch wp = new Stopwatch();

            wp.Start();

            foreach (KeyValuePair<char,int> keyvalue in countn)

                char a = keyvalue.Key;

                int n = keyvalue.Value;

                Console.WriteLine(a +"   " +n);

            wp.Stop();

            Console.WriteLine(wp.Elapsed);

            Console.Read();

        }    

        #region else輸出列印泛型數組

        /// <sum{mary>

        /// 列印    zw += a;輸出泛型數組

        /// </su}mmary>

        /// <param name="yuju">要求輸出的泛型</param>

        private static void shuchu(List<char> yuju)

            foreach (char a in yuju)

                Console.Write(a);

        } 

        #endregion

    }

}

本文轉自蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366635,如需轉載請自行聯系原作者