Holey_yan

[C#] ValueCounter

May 13th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ValueCounter
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             // Record start time and the end
  13.             DateTime startTime = DateTime.Now, overTime = DateTime.Now;
  14.  
  15.             LogRecoder("Create source file path");
  16.             string sourcePath = "SourceTxt.txt";
  17.  
  18.             LogRecoder("Create dictionary");
  19.             Dictionary<string, int> myDic = new Dictionary<string, int>();
  20.  
  21.             // Load value
  22.             LogRecoder("Load value from source file");
  23.             string data = System.IO.File.ReadAllText(sourcePath);
  24.             foreach (string line in data.Split('\n'))
  25.             {
  26.                 foreach (string value in line.Split(','))
  27.                 {
  28.                     if (myDic.ContainsKey(value))
  29.                     {
  30.                         myDic[value]++;
  31.                     }
  32.                     else
  33.                     {
  34.                         myDic.Add(value, 1);
  35.                     }
  36.                 }
  37.             }
  38.  
  39.             // Search Max
  40.             LogRecoder("Search max value and count");
  41.             int maxValue = 0, maxValueCount = 0;
  42.             foreach (KeyValuePair<string, int> item in myDic)
  43.             {
  44.                 if (item.Value > maxValueCount)
  45.                 {
  46.                     maxValue = int.Parse(item.Key);
  47.                     maxValueCount = item.Value;
  48.                 }
  49.             } overTime = DateTime.Now;
  50.  
  51.             // Output
  52.             LogRecoder(string.Format("maxValue : {0}, maxValueCount : {1}", maxValue.ToString(), maxValueCount.ToString()));
  53.             LogRecoder(string.Format("Process exited after {0} seconds", (Math.Round((overTime - startTime).TotalMilliseconds) / 1000).ToString("0.000")));
  54.             LogRecoder("Press any key to continue ..");
  55.             Console.ReadKey();
  56.         }
  57.  
  58.         static bool LogRecoder(string context)
  59.         {
  60.             Console.ForegroundColor = ConsoleColor.Green;
  61.             Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + context);
  62.             return true;
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment