Advertisement
solidsnake

C#CombinationAlgorithm

May 18th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System.Windows.Forms;    
  2.  
  3.     public partial class Form1 : Form
  4.     {
  5.         private int size;
  6.         private int pop;
  7.         //private int k;
  8.         private List<double> p = new List<double>();
  9.         private List<double> m = new List<double>();
  10.         private int counter;
  11.  
  12.         public Form1()
  13.         {
  14.             InitializeComponent();
  15.         }
  16.         private void button3_Click(object sender, EventArgs e)
  17.         {
  18.             pop = int.Parse(textBox1.Text);
  19.             size = int.Parse(textBox3.Text);
  20.             textBox1.ReadOnly = true;
  21.             textBox3.ReadOnly = true;
  22.         }
  23.  
  24.         private void button1_Click(object sender, EventArgs e)
  25.         {
  26.             if (counter < pop)
  27.             {
  28.                 p.Add(double.Parse(textBox2.Text));
  29.                 textBox2.Text = "";
  30.                 counter++;
  31.                 if (counter == pop)
  32.                 {
  33.                     textBox2.ReadOnly = true;
  34.                     button1.Hide();
  35.                 }
  36.             }
  37.         }
  38.  
  39.         private void button2_Click(object sender, EventArgs e)
  40.         {
  41.             lessCombo(p, 0, 0, 0);
  42.             string str = "";
  43.             str += "Mean - Relative Frequency\n";
  44.             if (m.Count > 100)
  45.             {
  46.                 double[] array = new double[100];
  47.                 for (int b = 0; b < array.Length; b++)
  48.                 {
  49.                     array[b] = m[b];
  50.                 }
  51.                 foreach (var group in array.GroupBy(n => n))
  52.                 {
  53.                     str += string.Format("{0} - {1} \n", group.Key, group.Count());
  54.                 }
  55.             }
  56.             else
  57.             {
  58.                 foreach (var group in m.GroupBy(n => n))
  59.                 {
  60.                     str += string.Format("{0} - {1} \n", group.Key, group.Count());
  61.                 }
  62.             }
  63.             richTextBox1.Text = str;
  64.             button2.Hide();
  65.         }
  66.  
  67.         private void lessCombo(List<double> p, int i, int l, double a)
  68.         {
  69.             if (l == size)
  70.             {
  71.                 double mean = a / size;
  72.                 m.Add(mean);
  73.                
  74.             }
  75.             if (i < p.Count)
  76.             {
  77.                 if (l != size)
  78.                 {
  79.                     lessCombo(p, i + 1, l + 1, a + p[i]);
  80.                     lessCombo(p, i + 1, l, a);
  81.                 }
  82.             }
  83.         }
  84.  
  85.        
  86.  
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement