Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace lab4
- { [Serializable]
- struct TOYS
- {
- public string name;
- public int cost, age1, age2;
- public TOYS(string s, int c, int a1, int a2)
- {
- name = s;
- cost = c;
- age1 = a1;
- age2 = a2;
- }
- public override string ToString() // перегрузка метода ToString() для формирования строки
- {
- return ($"Игрушка - {name}, стоимость - {cost}, подходит детям от {age1} до {age2} лет");
- }
- }
- class Program
- {
- static int n = 0;
- static private Random rnd = new Random();
- static List <int> list = new List<int>();
- static List<int> list_f = new List<int>();
- static int[,] arr = new int[40, 40];
- static void Write_(FileStream f)
- {
- try
- {
- using (f)
- {
- using (BinaryWriter fout = new BinaryWriter(f))
- {
- Console.WriteLine("Исходный файл:");
- int a;
- int n = rnd.Next(5, 20);
- for (int i = 1; i <= n; i++)
- {
- a = rnd.Next(-10, 10);
- Console.Write(a + " ");
- fout.Write(a);
- }
- }
- }
- }
- catch(IOException e)
- {
- Console.WriteLine("Error:" + e.Message);
- return;
- }
- }
- static void Read_(FileStream f)
- {
- int a;
- try
- {
- using (f)
- {
- using (BinaryReader F = new BinaryReader(f))
- {
- for (; F.BaseStream.Position < F.BaseStream.Length;)
- {
- a = F.ReadInt32();
- list.Add(a);
- }
- }
- }
- }
- catch (FileNotFoundException e)
- {
- Console.WriteLine("Проверьте правильность имени файла" + e.Message);
- return;
- }
- catch (Exception e)
- {
- Console.WriteLine("Error:" + e.Message);
- return;
- }
- }
- static void Save(string fileName)
- {
- BinaryFormatter bf = new BinaryFormatter();
- using (Stream writer = new FileStream(fileName, FileMode.Create))
- {
- bf.Serialize(writer, list);
- }
- }
- static void Load(string fileName)
- {
- BinaryFormatter bf = new BinaryFormatter();
- using (Stream reader = new FileStream(fileName, FileMode.Open))
- {
- list_f = (List<int>)bf.Deserialize(reader);
- }
- }
- static void inp()
- {
- string buf;
- Console.WriteLine();
- Console.WriteLine("Введите n");
- buf = Console.ReadLine();
- n = int.Parse(buf);
- int k = 0;
- for (int i=0; i< n; i++)
- {
- for (int j=0; j<n; j++)
- {
- if (k <= list_f.Count - 1)
- arr[i, j] = list[k];
- else break;
- k++;
- }
- }
- }
- static void out1(int[,] arr) //вывод массива
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- Console.Write("{0}\t", arr[i, j]);
- }
- Console.WriteLine();
- }
- }
- static int find(int [] max_, int str)
- {
- for (int i = 0; i < str; i++)
- {
- for (int j = 0; j < n; j++)
- {
- max_[i] *= arr[i,j];
- }
- }
- Console.WriteLine();
- for (int i = 0; i < str; i++)
- Console.WriteLine(max_[i]);
- int max = Array.IndexOf(max_, max_.Max());
- return max;
- }
- static void reverse(int rez)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- arr[i,j] = arr[rez, j];
- }
- }
- }
- static void Create_BF(string name)
- {
- FileStream f = new FileStream(name, FileMode.Create);
- BinaryFormatter bf = new BinaryFormatter();
- TOYS D;
- int n, i;
- Console.WriteLine("Сколько игрушек?");
- n = int.Parse(Console.ReadLine());
- string[] d;
- for (i = 1; i <= n; i++)
- {
- Console.WriteLine("{0}-я игрушка", i);
- d = Console.ReadLine().Split(' ');
- D.name = d[0];
- D.cost = int.Parse(d[1]);
- D.age1 = int.Parse(d[2]);
- D.age2 = int.Parse(d[3]);
- bf.Serialize(f, D);
- }
- f.Close();
- }
- static void Toy(string name, int k)
- {
- FileStream f = new FileStream(name, FileMode.Open);
- BinaryFormatter bf = new BinaryFormatter();
- TOYS D;
- while (f.Position < f.Length) // пока в файле есть непрочитанные записи
- {
- D = (TOYS)bf.Deserialize(f);
- if ((D.age1 <= 5 && D.age2 >= 5) && D.cost <= k)
- Console.WriteLine(D);
- }
- f.Close();
- }
- static void Write_text(string name)
- {
- try
- {
- using(StreamWriter f= new StreamWriter(name))
- {
- int a;
- Console.WriteLine("Введите n");
- string buf = Console.ReadLine();
- n = int.Parse(buf);
- Console.WriteLine("Заполнение файла");
- for(int i=1; i <= n; i++)
- {
- buf = Console.ReadLine();
- a = int.Parse(buf);
- f.WriteLine(a);
- }
- }
- }
- catch (IOException e)
- {
- Console.WriteLine("Error:" + e.Message);
- return;
- }
- }
- static bool Find_Text(int b, string name)
- {
- try
- {
- using (StreamReader f = new StreamReader(name))
- {
- string s;
- while ((s = f.ReadLine()) != null)
- {
- int a = int.Parse(s);
- if (a == b) return true;
- }
- return false;
- }
- }
- catch (IOException e)
- {
- Console.WriteLine("Error:" + e.Message);
- return false;
- }
- }
- static void Write_text2(string name)
- {
- try
- {
- using (StreamWriter f = new StreamWriter(name))
- {
- Console.WriteLine("Введите n");
- string buf = Console.ReadLine();
- n = int.Parse(buf);
- Console.WriteLine("Заполнение файла");
- for (int i = 1; i <= n; i++)
- {
- buf = Console.ReadLine();
- f.WriteLine(buf);
- }
- }
- }
- catch (IOException e)
- {
- Console.WriteLine("Error:" + e.Message);
- return;
- }
- }
- static int Find_Text2(int b, string name)
- {
- try
- {
- using (StreamReader f = new StreamReader(name))
- {
- int a=0;
- int sum = 0;
- string s;
- while ((s = f.ReadLine()) != null)
- {
- string[] splitLine = s.Split(' ');
- for (int i = 0; i < splitLine.Length; i++)
- {
- a = int.Parse(splitLine[i]);
- if (a % b == 0) sum += a;
- }
- }
- return sum;
- }
- }
- catch (IOException e)
- {
- Console.WriteLine("Error:" + e.Message);
- return -1;
- }
- }
- static void Find_Text3( string name, StreamWriter f2)
- { int myInt;
- bool isNumerical = true;
- try
- {
- using (StreamReader f = new StreamReader(name))
- {
- string s;
- while ((s = f.ReadLine()) != null)
- {
- string[] splitLine = s.Split(' ');
- for (int i = 0; i < splitLine.Length; i++)
- {
- isNumerical = int.TryParse(splitLine[i], out myInt);
- if (isNumerical)
- break;
- }
- if (!isNumerical)
- {
- Console.WriteLine(s);
- f2.WriteLine(s);
- }
- }
- }
- }
- catch (IOException e)
- {
- Console.WriteLine("Error:" + e.Message);
- return;
- }
- }
- static void Main(string[] args)
- {
- FileStream f = new FileStream("test.dat", FileMode.Create);
- Write_(f);
- FileStream f1 = new FileStream("test.dat", FileMode.Open);
- Read_(f1);
- var uniq = list.Distinct();
- list = uniq.ToList();
- Save("finish.dat");
- Load("finish.dat");
- Console.WriteLine();
- foreach (var item in list_f)
- {
- Console.Write(item + " ");
- }
- inp();
- out1(arr);
- int str;
- str = list_f.Count / n;
- Console.WriteLine();
- Console.WriteLine("Количество заполненных строк " + str);
- int[] max_ = new int[str];
- for (int i = 0; i < str; i++)
- {
- max_[i] = 1;
- }
- int rez =find(max_, str);
- reverse(rez);
- Console.WriteLine();
- out1(arr);
- Create_BF("toy.dat");
- string buf; int k;
- Console.WriteLine();
- Console.WriteLine("Введите стоимость");
- buf = Console.ReadLine();
- k = int.Parse(buf);
- Toy("toy.dat",k);
- Write_text("Text.dat");
- Console.WriteLine("Введите заданное число");
- buf = Console.ReadLine();
- int b = int.Parse(buf);
- bool ok = Find_Text(b, "Text.dat");
- Console.WriteLine(ok);
- Write_text2("Text2.dat");
- Console.WriteLine("Введите заданное число");
- buf = Console.ReadLine();
- b = int.Parse(buf);
- int res = Find_Text2(b, "Text2.dat");
- Console.WriteLine($"Сумма:{res}");
- Write_text2("Text3.dat");
- StreamWriter f_ = new StreamWriter("Text3_.dat");
- Console.WriteLine();
- Find_Text3("Text3.dat", f_);
- f.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement