Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace WindowsFormsApplication1
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         Thread thr;
  18.         Stopwatch stoper = new Stopwatch();
  19.         int n, sil, progres, czas;
  20.  
  21.         int stan_value = 0; //0 - stop, 1 - run, 2 - pause
  22.         int stan
  23.         {
  24.             get { return stan_value; }
  25.             set
  26.             {
  27.                 stan_value = value;
  28.                 zmiana_stanu();
  29.             }
  30.         }
  31.  
  32.         int silnia(int n)
  33.         {
  34.             int s = 1;
  35.             for (int i = 1; i <= n; i++)
  36.             {
  37.                 s *= i;
  38.             }
  39.             return s;
  40.         }
  41.  
  42.         void zmiana_stanu()
  43.         {
  44.             switch (stan)
  45.             {
  46.                 case 0: //stop
  47.                     stoper.Stop();
  48.                     numericUpDown1.Enabled = true;
  49.                     button1.Enabled = true;
  50.                     button2.Enabled = false;
  51.                     button3.Enabled = false;
  52.                     break;
  53.                 case 1: //run
  54.                     stoper.Start();
  55.                     numericUpDown1.Enabled = false;
  56.                     button1.Enabled = false;
  57.                     button2.Enabled = true;
  58.                     button3.Enabled = true;
  59.                     break;
  60.                 case 2: //pause
  61.                     stoper.Stop();
  62.                     numericUpDown1.Enabled = false;
  63.                     button1.Enabled = true;
  64.                     button2.Enabled = false;
  65.                     button3.Enabled = false;
  66.                     break;
  67.             }
  68.         }
  69.  
  70.         void reset()
  71.         {
  72.             progres = 0;
  73.             czas = 0;
  74.             progressBar1.Value = 0;
  75.             stoper.Reset();
  76.             textBox1.Clear();
  77.             label3.Text = "0 %";
  78.             label4.Text = "Czas od początku obliczeń: " + stoper.Elapsed;
  79.             label5.Text = "Czas do zakończenia obliczeń: 0 s";
  80.             stan = 0;
  81.         }
  82.  
  83.         void wyswietl(int[] a)
  84.         {
  85.             string perm = "";
  86.             for (int i = 0; i < a.Length; i++)
  87.                 perm += a[i] + " ";
  88.  
  89.             progres++;
  90.             czas = (int)((sil - progres) * stoper.ElapsedMilliseconds / progres) / 1000;
  91.  
  92.             textBox1.Invoke(new MethodInvoker(() => textBox1.AppendText(perm + "\n")));
  93.             progressBar1.Invoke(new MethodInvoker(() => progressBar1.Value = progres));
  94.             label3.Invoke(new MethodInvoker(() => label3.Text = string.Format("{0} %", (int)((float)progres / sil * 100))));
  95.             label4.Invoke(new MethodInvoker(() => label4.Text = "Czas od początku obliczeń: " + stoper.Elapsed));
  96.             if (czas < 60)
  97.                 label5.Invoke(new MethodInvoker(() => label5.Text = "Czas do zakończenia obliczeń: " + czas + " s"));
  98.             else
  99.                 label5.Invoke(new MethodInvoker(() => label5.Text = "Czas do zakończenia obliczeń: " + czas / 60 + " min " + czas % 60 + " s"));
  100.         }
  101.  
  102.         void koniec()
  103.         {
  104.             stan_value = 0; //metoda zmiana_stanu tu nie działa, trzeba to zrobić ręcznie.
  105.             stoper.Stop();
  106.             numericUpDown1.Invoke(new MethodInvoker(() => numericUpDown1.Enabled = true));
  107.             button1.Invoke(new MethodInvoker(() => button1.Enabled = true));
  108.             button2.Invoke(new MethodInvoker(() => button2.Enabled = false));
  109.             button3.Invoke(new MethodInvoker(() => button3.Enabled = false));
  110.         }
  111.  
  112.         void QuickPerm() //http://www.c-sharpcorner.com/Forums/Thread/213338/C-Sharp-windows-form-application-pleasee-helllppp.aspx
  113.         {
  114.             int[] a = new int[n];
  115.             int[] p = new int[n]; // all zero by default
  116.             int i, j, tmp;
  117.             for (i = 0; i < n; i++)
  118.                 a[i] = i + 1;
  119.             wyswietl(a);
  120.             i = 1;
  121.             while (i < n)
  122.             {
  123.                 if (p[i] < i)
  124.                 {
  125.                     j = i % 2 * p[i];
  126.                     tmp = a[j];
  127.                     a[j] = a[i];
  128.                     a[i] = tmp;
  129.                     wyswietl(a);
  130.                     p[i]++;
  131.                     i = 1;
  132.                 }
  133.                 else
  134.                 {
  135.                     p[i] = 0;
  136.                     i++;
  137.                 }
  138.             }
  139.             koniec();
  140.         }
  141.  
  142.         private void numericUpDown1_ValueChanged(object sender, EventArgs e)
  143.         {
  144.             label2.Text = "n! = " + silnia((int)numericUpDown1.Value).ToString();
  145.         }
  146.  
  147.         private void button1_Click(object sender, EventArgs e) //Start
  148.         {
  149.             if (stan == 2)
  150.             {
  151.                 try
  152.                 {
  153.                     thr.Resume();
  154.                     stan = 1;
  155.                 }
  156.                 catch { }
  157.             }
  158.             else
  159.             {
  160.                 reset();
  161.                 n = (int)numericUpDown1.Value;
  162.                 sil = silnia(n);
  163.                 progressBar1.Maximum = sil;
  164.                 thr = new Thread(QuickPerm);
  165.                 thr.Start();
  166.                 stan = 1;
  167.             }
  168.         }
  169.  
  170.         private void button2_Click(object sender, EventArgs e) //Pauza
  171.         {
  172.             try
  173.             {
  174.                 thr.Suspend();
  175.                 stan = 2;
  176.             }
  177.             catch { }
  178.         }
  179.  
  180.         private void button3_Click(object sender, EventArgs e) //Stop
  181.         {
  182.             try
  183.             {
  184.                 thr.Abort();
  185.                 stan = 0;
  186.             }
  187.             catch { }
  188.             reset();
  189.         }
  190.  
  191.         public Form1()
  192.         {
  193.             InitializeComponent();
  194.             reset();
  195.         }
  196.  
  197.         /*
  198.         void swap(int a, int b)
  199.         {
  200.             int tmp = list[a];
  201.             list[a] = list[b];
  202.             list[b] = tmp;
  203.         }
  204.  
  205.         void perm(int k) //Heap's algorithm
  206.         {
  207.             if (k == 0)
  208.                 wyswietl_liste();
  209.             else
  210.             {
  211.                 for (int i = 0; i <= k; i++)
  212.                 {
  213.                     swap(i, k);
  214.                     perm(k - 1);
  215.                     swap(i, k);
  216.                 }
  217.             }
  218.         }
  219.         */
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement