题目位置: http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/sdevent6.mspx
官方解题: http://www.microsoft.com/technet/scriptcenter/funzone/games/solutions08/sdsol06.mspx
题目就是把大小写反过来, 然后把数字减1. 要求非常简单, 代码也不错, 而且我很早以前写过类似的东西, 就是完成大小写互相转换的. 我们首先从文件读取内容并存储到变量$in中. 设置$ofs为空, 然后利用switch对每个字符, 进行处理.
这里有一个非常小的技巧, $_表示字符'3'时候, 对应的十进制为: 51, 因此51 + 1就等于52, 这时候模10, 就得出了2. 这个写法其实不好, 影响大家理解, 不过结果是正确.
$in
=
get
-
content
./
lettercase
.
txt;
$ofs = '' ;
" $(switch ($in.ToCharArray()) {
{[char]::IsLower($_)} {[char]::ToUpper($_)}
{[char]::IsUpper($_)} {[char]::ToLower($_)}
{[char]::IsDigit($_)} {([int]$_ + 1) % 10 }
default {$_};
}) "
$ofs = '' ;
" $(switch ($in.ToCharArray()) {
{[char]::IsLower($_)} {[char]::ToUpper($_)}
{[char]::IsUpper($_)} {[char]::ToLower($_)}
{[char]::IsDigit($_)} {([int]$_ + 1) % 10 }
default {$_};
}) "