英語版
このページの英語版を見る
単語カウント
このプログラムは古典的なwc (wordcount) CプログラムのDバージョンである。 プログラムのDバージョンである。 ファイルの読み方、配列のスライス、連想配列を使った簡単なシンボル・テーブルの管理などを説明するものである、 そして連想配列を使った簡単なシンボル・テーブルの管理である。
import std.stdio; import std.algorithm; void main(string[] args) { ulong wordCount; ulong lineCount; ulong charCount; int[string] dictionary; writeln(" lines words bytes file"); foreach (arg; args[1 .. $]) { ulong lWordCount; ulong lCharCount; ulong lLineCount; auto file = File(arg); foreach (line; file.byLine(KeepTerminator.yes)) { lCharCount += line.length; foreach (word; splitter(line)) { lWordCount += 1; if (auto count = word in dictionary) *count += 1; else dictionary[word.idup] = 1; } lLineCount += 1; } writefln("%8s%8s%8s %s\n", lLineCount, lWordCount, lCharCount, arg); wordCount += lWordCount; lineCount += lLineCount; charCount += lCharCount; } const char[37] hr = '-'; if (args.length > 2) { writeln(hr); writefln("%8s%8s%8s total", lineCount, wordCount, charCount); } writeln(hr); foreach (word; sort(dictionary.keys)) { writefln("%3s %s", dictionary[word], word); } }
Copyright © 1999-2024 by the D Language Foundation
DEEPL APIにより翻訳、ところどころ修正。
このページの最新版(英語)
このページの原文(英語)
翻訳時のdmdのバージョン: 2.108.0
ドキュメントのdmdのバージョン: 2.109.1
翻訳日付 :
HTML生成日時:
編集者: dokutoku