Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace Products
  9. {
  10.  public class Book
  11.  {
  12.  public int Id { get; set; }
  13.  public string Name { get; set; }
  14.  public double Price { get; set; }
  15.  public string PublisherName { get; set; }
  16.  public string PublisherCity { get; set; }
  17.  public double SrPrice=0;
  18.  
  19.  
  20.  public void PrintInFront()//Печать книги
  21.  {
  22.  Console.WriteLine("Name={0},Id={1},Price={2},PublisherName={3},PublisherCity={4}",Name,Id,Price,PublisherName,PublisherCity);
  23.  }
  24.  
  25.  }
  26.  
  27.  class Program
  28.  {
  29.  static void GetBooksFromFail(string FileName)
  30.  {
  31.  List<Book> list = new List<Book>();//список
  32.  try
  33.  {
  34.  string str = "";
  35.  bool flagFirst = false;
  36.  StreamReader reader = new StreamReader(FileName);//читающий курсор
  37.  while ((str = reader.ReadLine()) != null)
  38.  {
  39.  if (!flagFirst)
  40.  {
  41.  flagFirst = true;
  42.  continue;
  43.  }
  44.  string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
  45.  if (strArray.Length < 5)
  46.  continue;
  47.  Book simple = new Book();
  48.  simple.Id = int.Parse(strArray[0]);
  49.  simple.Name = strArray[1];
  50.  simple.Price = double.Parse(strArray[2]);
  51.  simple.PublisherCity = strArray[3];
  52.  simple.PublisherName = strArray[4];
  53.  Console.WriteLine(str);
  54.  list.Add(simple);
  55.  }
  56.  reader.Close();
  57.  }
  58.  catch (Exception exp)
  59.  {
  60.  Console.WriteLine(exp.Message);
  61.  }
  62.  
  63.  }
  64.  static double FindSredFromFail(string FileName)
  65.  {
  66.  double SrPrice = 0;
  67.  double i = 0;
  68.  double itogo = 0;
  69.  
  70.  try
  71.  {
  72.  string str = "";
  73.  bool flagFirst = false;
  74.  StreamReader reader = new StreamReader(FileName);//читающий курсор
  75.  while ((str = reader.ReadLine()) != null)
  76.  {
  77.  if (!flagFirst)
  78.  {
  79.  flagFirst = true;
  80.  continue;
  81.  }
  82.  string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
  83.  if (strArray.Length < 5)
  84.  continue;
  85.  SrPrice += double.Parse(strArray[2]);
  86.  i++;
  87.  
  88.  }
  89.  reader.Close();
  90.  itogo = SrPrice / i;
  91.  }
  92.  catch (Exception exp)
  93.  {
  94.  Console.WriteLine(exp.Message);
  95.  }
  96.  
  97.  return itogo;
  98.  }
  99.  
  100.  static string FindMinPrice(string FileName)
  101.  {
  102.  string strMin = "";
  103.  double min = 0;
  104.  bool flagSecond = false;
  105.  try
  106.  {
  107.  string str = "";
  108.  bool flagFirst = false;
  109.  StreamReader reader = new StreamReader(FileName);//читающий курсор
  110.  while ((str = reader.ReadLine()) != null)
  111.  {
  112.  if (!flagFirst)
  113.  {
  114.  flagFirst = true;
  115.  continue;
  116.  
  117.  }
  118.  string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
  119.  if (strArray.Length < 5)
  120.  continue;
  121.  if (!flagSecond)
  122.  {
  123.  min = double.Parse(strArray[2]);
  124.  strMin = strArray[1];
  125.  flagSecond = true;
  126.  }
  127.  if (double.Parse(strArray[2]) <= min)
  128.  {
  129.  min = double.Parse(strArray[2]);
  130.  strMin = strArray[1];
  131.  }
  132.  }
  133.  reader.Close();
  134.  }
  135.  catch (Exception exp)
  136.  {
  137.  Console.WriteLine(exp.Message);
  138.  }
  139.  return strMin;
  140.  }
  141.  
  142.  static string FindMaxPrice(string FileName)
  143.  {
  144.  string strMax = "";
  145.  double max = 0;
  146.  bool flagSecond = false;
  147.  try
  148.  {
  149.  string str = "";
  150.  bool flagFirst = false;
  151.  StreamReader reader = new StreamReader(FileName);//читающий курсор
  152.  while ((str = reader.ReadLine()) != null)
  153.  {
  154.  if (!flagFirst)
  155.  {
  156.  flagFirst = true;
  157.  continue;
  158.  
  159.  }
  160.  string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
  161.  if (strArray.Length < 5)
  162.  continue;
  163.  if (!flagSecond)
  164.  {
  165.  max = double.Parse(strArray[2]);
  166.  strMax = strArray[1];
  167.  flagSecond = true;
  168.  }
  169.  if (double.Parse(strArray[2]) >= max)
  170.  {
  171.  max = double.Parse(strArray[2]);
  172.  strMax = strArray[1];
  173.  }
  174.  }
  175.  reader.Close();
  176.  }
  177.  catch (Exception exp)
  178.  {
  179.  Console.WriteLine(exp.Message);
  180.  }
  181.  return strMax;
  182.  }
  183.  public static void SortIncrease(string FileName)//восходящая
  184.  {
  185.  string str = "";
  186.  List<Book> Sorted = new List<Book>();
  187.  bool flagFirst = false;
  188.  bool flagSecond = false;
  189.  StreamReader reader = new StreamReader(FileName);//читающий курсор
  190.  while ((str=reader.ReadLine()) != null)
  191.  {
  192.  if (!flagFirst)
  193.  {
  194.  flagFirst = true;
  195.  continue;
  196.  }
  197. string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
  198.  if (strArray.Length < 5)
  199.  continue;
  200.  if (!flagSecond)
  201.  {
  202.  Book q = new Book();
  203.  q.Id =int.Parse(strArray[0]);
  204.  q.Name = strArray[1];
  205.  q.Price=double.Parse(strArray[2]);
  206.  q.PublisherCity = strArray[3];
  207.  q.PublisherName = strArray[4];
  208.  Sorted.Add(q);
  209.  }
  210.  
  211.  }
  212.  reader.Close();
  213.  
  214.  Book[] Sorted2 = Sorted.ToArray();
  215.  for (int i = 0; i < Sorted2.Length - 1; i++)
  216.  {
  217.  //поиск минимального числа
  218.  int min=i;
  219.  for (int j = i + 1; j < Sorted2.Length; j++)
  220.  {
  221.  if (Sorted2[j].Price < Sorted2[min].Price)
  222.  {
  223.  min = j;
  224.  }
  225.  }
  226.  //обмен элементов
  227.  Book temp = Sorted2[min];
  228.  Sorted2[min] = Sorted2[i];
  229.  Sorted2[i] = temp;
  230.  }
  231.  for (int i = 0; i < Sorted2.Count(); i++)
  232.  Sorted2[i].PrintInFront();
  233.  }
  234.  
  235.  public static void SortDecrease(string FileName)//восходящая
  236.  {
  237.  string str = "";
  238.  List<Book> Sorted = new List<Book>();
  239.  bool flagFirst = false;
  240.  bool flagSecond = false;
  241.  StreamReader reader = new StreamReader(FileName);//читающий курсор
  242.  while ((str = reader.ReadLine()) != null)
  243.  {
  244.  if (!flagFirst)
  245.  {
  246.  flagFirst = true;
  247.  continue;
  248.  }
  249.  string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
  250.  if (strArray.Length < 5)
  251.  continue;
  252.  if (!flagSecond)
  253.  {
  254.  Book q = new Book();
  255.  q.Id = int.Parse(strArray[0]);
  256.  q.Name = strArray[1];
  257.  q.Price = double.Parse(strArray[2]);
  258.  q.PublisherCity = strArray[3];
  259.  q.PublisherName = strArray[4];
  260.  Sorted.Add(q);
  261.  }
  262.  
  263.  }
  264.  reader.Close();
  265.  
  266.  Book[] Sorted2 = Sorted.ToArray();
  267.  for (int i = 0; i < Sorted2.Length - 1; i++)
  268.  {
  269.  //поиск минимального числа
  270.  int max = i;
  271.  for (int j = i + 1; j < Sorted2.Length; j++)
  272.  {
  273.  if (Sorted2[j].Price > Sorted2[max].Price)
  274.  {
  275.  max = j;
  276.  }
  277.  }
  278.  //обмен элементов
  279.  Book temp = Sorted2[max];
  280.  Sorted2[max] = Sorted2[i];
  281.  Sorted2[i] = temp;
  282.  }
  283.  for (int i = 0; i < Sorted2.Count(); i++)
  284.  Sorted2[i].PrintInFront();
  285.  }
  286.  
  287.  public static void SortName(string FileName)//восходящая
  288.  {
  289.  string str = "";
  290.  List<Book> Sorted = new List<Book>();
  291.  bool flagFirst = false;
  292.  bool flagSecond = false;
  293.  StreamReader reader = new StreamReader(FileName);//читающий курсор
  294.  while ((str = reader.ReadLine()) != null)
  295.  {
  296.  if (!flagFirst)
  297.  {
  298.  flagFirst = true;
  299.  continue;
  300.  }
  301.  string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
  302.  if (strArray.Length < 5)
  303.  continue;
  304.  if (!flagSecond)
  305.  {
  306.  Book q = new Book();
  307.  q.Id = int.Parse(strArray[0]);
  308.  q.Name = strArray[1];
  309.  q.Price = double.Parse(strArray[2]);
  310.  q.PublisherCity = strArray[3];
  311.  q.PublisherName = strArray[4];
  312.  Sorted.Add(q);
  313.  }
  314.  
  315.  }
  316.  reader.Close();
  317.  
  318.  Book[] Sorted2 = Sorted.ToArray();
  319.  for (int i = 0; i < Sorted2.Length - 1; i++)
  320.  {
  321.  //поиск минимального cлова
  322.  int min = i;
  323.  for (int j = i + 1; j < Sorted2.Length; j++)
  324.  {
  325.  int res= string.Compare(Sorted2[j].Name, Sorted2[min].Name);
  326.  if (res<0)
  327.  {
  328.  Book temp = Sorted2[min];
  329.  Sorted2[min] = Sorted2[j];
  330.  Sorted2[j] = temp;
  331.  }
  332.  }
  333.  }
  334.  for (int i = 0; i < Sorted2.Count(); i++)
  335.  Sorted2[i].PrintInFront();
  336.  }
  337.  static void Main(string[] args)
  338.  {
  339.  Book book1 = new Book();
  340.  book1.Id = 55;
  341.  book1.Name = "Война и мир";
  342.  book1.Price = 120.5;
  343.  book1.PublisherName = "Эксмо";
  344.  book1.PublisherCity = "Москва";
  345.  
  346.  Book book2 = new Book();
  347.  book2.Id = 5;
  348.  book2.Name = "Три поросёнка ";
  349.  book2.Price = 95.99;
  350.  book2.PublisherName = "Просвящение";
  351.  book2.PublisherCity = "Саратов";
  352.  
  353.  Book book3 = new Book();
  354.  book3.Id = 35;
  355.  book3.Name = "Салют7 : история о космосе ";
  356.  book3.Price = 85.46;
  357.  book3.PublisherName = "Нева";
  358.  book3.PublisherCity = "Санкт-Петербург ";
  359.  
  360.  Book[] a = new Book[3];//Заполнили массив;
  361.  a[0] = book1;
  362.  a[1] = book2;
  363.  a[2] = book3;
  364.  
  365.  for (int i = 0; i < a.Length; i++)
  366.  {
  367.  a[i].PrintInFront();
  368.  Console.WriteLine();
  369.  }
  370.  
  371.  GetBooksFromFail("input.txt");
  372.  Console.WriteLine("Среднее значение стоимости книг " + FindSredFromFail("input.txt").ToString());
  373. Console.WriteLine(" Минимальная цена " + FindMinPrice("input.txt"));
  374.  Console.WriteLine(" Максимальная цена " + FindMaxPrice("input.txt"));
  375.  Console.WriteLine("Сортровка по возрастанию цены");
  376.  SortIncrease("input.txt");
  377.  Console.WriteLine("Сортровка по убыванию цены");
  378.  SortDecrease("input.txt");
  379.  Console.WriteLine("Сортировка по имени");
  380.  SortName("input.txt");
  381.  }
  382.  }
  383.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement