Вывод предложений из текстового файла в обратном порядке - C#
Формулировка задачи:
Помогите пожалуйста решить задачу!
Написать программу, которая считывает из текстового файла три предложения и выводит их в обратном порядке.
Что то как то пыталась:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader("111.txt");
string[] text = new string[3];
for (int i = 0; i < 3; i++)
{
text[i] = sr.ReadLine();
}
sr.Close();
for (int i = 2; i >= 0; i--)
{
Console.WriteLine(text[i]);
}
/* string text1 = File.ReadAllText("111.txt");
string[] sentences = text1.Split(new char[] { '.', '!', '?' });
foreach (string s in sentences)
{
if (s.Contains("оператор"))
Console.WriteLine(s);*/
Console.ReadLine();
}
}
}
}Решение задачи: «Вывод предложений из текстового файла в обратном порядке»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
var text = File.ReadAllText("111.txt");
var sentences = text.GetSentences();
var numbers = text.GetNumbers();
/*1 задание*/
foreach (var sentence in sentences.Take(3).Reverse())
Console.WriteLine(sentence);
/*2 задание*/
foreach (var sentence in sentences.Where(sentence => sentence.Contains("Hello")))
Console.WriteLine(sentence);
/*3 задание*/
foreach (var number in numbers)
Console.WriteLine(number);
Console.ReadKey();
}
}
static class Extensions
{
public static List<string> GetSentences(this string text)
{
if (string.IsNullOrEmpty(text))
throw new ArgumentNullException(nameof(text));
var matches = Regex.Matches(text, @"(\S.+?[.!?])(?=\s+|$)", RegexOptions.Compiled);
var sentences = matches.Cast<Match>().Select(match => match.Value).ToList();
return sentences;
}
public static List<double> GetNumbers(this string text)
{
if (string.IsNullOrEmpty(text))
throw new ArgumentNullException(nameof(text));
var matches = Regex.Matches(text, @"-?\d+(?:\.\d+)?", RegexOptions.Compiled);
var numbers = matches.Cast<Match>().Select(match => double.Parse(match.Value, CultureInfo.InvariantCulture)).ToList();
return numbers;
}
}
}