Чтение списка из файла и дальнейшее использование информации из файла - C#
Формулировка задачи:
основная
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication22
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private static string[] c3()
{
string[] streets = new string[4];//то же самое что и районы толко улицы
streets[0] = "Улица 1";
streets[1] = "Улица 2";
streets[2] = "Улица 3";
streets[3] = "Улица 4";
return streets;
}
private static void c2(ListBox listBox1, out Random rnd, out Community[] communitys)
{
listBox1.Items.Clear();
rnd = new Random();
communitys = new Community[4];//задаем районы через массив
communitys[0] = new Community(1, "Район 1");
communitys[1] = new Community(2, "Район 2");
communitys[2] = new Community(3, "Район 3");
communitys[3] = new Community(4, "Район 4");
}
class S1
{
public static void c4(ListBox listBox1, Random rnd, Community[] communitys, string[] streets)
{
Building[] buildings = new Building[60];//по заданию у нас 60 разных строений (домов) и тут мы задаем их через массив
for (int i = 0; i < buildings.Length; i++)//эта штучка стандартная она не дает нам уйти дальше 60
{
buildings[i] = new Building();
buildings[i].street1 = streets[rnd.Next(0, streets.Length)];//тут и далее идет обычный рандом случаная улица номер дома год район
buildings[i].number1 = rnd.Next(1, 11);
buildings[i].year1 = rnd.Next(1990, 2017);
Community community = communitys[rnd.Next(0, communitys.Length)];
buildings[i].SetCommunity(community);
}
foreach (Building building in buildings)//cравнивает года постройки домов в районе и самое малое это тру
{
if (building.community1.minyear1 == building.year1) building.community1.minyearbcount1++;
}
Community bigcommunity = communitys[0];
foreach (Community community in communitys)//считает дома с минимальным годом постройки
{
if (community.minyearbcount1 > bigcommunity.minyearbcount1) bigcommunity = community;
}
foreach (Community community in communitys)
{
listBox1.Items.Add(community.name1 + ", минимальный год " + community.minyear1.ToString() + ", домов с минимальным годом " + community.minyearbcount1.ToString());
}
foreach (Building building in buildings)
{
if (building.community1.id1 == bigcommunity.id1 && building.year1 == bigcommunity.minyear1)
listBox1.Items.Add("Район: " + building.community1.name1 + ", Улица: " + building.street1 + ", Год: " + building.year1.ToString() + ", Номер дома: " + building.number1.ToString());
}
}
}
class S2 : S1
{public void c1(ListBox listBox1)
{
Random rnd;
Community[] communitys;
c2(listBox1, out rnd, out communitys);
string[] streets = c3();
c4(listBox1, rnd, communitys, streets);
}
}
private void button1_Click(object sender, EventArgs e)
{
S2 m = new S2();
m.c1(listBox1);
}
}
}Решение задачи: «Чтение списка из файла и дальнейшее использование информации из файла»
textual
Листинг программы
private static void c2(ListBox listBox1, out Random rnd, out Community[] communitys)
{
listBox1.Items.Clear();
rnd = new Random();
communitys = new Community[4];//задаем районы через массив
communitys[0] = new Community(1, "Район 1");
communitys[1] = new Community(2, "Район 2");
communitys[2] = new Community(3, "Район 3");
communitys[3] = new Community(4, "Район 4");
}
public void с2(string rai)
{
string[] lines = File.ReadAllLines(rai, Encoding.GetEncoding(1251));
int U = lines.Length;
Community[] communitys = new Community[U];
for (int i = 0; i < lines.Length; i++)
{
string[] obiom = lines[i].Split('_');
if (obiom.Length == 5)
{
Community community = new Community(obiom[0], Convert.ToInt32(obiom[1]),Convert.ToInt32(obiom[3]), Convert.ToDecimal(obiom[4]));
communitys[i] = community;
}
}