ozimas

PO zad. 4.1

Jan 20th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 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.Threading;
  10. using System.Windows.Forms;
  11. using System.Diagnostics;
  12.  
  13. namespace WindowsFormsApplication1
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         Thread genperm;
  23.         Thread monitor;
  24.         static Stopwatch czas_pracy;
  25.         static int uplynelo = 0;
  26.         static int kiedykoniec = 0;
  27.         public static int wynik_silnia;
  28.         static bool czyzwiekszono = false;
  29.         static float przetworzono;
  30.         int g;
  31.         public static int n;
  32.         public static int[] perm = new int[100];
  33.         public static float L = 0;
  34.        
  35.         static void perminit(int n, int[] perm)
  36.         {
  37.             for (int i = 1; i <= n; i++) perm[i] = i;
  38.         }
  39.        
  40.         static bool permNext(int n, int[] perm)
  41.         {
  42.             int i, j, x;
  43.             bool b = false;
  44.             i = n;
  45.             while (i > 1)
  46.             {
  47.                 if (perm[i] > perm[i - 1])
  48.                 {
  49.                     j = n;
  50.                     while (perm[j] < perm[i - 1]) j--;
  51.                     x = perm[j]; perm[j] = perm[i - 1]; perm[i - 1] = x;
  52.                     while (i < n)
  53.                     {
  54.                         x = perm[i]; perm[i] = perm[n]; perm[n] = x;
  55.                         i++; n--;
  56.                     }
  57.                     b = true; break;
  58.                 }
  59.                 i--;
  60.             }
  61.             czas_pracy.Stop();
  62.             uplynelo = Convert.ToInt32(czas_pracy.ElapsedMilliseconds);
  63.             return b;
  64.         }
  65.  
  66.         static void druk(int n, int[] perm)
  67.         {
  68.             for (int i = 1; i <= n; i++) Console.Write(perm[i] + " ");
  69.             Console.WriteLine();
  70.         }
  71.  
  72.         static void generuj_permutacje()
  73.         {
  74.             czas_pracy = new Stopwatch();
  75.             perminit(n, perm);
  76.             do
  77.             {
  78.                 czas_pracy.Start();
  79.                 L++;
  80.                 czyzwiekszono = true;
  81.                 druk(n, perm);
  82.                 while (czyzwiekszono)
  83.                 {
  84.                 }
  85.             } while (permNext(n, perm));
  86.         }
  87.         private static int silnia(int i)
  88.         {
  89.             if (i < 1)
  90.                 return 1;
  91.             else return i * silnia(i - 1);
  92.         }
  93.         private void sprawdz_postepy_programu_wspolnieznie()
  94.         {
  95.             while (L < wynik_silnia)
  96.             {
  97.                 if (czyzwiekszono)
  98.                 {
  99.                     przetworzono = ((L / wynik_silnia) * 100);
  100.                     g = (int)przetworzono;
  101.                     MethodInvoker action1 = delegate { progressBar1.Value = g; };
  102.                     progressBar1.BeginInvoke(action1);
  103.                     MethodInvoker action2 = delegate { label7.Text = ((g).ToString() + " %"); };
  104.                     progressBar1.BeginInvoke(action2);
  105.                     MethodInvoker action3 = delegate { label5.Text = (((uplynelo / 1000)).ToString()) + " sekund"; };
  106.                     progressBar1.BeginInvoke(action3);
  107.                     if (g != 0)
  108.                     {
  109.                         kiedykoniec = ((((uplynelo / 1000) / g) * 100) - (uplynelo / 1000));
  110.                         MethodInvoker action4 = delegate { label7.Text = (kiedykoniec).ToString() + " sekund"; };
  111.                         progressBar1.BeginInvoke(action4);
  112.                     }
  113.                     czyzwiekszono = false;
  114.                 }
  115.             }
  116.         }
  117.        
  118.         private void button1_Click_1(object sender, EventArgs e)
  119.         {
  120.             n = Int32.Parse(textBox1.Text);
  121.             wynik_silnia = silnia(n);
  122.             textBox2.Text = (wynik_silnia).ToString();
  123.             genperm = new Thread(generuj_permutacje);
  124.             genperm.Start();
  125.             monitor = new Thread(sprawdz_postepy_programu_wspolnieznie);
  126.             monitor.Start();
  127.         }
  128.     }
  129. }
Add Comment
Please, Sign In to add comment