Advertisement
DaniPasteBin

Untitled

Jun 15th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _06.UserLog
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             SortedDictionary<string, List<string>> users = new SortedDictionary<string, List<string>>();
  12.             string command = "";
  13.             while (true)
  14.             {
  15.                 List<string> input = Console.ReadLine().Split().ToList();
  16.                 command = input[0];
  17.                 if (command == "end") break;
  18.                 int indexOfIP = command.IndexOf('=') + 1;
  19.                 string ip = command.Substring(indexOfIP);
  20.                 int indexOfUser = input[2].LastIndexOf('=') + 1;
  21.                 string user = input[2].Substring(indexOfUser);
  22.                 List<string> IPs = new List<string>();
  23.                 IPs.Add(ip);
  24.                 if (!users.ContainsKey(user))
  25.                 {
  26.                     users[user] = IPs;
  27.                 }
  28.                 else
  29.                 {
  30.                     users[user].AddRange(IPs);
  31.                 }
  32.             }
  33.  
  34.             foreach (var user in users)
  35.             {
  36.                 Console.WriteLine(user.Key + ": ");
  37.                 List<string> IPs = user.Value;
  38.                 Dictionary<string, int> numberOfIPs = new Dictionary<string, int>();
  39.                 foreach (var ip in IPs)
  40.                 {
  41.                     if (!numberOfIPs.ContainsKey(ip))
  42.                     {
  43.                         numberOfIPs[ip] = 1;
  44.                     }
  45.                     else
  46.                     {
  47.                         numberOfIPs[ip] += 1;
  48.                     }
  49.                 }
  50.  
  51.                 int count = numberOfIPs.Count;
  52.                 foreach (var ip in numberOfIPs)
  53.                 {
  54.                     count--;
  55.                     if (count > 0)
  56.                     {
  57.                         Console.Write(ip.Key + " => " + ip.Value + ", ");
  58.                     }
  59.                     else
  60.                     {
  61.                         Console.Write(ip.Key + " => " + ip.Value + ". ");
  62.                     }
  63.                 }
  64.                 Console.WriteLine();
  65.  
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement