Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace Products
- {
- public class Book
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public double Price { get; set; }
- public string PublisherName { get; set; }
- public string PublisherCity { get; set; }
- public double SrPrice=0;
- public void PrintInFront()//Печать книги
- {
- Console.WriteLine("Name={0},Id={1},Price={2},PublisherName={3},PublisherCity={4}",Name,Id,Price,PublisherName,PublisherCity);
- }
- }
- class Program
- {
- static void GetBooksFromFail(string FileName)
- {
- List<Book> list = new List<Book>();//список
- try
- {
- string str = "";
- bool flagFirst = false;
- StreamReader reader = new StreamReader(FileName);//читающий курсор
- while ((str = reader.ReadLine()) != null)
- {
- if (!flagFirst)
- {
- flagFirst = true;
- continue;
- }
- string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
- if (strArray.Length < 5)
- continue;
- Book simple = new Book();
- simple.Id = int.Parse(strArray[0]);
- simple.Name = strArray[1];
- simple.Price = double.Parse(strArray[2]);
- simple.PublisherCity = strArray[3];
- simple.PublisherName = strArray[4];
- Console.WriteLine(str);
- list.Add(simple);
- }
- reader.Close();
- }
- catch (Exception exp)
- {
- Console.WriteLine(exp.Message);
- }
- }
- static double FindSredFromFail(string FileName)
- {
- double SrPrice = 0;
- double i = 0;
- double itogo = 0;
- try
- {
- string str = "";
- bool flagFirst = false;
- StreamReader reader = new StreamReader(FileName);//читающий курсор
- while ((str = reader.ReadLine()) != null)
- {
- if (!flagFirst)
- {
- flagFirst = true;
- continue;
- }
- string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
- if (strArray.Length < 5)
- continue;
- SrPrice += double.Parse(strArray[2]);
- i++;
- }
- reader.Close();
- itogo = SrPrice / i;
- }
- catch (Exception exp)
- {
- Console.WriteLine(exp.Message);
- }
- return itogo;
- }
- static string FindMinPrice(string FileName)
- {
- string strMin = "";
- double min = 0;
- bool flagSecond = false;
- try
- {
- string str = "";
- bool flagFirst = false;
- StreamReader reader = new StreamReader(FileName);//читающий курсор
- while ((str = reader.ReadLine()) != null)
- {
- if (!flagFirst)
- {
- flagFirst = true;
- continue;
- }
- string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
- if (strArray.Length < 5)
- continue;
- if (!flagSecond)
- {
- min = double.Parse(strArray[2]);
- strMin = strArray[1];
- flagSecond = true;
- }
- if (double.Parse(strArray[2]) <= min)
- {
- min = double.Parse(strArray[2]);
- strMin = strArray[1];
- }
- }
- reader.Close();
- }
- catch (Exception exp)
- {
- Console.WriteLine(exp.Message);
- }
- return strMin;
- }
- static string FindMaxPrice(string FileName)
- {
- string strMax = "";
- double max = 0;
- bool flagSecond = false;
- try
- {
- string str = "";
- bool flagFirst = false;
- StreamReader reader = new StreamReader(FileName);//читающий курсор
- while ((str = reader.ReadLine()) != null)
- {
- if (!flagFirst)
- {
- flagFirst = true;
- continue;
- }
- string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
- if (strArray.Length < 5)
- continue;
- if (!flagSecond)
- {
- max = double.Parse(strArray[2]);
- strMax = strArray[1];
- flagSecond = true;
- }
- if (double.Parse(strArray[2]) >= max)
- {
- max = double.Parse(strArray[2]);
- strMax = strArray[1];
- }
- }
- reader.Close();
- }
- catch (Exception exp)
- {
- Console.WriteLine(exp.Message);
- }
- return strMax;
- }
- public static void SortIncrease(string FileName)//восходящая
- {
- string str = "";
- List<Book> Sorted = new List<Book>();
- bool flagFirst = false;
- bool flagSecond = false;
- StreamReader reader = new StreamReader(FileName);//читающий курсор
- while ((str=reader.ReadLine()) != null)
- {
- if (!flagFirst)
- {
- flagFirst = true;
- continue;
- }
- string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
- if (strArray.Length < 5)
- continue;
- if (!flagSecond)
- {
- Book q = new Book();
- q.Id =int.Parse(strArray[0]);
- q.Name = strArray[1];
- q.Price=double.Parse(strArray[2]);
- q.PublisherCity = strArray[3];
- q.PublisherName = strArray[4];
- Sorted.Add(q);
- }
- }
- reader.Close();
- Book[] Sorted2 = Sorted.ToArray();
- for (int i = 0; i < Sorted2.Length - 1; i++)
- {
- //поиск минимального числа
- int min=i;
- for (int j = i + 1; j < Sorted2.Length; j++)
- {
- if (Sorted2[j].Price < Sorted2[min].Price)
- {
- min = j;
- }
- }
- //обмен элементов
- Book temp = Sorted2[min];
- Sorted2[min] = Sorted2[i];
- Sorted2[i] = temp;
- }
- for (int i = 0; i < Sorted2.Count(); i++)
- Sorted2[i].PrintInFront();
- }
- public static void SortDecrease(string FileName)//восходящая
- {
- string str = "";
- List<Book> Sorted = new List<Book>();
- bool flagFirst = false;
- bool flagSecond = false;
- StreamReader reader = new StreamReader(FileName);//читающий курсор
- while ((str = reader.ReadLine()) != null)
- {
- if (!flagFirst)
- {
- flagFirst = true;
- continue;
- }
- string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
- if (strArray.Length < 5)
- continue;
- if (!flagSecond)
- {
- Book q = new Book();
- q.Id = int.Parse(strArray[0]);
- q.Name = strArray[1];
- q.Price = double.Parse(strArray[2]);
- q.PublisherCity = strArray[3];
- q.PublisherName = strArray[4];
- Sorted.Add(q);
- }
- }
- reader.Close();
- Book[] Sorted2 = Sorted.ToArray();
- for (int i = 0; i < Sorted2.Length - 1; i++)
- {
- //поиск минимального числа
- int max = i;
- for (int j = i + 1; j < Sorted2.Length; j++)
- {
- if (Sorted2[j].Price > Sorted2[max].Price)
- {
- max = j;
- }
- }
- //обмен элементов
- Book temp = Sorted2[max];
- Sorted2[max] = Sorted2[i];
- Sorted2[i] = temp;
- }
- for (int i = 0; i < Sorted2.Count(); i++)
- Sorted2[i].PrintInFront();
- }
- public static void SortName(string FileName)//восходящая
- {
- string str = "";
- List<Book> Sorted = new List<Book>();
- bool flagFirst = false;
- bool flagSecond = false;
- StreamReader reader = new StreamReader(FileName);//читающий курсор
- while ((str = reader.ReadLine()) != null)
- {
- if (!flagFirst)
- {
- flagFirst = true;
- continue;
- }
- string[] strArray = str.Split(';');//Разобьём строку на 6 элементов
- if (strArray.Length < 5)
- continue;
- if (!flagSecond)
- {
- Book q = new Book();
- q.Id = int.Parse(strArray[0]);
- q.Name = strArray[1];
- q.Price = double.Parse(strArray[2]);
- q.PublisherCity = strArray[3];
- q.PublisherName = strArray[4];
- Sorted.Add(q);
- }
- }
- reader.Close();
- Book[] Sorted2 = Sorted.ToArray();
- for (int i = 0; i < Sorted2.Length - 1; i++)
- {
- //поиск минимального cлова
- int min = i;
- for (int j = i + 1; j < Sorted2.Length; j++)
- {
- int res= string.Compare(Sorted2[j].Name, Sorted2[min].Name);
- if (res<0)
- {
- Book temp = Sorted2[min];
- Sorted2[min] = Sorted2[j];
- Sorted2[j] = temp;
- }
- }
- }
- for (int i = 0; i < Sorted2.Count(); i++)
- Sorted2[i].PrintInFront();
- }
- static void Main(string[] args)
- {
- Book book1 = new Book();
- book1.Id = 55;
- book1.Name = "Война и мир";
- book1.Price = 120.5;
- book1.PublisherName = "Эксмо";
- book1.PublisherCity = "Москва";
- Book book2 = new Book();
- book2.Id = 5;
- book2.Name = "Три поросёнка ";
- book2.Price = 95.99;
- book2.PublisherName = "Просвящение";
- book2.PublisherCity = "Саратов";
- Book book3 = new Book();
- book3.Id = 35;
- book3.Name = "Салют7 : история о космосе ";
- book3.Price = 85.46;
- book3.PublisherName = "Нева";
- book3.PublisherCity = "Санкт-Петербург ";
- Book[] a = new Book[3];//Заполнили массив;
- a[0] = book1;
- a[1] = book2;
- a[2] = book3;
- for (int i = 0; i < a.Length; i++)
- {
- a[i].PrintInFront();
- Console.WriteLine();
- }
- GetBooksFromFail("input.txt");
- Console.WriteLine("Среднее значение стоимости книг " + FindSredFromFail("input.txt").ToString());
- Console.WriteLine(" Минимальная цена " + FindMinPrice("input.txt"));
- Console.WriteLine(" Максимальная цена " + FindMaxPrice("input.txt"));
- Console.WriteLine("Сортровка по возрастанию цены");
- SortIncrease("input.txt");
- Console.WriteLine("Сортровка по убыванию цены");
- SortDecrease("input.txt");
- Console.WriteLine("Сортировка по имени");
- SortName("input.txt");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement