using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace hashOpen { public partial class Form1 : Form { private System.ComponentModel.Container components = null; private const int HASHVALUE = 16; private string[] Bucket = new string[HASHVALUE * 2]; public Form1() { InitializeComponent(); } private void ハッシュ登録(string Str, int HashMod) { int ID = ハッシュ値(Str, HashMod); while (Bucket[ID] != "\r" && Bucket[ID] != "\n" && ID < Bucket.Length) { if (Str == Bucket[ID]) { MessageBox.Show("同一文字列が登録されています"); return; } ID++; } if (ID >= Bucket.Length) MessageBox.Show("登録できません"); else Bucket[ID] = Str; } private int ハッシュ探索(string Str, int HashMod) { int ID = ハッシュ値(Str, HashMod); while (Bucket[ID] != "\r" && ID < Bucket.Length) { if (Str == Bucket[ID]) return ID; ID++; } return -1; } private void ハッシュ削除(string Str, int HashMod) { int ID = ハッシュ探索(Str, HashMod); if (ID >= 0) Bucket[ID] = "\n"; } private void ハッシュ表示() { listBox1.Items.Clear(); for (int i = 0; i < Bucket.Length; i++) { if (Bucket[i] == "\r") listBox1.Items.Add("----"); else if (Bucket[i] == "\n") listBox1.Items.Add("****"); else listBox1.Items.Add(Bucket[i]); } } private void button1_Click(object sender, System.EventArgs e) { string V = textBox1.Text; ハッシュ登録(V, HASHVALUE); ハッシュ表示(); } private int ハッシュ値(string S, int H) { int CH, i; CH = 0; for (i = 0; i < S.Length; i++) { CH += (int)(S[i] % H); CH %= H; } return CH; } private void Form1_Load(object sender, System.EventArgs e) { for (int i = 0; i < Bucket.Length; i++) { Bucket[i] = "\r"; } ハッシュ表示(); } private void button2_Click(object sender, System.EventArgs e) { string V = textBox1.Text; int ID = ハッシュ探索(V, HASHVALUE); listBox1.SelectedIndex = ID; } private void button3_Click(object sender, System.EventArgs e) { string V = textBox1.Text; ハッシュ削除(V, HASHVALUE); ハッシュ表示(); } private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) { textBox1.Text = listBox1.Text; } } }