C to D

void demo1()
{
// 基本类型占用的字节数和取值范围
writefln("type\tsizeof\tmin\tmax\tinit");
writefln("bool\t%d\t%#x\t%#x\t%#x", bool.sizeof, bool.min, bool.max, bool.init); // bool类型只占1个二进制位
writefln("char\t%d\t%#x\t%#x\t%#x", char.sizeof, char.min, char.max, char.init); // char都是无符号的,占1个字节
writefln("wchar\t%d\t%#x\t%#x\t%#x", wchar.sizeof, wchar.min, wchar.max, wchar.init); // 占2个字节
writefln("dchar\t%d\t%#x\t%#x\t%#x", dchar.sizeof, dchar.min, dchar.max, dchar.init); // 占4个字节
writefln("byte\t%d\t%#x\t%#x\t%#x", byte.sizeof, byte.min, byte.max, byte.init);
writefln("ubyte\t%d\t%#x\t%#x\t%#x", ubyte.sizeof, ubyte.min, ubyte.max, ubyte.init);
writefln("short\t%d\t%#x\t%#x\t%#x", short.sizeof, short.min, short.max, short.init);
writefln("ushort\t%d\t%#x\t%#x\t%#x", ushort.sizeof, ushort.min, ushort.max, ushort.init);
writefln("int\t%d\t%#x\t%#x\t%#x", int.sizeof, int.min, int.max, int.init);
writefln("uint\t%d\t%#x\t%#x\t%#x", uint.sizeof, uint.min, uint.max, uint.init);
writefln("long\t%d\t%#x\t%#x\t%#x", long.sizeof, long.min, long.max, long.init);
writefln("ulong\t%d\t%#x\t%#x\t%#x", ulong.sizeof, ulong.min, ulong.max, ulong.init);
writefln("float\t%d\t%e\t%e\t%f", float.sizeof, float.min, float.max, float.init);
writefln("float\t%d\t%f\t%f\t%f", float.sizeof, float.min, float.max, float.init);
writefln("double\t%d\t%e\t%e\t%f", double.sizeof, double.min, double.max, double.init);
writefln("double\t%d\t%f\t%f\t%f", double.sizeof, double.min, double.max, double.init);
writefln("real\t%d\t%e\t%e\t%f", real.sizeof, real.min, real.max, real.init);
//writefln("real\t%d\t%f\t%f", real.sizeof, real.min, real.max); // %f会溢出,格式化字符是?


// 特殊的浮点值
//double.nan
//double.infinity
//double.dig
//double.epsilon
//double.mant_dig
//double.max_10_exp
//double.max_exp
//double.min_10_exp
//double.min_exp 


// 浮点数取模
float x = 10;
float y = 6;
float f = x % y;
double d = x % y;
real r = x % y; 


bool result = (x < y);    // 如果 x 或 y 为 nan,则值为 false


// 初始化数组,不需要像C语言中那样循环
int[17] array;
array = 123; // 数组中的所有元素都被初始化为123


int[] aa1 = [ 3, 2, 1 ]; // 按索引顺序初始化数组中的每一个值,注意是中括号
    int[3] aa2 = [ 3, 2, 1 ];
//int[3] aa3 = [ 3, 2 ]; // 错误,元素个数与数组大小不一致
int[3] aa4 = [ 2:1, 0:3, 1:2 ]; // 指定初始化哪一个索引的元素
    int[3] aa5 = [ 2:1, 1:2 ]; // 只初始化部分元素,未赋值的元素被初始化为0
int[3] aa6 = [ 2:1, 0:3, 2 ]; // 如果未提供索引,则索引为前面的索引加1,这样混用不好
int[2][3] ab1 = [[2,3], [6,5], [3,4]]; // 3行、2列,从后往前看
int[3][2] ab2 = [[2,6,3],[3,5,4]];    // 2行、3列
    int[3][2] ab3;
    ab3[0] = [1, 2, 3]; // 初始化第一行
    ab3 = [[10, 20, 30], [11, 22, 33]]; // 初始化整个二维数组


// 遍历数组方法1
foreach (int value; array)
{
writeln(value);
}


// 遍历数组方法2
int i, j;


for (i = 0; i < array.length; i++)
{
writeln(array[i]);
}


// D 支持动态数组,可以轻易地改变大小
int[] array2;


array2.length = array2.length + 1; // 改变数组的大小
array2[array2.length - 1] = 100; 


// D 为 char 和 wchar 数组分别重载了‘~’和‘~=’运算符用于连接和追加
char[] s1; // 自动初始化为null
char[] s2; // 自动初始化为null
char[] s;  // 自动初始化为null


s = s1 ~ s2; // 两个null字符串连接结果仍为null
s ~= "hello"; // null与hello连接结果为hello


// break 和 continue 语句后可以带有标号。该标号是循环或 switch 结构外围的,break 用于退出该循环。
Louter:
    for (i = 0; i < 10; i++)
    {
        for (j = 0; j < 10; j++)
        {
            if (j == 3)
                break Louter;
            if (j == 4)
                continue Louter;
        }
    }
    // break Louter 跳转到这里 


// switch支持字符串
void dostring(char[] s)
{
switch (s)
{
case "hello":
break;
case "goodbye":
break;
case "maybe":
break;
default:
break;
}



// 设置结构对齐方式
struct ABC
{
int z;    // z is aligned to the default


align (1) int x;    // x is byte aligned
align (4)
{
int i; // declarations in {} are dword aligned
}


align (2):    // switch to word alignment from here on
int y;    // y is word aligned



// 匿名结构和联合
struct Foo
{
int i;
union
{
struct

int x; 
long y; 
}


char* p;
}
} // 结构、联合等的定义不需要分号结尾


Foo foo;


writeln(foo.i);
writeln(foo.x);
writeln(foo.y);
writeln(foo.p); 


// 结构成员的偏移:offsetof属性
auto off = Foo.y.offsetof;
writefln("off: %d", off);


// 可以显式地初始化结构体的成员,这样意义明确,并且不依赖于成员的位置
Foo foo1 = { i:3, y:5 };


union U { int a; long b; }
U u1 = { a:5 }; 
U u2 = { 5 }; // 与上面语句的效果相同
//U u3 = { b:5 }; // 错?


    // string类型与char[] wchar[] dchar[]数组的区别?


    // 枚举与数组
    enum COLORS { red, blue, green, max } // 枚举是怎么取值的?
int[COLORS] ab0 = [ COLORS.blue:3, COLORS.green:2, COLORS.red:5 ]; // 数组下标为枚举值?
    //char[][COLORS] cstring =
    //[
    //    COLORS.red : "red",
    //    COLORS.blue : "blue",
    //    COLORS.green : "green",
    //];


    // 字符串比较
    string str1 = "hello";
    if ("betty" == str1)
    {
    }


    if (str1 < "world")
    {
    }


    // 数组排序
    int[5] array1 = [ 4, 2, 5, 1, 3 ];
    array1.sort;


int xxxx;


}


// 动态闭包
//class Collection
//{
//    int[10] array;
//
//    void apply(void delegate(int) fp) // 什么意思?
//    {
//        for (int i = 0; i < array.length; i++)
//        {
//            fp(array);
//        }
//    }
//}
//
//void func1(Collection c)
//{
//    int max = int.min;
//
//    void comp_max(int i)
//    {
//        if (i > max)
//            max = i;
//    }
//
//    c.apply(comp_max); // 报错
//
//    writeln(max);
//} 
//
//void func2(Collection c) // 与func1等价
//{
//    int max = int.min;
//
//    c.apply(delegate(int i) { if (i > max) max = i; } ); // 匿名函数
//} 


//void demo2()
//{
//    Collection c = new Collection();
//    c.array = [ 9, 4, 3, 7, 1, 6, 3, 8 ];
//    func1(c);
//    func2(c);
//}


// 变参函数
// The ... following an array parameter declaration means that the trailing arguments are collected together 
// to form an array. The arguments are type checked against the array type, and the number of arguments becomes a property of the array
int sum(int[] values ...)
{
    int s = 0;


    foreach (int x; values)
    {
        s += x;
    }


    return s;
}


void demo3()
{
    int i;


    i = sum(8, 7, 6);
    writefln("sum = %d\n", i);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值