Поиск года в файле - C#
Формулировка задачи:
В текстовом файле, в каждой строке хранятся данные: ФИО автора, название, год издания, количество страниц. Как организовать поиск выводящий информацию о книгах, выпущенных после указанного года?
Вот что имеем:
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace ConsoleApplication7
- {
- class Program
- {
- static void Main(string[] args)
- {
- string stroka;
- Console.WriteLine("Введите год:");
- string year = Console.ReadLine();
- FileStream aFile = new FileStream("Рез.txt", FileMode.OpenOrCreate);
- StreamReader op = new StreamReader("Библ.txt", Encoding.Default);
- Regex regex = new Regex(year, RegexOptions.IgnoreCase);
- StreamWriter sw = new StreamWriter(aFile);
- stroka = op.ReadLine();
- Console.WriteLine("Результат поиска:");
- while (stroka != null)
- {
- string[] Mass = stroka.Split(';');
- string text = Mass[0];
- if (regex.IsMatch(text))
- {
- for (int i = 0; i < Mass.Length; i++)
- {
- Console.Write(Mass[i] + "\t");
- sw.Write(Mass[i] + ";");
- }
- sw.WriteLine();
- Console.WriteLine();
- }
- stroka = op.ReadLine();
- }
- sw.Close();
- Console.ReadKey();
- }
- }
- }
Решение задачи: «Поиск года в файле»
textual
Листинг программы
- static void Main(string[] args)
- {
- string stroka;
- Console.WriteLine("Введите год:");
- string year = Console.ReadLine();
- FileStream aFile = new FileStream("Рез.txt", FileMode.OpenOrCreate);
- StreamReader op = new StreamReader("Библ.txt", Encoding.Default);
- StreamWriter sw = new StreamWriter(aFile);
- stroka = op.ReadToEnd();
- Console.WriteLine("Результат поиска:");
- if (stroka != null)
- {
- string[] Mass = stroka.Split(new Char[] {'\n'});
- foreach(string mas in Mass)
- {
- Match m;
- string pat = @"\w*;";
- string pat1 = @"\d*";
- m = Regex.Match(mas, pat );
- string mas1=mas.Replace(Convert.ToString(m), "");
- m = Regex.Match(mas1, pat1);
- string ch = Convert.ToString(m).Replace("{}", "");
- if(Convert.ToInt32(ch)>Convert.ToInt32(year))
- {
- Console.WriteLine(mas);
- sw.WriteLine(mas);
- }
- }
- }
- else { Console.WriteLine("Error"); }
- //stroka = op.ReadLine();
- sw.Close();
- Console.ReadKey();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д