Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Windows.Forms;
- using System.Diagnostics;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- Thread genperm;
- Thread monitor;
- static Stopwatch czas_pracy;
- static int uplynelo = 0;
- static int kiedykoniec = 0;
- public static int wynik_silnia;
- static bool czyzwiekszono = false;
- static float przetworzono;
- int g;
- public static int n;
- public static int[] perm = new int[100];
- public static float L = 0;
- static void perminit(int n, int[] perm)
- {
- for (int i = 1; i <= n; i++) perm[i] = i;
- }
- static bool permNext(int n, int[] perm)
- {
- int i, j, x;
- bool b = false;
- i = n;
- while (i > 1)
- {
- if (perm[i] > perm[i - 1])
- {
- j = n;
- while (perm[j] < perm[i - 1]) j--;
- x = perm[j]; perm[j] = perm[i - 1]; perm[i - 1] = x;
- while (i < n)
- {
- x = perm[i]; perm[i] = perm[n]; perm[n] = x;
- i++; n--;
- }
- b = true; break;
- }
- i--;
- }
- czas_pracy.Stop();
- uplynelo = Convert.ToInt32(czas_pracy.ElapsedMilliseconds);
- return b;
- }
- static void druk(int n, int[] perm)
- {
- for (int i = 1; i <= n; i++) Console.Write(perm[i] + " ");
- Console.WriteLine();
- }
- static void generuj_permutacje()
- {
- czas_pracy = new Stopwatch();
- perminit(n, perm);
- do
- {
- czas_pracy.Start();
- L++;
- czyzwiekszono = true;
- druk(n, perm);
- while (czyzwiekszono)
- {
- }
- } while (permNext(n, perm));
- }
- private static int silnia(int i)
- {
- if (i < 1)
- return 1;
- else return i * silnia(i - 1);
- }
- private void sprawdz_postepy_programu_wspolnieznie()
- {
- while (L < wynik_silnia)
- {
- if (czyzwiekszono)
- {
- przetworzono = ((L / wynik_silnia) * 100);
- g = (int)przetworzono;
- MethodInvoker action1 = delegate { progressBar1.Value = g; };
- progressBar1.BeginInvoke(action1);
- MethodInvoker action2 = delegate { label7.Text = ((g).ToString() + " %"); };
- progressBar1.BeginInvoke(action2);
- MethodInvoker action3 = delegate { label5.Text = (((uplynelo / 1000)).ToString()) + " sekund"; };
- progressBar1.BeginInvoke(action3);
- if (g != 0)
- {
- kiedykoniec = ((((uplynelo / 1000) / g) * 100) - (uplynelo / 1000));
- MethodInvoker action4 = delegate { label7.Text = (kiedykoniec).ToString() + " sekund"; };
- progressBar1.BeginInvoke(action4);
- }
- czyzwiekszono = false;
- }
- }
- }
- private void button1_Click_1(object sender, EventArgs e)
- {
- n = Int32.Parse(textBox1.Text);
- wynik_silnia = silnia(n);
- textBox2.Text = (wynik_silnia).ToString();
- genperm = new Thread(generuj_permutacje);
- genperm.Start();
- monitor = new Thread(sprawdz_postepy_programu_wspolnieznie);
- monitor.Start();
- }
- }
- }
Add Comment
Please, Sign In to add comment