-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOFiles.cs
183 lines (172 loc) · 7.69 KB
/
IOFiles.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
IOFiles.cs includes two main classes. InputFile which facilitates reading
Google Code Jam competition input files/data. OutputFile facilitates writing
output data in a form compliant with the competion's standard output format
Copyright (C) 2012 Mohamed Abu Marsa a.k.a. (VC, kOVC) (vc@korganization.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
The GNU General Public License copy can be found in the COPYRIGHT file
at the root of the project directory structure.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace CJ {
public class InputFile {
#region Protected Fields
protected string _filePath;
protected FileStream _stream;
protected StreamReader _reader;
#endregion
#region Constructors
public InputFile() { _initialize("a.in"); }
public InputFile(string path) { _initialize(path); }
private void _initialize(string path) {
_filePath = path;
_stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
_reader = new StreamReader(_stream);
}
#endregion
#region Public Properties
public bool EOF { get { return _reader.EndOfStream; } }
public StreamReader Reader { get { return _reader; } }
#endregion
#region Reader Methods
public string ReadLine() { return _reader.ReadLine(); }
public string ReadToEnd() { return _reader.ReadToEnd(); }
public int Read() { return _reader.Read(); }
public int Read(char[] buffer, int index, int count) { return _reader.Read(buffer, index, count); }
public int ReadBlock(char[] buffer, int index, int count) { return _reader.ReadBlock(buffer, index, count); }
public int Peek() { return _reader.Peek(); }
public void Close() { _reader.Close(); _stream.Close(); _reader.Dispose(); _stream.Dispose(); }
#endregion
#region CJ Methods
public int ReadIntLine() { return int.Parse(_reader.ReadLine()); }
public List<int> ReadIntList() {
string[] nums = _reader.ReadLine().Split(' ');
List<int> res = new List<int>(nums.Length);
for (int i = 0; i < nums.Length; i++) {
res.Add(int.Parse(nums[i]));
}
return res;
}
public int[] ReadIntArray() {
string[] nums = _reader.ReadLine().Split(' ');
int[] res = new int[nums.Length];
for (int i = 0; i < nums.Length; i++) {
res[i] = int.Parse(nums[i]);
}
return res;
}
public List<string> ReadStringList() { return new List<string>(_reader.ReadLine().Split(' ')); }
public string[] ReadStringArray() { return _reader.ReadLine().Split(' '); }
public long ReadLongLine() { return long.Parse(_reader.ReadLine()); }
public List<long> ReadLongList() {
string[] nums = _reader.ReadLine().Split(' ');
List<long> res = new List<long>(nums.Length);
for (int i = 0; i < nums.Length; i++) {
res.Add(long.Parse(nums[i]));
}
return res;
}
public long[] ReadLongArray() {
string[] nums = _reader.ReadLine().Split(' ');
long[] res = new long[nums.Length];
for (int i = 0; i < nums.Length; i++) {
res[i] = long.Parse(nums[i]);
}
return res;
}
public double ReadDoubleLine() { return double.Parse(_reader.ReadLine()); }
public List<double> ReadDoubleList() {
string[] nums = _reader.ReadLine().Split(' ');
List<double> res = new List<double>(nums.Length);
for (int i = 0; i < nums.Length; i++) {
res.Add(double.Parse(nums[i]));
}
return res;
}
public double[] ReadDoubleArray() {
string[] nums = _reader.ReadLine().Split(' ');
double[] res = new double[nums.Length];
for (int i = 0; i < nums.Length; i++) {
res[i] = double.Parse(nums[i]);
}
return res;
}
public decimal ReadDecimalLine() { return decimal.Parse(_reader.ReadLine()); }
public List<decimal> ReadDecimalList() {
string[] nums = _reader.ReadLine().Split(' ');
List<decimal> res = new List<decimal>(nums.Length);
for (int i = 0; i < nums.Length; i++) {
res.Add(decimal.Parse(nums[i]));
}
return res;
}
public decimal[] ReadDecimalArray() {
string[] nums = _reader.ReadLine().Split(' ');
decimal[] res = new decimal[nums.Length];
for (int i = 0; i < nums.Length; i++) {
res[i] = decimal.Parse(nums[i]);
}
return res;
}
public DateTime ReadDateTimeLine() { return DateTime.Parse(_reader.ReadLine()); }
public List<DateTime> ReadDateTimeList() {
string[] nums = _reader.ReadLine().Split(' ');
List<DateTime> res = new List<DateTime>(nums.Length);
for (int i = 0; i < nums.Length; i++) {
res.Add(DateTime.Parse(nums[i]));
}
return res;
}
public DateTime[] ReadDateTimeArray() {
string[] nums = _reader.ReadLine().Split(' ');
DateTime[] res = new DateTime[nums.Length];
for (int i = 0; i < nums.Length; i++) {
res[i] = DateTime.Parse(nums[i]);
}
return res;
}
#endregion
}
public class OutputFile {
#region Protected Fields
protected string _filePath;
protected FileStream _stream;
protected StreamWriter _writer;
protected int _lastCase;
#endregion
#region Public Properties
public StreamWriter Writer { get { return _writer; } }
public int CasesCount { get { return _lastCase; } }
#endregion
#region Constructors
public OutputFile() { _initialize("a.out"); }
public OutputFile(string filename) { _initialize(filename); }
private void _initialize(string filename) {
_filePath = filename;
_stream = new FileStream(_filePath, FileMode.Create, FileAccess.Write, FileShare.None);
_writer = new StreamWriter(_stream);
_lastCase = 0;
}
#endregion
#region Writer Methods
public void Flush() { _writer.Flush(); }
public void Close() { _stream.Flush(); _writer.Flush(); _writer.Close(); _writer.Dispose(); _stream.Close(); _stream.Dispose(); }
#endregion
#region Case Outputting Methods
public void WriteCase(string s) { _lastCase++; _writer.WriteLine("Case #{0}: {1}", _lastCase, s); }
public void WriteCase(int caseNumber, string s) { _writer.WriteLine("Case #{0}: {1}", caseNumber, s); }
#endregion
}
}