Чтение списка из файла и дальнейшее использование информации из файла - C#

Узнай цену своей работы

Формулировка задачи:

основная
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace WindowsFormsApplication22
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void Form1_Load(object sender, EventArgs e)
  19. {
  20. }
  21. private static string[] c3()
  22. {
  23. string[] streets = new string[4];//то же самое что и районы толко улицы
  24. streets[0] = "Улица 1";
  25. streets[1] = "Улица 2";
  26. streets[2] = "Улица 3";
  27. streets[3] = "Улица 4";
  28. return streets;
  29. }
  30. private static void c2(ListBox listBox1, out Random rnd, out Community[] communitys)
  31. {
  32. listBox1.Items.Clear();
  33. rnd = new Random();
  34. communitys = new Community[4];//задаем районы через массив
  35. communitys[0] = new Community(1, "Район 1");
  36. communitys[1] = new Community(2, "Район 2");
  37. communitys[2] = new Community(3, "Район 3");
  38. communitys[3] = new Community(4, "Район 4");
  39. }
  40.  
  41. class S1
  42. {
  43. public static void c4(ListBox listBox1, Random rnd, Community[] communitys, string[] streets)
  44. {
  45. Building[] buildings = new Building[60];//по заданию у нас 60 разных строений (домов) и тут мы задаем их через массив
  46.  
  47. for (int i = 0; i < buildings.Length; i++)//эта штучка стандартная она не дает нам уйти дальше 60
  48. {
  49. buildings[i] = new Building();
  50. buildings[i].street1 = streets[rnd.Next(0, streets.Length)];//тут и далее идет обычный рандом случаная улица номер дома год район
  51. buildings[i].number1 = rnd.Next(1, 11);
  52. buildings[i].year1 = rnd.Next(1990, 2017);
  53. Community community = communitys[rnd.Next(0, communitys.Length)];
  54. buildings[i].SetCommunity(community);
  55. }
  56. foreach (Building building in buildings)//cравнивает года постройки домов в районе и самое малое это тру
  57. {
  58. if (building.community1.minyear1 == building.year1) building.community1.minyearbcount1++;
  59. }
  60. Community bigcommunity = communitys[0];
  61. foreach (Community community in communitys)//считает дома с минимальным годом постройки
  62. {
  63. if (community.minyearbcount1 > bigcommunity.minyearbcount1) bigcommunity = community;
  64. }
  65. foreach (Community community in communitys)
  66. {
  67. listBox1.Items.Add(community.name1 + ", минимальный год " + community.minyear1.ToString() + ", домов с минимальным годом " + community.minyearbcount1.ToString());
  68. }
  69. foreach (Building building in buildings)
  70. {
  71. if (building.community1.id1 == bigcommunity.id1 && building.year1 == bigcommunity.minyear1)
  72. listBox1.Items.Add("Район: " + building.community1.name1 + ", Улица: " + building.street1 + ", Год: " + building.year1.ToString() + ", Номер дома: " + building.number1.ToString());
  73. }
  74. }
  75. }
  76. class S2 : S1
  77. {public void c1(ListBox listBox1)
  78. {
  79. Random rnd;
  80. Community[] communitys;
  81. c2(listBox1, out rnd, out communitys);
  82.  
  83. string[] streets = c3();
  84. c4(listBox1, rnd, communitys, streets);
  85. }
  86. }
  87. private void button1_Click(object sender, EventArgs e)
  88. {
  89. S2 m = new S2();
  90. m.c1(listBox1);
  91. }
  92.  
  93. }
  94.  
  95. }

Решение задачи: «Чтение списка из файла и дальнейшее использование информации из файла»

textual
Листинг программы
  1. private static void c2(ListBox listBox1, out Random rnd, out Community[] communitys)
  2.       {
  3.           listBox1.Items.Clear();
  4.           rnd = new Random();
  5.           communitys = new Community[4];//задаем районы через массив
  6.           communitys[0] = new Community(1, "Район 1");
  7.           communitys[1] = new Community(2, "Район 2");
  8.           communitys[2] = new Community(3, "Район 3");
  9.           communitys[3] = new Community(4, "Район 4");
  10.        
  11.       }
  12.       public void с2(string rai)
  13.       {
  14.           string[] lines = File.ReadAllLines(rai, Encoding.GetEncoding(1251));
  15.           int U = lines.Length;
  16.  
  17.           Community[] communitys = new Community[U];
  18.           for (int i = 0; i < lines.Length; i++)
  19.           {
  20.               string[] obiom = lines[i].Split('_');
  21.               if (obiom.Length == 5)
  22.               {
  23.                     Community community = new Community(obiom[0], Convert.ToInt32(obiom[1]),Convert.ToInt32(obiom[3]), Convert.ToDecimal(obiom[4]));
  24.                  communitys[i] = community;
  25.               }
  26.           }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 4.545 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы