-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathKattio.cs
102 lines (86 loc) · 1.83 KB
/
Kattio.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.IO;
namespace Kattis.IO
{
public class NoMoreTokensException : Exception
{
}
public class Tokenizer
{
string[] tokens = new string[0];
private int pos;
StreamReader reader;
public Tokenizer(Stream inStream)
{
var bs = new BufferedStream(inStream);
reader = new StreamReader(bs);
}
public Tokenizer() : this(Console.OpenStandardInput())
{
// Nothing more to do
}
private string PeekNext()
{
if (pos < 0)
// pos < 0 indicates that there are no more tokens
return null;
if (pos < tokens.Length)
{
if (tokens[pos].Length == 0)
{
++pos;
return PeekNext();
}
return tokens[pos];
}
string line = reader.ReadLine();
if (line == null)
{
// There is no more data to read
pos = -1;
return null;
}
// Split the line that was read on white space characters
tokens = line.Split(null);
pos = 0;
return PeekNext();
}
public bool HasNext()
{
return (PeekNext() != null);
}
public string Next()
{
string next = PeekNext();
if (next == null)
throw new NoMoreTokensException();
++pos;
return next;
}
}
public class Scanner : Tokenizer
{
public int NextInt()
{
return int.Parse(Next());
}
public long NextLong()
{
return long.Parse(Next());
}
public float NextFloat()
{
return float.Parse(Next());
}
public double NextDouble()
{
return double.Parse(Next());
}
}
public class BufferedStdoutWriter : StreamWriter
{
public BufferedStdoutWriter() : base(new BufferedStream(Console.OpenStandardOutput()))
{
}
}
}