Digital Mars官方网站:
D语言主页:d/index.html
D语言是一个系统程序设计语言。他基于融合了c和c++的高效以及像一些现代语言如ruby和python的特征。D语言是一种静态类型的,并且可以直接编译为本地代码。它是多范例的:强效支持,面向对象,模板风格。它的语法与C相似,并且看上去非常像是c++的。D语言可以在win32和linux下面编译,他们有不同的版本。
D语言没有单独开发的基于gui的工具,他所提供的编译工具都是基于命令提示符的,不过,我们可以和其它一些编译工具一起联合起来使用,只需配置一下就可以了。具体配置请看官方网站说明,当然,也可以看我的另一份说明http://riverbird2005.cublog.cn
一个D语言的示例子:
#!/usr/bin/dmd -run
/* sh style script syntax is supported */
/* Hello World in D
To compile:
dmd hello.d
or to optimize:
dmd -O -inline -release hello.d
*/
import std.stdio;
void main(char[][] args)
{
writefln("Hello World, Reloaded");
// auto type inference and built-in foreach
foreach (argc, argv; args)
{
// Object Oriented Programming
CmdLin cl = new CmdLin(argc, argv);
// Improved typesafe printf
writefln(cl.argnum, cl.suffix, " arg: %s", cl.argv);
// Automatic or explicit memory management
delete cl;
}
// Nested structs and classes
struct specs
{
// all members automatically initialized
int count, allocated;
}
// Nested functions can refer to outer
// variables like args
specs argspecs()
{
specs* s = new specs;
// no need for '->'
s.count = args.length; // get length of array with .length
s.allocated = typeof(args).sizeof; // built-in native type properties
foreach (argv; args)
s.allocated += argv.length * typeof(argv[0]).sizeof;
return *s;
}
// built-in string and common string operations
writefln("argc = %d, " ~ "allocated = %d",
argspecs().count, argspecs().allocated);
}
class CmdLin
{
private int _argc;
private char[] _argv;
public:
this(int argc, char[] argv) // constructor
{
_argc = argc;
_argv = argv;
}
int argnum()
{
return _argc + 1;
}
char[] argv()
{
return _argv;
}
char[] suffix()
{
char[] suffix = "th";
switch (_argc)
{
case 0:
suffix = "st";
break;
case 1:
suffix = "nd";
break;
case 2:
suffix = "rd";
break;
default:
break;
}
return suffix;
}
}
当然,我觉得下面的示例会更好些:
import std.stdio;
import std.stream;
int main (char[][] args)
{
int w_total;
int l_total;
ulong c_total;
int[char[]] dictionary;
writefln("baby do you love me?");
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;
}
阅读(2271) | 评论(0) | 转发(0) |