zoukankan      html  css  js  c++  java
  • 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
        }
    }

    感谢来访,共同学习!
  • 相关阅读:
    vba合并报表(搬一下)
    自选股票并绘制图像
    python代码--利用python进行数据分析里第十四章缺失代码
    python时间戳(1)
    python学习--列替换问题
    python学习--练习题28
    报错记录--004--panda删除添加问题
    python学习--练习题21-27
    python学习--练习题14-20
    python学习--练习题111213--分阶段判断问题、输出指定日期格式问题、统计字符问题
  • 原文地址:https://www.cnblogs.com/dingxiaowei/p/3058834.html
Copyright © 2011-2022 走看看