Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Net;
  8. using System.Net.Sockets;
  9.  
  10. namespace Bus
  11. {
  12.     class Program
  13.     {
  14.         static string path = @"C:\Users\Кирилл\source\repos\Bus\Bus\graph.txt";
  15.         static float[,] g = new float[5, 5];
  16.         static void read_graph()
  17.         {
  18.             using (StreamReader st = new StreamReader(path, System.Text.Encoding.Default))
  19.             {
  20.                 string line;
  21.                 while ((line = st.ReadLine()) != null)
  22.                 {
  23.                     int i = 0;
  24.                     int u = get_int(line, ref i);
  25.                     int v = get_int(line, ref i);
  26.                     g[u, v] = get_int(line, ref i);
  27.                 }
  28.             }
  29.         }
  30.         static void update(int[] way,int N,ref int pos,DateTime sdt)
  31.         {
  32.             float v = 0.2F;
  33.             DateTime ndt = DateTime.Now;
  34.             int t = Convert.ToInt32(ndt - sdt);
  35.             float S = t * v;
  36.             int i = 1;
  37.             while(S > 0)
  38.             {
  39.                 S -= g[way[1], way[0]];
  40.                 for (int j = 1; j < N; j++)
  41.                     way[j - 1] = way[j];
  42.             }
  43.             pos = way[0];
  44.         }//Обновляет путь
  45.         static int get_int(string str,ref int pos)
  46.         {
  47.             string res = "";
  48.             while (str[pos] != ' ')
  49.                 res += str[pos++];
  50.             pos++;
  51.             return Convert.ToInt32(res);
  52.         }
  53.         static void Main(string[] args)
  54.         {
  55.             read_graph();
  56.             int[] way = new int[5];
  57.             while(true)
  58.             {
  59.                 IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8049);
  60.                 Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  61.                 Random rnd = new Random();
  62.                 Console.WriteLine("Введите номер автобуса: ");
  63.                 int N = 0;
  64.                 int pos = rnd.Next(0, 4);
  65.                 string message = Console.ReadLine() + Convert.ToString(pos);
  66.                 try
  67.                 {
  68.                     sock.Connect(ip);
  69.                     sock.Send(Encoding.Unicode.GetBytes(message));
  70.                     int bytes = 0;
  71.                     byte[] buff = new byte[256];
  72.                     do
  73.                     {
  74.                         bytes = sock.Receive(buff);
  75.                         message = Encoding.Unicode.GetString(buff, 0, bytes);
  76.                     } while (sock.Available > 0);
  77.  
  78.                     if (message[0] == 'g')
  79.                     {
  80.                         DateTime n = DateTime.Now;
  81.                         update(way, N, ref pos, n);
  82.                         Console.WriteLine("Местоположение обновленно");
  83.                         sock.Send(Encoding.Unicode.GetBytes(Convert.ToString(pos)));
  84.                         Console.WriteLine("Местоположение отправленно на сервер");
  85.                     }
  86.                     else
  87.                     {
  88.                         int i = 0;
  89.                         N = 0;
  90.                         while (i < message.Length)
  91.                             way[N++] = get_int(message, ref i);
  92.                     }
  93.                 }
  94.                 catch(Exception ex)
  95.                 {
  96.                     Console.WriteLine(ex.Message);
  97.                 }
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement