Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17.  
  18. namespace Lab5_siszarp
  19. {
  20. /// <summary>
  21. /// Logika interakcji dla klasy MainWindow.xaml
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. public MainWindow()
  26. {
  27. InitializeComponent();
  28. }
  29.  
  30. public static uint Mianownik_Del(uint k)
  31. {
  32. uint result = 1;
  33. for (uint i = 1; i <=k; i++)
  34. {
  35. result *= i;
  36. }
  37.  
  38. return result;
  39. }
  40.  
  41. public static uint Licznik_Del(uint n, uint k)
  42. {
  43. uint result = 1;
  44. for (uint i = 1; i <= k; i++)
  45. {
  46. result *= (n - i + 1);
  47. }
  48.  
  49. return result;
  50. }
  51.  
  52. private void Click_Taski(object sender, RoutedEventArgs e)
  53. {
  54. if ((!int.TryParse(K_val.Text, out int k) || !int.TryParse(N_val.Text, out int n)))
  55. return;
  56.  
  57. var tpl = Tuple.Create<int, int>(n, k);
  58.  
  59. Task<int> task_l = Task.Factory.StartNew<int>(
  60. (tuple) =>
  61. {
  62. var temp = (Tuple<int, int>)tuple;
  63. int result = 1;
  64. for (int i = 1; i <= temp.Item2; i++)
  65. {
  66. result *= (temp.Item1 - i + 1);
  67. }
  68.  
  69. return result;
  70. }, tpl
  71. );
  72.  
  73. Task<int> task_m = Task.Factory.StartNew<int>(
  74. (K) =>
  75. {
  76. int result = 1;
  77. for (int i = 1; i <= (int)K; i++)
  78. {
  79. result *= i;
  80. }
  81. return result;
  82. }, k
  83. );
  84.  
  85. Task_val.Text = (task_l.Result / task_m.Result).ToString();
  86. }
  87.  
  88. private void Click_Delegaty(object sender, RoutedEventArgs e)
  89. {
  90. if ((!uint.TryParse(K_val.Text, out uint k) || !uint.TryParse(N_val.Text, out uint n)))
  91. return;
  92.  
  93. Func<uint, uint, uint> l_d= Licznik_Del;
  94. Func<uint, uint> m_d = Mianownik_Del;
  95. IAsyncResult result_l = l_d.BeginInvoke(n, k, null, null);
  96. IAsyncResult result_m = m_d.BeginInvoke(k, null, null);
  97.  
  98. while (!result_l.IsCompleted || !result_m.IsCompleted) ;
  99. Del_val.Text = (l_d.EndInvoke(result_l) / m_d.EndInvoke(result_m)).ToString();
  100. }
  101.  
  102. private async void Click_Async(object sender, RoutedEventArgs e)
  103. {
  104. if ((!int.TryParse(K_val.Text, out int k) || !int.TryParse(N_val.Text, out int n)))
  105. return;
  106.  
  107. var tpl = Tuple.Create<int, int>(n, k);
  108.  
  109. int res_l = await Task.Factory.StartNew<int>(
  110. (tuple) =>
  111. {
  112. var temp = (Tuple<int, int>)tuple;
  113. int result = 1;
  114. for (int i = 1; i <= temp.Item2; i++)
  115. {
  116. result *= (temp.Item1 - i + 1);
  117. }
  118.  
  119. return result;
  120. }, tpl
  121. );
  122.  
  123. int res_m = await Task.Factory.StartNew<int>(
  124. (K) =>
  125. {
  126. int result = 1;
  127. for (int i = 1; i <= (int)K; i++)
  128. {
  129. result *= i;
  130. }
  131. return result;
  132. }, k
  133. );
  134. Async_val.Text = (res_l / res_m).ToString();
  135. }
  136.  
  137. private void Click_Fib(object sender, RoutedEventArgs e)
  138. {
  139. if ((!int.TryParse(i_val.Text, out int fibo)))
  140. return;
  141.  
  142. BackgroundWorker bw = new BackgroundWorker();
  143. bw.DoWork += ((object sndr, DoWorkEventArgs args) => {
  144. BackgroundWorker worker = sndr as BackgroundWorker;
  145. int fib = (int)args.Argument;
  146. long t1 = 0, t2 = 1, i = 0;
  147. for (int n = 1; n <= fib; ++n)
  148. {
  149. System.Threading.Thread.Sleep(20);
  150. worker.ReportProgress(n * 100 / fib);
  151. if (n == 1) continue;
  152.  
  153. i = t1 + t2;
  154. t1 = t2;
  155. t2 = i;
  156. }
  157.  
  158. args.Result = i;
  159. });
  160. bw.ProgressChanged += ((object sndr, ProgressChangedEventArgs args) => {
  161. progress_bar.Value = args.ProgressPercentage;
  162. });
  163. bw.RunWorkerCompleted += ((object sndr, RunWorkerCompletedEventArgs args) => {
  164. progress_bar.Value = 100.0;
  165. Fib_res.Text = args.Result.ToString();
  166. });
  167. bw.WorkerReportsProgress = true;
  168. bw.RunWorkerAsync(fibo);
  169. }
  170.  
  171. private void Click_IP(object sender, RoutedEventArgs e)
  172. {
  173. string[] hostNames = { "www.microsoft.com", "www.apple.com",
  174. "www.google.com", "www.ibm.com", "cisco.netacad.net",
  175. "www.oracle.com", "www.nokia.com", "www.hp.com", "www.dell.com",
  176. "www.samsung.com", "www.toshiba.com", "www.siemens.com",
  177. "www.amazon.com", "www.sony.com", "www.canon.com", "www.alcatellucent.com",
  178. "www.acer.com", "www.motorola.com" };
  179.  
  180. var IPs = hostNames.AsParallel().Select(str => {
  181. string res = str + " => ";
  182. try
  183. {
  184. res += Dns.GetHostAddresses(str).Last().ToString();
  185. }
  186. catch (System.Net.Sockets.SocketException)
  187. {
  188. res += "No connection :(";
  189. }
  190. return res;
  191. }).ToList();
  192.  
  193. foreach(string ip in IPs)
  194. {
  195. Adr_box.AppendText(ip + '\n');
  196. }
  197. }
  198. }
  199. }
  200.  
  201.  
  202. <Window x:Class="Lab5_siszarp.MainWindow"
  203. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  204. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  205. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  206. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  207. xmlns:local="clr-namespace:Lab5_siszarp"
  208. mc:Ignorable="d"
  209. Title="MainWindow" Height="450" Width="800">
  210. <Grid>
  211. <TextBox Name="K_val" HorizontalAlignment="Left" Height="32" Margin="30,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="152"/>
  212. <Label Content="K" HorizontalAlignment="Left" Height="34" Margin="30,21,0,0" VerticalAlignment="Top" Width="152" RenderTransformOrigin="0.493,1.407"/>
  213. <Label Content="N" HorizontalAlignment="Left" Height="31" Margin="30,104,0,0" VerticalAlignment="Top" Width="152"/>
  214. <TextBox Name="N_val" HorizontalAlignment="Left" Height="30" Margin="30,135,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="152"/>
  215. <Button Content="Taski" HorizontalAlignment="Left" Height="21" Margin="275,23,0,0" VerticalAlignment="Top" Width="99" Click="Click_Taski"/>
  216. <Button Content="Delegaty" HorizontalAlignment="Left" Height="23" Margin="275,81,0,0" VerticalAlignment="Top" Width="99" Click="Click_Delegaty"/>
  217. <Button Content="Async await" HorizontalAlignment="Left" Height="30" Margin="275,135,0,0" VerticalAlignment="Top" Width="99" Click="Click_Async"/>
  218. <TextBox Name="Task_val" IsReadOnly="True" HorizontalAlignment="Left" Height="21" Margin="394,23,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="114"/>
  219. <TextBox Name="Del_val" IsReadOnly="True" HorizontalAlignment="Left" Height="23" Margin="394,81,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="114"/>
  220. <TextBox Name="Async_val" IsReadOnly="True" HorizontalAlignment="Left" Height="30" Margin="394,135,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="114"/>
  221. <Label Content="Fibonacci" HorizontalAlignment="Left" Height="31" Margin="30,207,0,0" VerticalAlignment="Top" Width="112"/>
  222. <Label Content="i:" HorizontalAlignment="Left" Height="27" Margin="30,243,0,0" VerticalAlignment="Top" Width="18"/>
  223. <TextBox Name="i_val" HorizontalAlignment="Left" Height="27" Margin="53,243,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="129"/>
  224. <Button Content="Licz" HorizontalAlignment="Left" Height="27" Margin="202,243,0,0" VerticalAlignment="Top" Width="59" Click="Click_Fib"/>
  225. <ProgressBar Name="progress_bar" HorizontalAlignment="Left" Height="11" Margin="30,290,0,0" VerticalAlignment="Top" Width="231"/>
  226. <TextBox Name="Fib_res" IsReadOnly="True" HorizontalAlignment="Left" Height="26" Margin="30,306,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="231"/>
  227. <Label Content="DNS" HorizontalAlignment="Left" Height="31" Margin="275,207,0,0" VerticalAlignment="Top" Width="52"/>
  228. <Button Content="Zbierz IP" HorizontalAlignment="Left" Height="31" Margin="327,207,0,0" VerticalAlignment="Top" Width="81" Click="Click_IP"/>
  229. <TextBox Name="Adr_box" IsReadOnly="True" HorizontalAlignment="Left" Height="89" Margin="275,243,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233"/>
  230.  
  231. </Grid>
  232. </Window>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement