forked from kontur-edu/git-merge-task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
57 lines (52 loc) · 1.29 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Linq;
namespace Kontur.Courses.Git
{
class Program
{
private static void Main()
{
Calculator calculator = new Calculator();
RunLoop(calculator);
}
private static void RunLoop(Calculator calculator)
{
while (true)
{
Console.ForegroundColor = ConsoleColor.Gray;
var line = Console.ReadLine();
if (line == null) break;
var args = SplitInput(line);
var result = calculator.Calculate(args);
Console.ForegroundColor = result.HasValue ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine("> " + result);
}
}
private static string[] SplitInput(string line)
{
if (line.Length == 0) return new string[0];
List<string> res = new List<string> {""};
int charClass = GetCharClass(line[0]);
foreach (var ch in line)
{
if (GetCharClass(ch) != charClass)
{
if (res.Last() != "") res.Add("");
charClass = GetCharClass(ch);
}
if (!char.IsWhiteSpace(ch))
res[res.Count - 1] += ch;
}
return res.Select(item => item.Trim()).ToArray();
}
private static int GetCharClass(char c)
{
return char.IsDigit(c) ? 0 : char.IsLetter(c) ? 1 : 2;
}
private static int GetCharClass(char c)
{
return char.IsDigit(c) ? 0 : char.IsLetter(c) ? 1 : 2;
}
}
}