-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.cs
110 lines (85 loc) · 2.83 KB
/
IO.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Tools.Converter;
namespace Tools.IO
{
public class Writer
{
public static void WriteFile(ICollection<String> rows, String path)
{
using (StreamWriter sw = File.CreateText(path))
{
foreach (string line in rows)
sw.WriteLine(line);
}
}
}
public class Reader
{
public static LinkedList<string> ReadFile(string path)
{
LinkedList<string> file = new LinkedList<string>();
using (StreamReader sr = new StreamReader(path, Encoding.Default))
{
while (sr.Peek() != -1)
file.AddLast(sr.ReadLine().Trim());
}
return file;
}
public static LinkedList<bool[]> ReadBoolFile(string path, string separator)
{
LinkedList<string> file = ReadFile(path);
LinkedList<bool[]> res = new LinkedList<bool[]>();
bool[] now;
string[] splitted_line;
int k = 0;
foreach (string line in file)
{
splitted_line = line.Split(separator.ToCharArray());
now = new bool[splitted_line.Length];
for (int i = 0; i < now.Length; ++i)
now[i] = int.Parse(splitted_line[i]) != 0;
res.AddLast(now);
}
return res;
}
public static List<int>[] ReadIntFile(string path, string separator)
{
LinkedList<string> file = ReadFile(path);
List<int>[] res = new List<int>[file.Count];
List<int> now;
string[] splitted_line;
int k = 0;
foreach (string line in file)
{
now = new List<int>();
splitted_line = line.Split(separator.ToCharArray());
foreach (string number in splitted_line)
now.Add(int.Parse(number) - 1);
now.Sort();
res[k++] = now;
}
return res;
}
public static List<double>[] ReadDoubleFileToList(string path, string separator)
{
LinkedList<string> file = ReadFile(path);
List<double>[] res = new List<double>[file.Count];
List<double> now;
string[] splitted_line;
int k = 0;
foreach (string line in file)
{
now = new List<double>();
splitted_line = line.Trim().Split(separator.ToCharArray());
foreach (string number in splitted_line)
now.Add(double.Parse(number));
res[k++] = now;
}
return res;
}
}
}