开开眼界

cheniwantyou 2007-06-06 05:36:45
import std.stdio;
import std.stream;

int main (char[][] args)
{
int w_total;
int l_total;
ulong c_total;
int[char[]] dictionary;

writefln(" lines words bytes file");
foreach (arg; args[1 .. args.length])
{
int w_cnt, l_cnt;
bool inword;

auto c_cnt = std.file.getSize(arg);
if (c_cnt < 10_000_000)
{
size_t wstart;
auto input = cast(char[])std.file.read(arg);

foreach (j, c; input)
{
if (c == '\n')
++l_cnt;
if (c >= '0' && c <= '9')
{
}
else if (c >= 'a' && c <= 'z' ||
c >= 'A' && c <= 'Z')
{
if (!inword)
{
wstart = j;
inword = true;
++w_cnt;
}
}
else if (inword)
{ auto word = input[wstart .. j];

dictionary[word]++;
inword = false;
}
}
if (inword)
{ auto w = input[wstart .. input.length];
dictionary[w]++;
}
}
else
{
auto f = new BufferedFile(arg);
char[] buf;

while (!f.eof())
{ char c;

f.read(c);
if (c == '\n')
++l_cnt;
if (c >= '0' && c <= '9')
{
if (inword)
buf ~= c;
}
else if (c >= 'a' && c <= 'z' ||
c >= 'A' && c <= 'Z')
{
if (!inword)
{
buf.length = 1;
buf[0] = c;
inword = 1;
++w_cnt;
}
else
buf ~= c;
}
else if (inword)
{
if (++dictionary[buf] == 1)
buf = null;
inword = 0;
}
}
if (inword)
{
dictionary[buf]++;
}
}
writefln("%8s%8s%8s %s\n", l_cnt, w_cnt, c_cnt, arg);
l_total += l_cnt;
w_total += w_cnt;
c_total += c_cnt;
}

if (args.length > 2)
{
writefln("--------------------------------------\n%8s%8s%8s total",
l_total, w_total, c_total);
}

writefln("--------------------------------------");

foreach (word1; dictionary.keys.sort)
{
writefln("%3s %s", dictionary[word1], word1);
}
return 0;
}
...全文
192 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xenix 2007-06-06
  • 打赏
  • 举报
回复
楼主,您学不会D语言的
mLee79 2007-06-06
  • 打赏
  • 举报
回复
啥乱七八糟的 ... 貌似个无聊的脚本 ...
Vitin 2007-06-06
  • 打赏
  • 举报
回复
是C#吗,发错地方了哦。

计算所有文件中单词(以字母开头的字母数字串)字频的程序,对大文件使用了BufferedFile(估计是效率比较高)。对其中的dictionary比较感兴趣,使用Hash实现的吧,呵呵。

64,650

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧