Перенос страниц
This commit is contained in:
parent
4d57446057
commit
4c954c9186
129 changed files with 14 additions and 15 deletions
68
book/01-знакомство-с-языком-d/src/chapter-1-5/app.d
Normal file
68
book/01-знакомство-с-языком-d/src/chapter-1-5/app.d
Normal file
|
@ -0,0 +1,68 @@
|
|||
import std.algorithm, std.conv, std.regex, std.range, std.stdio, std.string, std.ascii;
|
||||
|
||||
struct PersonaData
|
||||
{
|
||||
uint totalWordsSpoken;
|
||||
uint[string] wordCount;
|
||||
}
|
||||
|
||||
void addParagraph(string line, ref PersonaData[string] info)
|
||||
{
|
||||
// Выделить имя персонажа и его реплику
|
||||
line = strip(line);
|
||||
// auto sentence = std.algorithm.find(line, ". ");
|
||||
auto sentence = line.find(". ");
|
||||
if (sentence.empty)
|
||||
{
|
||||
return;
|
||||
}
|
||||
auto persona = line[0 .. $ - sentence.length];
|
||||
sentence = toLower(strip(sentence[2 .. $]));
|
||||
// Выделить произнесенные слова
|
||||
auto words = split(sentence, regex("[ \t,.;:?]+"));
|
||||
// Вставка или обновление информации
|
||||
if (!(persona in info))
|
||||
{
|
||||
// Первая реплика персонажа
|
||||
info[persona] = PersonaData();
|
||||
}
|
||||
info[persona].totalWordsSpoken += words.length;
|
||||
foreach (word; words)
|
||||
{
|
||||
++info[persona].wordCount[word];
|
||||
}
|
||||
}
|
||||
|
||||
void printResults(PersonaData[string] info)
|
||||
{
|
||||
foreach (persona, data; info)
|
||||
{
|
||||
writefln("%20s %6u %6u", persona, data.totalWordsSpoken, data.wordCount.length);
|
||||
}
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
// Накапливает информацию о главных героях
|
||||
PersonaData[string] info;
|
||||
// Заполнить info
|
||||
string currentParagraph;
|
||||
foreach (line; stdin.byLine())
|
||||
{
|
||||
// 4 символа отступа
|
||||
if (line.startsWith(" ") && line.length > 4 && isAlpha(line[4]))
|
||||
{
|
||||
// Персонаж продолжает высказывание
|
||||
currentParagraph ~= line[3 .. $];
|
||||
}
|
||||
// 2 символа отступа
|
||||
else if (line.startsWith(" ") && line.length > 2 && isAlpha(line[2]))
|
||||
{
|
||||
// Персонаж только что начал говорить
|
||||
addParagraph(currentParagraph, info);
|
||||
currentParagraph = to!string(line[2 .. $]);
|
||||
}
|
||||
}
|
||||
// Закончили, теперь напечатаем собранную информацию
|
||||
printResults(info);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue