您的位置:首页 > 编程语言 > C#

C# 统计文章中字符的种类和个数 哈希表和字典的使用

2012-11-01 14:37 405 查看
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

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐