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
        }
    }

  • 相关阅读:
    747. Largest Number Greater Than Twice of Others 最大数量大于其他两倍
    Python代码规范和命名规范
    Pycharm2018中DataBase的使用
    Pycharm新建文件css文件无后缀,html中无法正确引入
    Pycharm DataBase Navigator Plugins 使用
    Python怎么去写单元测试用例去测试hello world呢
    最近的电视剧推荐
    RobotFrameWork基本语法练习
    RobotFrameWork中使用自定义关键字
    Windows下同时安装了Python2与Python3时如何使用RobotFrameWork
  • 原文地址:https://www.cnblogs.com/java20130723/p/3211552.html
Copyright © 2011-2022 走看看