Skip to content

Instantly share code, notes, and snippets.

@TheVice
Last active December 25, 2016 20:26
Show Gist options
  • Save TheVice/85e548d6d711db83e4a140c94359a3f0 to your computer and use it in GitHub Desktop.
Save TheVice/85e548d6d711db83e4a140c94359a3f0 to your computer and use it in GitHub Desktop.
Response to some package immediately without press Enter key
Algorithm
Program create class Task. In constructor locate initialization of object StringBuilder and call of method onInput that contain main loop of application.
At begin of onInput method program set variables of class to it initial state: StringBuilder object should not contain any character, two boolean flags that signal of start and end of package should set to off state.
Next “infinity” loop start. Exit provided by standard console command Ctrl+C and also application addition provided exit by pressing Escape key. In this loop any key that do not represent keys from diapason from task test will be ignored. If start or end of package set and signal set to off switch signal to on position. Otherwise signal character will be interpreted as part of package body.
If package complete, in other words both signals set to on, responseToPackage method call. After end of it class members set to its initial states.
At responseToPackage provided validation and response of valid package. Validate provided by method isValidCommonPackage. This method check that package consist two times of dots (‘:’) and it positions at begin and end of package. Also key of package should be known – package start from symbols ‘T’ or ‘S’. Method return answer to question that package is valid and addition provided positions of dots. Return type is a Tuple object that locate from one boolean and two integers parts.
For common valid package provided response directly to it type: for package with text command provided output of message that taken by substring command from package. Package with sound command provide addition validation – body of package cannot consist any character except digit and symbol coma (‘,’) that can be at begin or end of body of package with sound command.
Globally response to text package – Console.WriteLine, to sound package – Console.Beep. Reading of input provided by Console.ReadKey that allow to process symbols without press Enter.
using System;
using System.Text;
namespace Test
{
class Task
{
private StringBuilder sb;
private bool packStart;
private bool packEnd;
public static void Main()
{
new Task();
}
public Task()
{
sb = new StringBuilder();
onInput();
}
private void initialState()
{
sb.Clear();
packStart = false;
packEnd = false;
}
private static Tuple<bool, int, int> isValidCommonPackage(String package)
{
int countOfDots = 0;
int firstDotsPos = 0;
int secondDotsPos = 0;
int i = 0;
foreach (var ch in package)
{
if (ch == ':')
{
countOfDots++;
if (countOfDots > 2)
{
break;
}
if (firstDotsPos == 0)
{
firstDotsPos = i;
}
else
{
secondDotsPos = i;
}
}
++i;
}
if (countOfDots != 2 || (package[0] != 'T' && package[0] != 'S') || firstDotsPos != 1 || secondDotsPos != package.Length - 1)
{
return new Tuple<bool, int, int>(false, 0, 0);
}
return new Tuple<bool, int, int>(true, firstDotsPos, secondDotsPos);
}
private static Tuple<bool, int, int> isValidBeppPackage(String package)
{
int countOfComa = 0;
int comaPosition = 0;
int i = 0;
foreach (var ch in package)
{
if (!Char.IsDigit(ch) && ',' != ch)
{
return new Tuple<bool, int, int>(false, 0, 0);
}
else if (',' == ch)
{
comaPosition = i;
countOfComa++;
}
if (countOfComa > 1)
{
return new Tuple<bool, int, int>(false, 0, 0);
}
++i;
}
if (countOfComa != 1 || comaPosition == 0 || comaPosition == package.Length - 1)
{
return new Tuple<bool, int, int>(false, 0, 0);
}
int frequency = int.Parse(package.Substring(0, comaPosition));
int duration = int.Parse(package.Substring(comaPosition + 1));
return new Tuple<bool, int, int>(true, frequency, duration);
}
private static void responseToPackage(String package)
{
var isPackageValid = isValidCommonPackage(package);
if (!isPackageValid.Item1)
{
Console.WriteLine("NACK");
}
else if (package[0] == 'T')
{
String message = package.Substring(isPackageValid.Item2 + 1, isPackageValid.Item3 - isPackageValid.Item2 - 1);
Console.WriteLine(message);
}
else if (package[0] == 'S')
{
String beepBodyPackage = package.Substring(isPackageValid.Item2 + 1, isPackageValid.Item3 - isPackageValid.Item2 - 1);
isPackageValid = isValidBeppPackage(beepBodyPackage);
if (!isPackageValid.Item1)
{
Console.WriteLine("NACK");
}
else
{
Console.Beep(isPackageValid.Item2, isPackageValid.Item3);
}
}
}
private void onInput()
{
initialState();
while (true)
{
if (packStart && packEnd)
{
responseToPackage(sb.ToString());
initialState();
}
var cki = Console.ReadKey();
if (cki.Key == ConsoleKey.Escape)
{
break;
}
else if (cki.KeyChar < 32 || cki.KeyChar > 127)
{
continue;
}
else if (cki.KeyChar == 'P' && !packStart)
{
packStart = true;
continue;
}
else if (cki.KeyChar == 'E' && !packEnd)
{
packEnd = true;
continue;
}
sb.Append(cki.KeyChar);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment