Facebook
From pussyhounter, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 186
  1. using System;
  2. using System.Text;
  3. using System.Threading;
  4. using System.IO;
  5.  
  6. namespace zad1
  7. {
  8.     class Program
  9.     {
  10.  
  11.         delegate long DelegateType(long arguments);
  12.         static DelegateType delegateLong;
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             FileStream fs;
  17.             Console.WriteLine("Zad6");
  18.             byte[] buffer = new byte[128];
  19.             using (fs = new FileStream("C:\\Users\\s0163841\\Desktop\\file.txt", FileMode.Open))
  20.             {
  21.                 AutoResetEvent are = new AutoResetEvent(false);
  22.                 fs.BeginRead(buffer, 0, buffer.Length, AsyncCallback, new { Stream = fs, Data = buffer, AutoResetEvent = are });
  23.  
  24.  
  25.                 double q = 0.0001;
  26.                 for (int i = 0; i < 100000; i++)
  27.                 {
  28.                     q *= 1.0001;
  29.                 }
  30.  
  31.                 are.WaitOne();
  32.                 fs.Close();
  33.             }
  34.  
  35.  
  36.             Console.WriteLine("\n\nZad7");
  37.  
  38.             fs = new FileStream("C:\\Users\\s0163841\\Desktop\\file.txt", FileMode.Open);
  39.             buffer = new byte[128];
  40.             var state = fs.BeginRead(buffer, 0, buffer.Length, null, null);
  41.             Console.WriteLine("State: " + state);
  42.  
  43.             double m = 0.0001;
  44.  
  45.             for (int i = 0; i < 100000; i++)
  46.             {
  47.                 m *= 1.0001;
  48.             }
  49.  
  50.             fs.EndRead(state);
  51.             fs.Close();
  52.  
  53.             Console.WriteLine(Encoding.ASCII.GetString(buffer));
  54.  
  55.  
  56.  
  57.             Console.WriteLine("\n\nZad7");
  58.  
  59.             long result;
  60.             IAsyncResult ar;
  61.  
  62.             long n = 50;
  63.  
  64.  
  65.             //fibr
  66.             delegateLong = new DelegateType(FibR);
  67.             ar = delegateLong.BeginInvoke(n, null, null);
  68.             result = delegateLong.EndInvoke(ar);
  69.             Console.WriteLine("Fibr " + result);
  70.  
  71.  
  72.             //fibi
  73.             delegateLong = new DelegateType(FibI);
  74.             ar = delegateLong.BeginInvoke(n, null, null);
  75.             result = delegateLong.EndInvoke(ar);
  76.             Console.WriteLine("Fibi " + result);
  77.  
  78.             //edgr
  79.             delegateLong = new DelegateType(EdgR);
  80.             ar = delegateLong.BeginInvoke(n, null, null);
  81.             result = delegateLong.EndInvoke(ar);
  82.             Console.WriteLine("EdgR " + result);
  83.  
  84.             //edgi
  85.             delegateLong = new DelegateType(EdgI);
  86.             ar = delegateLong.BeginInvoke(n, null, null);
  87.             result = delegateLong.EndInvoke(ar);
  88.             Console.WriteLine("EdgI " + result);
  89.  
  90.  
  91.             Console.WriteLine("\nend");
  92.  
  93.             Console.ReadKey();
  94.         }
  95.  
  96.  
  97.  
  98.         public static long FibR(long n)
  99.         {
  100.             if (n == 0)
  101.             {
  102.                 return 0;
  103.             }
  104.             else if (n == 1)
  105.             {
  106.                 return 1;
  107.             }
  108.             else
  109.             {
  110.                 return (FibR(n - 1) + FibR(n - 2));
  111.             }
  112.  
  113.  
  114.         }
  115.  
  116.         public static long FibI(long n)
  117.         {
  118.             long a = 0;
  119.             long b = 1;
  120.             for (long i = 0; i < n; i++)
  121.             {
  122.                 long temp = a;
  123.                 a = b;
  124.                 b = temp + b;
  125.             }
  126.             return a;
  127.         }
  128.  
  129.         public static long EdgR(long number)
  130.         {
  131.             if (number <= 1)
  132.                 return 1;
  133.             else
  134.                 return number + EdgR(number - 1);
  135.         }
  136.  
  137.         public static long EdgI(long n)
  138.         {
  139.             if (n <= 1)
  140.                 return 1;
  141.             long result = 1;
  142.             for (long i = 2; i <= n; i++)
  143.             {
  144.                 result = result + i;
  145.             }
  146.             return result;
  147.         }
  148.  
  149.         static void ThreadProc(Object stateInfo)
  150.         {
  151.  
  152.         }
  153.  
  154.         static void AsyncCallback(IAsyncResult state)
  155.         {
  156.             dynamic args = state.AsyncState;
  157.             FileStream fs = args.Stream;
  158.             byte[] data = args.Data;
  159.  
  160.             AutoResetEvent are = args.AutoResetEvent;
  161.  
  162.             int l = fs.EndRead(state); //drukowanie tegfo co zostało przeczytane
  163.             Console.WriteLine(Encoding.ASCII.GetString(data));
  164.             are.Set();
  165.         }
  166.  
  167.  
  168.     }
  169. }