D语言中国主页  D语言编辑器SciTE4D   DWin库 D语言官方网站
D语言编译器1.x最新版 OpenSource   Tango   webnews  Wiki

查看完整版本: 6 D语言简单实例

yidabu 2007-4-29 09:08

6 D语言简单实例

6 D语言简单实例

tag:d language tutorial,d language program,d language 编程

下面所有代码都用最好的 D语言编辑器 SciTE4D为例,如果你还没有安装,可以到下面地址免费下功:

    [url]http://scite4d.d-programming-language-china.org/[/url]

[b]最简单的D语言例子[/b]

    在SciTE4D里新建一个文件,保存为D:\hello.d
   
        输入下面的代码并按Ctrl+S保存.虽然什么也没有做,却可以编译成功。[code]module hello.d;
void main()
{
}[/code]按Ctrl+F5运行代码看看.
        每个D语言程序必须包含一个main函数。在上面的例子中,只有一个main函数。main函数可能返回void或者int。可以没有参数,或者带参数char[][]支持命令行调用。


[b]输出hello[/b]

        示例代码如下:[code]module hello.d;

import tango.io.Console;
void main(char[][] args)
{
    Cout("hello, http://bbs.d-programming-language-china.org/ ").newline;
}[/code]import语句表示从tango.io.Console模块中导入符号。


[b]初始化变量[/b]

        在这个例子中,我们学习如何初始化整数和字符串变量。[code]module hello.d;
import tango.io.Stdout;
void main(char[][] args)
{
    int fid = 18;
    Stdout("D语言入门论坛 fid ")(fid).newline;
    Stdout("D语言论坛")(" http://bbs.d-programming-language-china.org/ ").newline;
}[/code]这里使用了Stdout函数,newline就是输出换行符。按Ctrl+F5运行.


[b]数据类型[/b]

        下面例子演示了用min,max,sizeof分别获得数据类型的最小值,最大值,和bytes[code]module hello.d;
import tango.io.Stdout;
int main()
{
    // 整型...
    Stdout("bool\tmin: {}\tmax: {} ({})", bool.min, bool.max, bool.sizeof).newline;
    Stdout("ubyte\tmin: {}\tmax: {} ({})", ubyte.min, ubyte.max, ubyte.sizeof).newline;
    Stdout("ushort\tmin: {}\tmax: {} ({})", ushort.min, ushort.max, ushort.sizeof).newline;
    Stdout("uint\tmin: {}\tmax: {} ({})", uint.min, uint.max, uint.sizeof).newline;
    Stdout("ulong\tmin: {}\tmax: {} ({})\n", ulong.min, ulong.max, ulong.sizeof).newline;
    Stdout("byte\tmin: {}\tmax: {} ({})", byte.min, byte.max, byte.sizeof).newline;
    Stdout("short\tmin: {}\tmax: {} ({})", short.min, short.max, short.sizeof).newline;
    Stdout("int\tmin: {}\tmax: {} ({})", int.min, int.max, int.sizeof).newline;
    Stdout("long\tmin: {}\tmax: {} ({})\n", long.min, long.max, long.sizeof).newline;

    // 浮点型...
    Stdout("float ({})\tdouble ({})\treal ({})\t", float.sizeof, double.sizeof, real.sizeof).newline;
    Stdout("ifloat ({})\tidouble ({})\tireal ({})\t", ifloat.sizeof, idouble.sizeof, ireal.sizeof).newline;
    Stdout("cfloat ({})\tcdouble ({})\tcreal ({})\t", cfloat.sizeof, cdouble.sizeof, creal.sizeof).newline;
    Stdout("cfloat ({})\tcdouble ({})\tcreal ({})\t", cfloat.sizeof, cdouble.sizeof, creal.sizeof).newline;

    // 字符型...
    Stdout("char ({})\twchar ({})\tdchar ({})\t", char.sizeof, wchar.sizeof, dchar.sizeof).newline;

    return 0;
}[/code]在D语言编器SciTE4D里,按Ctrl+F5运行程序,在编辑器的输出区将打印出信息.


[b]简单的If/Else语句[/b]

        代码如下:[code]                import tango.io.Stdout;

                void main() {
                        int i;
                        if (i == 1)
                                Stdout("我是1!").newline;
                        else
                                Stdout("我可不是1!").newline;
                }
        [/code][b]D语言的for循环[/b]

        多次重复做某事是很让人讨厌的。比如让你写下面的代码:[code]                Stdout("1\n");
                Stdout("2\n");
                Stdout("3\n");
                Stdout("4\n");
                ...
        [/code]这时for循环语句就派上用场了。它会返回原来的位置,并告诉计算机:从1数到10,打印出每个数字。[code]                import tango.io.Stdout;
                int main()
                {
                        for (int i = 1; i <= 10; i++)
                                Stdout(i).newline;
                        return 0;
                }
        [/code]等价于QuickBASIC的语句:[code]                DIM I%

                FOR I% = 1 TO 10
                        PRINT I%
                NEXT I%
        [/code]打印出:[code]                1
                2
                3
                4
                5
                6
                7
                8
                9
                10
        [/code][b]变量声明方法[/b][code]                void main() {
                        int myInt,
                                myint,
                                MYINT;
                }
        [/code]声明了三个int类型的变量,myInt,myint和MYINT。类似于C,C++和Java,在D语言里,所有变量必须在使用前声明,可以用,分隔一次声明多个变量。

        关于标识符,补充几点。在程序设计中,标识符如同人的名字,用来标识变量,函数..
        标识符必须以字母或下划线开始。一般我们在应用中标识符不用下划线开始,以避免和保留名字(如_arguments)冲突。
        标识符第一个以后的字符可以是字母,数字或下划线。
        标识符可以很长。
        在D语言里,标识符是大小写敏感的,在上面例子中myInt,myint和MYINT是三个不同的标识符。

[b]使用表达式[/b]

        下面的例子演示了数学表达式的使用:[code]                import tango.io.Stdout;

                void main() {
                        int i, j;

                        i = 12 * 12; /* 乘法 */
                        Stdout("12 x 12 = {}", i).newline; /* 144 */

                        j = i / 8; /* 除法 */
                        Stdout("144 / 8 = {}", j).newline; /* 18 */

                        i -= 44; /* 等价于 "i = i - 44;" */
                        Stdout("144 - 44 = {}", i).newline; /* 100 */
                }
        [/code][b]等待键盘输入的小程序[/b]

        这是一个Windows下待候用户键盘输入再继续的程序。[code]module hello.d;

import tango.io.Console;

void main()
{
    Cout("请输入你的名字").newline;
    char[] name;   
    auto result = Cin.readln(name);
    if(result && name.length)
        Cout("你的名字是 ")(name).newline;
    else
        Cout("难道你是火星人???").newline;
    Cin.readln(name);
}
        [/code]然后写一个dsss.conf文件,里面只有一行:[code][hello.d][/code]然后在SciTE4D里按F7编译,进入D:\,点击hello.exe, 试一下运行结果.如果你从未接触D语言,第一次运行本例就成功了,请到 D语言论坛 [url]http://bbs.d-programming-language-china.org/[/url] 本贴下报喜.
   

[b]开始使用函数[/b]

        我们把常用的操作写成函数,然后可以在任何时候,你需要的地方调用。[code]module hello.d;
import tango.io.Stdout;
uint squareIt(uint x)
{
    return x * x;
}

void main()
{
    uint i;
    Stdout("\t(初始值)", i).newline;

    i = squareIt(3);
    Stdout("\t(3 * 3)", i).newline;

    i = squareIt(4);
    Stdout("\t(4 * 4)", i).newline;

    i = squareIt(5);
    Stdout("\t(5 * 5)", i).newline;
}

        [/code][b]字符串连接的例子[/b][code]module hello.d;
import tango.io.Stdout;

void main()
{
    char[] s;
    Stdout.format("长度: {}\t字符串: {}", s.length, s).newline;

    s ~= "something ";
    Stdout.format("长度: {}\t字符串: {}", s.length, s).newline;

    s ~= "whatever";
    Stdout.format("长度: {}\t字符串: {}", s.length, s).newline;
}
        [/code][b]断言的例子[/b][code]module hello.d;
import tango.io.Stdout;

void main()
{
    Stdout("下面测试一下D语言的断言...");

    /* 下面的断言成功,因为他们的值是 true. */

    assert(true);
    assert('\xAA' == '\u00AA');
    assert(221 % 5 == 1);
    assert(25 * 4 * 20 + 4 == 2004);

    /*        断言失败...
        tango.core.Exception.AssertException@hello.d(行号): Assertion failure */

    assert(false);        //断言失败,会在输出区提示错误信息
    Stdout("如果看到这句,就是全部断言都成功(true).").newline;
}

        [/code][b]本文历史[/b]

    20070421
    初版
   
    20080315
    全部用例改用 Tango, 想想Ruby,PHP,Apache...开源的巨大成功,比较封闭且不兼容Tango的Phobos实在没有使用它的理由

caihemm 2008-2-3 13:58

变量名要小写 ok?

yidabu 2008-3-15 14:22

变量名大小写都可以
页: [1]
查看完整版本: 6 D语言简单实例