Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace Lab5_siszarp
- {
- /// <summary>
- /// Logika interakcji dla klasy MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- public static uint Mianownik_Del(uint k)
- {
- uint result = 1;
- for (uint i = 1; i <=k; i++)
- {
- result *= i;
- }
- return result;
- }
- public static uint Licznik_Del(uint n, uint k)
- {
- uint result = 1;
- for (uint i = 1; i <= k; i++)
- {
- result *= (n - i + 1);
- }
- return result;
- }
- private void Click_Taski(object sender, RoutedEventArgs e)
- {
- if ((!int.TryParse(K_val.Text, out int k) || !int.TryParse(N_val.Text, out int n)))
- return;
- var tpl = Tuple.Create<int, int>(n, k);
- Task<int> task_l = Task.Factory.StartNew<int>(
- (tuple) =>
- {
- var temp = (Tuple<int, int>)tuple;
- int result = 1;
- for (int i = 1; i <= temp.Item2; i++)
- {
- result *= (temp.Item1 - i + 1);
- }
- return result;
- }, tpl
- );
- Task<int> task_m = Task.Factory.StartNew<int>(
- (K) =>
- {
- int result = 1;
- for (int i = 1; i <= (int)K; i++)
- {
- result *= i;
- }
- return result;
- }, k
- );
- Task_val.Text = (task_l.Result / task_m.Result).ToString();
- }
- private void Click_Delegaty(object sender, RoutedEventArgs e)
- {
- if ((!uint.TryParse(K_val.Text, out uint k) || !uint.TryParse(N_val.Text, out uint n)))
- return;
- Func<uint, uint, uint> l_d= Licznik_Del;
- Func<uint, uint> m_d = Mianownik_Del;
- IAsyncResult result_l = l_d.BeginInvoke(n, k, null, null);
- IAsyncResult result_m = m_d.BeginInvoke(k, null, null);
- while (!result_l.IsCompleted || !result_m.IsCompleted) ;
- Del_val.Text = (l_d.EndInvoke(result_l) / m_d.EndInvoke(result_m)).ToString();
- }
- private async void Click_Async(object sender, RoutedEventArgs e)
- {
- if ((!int.TryParse(K_val.Text, out int k) || !int.TryParse(N_val.Text, out int n)))
- return;
- var tpl = Tuple.Create<int, int>(n, k);
- int res_l = await Task.Factory.StartNew<int>(
- (tuple) =>
- {
- var temp = (Tuple<int, int>)tuple;
- int result = 1;
- for (int i = 1; i <= temp.Item2; i++)
- {
- result *= (temp.Item1 - i + 1);
- }
- return result;
- }, tpl
- );
- int res_m = await Task.Factory.StartNew<int>(
- (K) =>
- {
- int result = 1;
- for (int i = 1; i <= (int)K; i++)
- {
- result *= i;
- }
- return result;
- }, k
- );
- Async_val.Text = (res_l / res_m).ToString();
- }
- private void Click_Fib(object sender, RoutedEventArgs e)
- {
- if ((!int.TryParse(i_val.Text, out int fibo)))
- return;
- BackgroundWorker bw = new BackgroundWorker();
- bw.DoWork += ((object sndr, DoWorkEventArgs args) => {
- BackgroundWorker worker = sndr as BackgroundWorker;
- int fib = (int)args.Argument;
- long t1 = 0, t2 = 1, i = 0;
- for (int n = 1; n <= fib; ++n)
- {
- System.Threading.Thread.Sleep(20);
- worker.ReportProgress(n * 100 / fib);
- if (n == 1) continue;
- i = t1 + t2;
- t1 = t2;
- t2 = i;
- }
- args.Result = i;
- });
- bw.ProgressChanged += ((object sndr, ProgressChangedEventArgs args) => {
- progress_bar.Value = args.ProgressPercentage;
- });
- bw.RunWorkerCompleted += ((object sndr, RunWorkerCompletedEventArgs args) => {
- progress_bar.Value = 100.0;
- Fib_res.Text = args.Result.ToString();
- });
- bw.WorkerReportsProgress = true;
- bw.RunWorkerAsync(fibo);
- }
- private void Click_IP(object sender, RoutedEventArgs e)
- {
- string[] hostNames = { "www.microsoft.com", "www.apple.com",
- "www.google.com", "www.ibm.com", "cisco.netacad.net",
- "www.oracle.com", "www.nokia.com", "www.hp.com", "www.dell.com",
- "www.samsung.com", "www.toshiba.com", "www.siemens.com",
- "www.amazon.com", "www.sony.com", "www.canon.com", "www.alcatellucent.com",
- "www.acer.com", "www.motorola.com" };
- var IPs = hostNames.AsParallel().Select(str => {
- string res = str + " => ";
- try
- {
- res += Dns.GetHostAddresses(str).Last().ToString();
- }
- catch (System.Net.Sockets.SocketException)
- {
- res += "No connection :(";
- }
- return res;
- }).ToList();
- foreach(string ip in IPs)
- {
- Adr_box.AppendText(ip + '\n');
- }
- }
- }
- }
- <Window x:Class="Lab5_siszarp.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:Lab5_siszarp"
- mc:Ignorable="d"
- Title="MainWindow" Height="450" Width="800">
- <Grid>
- <TextBox Name="K_val" HorizontalAlignment="Left" Height="32" Margin="30,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="152"/>
- <Label Content="K" HorizontalAlignment="Left" Height="34" Margin="30,21,0,0" VerticalAlignment="Top" Width="152" RenderTransformOrigin="0.493,1.407"/>
- <Label Content="N" HorizontalAlignment="Left" Height="31" Margin="30,104,0,0" VerticalAlignment="Top" Width="152"/>
- <TextBox Name="N_val" HorizontalAlignment="Left" Height="30" Margin="30,135,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="152"/>
- <Button Content="Taski" HorizontalAlignment="Left" Height="21" Margin="275,23,0,0" VerticalAlignment="Top" Width="99" Click="Click_Taski"/>
- <Button Content="Delegaty" HorizontalAlignment="Left" Height="23" Margin="275,81,0,0" VerticalAlignment="Top" Width="99" Click="Click_Delegaty"/>
- <Button Content="Async await" HorizontalAlignment="Left" Height="30" Margin="275,135,0,0" VerticalAlignment="Top" Width="99" Click="Click_Async"/>
- <TextBox Name="Task_val" IsReadOnly="True" HorizontalAlignment="Left" Height="21" Margin="394,23,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="114"/>
- <TextBox Name="Del_val" IsReadOnly="True" HorizontalAlignment="Left" Height="23" Margin="394,81,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="114"/>
- <TextBox Name="Async_val" IsReadOnly="True" HorizontalAlignment="Left" Height="30" Margin="394,135,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="114"/>
- <Label Content="Fibonacci" HorizontalAlignment="Left" Height="31" Margin="30,207,0,0" VerticalAlignment="Top" Width="112"/>
- <Label Content="i:" HorizontalAlignment="Left" Height="27" Margin="30,243,0,0" VerticalAlignment="Top" Width="18"/>
- <TextBox Name="i_val" HorizontalAlignment="Left" Height="27" Margin="53,243,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="129"/>
- <Button Content="Licz" HorizontalAlignment="Left" Height="27" Margin="202,243,0,0" VerticalAlignment="Top" Width="59" Click="Click_Fib"/>
- <ProgressBar Name="progress_bar" HorizontalAlignment="Left" Height="11" Margin="30,290,0,0" VerticalAlignment="Top" Width="231"/>
- <TextBox Name="Fib_res" IsReadOnly="True" HorizontalAlignment="Left" Height="26" Margin="30,306,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="231"/>
- <Label Content="DNS" HorizontalAlignment="Left" Height="31" Margin="275,207,0,0" VerticalAlignment="Top" Width="52"/>
- <Button Content="Zbierz IP" HorizontalAlignment="Left" Height="31" Margin="327,207,0,0" VerticalAlignment="Top" Width="81" Click="Click_IP"/>
- <TextBox Name="Adr_box" IsReadOnly="True" HorizontalAlignment="Left" Height="89" Margin="275,243,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233"/>
- </Grid>
- </Window>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement