Richard Bao 的显示图片
Richard Bao
La vie est belle !
日志 - 2008年4月
2008/4/24 0:02:04

买了个魔方

话说北京 office 里有两个同事桌上都有魔方,我们把玩了很久,一直停留在只能拼出一个面的水平上。网上其实也有很多教程,但看了一眼就晕了。

两天前一个偶然,发现无意中拼出了教程中前两步的状态,翻回教程仔细研究,居然照着一步步地完成了整个魔方。这顿时改变了我的世界观。

理解了全部教程内容以后,现在已经可以在 15 分钟内拼好任意一个打乱的魔方了——至少不用再不停地翻看教程。估计越来越熟以后,速度还可以加快许多的吧。

在网上和 Blake 一人订了一个魔方,哈哈,走火入魔了~

贴一下这个教程的地址:http://www.rubik.com.cn/

最后更新: 2008/4/24 0:02:04 | Windows Live Space 网站中的链接 添加评论 此链接将在新窗口中打开 | 评论 评论 (14) | Windows Live Space 网站中的链接 原文地址 此链接将在新窗口中打开
2008/4/22 0:23:38

写了个简单的代码生成器

前些日子,工作上的需要,为了避免手工编写大量近似代码而编写了一个特定的“代码生成器”,可以将数据从 XML 配置文件中读出,然后输出合并后的代码。虽然代码内容都是直接 hard code 的,但也确实解决了不少问题,减少了相当一部分工作量。

事实上,很多情况下,我们都会需要这种“代码生成器”,就像 Word 中的邮件合并功能一样。只不过,这种生成器应当是为编程语言优化的——它应当有内置的大小写模式转换,应当有层次嵌套/循环的能力等等。基于这样的需求,我写了一个简单的但也足以应付绝大多数情况的代码生成器。

具体的不说,就看看它的输入输出好了。假设我们需要这样的代码:

namespace autoCode
{
    public class RectangleShape : Shape
    {
        public RectangleShape(int x, int y, int width, int height)
        {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        private int x = 0;
        public int x
        {
            get { return this.x; }
            set { this.x = value; }
        }

        private int y = 0;
        public int y
        {
            get { return this.y; }
            set { this.y = value; }
        }

        private int width = 100;
        public int width
        {
            get { return this.width; }
            set { this.width = value; }
        }

        private int height = 50;
        public int height
        {
            get { return this.height; }
            set { this.height = value; }
        }
    }
}

以及,

namespace autoCode
{
    public class CircleShape : Shape
    {
        public CircleShape(int centerX, int centerY, int radius)
        {
            this.centerX = centerX;
            this.centerY = centerY;
            this.radius = radius;
        }

        private int centerX = 0;
        public int centerX
        {
            get { return this.centerX; }
            set { this.centerX = value; }
        }

        private int centerY = 0;
        public int centerY
        {
            get { return this.centerY; }
            set { this.centerY = value; }
        }

        private int radius = 50;
        public int radius
        {
            get { return this.radius; }
            set { this.radius = value; }
        }
    }
}

可以看到,这些代码是有着高度规律的。于是,我们可以编写一个数据源 XML:

<?xml version="1.0" encoding="utf-8" ?>
<shapes>
    <shape name="rectangle">
        <inputs>
            <input name="x" type="int" default="0" />
            <input name="y" type="int" default="0" />
            <input name="width" type="int" default="100" />
            <input name="height" type="int" default="50" />
        </inputs>
    </shape>
    <shape name="circle">
        <inputs>
            <input name="centerX" type="int" default="0" />
            <input name="centerY" type="int" default="0" />
            <input name="radius" type="int" default="50" />
        </inputs>
    </shape>
</shapes>

然后再使用一定的语法编写一个模板:

<# FILE path={//shape} filename="{@name:Pascal}Shape.cs" #>
namespace autoCode
{
    public class <# ={@name:Pascal} #>Shape : Shape
    {
        public <# ={@name:Pascal} #>Shape(<# FOREACH path={inputs/input} separator=", " #><# ={@type} #> <# ={@name:camel} #><# END FOREACH #>)
        {
<# FOREACH path={inputs/input} #>
            this.<# ={@name:camel} #> = <# ={@name:camel} #>;
<# END FOREACH #>
        }
       
<# FOREACH path={inputs/input} #>
        private <# ={@type} #> <# ={@name:camel} #> = <# ={@default} #>;
        public <# ={@type} #> <# ={@name:Pascal} #>
        {
            get { return this.<# ={@name:camel} #>; }
            set { this.<# ={@name:camel} #> = value; }
        }
<# END FOREACH #>
    }
}

这样一来,只需要通过这个工具,给定上述数据源 XML 和模板文件,即可完成所有代码文件的自动生成。

最后更新: 2008/4/22 0:23:38 | Windows Live Space 网站中的链接 添加评论 此链接将在新窗口中打开 | 评论 评论 (2) | Windows Live Space 网站中的链接 原文地址 此链接将在新窗口中打开
2008/4/8 22:23:35

地铁抛锚

今天早上刚到北京,照常下地铁坐二号线去西直门。不到长椿街时,地铁突然停了,大概就这样停了半分钟的样子,然后慢慢驶进长椿街站。之后又忽快忽慢地驶至复兴门。震撼的是,当人流习惯性地向里狂挤时,列车司机在广播里喊道:本车是故障车,只开到下一站车公庄,到站后,本车,清人!
于是,我们就提前在车公庄下了,车站上本来就有狂多人,再加上整整一车刚从复兴门运出来的人,我们花了五分钟,才从车门口的人群中挪出来。生平第一次,遇到地铁抛锚。北京留念……
最后更新: 2008/4/8 22:23:35 | Windows Live Space 网站中的链接 添加评论 此链接将在新窗口中打开 | 评论 评论 (2) | Windows Live Space 网站中的链接 原文地址 此链接将在新窗口中打开