-
Notifications
You must be signed in to change notification settings - Fork 0
/
Class1.cs
118 lines (110 loc) · 3.86 KB
/
Class1.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using System.Text.RegularExpressions;
namespace Ice_Breaking
{
public class Person
{
public string name;
public string id;
public string male;
public string anonym;
public string photo;
public Person()
{
name = "";
id = "";
photo = "";
male = "T";
anonym = "F";
}
public Person(string n, string i, string m, string a, string ph)
{
name = n;
id = i;
male = m;
anonym = a;
photo = ph;
}
public Person(string s)
{
var deserializer = new DeserializerBuilder().WithNamingConvention(UnderscoredNamingConvention.Instance).Build();
Person p = deserializer.Deserialize<Person>(s);
name = p.name;
id = p.id;
photo = p.photo;
male = p.male;
anonym = p.anonym;
}
}
public class DataInitializer
{
public List<Person> person = new List<Person> { };
public string raw_str = ""; //从文件中读出的所有学生信息
public List<string> s_list = new List<string> { }; //分离后的学生信息
public List<int> Male = new List<int> { };
public List<int> Female = new List<int> { };
public async Task InitDataAsync()
{
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile File = await storageFolder.CreateFileAsync("ice_breaking\\student_data.txt", CreationCollisionOption.OpenIfExists);
raw_str = await FileIO.ReadTextAsync(File);
s_list = FindMatch(raw_str);
int i = 0;
Person t;
foreach (string a in s_list)
{
t = new Person(a);
if (t.anonym == "F")
{
if (t.male == "T")
Male.Add(i);
else Female.Add(i);
}
person.Add(t);
i++;
}
}
public async Task AddPersonAsync(string new_name, string new_id, string new_male, string new_photo)
{
Person new_person = new Person(new_name, new_id, new_male, "F", new_photo);
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile File = await storageFolder.GetFileAsync("ice_breaking\\student_data.txt");
var serializer = new SerializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build();
int i;
for (i = 0; i < person.Count; i++)
{
if (new_id.CompareTo(person[i].id) < 0)
break;
}
person.Insert(i, new_person);
s_list.Insert(i, serializer.Serialize(new_person));
if (new_male == "T")
Male.Add(i);
else
Female.Add(i);
raw_str = "";
foreach (string s in s_list)
{
raw_str += s;
}
await FileIO.WriteTextAsync(File, raw_str);
}
private List<string> FindMatch(string a)
{
List<string> s = new List<string> { };
MatchCollection mc = Regex.Matches(a, @"name: ([\u4e00-\u9fbb]*)([a-zA-Z\s]*)\r?\nid: (\d+)\r?\nmale: \w\r?\nanonym: \w\r?\nphoto: (\S+)\r?\n");
foreach (Match m in mc)
{
s.Add(m.ToString());
}
return s;
}
}
}