namespace файлы
{
struct TPeople
{
public string LastName;
public string Name;
public int school;
public int one;
public int two;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TB_1.ColumnCount = 5;
TB_1.RowCount = Convert.ToInt32(numericUpDown1.Value);
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
TB_1.RowCount = Convert.ToInt32(numericUpDown1.Value);
}
private void button1_Click(object sender, EventArgs e)
{
TPeople people;
using (BinaryWriter bw = new BinaryWriter(File.Open("data.dat", FileMode.Create)))
{
for (int i = 0; i < TB_1.RowCount; i++)
{
people.LastName = Convert.ToString(TB_1[0, i].Value);
people.Name = Convert.ToString(TB_1[1, i].Value);
people.school = Convert.ToInt32(TB_1[2, i].Value);
people.one = Convert.ToInt32(TB_1[3, i].Value);
people.two = Convert.ToInt32(TB_1[4, i].Value);
bw.Write(people.LastName);
bw.Write(people.Name);
bw.Write(people.school);
bw.Write(people.one);
bw.Write(people.two);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
TPeople[] peoples = new TPeople[0];
int n = 0;
using (BinaryReader br = new BinaryReader(File.Open("data.dat", FileMode.Open)))
{
while (br.PeekChar() != -1)
{
Array.Resize(ref peoples, ++n);
peoples[n - 1].LastName = br.ReadString();
peoples[n - 1].Name = br.ReadString();
peoples[n - 1].school = br.ReadInt32();
peoples[n - 1].one = br.ReadInt32();
peoples[n - 1].two = br.ReadInt32();
}
}
TB_2.RowCount = n;
for (int i = 0; i < n; i++)
{
TB_2[0, i].Value = peoples[i].LastName;
TB_2[1, i].Value = peoples[i].Name;
TB_2[2, i].Value = peoples[i].school;
TB_2[3, i].Value = peoples[i].one;
TB_2[4, i].Value = peoples[i].two;
}
int b = 0;
int sum = peoples[0].one + peoples[0].two;
TB_3.RowCount++;
TB_3[0, TB_3.RowCount - 1].Value = peoples[0].LastName;
TB_3[1, TB_3.RowCount - 1].Value = peoples[0].Name;
TB_3[2, TB_3.RowCount - 1].Value = peoples[0].school;
TB_3[3, TB_3.RowCount - 1].Value = sum;
for (int i = 0; i < n; i++)
{
sum = peoples[i].one + peoples[i].two;
for (int k = 0; k < TB_3.RowCount; k++)
{
int l = Convert.ToInt32(TB_3[2, k].Value);
if (k == TB_3.RowCount + 1 && peoples[i].school != l)
{
TB_3.RowCount++;
TB_3[0, TB_3.RowCount - 1].Value = peoples[i].LastName;
TB_3[1, TB_3.RowCount - 1].Value = peoples[i].Name;
TB_3[2, TB_3.RowCount - 1].Value = peoples[i].school;
TB_3[3, TB_3.RowCount - 1].Value = sum;
}
else
{
if (b == 1)
{ break; }
if (peoples[i].school == l)
{
if (i != 0)
{
int p = Convert.ToInt32(TB_3[3, TB_3.RowCount - 1].Value);
if (sum == p && peoples[i].LastName != Convert.ToString(TB_3[0, k].Value) && peoples[i].Name != Convert.ToString(TB_3[1, k].Value))
{
TB_3.RowCount++;
TB_3[0, TB_3.RowCount - 1].Value = peoples[i].LastName;
TB_3[1, TB_3.RowCount - 1].Value = peoples[i].Name;
TB_3[2, TB_3.RowCount - 1].Value = peoples[i].school;
TB_3[3, TB_3.RowCount - 1].Value = sum;
}
else { break; }
}
}
for (int j = 1; j < n; j++)
{
if (i == 0)
{
if (i + j == n)
{ break; }
if (peoples[i].school == peoples[i + j].school)
{
if (sum < peoples[i + j].one + peoples[i + j].two)
{
sum = peoples[i + j].one + peoples[i + j].two;
TB_3[0, TB_3.RowCount - 1].Value = peoples[i + j].LastName;
TB_3[1, TB_3.RowCount - 1].Value = peoples[i + j].Name;
TB_3[2, TB_3.RowCount - 1].Value = peoples[i + j].school;
TB_3[3, TB_3.RowCount - 1].Value = sum;
}
else
{
sum = peoples[i].one + peoples[i].two;
TB_3[0, TB_3.RowCount - 1].Value = peoples[i].LastName;
TB_3[1, TB_3.RowCount - 1].Value = peoples[i].Name;
TB_3[2, TB_3.RowCount - 1].Value = peoples[i].school;
TB_3[3, TB_3.RowCount - 1].Value = sum;
continue;
}
if (i == n - 1)
{
TB_3.RowCount++;
sum = peoples[i].one + peoples[i].two;
TB_3[0, TB_3.RowCount - 1].Value = peoples[i].LastName;
TB_3[1, TB_3.RowCount - 1].Value = peoples[i].Name;
TB_3[2, TB_3.RowCount - 1].Value = peoples[i].school;
TB_3[3, TB_3.RowCount - 1].Value = sum;
b += 1;
break;
}}}
else{
if (i + j == n)
{ break; }
if (peoples[i].school == peoples[i + j].school)
{
if (sum < peoples[i + j].one + peoples[i + j].two)
{
sum = peoples[i + j].one + peoples[i + j].two;
TB_3[0, TB_3.RowCount - 1].Value = peoples[i + j].LastName;
TB_3[1, TB_3.RowCount - 1].Value = peoples[i + j].Name;
TB_3[2, TB_3.RowCount - 1].Value = peoples[i + j].school;
TB_3[3, TB_3.RowCount - 1].Value = sum;
}
else
{
TB_3.RowCount++;
sum = peoples[i].one + peoples[i].two;
TB_3[0, TB_3.RowCount - 1].Value = peoples[i].LastName;
TB_3[1, TB_3.RowCount - 1].Value = peoples[i].Name;
TB_3[2, TB_3.RowCount - 1].Value = peoples[i].school;
TB_3[3, TB_3.RowCount - 1].Value = sum;
continue;
}
}
}
} continue;
}
}
}
}
}
}
void Test()
{
var peoples = new People[]
{
new People{LastName = "Ivanov", Name = "Ivan", School = 17, One = 5, Two = 4},
new People{LastName = "Petrov", Name = "Petr", School = 17, One = 5, Two = 3},
new People{LastName = "Dariyana", Name = "Daria", School = 17, One = 5, Two = 4},
new People{LastName = "Ivanov", Name = "Ivan", School = 16, One = 5, Two = 4},
new People{LastName = "Petrov", Name = "Petr", School = 16, One = 5, Two = 3},
new People{LastName = "Dariyana", Name = "Daria", School = 16, One = 5, Two = 4},
new People{LastName = "Dariyana", Name = "Daria", School = 19, One = 2, Two = 2},
};
var path = "file.xml";
Save(path,peoples);
peoples = Load(path);
var schoolList = peoples.Select(s => s.School).Distinct();
var best =
(from sn in schoolList let max = peoples.Where(s => s.School == sn)
.Max(x => x.One + x.Two)
select
peoples.First(x => x.School == sn && (x.One + x.Two) == max))
.ToList();
;
}
private static People[] Load(string path)
{
var formatter = new XmlSerializer(typeof(People[]));
using var fs = new FileStream(path, FileMode.OpenOrCreate);
var peoples = (People[])formatter.Deserialize(fs);
return peoples;
}
private static void Save(string path, People[] peoples)
{
var formatter = new XmlSerializer(typeof(People[]));
using var fs = new FileStream(path, FileMode.OpenOrCreate);
formatter.Serialize(fs, peoples);
}
[Serializable]
public class People
{
public string LastName { get; set; }
public string Name { get; set; }
public int School { get; set; }
public int One { get; set; }
public int Two { get; set; }
}