Advertisement
Guest User

AiSD4

a guest
Dec 11th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace AiSD4
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.         public Random rand = new Random();
  20.         public int[] E, hashtable1, hashtable2;
  21.         public int N = 499, kol, index, k;
  22.         public int[] Exp(int kol)
  23.         {
  24.             double l = 2, F, x;
  25.             int[] E = new int[N];
  26.             int i = 0;
  27.             while (i != kol)
  28.             {
  29.                 F = rand.NextDouble();
  30.                 x = Math.Log(F) / (-l);
  31.                 x *= 1000;
  32.                 E[i] = (int)Math.Ceiling(x);
  33.                 i++;
  34.             }
  35.             E = E.Distinct().ToArray();
  36.             Array.Resize<int>(ref E, N);
  37.             for (int i1 = 0; i1 < kol; i1++)
  38.             {
  39.                 listBox1.Items.Add(i1 + " " + E[i1]);
  40.             }
  41.             return E;
  42.         }
  43.         public int Hash1(int Key, int N) { return Key % N; }//деление
  44.         public int Find1(int chislo, int[] mass)
  45.         {
  46.             int index = Hash1(chislo, mass.Length);
  47.             int p = 0;
  48.             while (mass[index] != chislo && p < N)
  49.             {
  50.                 if (index != mass.Length - 1)
  51.                     index++;
  52.                 else index = 0;
  53.                 p++;
  54.             }
  55.             if (p == N)
  56.             {
  57.                 return -1;
  58.             }
  59.             return index;
  60.         }
  61.  
  62.         private void button1_Click(object sender, EventArgs e)
  63.         {
  64.             listBox1.Items.Clear();
  65.             listBox2.Items.Clear();
  66.             listBox3.Items.Clear();
  67.             kol = Convert.ToInt32(textBox1.Text);
  68.             E = Exp(kol);
  69.             k = 0;
  70.             hashtable1 = new int[N];//массив под деление
  71.             for (int i = 0; i < kol; i++)
  72.             {
  73.                 int p = 0;
  74.                 index = Hash1(E[i], N);
  75.                 while ((hashtable1[index] != 0))
  76.                 {
  77.                     index += 3 * p + 1 * p * p;
  78.                     if (index > N - 1)
  79.                         index -= (N - 1);
  80.                     k++;
  81.                     p++;
  82.                 }
  83.                 p = 0;
  84.                 hashtable1[index] = E[i];
  85.             }
  86.             listBox4.Items.Add("Д--" + kol + " " + k);
  87.             chart1.Series[0].Points.AddXY(kol, k);
  88.             k = 0;
  89.             for (int i = 0; i < N; i++)
  90.             {
  91.                 if (hashtable1[i] != 0) listBox2.Items.Add(i + " : " + hashtable1[i]);
  92.             }
  93.             hashtable2 = new int[N];//массив под умножение
  94.             for (int i = 0; i < kol; i++)
  95.             {
  96.                 index = Hash2(E[i], N);
  97.                 while ((hashtable2[index] != 0))
  98.                 {
  99.                     if (index != N - 1)
  100.                         index++;
  101.                     else index = 0;
  102.                     k++;
  103.                 }
  104.                 hashtable2[index] = E[i];
  105.             }
  106.             listBox4.Items.Add("У--" + kol + " " + k);
  107.             chart1.Series[1].Points.AddXY(kol, k);
  108.             for (int i = 0; i < N; i++)
  109.             {
  110.                 if (hashtable2[i] != 0) listBox3.Items.Add(i + " : " + hashtable2[i]);
  111.             }
  112.             FindKollision();
  113.         }
  114.  
  115.         public void FindKollision()
  116.         {
  117.             double l = 2, F, x;
  118.             int r = 0;
  119.             int p2 = 0;
  120.             int p1 = 0;
  121.             for(int k = 0; k < 50; k++)
  122.             {
  123.                 F = rand.NextDouble();
  124.                 x = Math.Log(F) / (-l);
  125.                 x *= 1000;
  126.                 r = (int)Math.Ceiling(x);
  127.                 for (int i = 0; i < E.Length; i++)
  128.                 {
  129.                     if (E[i] == r)
  130.                     {
  131.                         break;
  132.                     }
  133.                     if (i == E.Length - 1)
  134.                     {
  135.                         i = 0;
  136.                         F = rand.NextDouble();
  137.                         x = Math.Log(F) / (-l);
  138.                         x *= 1000;
  139.                         r = (int)Math.Ceiling(x);
  140.                     }
  141.                 }
  142.  
  143.                 int index = Hash2(r, E.Length);
  144.                 int curp2 = 0;
  145.                 while (E[index] != r && curp2 < N)
  146.                 {
  147.                     if (index != E.Length - 1)
  148.                         index++;
  149.                     else index = 0;
  150.                     p2++;
  151.                     curp2++;
  152.                 }
  153.  
  154.                 int index1 = Hash1(r, E.Length);
  155.                 int curp1 = 0;
  156.                 while (E[index1] != r && curp1 < N)
  157.                 {
  158.                     if (index1 < E.Length - 1)
  159.                         index1++;
  160.                     else index1 = 0;
  161.                     p1++;
  162.                     curp1++;
  163.                 }
  164.             }
  165.             label8.Text  = "Действий при делении: " + p1.ToString();
  166.             label14.Text = "Действий при сложении: " + p2.ToString();
  167.         }
  168.  
  169.         public int Find2(int chislo, int[] mass)
  170.         {
  171.             int index = Hash2(chislo, mass.Length);
  172.             int p = 0;
  173.             while (mass[index] != chislo && p < N)
  174.             {
  175.                 if (index != mass.Length - 1)
  176.                     index++;
  177.                 else index = 0;
  178.                 p++;
  179.             }
  180.             if(p == N)
  181.             {
  182.                 return -1;
  183.             }
  184.             return index;
  185.         }
  186.         private void button2_Click(object sender, EventArgs e)
  187.         {
  188.             int chislo = Convert.ToInt32(textBox2.Text);
  189.             label6.Text = "Индекс в делении: "   + Convert.ToString(Find1(chislo, hashtable1));
  190.             label7.Text = "Индекс в умножении: " + Convert.ToString(Find2(chislo, hashtable2));
  191.         }
  192.  
  193.         private void button3_Click(object sender, EventArgs e)
  194.         {
  195.             int chislo = Convert.ToInt32(textBox3.Text);
  196.             E[kol] = chislo;
  197.             kol++;
  198.             index = Hash1(chislo, N);
  199.             k = 0;
  200.             while ((hashtable1[index] != 0))
  201.             {
  202.                 if (index != N - 1)
  203.                     index++;
  204.                 else index = 0;
  205.                 k++;
  206.             }
  207.             label9.Text += Convert.ToString(index);
  208.             label11.Text += Convert.ToString(k);
  209.             hashtable1[index] = chislo;
  210.             k = 0;
  211.             index = Hash2(chislo, N);
  212.             while ((hashtable2[index] != 0))
  213.             {
  214.                 if (index != N - 1)
  215.                     index++;
  216.                 else index = 0;
  217.                 k++;
  218.             }
  219.             label10.Text += Convert.ToString(index);
  220.             label12.Text += Convert.ToString(k);
  221.             hashtable2[index] = chislo;
  222.             //------------------------------------
  223.             listBox1.Items.Clear();
  224.             listBox2.Items.Clear();
  225.             listBox3.Items.Clear();
  226.             for (int i = 0; i < kol; i++)
  227.             {
  228.                 listBox1.Items.Add(i + " : " + E[i]);
  229.             }
  230.             for (int i = 0; i < N; i++)
  231.             {
  232.                 if (hashtable1[i] != 0) listBox2.Items.Add(i + " : " + hashtable1[i]);
  233.             }
  234.             for (int i = 0; i < N; i++)
  235.             {
  236.                 if (hashtable2[i] != 0) listBox3.Items.Add(i + " : " + hashtable2[i]);
  237.             }
  238.         }
  239.  
  240.         public int Hash2(int Key, int N)//умножение
  241.         {
  242.             double A = 0.6180339887;
  243.             return (int)Math.Floor(N * (Key * A - Math.Floor(Key * A)));
  244.         }
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement