Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.IO;
  9. using System.Security.Cryptography;
  10. using System.Numerics;
  11.  
  12. namespace Zajecia2serwer
  13. {
  14. class Program
  15. {
  16. public static BigInteger[] funk = new BigInteger[12];
  17. public struct Vector2
  18. {
  19. public BigInteger x;
  20. public BigInteger y;
  21.  
  22. public Vector2(BigInteger x, BigInteger y)
  23. {
  24. this.x = x;
  25. this.y = y;
  26. }
  27. }
  28.  
  29. private static byte[] buffer = new byte[1024];
  30. private static List<Socket> clients = new List<Socket>();
  31. private static Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  32. static void Main(string[] args)
  33. {
  34. SetupServer();
  35. Console.ReadLine();
  36. }
  37. private static void SetupServer()
  38. {
  39. server.Bind(new IPEndPoint(IPAddress.Any, 8080));
  40. server.Listen(1);
  41. server.BeginAccept(AcceptCallback, null);
  42. }
  43. private static void AcceptCallback(IAsyncResult ar)
  44. {
  45. Socket socket = server.EndAccept(ar);
  46. clients.Add(socket);
  47. Console.WriteLine("Client {0} connected", clients.FindIndex(s => s == socket));
  48. socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, socket);
  49. server.BeginAccept(AcceptCallback, null);
  50. }
  51. private static void ReceiveCallback(IAsyncResult ar)
  52. {
  53.  
  54. Socket socket = (Socket)ar.AsyncState;
  55. int received;
  56. int w = 1;
  57. while (w < 11)
  58. {
  59. try
  60. {
  61. received = socket.EndReceive(ar);
  62. }
  63. catch (SocketException)
  64. {
  65. Console.WriteLine("Client {0} disconnected", clients.FindIndex(s => s == socket));
  66.  
  67. socket.Close();
  68. clients.Remove(socket);
  69. return;
  70. }
  71. BigInteger[] funk = new BigInteger[12];
  72. byte[] data = new byte[received];
  73.  
  74. funk[w] = new BigInteger(data);
  75. }
  76. BigInteger p = funk[1];
  77. Vector2[] contro = new Vector2[5];
  78. contro[0] = new Vector2(1, funk[1]);
  79. contro[1] = new Vector2(2, funk[2]);
  80. contro[2] = new Vector2(6, funk[6]);
  81. contro[3] = new Vector2(8, funk[8]);
  82. contro[4] = new Vector2(9, funk[9]);
  83.  
  84. BigInteger v = GetValue(contro, 0);
  85. v = BigInteger.Abs(v);
  86. v = BigInteger.ModPow(v, 1, p);
  87. Console.WriteLine(v);
  88. System.Threading.Thread.Sleep(100000);
  89. }
  90. public static void SendCallback(IAsyncResult ar)
  91. {
  92.  
  93. Socket socket = (Socket)ar.AsyncState;
  94. socket.EndSend(ar);
  95. }
  96. private static BigInteger Inner(Vector2[] controlPoints, BigInteger x, int j)
  97. {
  98. x = 0;
  99. BigInteger dividend = 1;
  100. BigInteger divisor = 1;
  101. for (var k = 0; k < controlPoints.Length; k++)
  102. {
  103. if (k == j)
  104. {
  105. continue;
  106. }
  107. dividend *= (x - controlPoints[k].x);
  108. divisor *= (controlPoints[j].x - controlPoints[k].x);
  109. }
  110. return (dividend / divisor) * controlPoints[j].y;
  111. }
  112.  
  113. public static BigInteger GetValue(Vector2[] controlPoints, BigInteger x)
  114. {
  115. BigInteger result = 0;
  116. for (var j = 0; j < controlPoints.Length; j++)
  117. {
  118. result += Inner(controlPoints, x, j);
  119. }
  120. return result;
  121. }
  122.  
  123.  
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement