哈 希 表 的 操 作

 

  1 None.gif using  System;
  2 None.gif using  System.Drawing;
  3 None.gif using  System.Collections;
  4 None.gif using  System.ComponentModel;
  5 None.gif using  System.Windows.Forms;
  6 None.gif using  System.Data;
  7 None.gif    
  8 None.gif
  9 None.gif namespace  哈希表
 10 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 12InBlock.gif    /// Form1 的摘要说明。
 13ExpandedSubBlockEnd.gif    /// </summary>

 14InBlock.gif    public class Form1 : System.Windows.Forms.Form
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 16InBlock.gif        private System.Windows.Forms.Button button1;
 17InBlock.gif        private System.Windows.Forms.RichTextBox richTextBox1;
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 19InBlock.gif        /// 必需的设计器变量。
 20ExpandedSubBlockEnd.gif        /// </summary>

 21InBlock.gif        private System.ComponentModel.Container components = null;
 22InBlock.gif
 23InBlock.gif        public Form1()
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 25InBlock.gif            //
 26InBlock.gif            // Windows 窗体设计器支持所必需的
 27InBlock.gif            //
 28InBlock.gif            InitializeComponent();
 29InBlock.gif
 30InBlock.gif            //
 31InBlock.gif            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 32InBlock.gif            //
 33ExpandedSubBlockEnd.gif        }

 34InBlock.gif
 35ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 36InBlock.gif        /// 清理所有正在使用的资源。
 37ExpandedSubBlockEnd.gif        /// </summary>

 38InBlock.gif        protected override void Dispose( bool disposing )
 39ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 40InBlock.gif            if( disposing )
 41ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 42InBlock.gif                if (components != null
 43ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 44InBlock.gif                    components.Dispose();
 45ExpandedSubBlockEnd.gif                }

 46ExpandedSubBlockEnd.gif            }

 47InBlock.gif            base.Dispose( disposing );
 48ExpandedSubBlockEnd.gif        }

 49InBlock.gif
 50ContractedSubBlock.gifExpandedSubBlockStart.gif        Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 52InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 53InBlock.gif        /// 此方法的内容。
 54ExpandedSubBlockEnd.gif        /// </summary>

 55InBlock.gif        private void InitializeComponent()
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 57InBlock.gif            this.button1 = new System.Windows.Forms.Button();
 58InBlock.gif            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
 59InBlock.gif            this.SuspendLayout();
 60InBlock.gif            // 
 61InBlock.gif            // button1
 62InBlock.gif            // 
 63InBlock.gif            this.button1.Location = new System.Drawing.Point(6328);
 64InBlock.gif            this.button1.Name = "button1";
 65InBlock.gif            this.button1.Size = new System.Drawing.Size(12840);
 66InBlock.gif            this.button1.TabIndex = 0;
 67InBlock.gif            this.button1.Text = "button1";
 68InBlock.gif            this.button1.Click += new System.EventHandler(this.button1_Click);
 69InBlock.gif            // 
 70InBlock.gif            // richTextBox1
 71InBlock.gif            // 
 72InBlock.gif            this.richTextBox1.Location = new System.Drawing.Point(2432);
 73InBlock.gif            this.richTextBox1.Name = "richTextBox1";
 74InBlock.gif            this.richTextBox1.Size = new System.Drawing.Size(584448);
 75InBlock.gif            this.richTextBox1.TabIndex = 1;
 76InBlock.gif            this.richTextBox1.Text = "";
 77InBlock.gif            // 
 78InBlock.gif            // Form1
 79InBlock.gif            // 
 80InBlock.gif            this.AutoScaleBaseSize = new System.Drawing.Size(614);
 81InBlock.gif            this.ClientSize = new System.Drawing.Size(768494);
 82InBlock.gif            this.Controls.Add(this.richTextBox1);
 83InBlock.gif            this.Controls.Add(this.button1);
 84InBlock.gif            this.Name = "Form1";
 85InBlock.gif            this.Text = "Form1";
 86InBlock.gif            this.ResumeLayout(false);
 87InBlock.gif
 88ExpandedSubBlockEnd.gif        }

 89ExpandedSubBlockEnd.gif        #endregion

 90InBlock.gif
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 92InBlock.gif        /// 应用程序的主入口点。
 93ExpandedSubBlockEnd.gif        /// </summary>

 94InBlock.gif        [STAThread]
 95InBlock.gif        static void Main() 
 96ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 97InBlock.gif            Application.Run(new Form1());
 98ExpandedSubBlockEnd.gif        }

 99InBlock.gif
100InBlock.gif//        每个元素是一个存储在 DictionaryEntry 对象中的键/值对。键不能为空引用(Visual Basic 中为 Nothing),但值可以。
101InBlock.gif//
102InBlock.gif//        用作 Hashtable 中的键的对象必须实现或继承 Object.GetHashCode 和 Object.Equals 方法。如果键相等性只是引用相等性,这些方法的继承实现将满足需要。此外,如果该键存在于 Hashtable 中,那么当使用相同参数调用这些方法时,这些方法必须生成相同的结果。只要键对象用作 Hashtable 中的键,它们就必须是永远不变的。
103InBlock.gif//
104InBlock.gif//        当把某个元素添加到 Hashtable 时,将根据键的哈希代码将该元素放入存储桶中。该键的后续查找将使用键的哈希代码只在一个特定存储桶中搜索,这将大大减少为查找一个元素所需的键比较的次数。
105InBlock.gif//
106InBlock.gif//        Hashtable 的加载因子确定元素与存储桶的最大比率。加载因子越小,平均查找速度越快,但消耗的内存也增加。默认的加载因子 1.0 通常提供速度和大小之间的最佳平衡。当创建 Hashtable 时,也可以指定其他加载因子。
107InBlock.gif//
108InBlock.gif//        当向 Hashtable 添加元素时,Hashtable 的实际加载因子将增加。当实际加载因子达到此加载因子时,Hashtable 中存储桶的数目自动增加到大于当前 Hashtable 存储桶数两倍的最小质数。
109InBlock.gif//
110InBlock.gif//        Hashtable 中的每个键对象必须提供其自己的哈希函数,可通过调用 GetHash 访问该函数。但是,可将任何实现 IHashCodeProvider 的对象传递到 Hashtable 构造函数,而且该哈希函数用于该表中的所有对象。
111InBlock.gif
112InBlock.gif       
113InBlock.gif 
114InBlock.gif        private void button1_Click(object sender, System.EventArgs e)
115ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
116InBlock.gif
117InBlock.gif            //为哈希表增加值
118InBlock.gif            this.richTextBox1.Text =""
119InBlock.gif            // Creates and initializes a new Hashtable.
120InBlock.gif            Hashtable myHT = new Hashtable();
121InBlock.gif            for(int j=0;j<20;j++)
122InBlock.gif            myHT.Add("frj"+ j.ToString() ,j ); //注意如果第二个参数采用非数值型将不能用于后面的数据加运算(Dchart.Data  =myHT).
123InBlock.gif
124InBlock.gif            // Displays the properties and values of the Hashtable.
125InBlock.gif            this.richTextBox1.Text += ( "我的哈希表:" )+"\n";
126InBlock.gif            this.richTextBox1.Text +="  Count:    "+ myHT.Count.ToString()+"\n"  );
127InBlock.gif            this.richTextBox1.Text +="\tKeys and Values:\n" );
128InBlock.gif            PrintKeysAndValues( myHT );
129InBlock.gif
130InBlock.gif            //将哈希表转为一维键/值对数组
131InBlock.gif            //这样将方便直接引用
132InBlock.gif             
133InBlock.gif            this.richTextBox1.Text +="将哈希表转换所得: \n";
134InBlock.gif
135InBlock.gif            DrawPieChart Dchart=new DrawPieChart(); 
136InBlock.gif            
137InBlock.gif            Dchart.Data  =myHT;
138InBlock.gif       
139InBlock.gif            for (int i=1;i<Dchart.data.Length ;i++)
140InBlock.gif                this.richTextBox1.Text +=("\t" +Dchart.data.Keys[i].ToString()+"\t" +Dchart.data.Values[i].ToString()+"\n" );
141InBlock.gif            
142InBlock.gif
143InBlock.gif            this.richTextBox1.Text +="取第 6 个数: "+ Dchart.data.Values[6].ToString()+"\n"+"各数总和 : "+Dchart.data.TotalValue.ToString()    ;   
144InBlock.gif
145InBlock.gif
146ExpandedSubBlockEnd.gif        }

147InBlock.gif
148InBlock.gif        
149InBlock.gif
150InBlock.gif            public  void PrintKeysAndValues( Hashtable myList )  
151ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
152InBlock.gif                IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
153InBlock.gif                this.richTextBox1.Text +="\t-KEY-\t-VALUE-\n" );
154InBlock.gif                while ( myEnumerator.MoveNext() )
155InBlock.gif                    this.richTextBox1.Text +=("\t" +myEnumerator.Key.ToString()+"\t" + myEnumerator.Value.ToString()+"\n" );
156InBlock.gif                this.richTextBox1.Text+="另一种遍历方法\n" ; 
157InBlock.gif                 
158InBlock.gif                this.richTextBox1.Text+="总个数:\t"+myList.Count.ToString()+"\n";
159InBlock.gif                foreach (DictionaryEntry myDE in  myList) 
160ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
161InBlock.gif                      
162InBlock.gif                    this.richTextBox1.Text +=("\t" +myDE.Key.ToString()+"\t" + myDE.Value.ToString()+"\n" );
163InBlock.gif            
164ExpandedSubBlockEnd.gif                }

165InBlock.gif
166ExpandedSubBlockEnd.gif            }

167InBlock.gif        
168InBlock.gif        
169InBlock.gif
170ExpandedSubBlockEnd.gif    }

171InBlock.gif
172InBlock.gif    
173InBlock.gif    public abstract class mrfuChart : Control   //创建一个抽象类
174ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
175InBlock.gif 
176InBlock.gif        public bool DataIsSorted = false;
177InBlock.gif        public bool DataIsGrouped = true;
178InBlock.gif        public bool IsStretch = true;
179InBlock.gif        protected const int MINIMUM_PIECE = 1;
180InBlock.gif        protected const int SMALLEST_DISPLAY_IN_PERCENT = 2;
181InBlock.gif       // protected StringInt64PairedArray data;
182InBlock.gif        public StringInt64PairedArray data;
183InBlock.gif        public IDictionary Data
184ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
185InBlock.gif            set
186ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
187InBlock.gif                data = new StringInt64PairedArray (value);
188InBlock.gif                
189InBlock.gif                int i=data.Length ;
190InBlock.gif                DataIsSorted=true;
191InBlock.gif                DataIsGrouped=true;
192InBlock.gif                if (DataIsSorted) data.SortValuesDesc ();
193InBlock.gif                if (DataIsGrouped) data.GroupValues (MINIMUM_PIECE, 
194InBlock.gif                                       SMALLEST_DISPLAY_IN_PERCENT);
195InBlock.gif            
196ExpandedSubBlockEnd.gif            }

197ExpandedSubBlockEnd.gif        }

198InBlock.gif        
199InBlock.gif        public mrfuChart ()
200ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
201InBlock.gif            Width = 100;
202InBlock.gif            Height = 100;
203ExpandedSubBlockEnd.gif        }

204InBlock.gif        
205ExpandedSubBlockEnd.gif    }

206InBlock.gif
207InBlock.gif    public class DrawPieChart : mrfuChart  //绘制饼状图  继承自抽象类
208ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
209InBlock.gif        //定义自已的相关操作
210InBlock.gif//        public int Diameter { get {return _diameter;} set {_diameter = value;}}
211InBlock.gif//        private int _diameter = 100;
212InBlock.gif//
213InBlock.gif//        // Method to draw the pie chart 
214InBlock.gif//        protected override void DrawChart()
215InBlock.gif//        {
216InBlock.gif//            // Calculate the size
217InBlock.gif//            int d  = (int) Math.Min (LEFT_SECTION_RATIO * Width, 0.9 * Height);
218InBlock.gif//            _diameter = (int) Math.Min (d, CHART_WIDTH_MAX);
219InBlock.gif//            ChartWidth = _diameter;
220InBlock.gif//
221InBlock.gif//            int topX = (int) (SPACE_RATIO * Width / 2);
222InBlock.gif//            int topY = (int) ((Height - _diameter) / 2);
223InBlock.gif//            int startAngle = -90;
224InBlock.gif//            int sweepAngle = 0;
225InBlock.gif//
226InBlock.gif//            // Loop to draw the Pies
227InBlock.gif//            for (int i=0; i<data.Length; i++)
228InBlock.gif//            {
229InBlock.gif//                Brush theBrush = brush[i % brush.Length];
230InBlock.gif//
231InBlock.gif//                if (i < data.Keys.Length-1)
232InBlock.gif//                    sweepAngle = (int) Math.Round( (float) data.Values[i] * 360 / data.TotalValue);
233InBlock.gif//                else
234InBlock.gif//                {
235InBlock.gif//                    sweepAngle = 270 - startAngle;
236InBlock.gif//                    if (data.IsGrouped)
237InBlock.gif//                        theBrush = Brushes.Gold;
238InBlock.gif//                }
239InBlock.gif//                graphics.FillPie (theBrush, topX, topY, 
240InBlock.gif//                    _diameter, _diameter, startAngle, sweepAngle);
241InBlock.gif//                startAngle += (int) sweepAngle;
242InBlock.gif//                startAngle = (startAngle>=360) ? startAngle - 360 : startAngle;
243InBlock.gif//            }
244InBlock.gif//        }
245InBlock.gif
246InBlock.gif
247ExpandedSubBlockEnd.gif    }

248InBlock.gif
249InBlock.gif
250InBlock.gif    public class StringInt64PairedArray   //对哈希表转换成包含自己所要获取的相关数据集合的类
251ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
252ExpandedSubBlockStart.gifContractedSubBlock.gif        public string[] Keys dot.gifget dot.gif{return _keys;} }
253ExpandedSubBlockStart.gifContractedSubBlock.gif        public long[] Values dot.gifget dot.gif{return _values;} }
254ExpandedSubBlockStart.gifContractedSubBlock.gif        public long TotalValue dot.gifget dot.gif{return _totalValue;} }
255ExpandedSubBlockStart.gifContractedSubBlock.gif        public int Length dot.gif{get dot.gif{return Keys.Length;} }
256ExpandedSubBlockStart.gifContractedSubBlock.gif        public bool IsGrouped dot.gif{get dot.gif{return _isGrouped;} }
257InBlock.gif
258InBlock.gif        private long _totalValue = 0;
259InBlock.gif        private string[] _keys;
260InBlock.gif        private long[] _values;
261InBlock.gif        private bool _isSortedDesc;
262InBlock.gif        private bool _isGrouped;
263InBlock.gif 
264InBlock.gif        public StringInt64PairedArray (IDictionary pData)
265ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
266InBlock.gif            _keys = new string [pData.Count];
267InBlock.gif            _values = new long[pData.Count];
268InBlock.gif            pData.Keys.CopyTo (_keys, 0);
269InBlock.gif            pData.Values.CopyTo (_values, 0);
270InBlock.gif            for (int i=0; i<_values.Length; i++
271InBlock.gif                _totalValue += _values[i];
272InBlock.gif            _isSortedDesc = false;
273InBlock.gif            _isGrouped = false;
274ExpandedSubBlockEnd.gif        }

275InBlock.gif
276InBlock.gif        public void SortValuesDesc()
277ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
278InBlock.gif            Array.Sort (_values, _keys, new DescendingComparer());  //DescendingComparer
279InBlock.gif            _isSortedDesc = true;
280ExpandedSubBlockEnd.gif        }

281InBlock.gif
282InBlock.gif        public void GroupValues(int pCountMinimum, int pPercentMinimum)
283ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
284InBlock.gif            if (!_isSortedDesc)
285InBlock.gif                SortValuesDesc();
286InBlock.gif
287InBlock.gif            bool boolStop = false;
288InBlock.gif            long sum = 0;
289InBlock.gif            int i = 0;
290InBlock.gif            while (i < _keys.Length && !boolStop)
291ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
292InBlock.gif                if (i<pCountMinimum)
293ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
294InBlock.gif                    sum += _values[i];
295ExpandedSubBlockEnd.gif                }

296InBlock.gif                else
297ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
298InBlock.gif                    sum += _values[i];
299InBlock.gif                    float percent = _values[i] * 100 / (float) _totalValue;
300InBlock.gif                    if (percent < pPercentMinimum)
301ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
302InBlock.gif                        long[] arTemp1 = new long[i+1];
303InBlock.gif                        string[] arTemp2 = new string[i+1];
304InBlock.gif
305InBlock.gif                        Array.Copy (_values, arTemp1, i+1);
306InBlock.gif                        Array.Copy (_keys, arTemp2, i+1);
307InBlock.gif                        _values = arTemp1;
308InBlock.gif                        _keys = arTemp2;
309InBlock.gif                        _values[i] = _totalValue - sum;
310InBlock.gif                        _keys[i] = "Others";
311InBlock.gif                        boolStop = true;
312InBlock.gif                        _isGrouped = true;
313ExpandedSubBlockEnd.gif                    }

314ExpandedSubBlockEnd.gif                }

315InBlock.gif                i++;
316ExpandedSubBlockEnd.gif            }

317ExpandedSubBlockEnd.gif        }

318ExpandedSubBlockEnd.gif    }

319InBlock.gif
320InBlock.gif    class DescendingComparer : IComparer
321ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
322InBlock.gif        public int Compare (Object x, Object y)
323ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
324InBlock.gif            return Decimal.Compare ((long) y, (long) x);
325ExpandedSubBlockEnd.gif        }

326ExpandedSubBlockEnd.gif    }

327ExpandedBlockEnd.gif}

328 None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值