namespace Nakov.IO { using System; using System.Text; using System.Globalization; /// /// Console input helper for C# and .NET. Allows simplified reading of numbers and string /// tokens from the console in a way similar to "cin" in C++ and java.util.Scanner in Java. /// /// /// /// (c) Svetlin Nakov, 2011 - http://www.nakov.com /// /// /// /// // In C++ we will use "cin >> x >> y;". Using Nakov.IO.Cin we can do the same as follows: /// int x = Cin.NextInt(); /// double y = Cin.NextDouble(); /// /// public static class Cin { /// /// Reads a string token from the console /// skipping any leading and trailing whitespace. /// public static string NextToken() { StringBuilder tokenChars = new StringBuilder(); bool tokenFinished = false; bool skipWhiteSpaceMode = true; while (!tokenFinished) { int nextChar = Console.Read(); if (nextChar == -1) { // End of stream reached tokenFinished = true; } else { char ch = (char)nextChar; if (char.IsWhiteSpace(ch)) { // Whitespace reached (' ', '\r', '\n', '\t') --> // skip it if it is a leading whitespace // or stop reading anymore if it is trailing if (!skipWhiteSpaceMode) { tokenFinished = true; if (ch == '\r' && (Environment.NewLine == "\r\n")) { // Reached '\r' in Windows --> skip the next '\n' Console.Read(); } } } else { // Character reached --> append it to the output skipWhiteSpaceMode = false; tokenChars.Append(ch); } } } string token = tokenChars.ToString(); return token; } /// /// Reads an integer number from the console /// skipping any leading and trailing whitespace. /// public static int NextInt() { string token = Cin.NextToken(); return int.Parse(token); } /// /// Reads a floating-point number from the console /// skipping any leading and trailing whitespace. /// /// /// Specifies whether to accept any decimal separator /// ("." and ",") or the system's default separator only. /// public static double NextDouble(bool acceptAnyDecimalSeparator = true) { string token = Cin.NextToken(); if (acceptAnyDecimalSeparator) { token = token.Replace(',', '.'); double result = double.Parse(token, CultureInfo.InvariantCulture); return result; } else { double result = double.Parse(token); return result; } } /// /// Reads a decimal number from the console /// skipping any leading and trailing whitespace. /// /// /// Specifies whether to accept any decimal separator /// ("." and ",") or the system's default separator only. /// public static decimal NextDecimal(bool acceptAnyDecimalSeparator = true) { string token = Cin.NextToken(); if (acceptAnyDecimalSeparator) { token = token.Replace(',', '.'); decimal result = decimal.Parse(token, CultureInfo.InvariantCulture); return result; } else { decimal result = decimal.Parse(token); return result; } } } }