2009年7月5日

Code
<html>
<script type="text/javascript" src="wz_jsgraphics.js"></script>
<body>
<script type="text/javascript">
<!--
function getPrice(distance, night) {
var unitPrice = 2.0,
basicDistance = 3,
basicPrice = 10,
extDistance = 15,
extPrice = 3.0;
var price = 0.0;
if ( distance == 0 ) return 0;
if ( distance <= basicDistance ) return basicPrice;
if ( distance <= extDistance ) {
price = basicPrice + unitPrice * (distance - basicDistance);
return price;
}
price += basicPrice + unitPrice*(extDistance-basicDistance) + extPrice*(distance-extDistance)
return price;
}
function getPriceWithReset(distance, night, reset) {
if ( reset == 0 ) {
return getPrice(distance, night);
} else {
return getPrice(reset, night) * Math.floor(distance/reset) + getPrice(distance % reset, night);
}
}
var coordXUnit = 8, coordYUnit = 3,
basicX = 100, basicY = 50,
coordMaxX = 101, coordMaxY = 251;
function drawString(str, x, y) {
jg.drawString(str, basicX+x*coordXUnit, basicY+(coordMaxY-y)*coordYUnit);
}
function drawPoint(x, y) {
drawLine(x, y, x, y);
}
function drawLine(x1, y1, x2, y2) {
jg.drawLine(basicX + x1*coordXUnit, basicY + (coordMaxY-y1)*coordYUnit, basicX + x2*coordXUnit, basicY + (coordMaxY-y2)*coordYUnit);
}
function drawPolyline(ax, ay) {
var axx = new Array(), ayy = new Array();
for (var e in ax) {
axx.push(basicX + (ax[e] * coordXUnit) );
}
for (var e in ay) {
ayy.push(basicY + (coordMaxY - ay[e])*coordYUnit);
}
jg.drawPolyline(axx, ayy);
}
function drawCoordinate() {
jg.setColor("#ee8800");
drawLine(0, 0, coordMaxX, 0);
drawLine(0, 0, 0, coordMaxY);
drawString(0, -2, -2);
for (var i = 1; i < coordMaxX; i++) {
if (i%5==0) {
// draw grid
jg.setStroke(Stroke.DOTTED);
drawLine(i, 0, i, coordMaxY);
// draw mark
jg.setStroke(0);
drawLine(i, 0, i, -2);
drawString(i, i-1, -3);
} else {
drawLine(i, 0, i, -1);
}
}
for (var i = 1; i < coordMaxY ; i++) {
if (i%5==0) {
// draw grid
jg.setStroke(Stroke.DOTTED);
drawLine(0, i, coordMaxX, i);
// draw mark
jg.setStroke(0);
drawLine(0, i, -2, i);
drawString(i, -7, i+1);
} else {
drawLine(0, i, -1, i);
}
}
}
function drawPriceLine(night, reset) {
var ax = new Array(), ay = new Array();
for (var d=1; d<80; d++) {
ax.push(d);
ay.push(getPriceWithReset(d, night, reset));
}
drawPolyline(ax, ay);
}
/////////////// main section ///////////////
var jg = new jsGraphics();
jg.setFont("verdana,geneva,sans-serif", "10px", Font.PLAIN);
drawCoordinate();
drawString("reset:", 0, -10);
jg.setColor("#000000");
drawPriceLine(0, 0);
drawString("0", 5, -10);
jg.setColor("#FF0000");
drawPriceLine(0, 10);
drawString("10", 10, -10);
jg.setColor("#00FF00");
drawPriceLine(0, 15);
drawString("15", 15, -10);
jg.setColor("#0000FF");
drawPriceLine(0, 20);
drawString("20", 20, -10);
///////////////// from wangjing to my home, 35KM /////////////////
jg.setColor("#880000");
var xp = new Array(), yp = new Array();
for ( var i=10; i<30; i++) {
xp.push(i);
yp.push(getPriceWithReset(35, 0, i));
}
drawPolyline(xp, yp);
drawString(getPriceWithReset(35, 0, 15), 0, -20);
jg.paint();
//-->
</script>
</body>
</html>

2006年6月21日
摘要: CodeLib是一个做的非常不错的软件. fish大约是已经实现了当初"为了快速了解.Net,为了将自己的想法实现,也为的是希望在生命中留下个东西"的想法, 现在开始有了其它的想法. 做为个人,我当然希望CodeLib能够一直免费下去. 如果fish决定要收费的话, 那也是他个人的权利. 只是联想到我以前做过的事情, 小心眼的我突然觉得很不爽.
阅读全文
2006年6月12日
编辑:
把下一行挪到当前行的末尾,在Windows下很简单——在当前行末尾按del或者在下一行的开头按bs就可以了,
而在vi里面是要在当前行任意位置按J(大写的J,也就是Shift+j)
快速保存推出:
很多人习惯用标准的 :wq! 保存退出,不过我更喜欢按Shift+z+z,也就是ZZ,一样的。
查找:
/sometext 从当前向后找
?sometext 从当前向前找 (这两个命令执行后 按n继续向后找 按N继续向前找)
:g/sometext/
前者直接定位到单词上,后者仅仅是定位到那一行上
ex命令扩展:
vi下有一个ex命令模式可以用ex命令来进行快速编辑操作,基本的模式是:
[范围] g[lobal] /模式/ [ex命令] 在指定的“范围”内,对匹配“模式”的行执行“ex命令”
[范围] v[lobal] /模式/ [ex命令] 在指定的“范围”内,对不匹配“模式”的行执行“ex命令”
- 如果没有指定“范围”,则默认为全部。
- 如果没有制定“ex命令”,则默认是显示结果。
- “模式”即正则表达式。
“ex命令”在编辑方面,大致有 m(移动)s(替换)d(删除)
例子:
:g/^/m0 会从第一行开始把每一行移动到第一行之后,结果就是所有行逆序反转
:g/127.0.0.1/s/http/ftp/g 对包含127.0.0.1的行,将http替换为ftp
:g/127.0.0.1/s/^/\t/g 对包含127.0.0.1的行,每行之前加一个制表符(TAB)
:g/127.0.0.1/d 删除包含127.0.0.1的行
如果命令的内容中包含/字符,可以选择转义 \/ 或者把ex的命令分隔符/换成别的字符(例如~@#$%等)。例如要把包含/home的行中的/simon删除:
:g/\/home/ s/\/simon//g 用转义字符输入/
:g#/home#s#/simon##g 用#作为命令分隔符,则字符/可以直接使用了
查找-替换:
通常情况下查找替换的ex命令是
:%s/oldtext/newtext/g
下面是对这个命令的解释
: 表示进入ex的命令模式
% 是表示ex命令的source的范围. 后面的命令在该范围内执行. 该范围有多种表达方式:
% 表示全文
$ 表示到行尾
. 表示当前行
x,y 表示从第x行到y行, 例如 10,20s/oldtext/newtext/
此外还可以用其它的ex搜索命令表达范围
g/sometext/ 表示包含sometext的行
s 是ex的替换命令
/.../.../ 表示"搜索-替换"的内容.
g 是表示替换所有. 默认s只执行一次
-= 例子1 =-
:10,20s/^/ / 把第10到20行的前面加上4个空格
可以分解为 : 10,20 s /^/ /
-= 例子2 =-
:g/small/s/^/#/ 把所有包含small的行都加上注释#
g/small/ 查找包含small的行
/^/ 表示第一个非空白字符
可以分解为 g/small/ s /^/#/
2006年4月19日
摘要: 公司上了MySQL 5.0, 随之而来的不是用的有多爽的问题, 而是一直用的很顺手的mysqlcc...不行了, 其表现形式为经常会在状态栏中提示 "[192.168.22.72] ERROR 1146: Table 'rimkpi.1' doesn't exist" 之类的。 选出数据来也不能在表格中直接修改了,到底mysqlcc出了啥问题呢?
阅读全文
2006年4月1日
前几天写一个perl的脚本 在:?运算符上遇到了一个很诡异的问题
$data->{$id}->{'total'} ?
$data->{$id}->{'ratio'} = sprintf("%.2f%%", 100 * $data->{$id}->{'succ'} / $data->{$id}->{'total'}) :
$data->{$id}->{'ratio'} = 'N/A';
我的本意是 如果 $data->{$id}->{'total'} 未定义则不计算ratio,把ratio赋值为N/A. 这条语句等同于
if ( $data->{$id}->{'total'} ) {
$data->{$id}->{'ratio'} = sprintf("%.2f%%", 100 * $data->{$id}->{'succ'} / $data->{$id}->{'total'});
} else {
$data->{$id}->{'ratio'} = 'N/A';
可奇怪的是,当无论total是否有定义 ratio的结果居然都是N/A. 可后面if……else……的语句是没有问题的,真的是让我百思不得其解.
跑去查Perl的文档, 其中对于?:的运算符号的解释是
Ternary ``?:'' is the conditional operator, just as in C. It works much like an if-then-else. If the argument before the ? is true, the argument before the : is returned, otherwise the argument after the : is returned.
貌似是return the argument, 于是乎 我突然有了一个想法, 在前后都加上了括号……
$data->{$id}->{'total'} ?
( $data->{$id}->{'ratio'} = sprintf("%.2f%%", 100 * $data->{$id}->{'succ'} / $data->{$id}->{'total'}) ) :
( $data->{$id}->{'ratio'} = 'N/A' );
……居然就对了. 既然是return the argument, 我就又换了一种方式:
$data->{$id}->{'ratio'} = $data->{$id}->{'total'} ?
sprintf("%.2f%%", 100 * $data->{$id}->{'succ'} / $data->{$id}->{'total'}) :
'N/A';
虽然后面两种方式都可以理解, 那确实是一种正确的做法. 但为什么第一种方式的结果不对呢? 难道return the argument的意思就是不要在那里做赋值运算吗?
为了测试, 我又写了一个简单的小程序
1 #!/usr/bin/perl
2
3 use strict;
4
5 my $total=1;
6 my $rval;
7
8 ############################
9 $total ?
10 $rval = $total :
11 $rval = 'N/A';
12
13 print $rval, "\n";
14
15 ############################
16 $total ?
17 ( $rval = $total ) :
18 ( $rval = 'N/A' );
19
20 print $rval, "\n";
21
22 ############################
23 $rval = $total ? $total : 'N/A';
24
25 print $rval, "\n";
26
27 ############################
28 if ($total) {
29 $rval = $total;
30 } else {
31 $rval = 'N/A';
32 }
33
34 print $rval;
运行的结果显示, 无论第5行给$total赋什么值……包括1, "abc", "true", undef 等,执行的结果第一个print打印出来的都是N/A. 难道 $total? 不等价于 if ($total) 吗?
后来偶然的一次机会在PerlChina上问过此问题,才猛然发现原来C和Perl对于:?和=的优先级定义是不同的。在C中,=的优先级高于:?,而Perl中则正好相反。这直接导致了第一种情况对于语句的解释顺序与C截然不同。
2006年2月18日
摘要: Ubuntu 5.10下Apache2 SSL的配置
阅读全文
2006年1月26日
摘要: echo'2p3p[dl!d2+s!%0=@l!l^!<#]s#[s/0ds^]s@[p]s&[ddvs^3s!l#x0<&2+l.x]ds.x'|dc这个命令是用unix下的计算器dc求素数。dc是一个简单的基于堆栈的计算器程序,虽然简单但是可以实现“几乎”所有计算机可能实现的算法。关于这条命令的详细解释,等我弄明白了再写了。
阅读全文
2005年11月6日
摘要: Boot FreeBSD from Windows Boot loader
阅读全文
2005年9月21日
摘要: Turtles - So Happy Together
阅读全文
2005年9月13日
摘要: 这其实,都是为了解决软件的一种复杂性而导致的另外一种复杂性,所不同的就是人们可以选择自己更能够接收哪种复杂性。
阅读全文