C# Hashtable应用

来源:互联网 发布:淘宝加购物车无提醒 编辑:程序博客网 时间:2024/06/10 17:19
/*编写一个程序,创建哈希集合实例,内含部分国家的名称和首都,提示用户输入一个国家名称,则在哈希集合中查找该国都名称并输出。*/using System;using System.Collections;using System.Linq;using System.Text;namespace ConsoleApplication2{    class Program    {        static void Main(string[] args)        {            Hashtable hst = new Hashtable();            hst.Add("中国", "北京");            hst.Add("英国", "伦敦");            hst.Add("美国", "华盛顿");            hst.Add("法国", "巴黎");            hst.Add("日本", "东京");            Console.WriteLine("输入一个国家名称:");            string s = Console.ReadLine();            if (hst.Contains(s))            {                 foreach (DictionaryEntry item in hst)             {                     if (s == item.Key.ToString())                         Console.WriteLine(item.Value);                 }             }            else            {                Console.WriteLine("无要查找的国家!");            }             Console.ReadKey();        }    }}

运行结果:


/*Hashtable基本操作。*/using System;using System.Collections.Generic;using System.Collections;using System.Linq;using System.Text;namespace ConsoleApplication2{    class Program    {        static void Main(string[] args)        {            Hashtable hst = new Hashtable();            hst.Add("郭晓东", 235);            if (!hst.Contains("赵刚"))            {                hst.Add("赵刚", 143);            }            hst["郭晓东"] = 200;   //修改元素      //通过键来访问哈希表中的值,即Hashtable[Key].            hst["赵刚"] = (int)hst["赵刚"] + 10;            foreach (DictionaryEntry item in hst)  //输出元素,DictionaryEntry进行遍历            {                Console.WriteLine("{0},{1}", item.Key, item.Value);            }            hst.Remove("赵刚");    //移除元素            foreach (DictionaryEntry item in hst)  //输出元素,DictionaryEntry进行遍历            {                Console.WriteLine("{0},{1}", item.Key, item.Value);            }            Console.ReadKey();        }    }}

运行结果:


/*输入学号和姓名,对不存在的学号加到hashtable类实例中,对存在学号给出提示。结束输入后,输出学号为奇数的所有学生。*/using System;using System.Collections.Generic;using System.Collections;using System.Linq;using System.Text;namespace ConsoleApplication2{    class Program    {        static void Main(string[] args)        {            Hashtable hst = new Hashtable();            int nums;            string name = "";            string s;            Console.WriteLine("请输入学生信息(以0作为结束):");            do            {                s = Console.ReadLine();                string[] c = s.Split(' ');                nums = Convert.ToInt32(c[0]);                if (nums == 0)                    break;                name = c[1];                if (!hst.Contains(nums))                {                    hst.Add(nums, name);                }                else                {                    Console.WriteLine("学号已存在!");                }            } while (true);            Console.WriteLine("输出学号为奇数的所有学生:");            foreach (DictionaryEntry item in hst)            {                if ((Convert.ToInt32(item.Key.ToString())) % 2 != 0)                {                    Console.WriteLine("{0},{1}", item.Key, item.Value);                }            }            Console.ReadKey();        }    }}

运行结果:


/*车辆进出闸口自动刷卡扣费,Hashtable哈希表用于检查是否重复读卡的问题。问题的提出:由于进出闸口读卡器距离很近,存在刷一次卡,被入口读卡器、出口读卡器同时读到的问题。解决方法是:由于哈希表的key为卡的ID+IP、value为时间。*/using System;using System.Collections.Generic;using System.Collections;using System.Linq;using System.Text;namespace ConsoleApplication2{    class Program    {        static Hashtable hst = new Hashtable();        public static bool Repeat(string key, int interval)        {            try            {                if (!hst.Contains(key))    //如果key未保存在哈希表中                {                    hst.Add(key, DateTime.Now);   //即第一次刷卡进入                    return false;                }                //如果key已存在哈希表中,则存在两种情况:                //超过时间间隔的(可能是返回车辆)、重复读卡                //其中,重复读卡又可能是:同一读卡器、不同读卡器                TimeSpan ts = DateTime.Now - Convert.ToDateTime(hst[key]);                if (ts.TotalSeconds > interval)                {                    hst[key] = DateTime.Now;                    return false;                }                return true;            }            catch (Exception ex) { throw ex; }        }        static void Main(string[] args)        {            Console.WriteLine(Repeat("KEY1", 3));  //interval设置为3秒            System.Threading.Thread.Sleep(4000);   //使用线程延迟4秒            Console.WriteLine(Repeat("KEY1", 3));            Console.ReadKey();        }    }}

运行结果:


0 0
原创粉丝点击