递归浅谈

in Develop

在经典上看到有朋友对”递归”不甚了解,于是乎发表一下自己的拙见
说得不对的地方还请多多指教.

所谓递归
就是一个函数在执行的过程中不断地调用它本身
递归在某种程度上来说是一个知道”死亡周期的死循环”

举个简单的例子
现在要写一个方法,该方法要计算一个目录占用的磁盘空间
这个时候就是要计算出该目录下的文件及所有子目录下的文件大小之和
假设要计算D:\test目录的大小
该目录下有
1.cs/2.cs/3.cs/4.cs/5.cs
及4个子目录
下面是我写的方法

public static long GetDirectoryLength(string dirPath)
{
if (!Directory.Exists(dirPath))
return 0;
long len = 0;
DirectoryInfo di = new DirectoryInfo(dirPath);
foreach (FileInfo fi in di.GetFiles())
{
len += fi.Length;
}
DirectoryInfo[] dis = di.GetDirectories();
if (dis.Length > 0)
{
for (int i = 0; i < dis.Length; i++)
{
len += GetDirectoryLength(dis[i].FullName);
}
}
return len;
}

该方法中需要传递一个绝对的磁盘路径
这里我们要传入的是"D:\test\"
我们来看看这个代码的执行情况
执行第一次的时候:
if (!Directory.Exists(dirPath))
return 0;
long len = 0;
DirectoryInfo di = new DirectoryInfo(dirPath);
foreach (FileInfo fi in di.GetFiles())
{
len += fi.Length;
}
变量dirPath为"D:\test\"
上面代码会计算出test目录下的5个.cs文件的大小
那余下的目录怎么办?所以,在遍历所有目录的时候,再调用GetDirectoryLength方法,把当前遍历到的目录作为参数传递
if (dis.Length > 0)
{
for (int i = 0; i < dis.Length; i++)
{
len += GetDirectoryLength(dis[i].FullName);
}
}
这就是递归,递归一般用在有隶属关系的数据之中
如常见的Tree、计算一个数的阶乘

public static long test(int num)
{
long result = num;
if (num < 0)
return -1;
if (num == 0)
return 1;
result = result * (test(num - 1));
return result;
}

上面的代码是计算一个数的阶乘,比较简单,细细体会就能看懂,也就知道什么是递归、如何写递归了。

1 Comment

AES加密

in Develop


using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

class RijndaelMemoryStream
{
//static SymmetricAlgorithm key = Rijndael.Create();

static SymmetricAlgorithm Key
{
get
{
SymmetricAlgorithm key = Rijndael.Create();
key.Key = Encoding.ASCII.GetBytes(global.RijndaelMemoryStreamKey()); //密钥字串可在配置文件中定义
key.IV = Encoding.ASCII.GetBytes(global.RijndaelMemoryStreamIv()); //密钥iv可以配置文件中定义
return key;
}
}

/// <summary>
/// aes加密
/// </summary>
/// <param name="PlainText">明文</param>
/// <returns></returns>
public static string Encrypt(string PlainText)
{
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream encStream = new CryptoStream(ms, Key.CreateEncryptor(), CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(encStream))
{
sw.Write(PlainText);
}
}
return Convert.ToBase64String(ms.ToArray());
}
}
/// <summary>
/// aes解密
/// </summary>
/// <param name="CypherText">密文</param>
/// <returns></returns>
public static string Decrypt(string CypherText)
{
string str = "";
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(CypherText)))
{
using (CryptoStream encStream = new CryptoStream(ms, Key.CreateDecryptor(), CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(encStream))
{
str= sr.ReadToEnd();
}
}
}
return str;
}
}

0 Comments

微软新发布的针对VS2005的编程字体Consolas

in 笔记本子

微软最近发布了一款针对Visual Studio 2005的编程字体Consolas,载下来看了一下,感觉还不错,如果使用2003的朋友就享受不到了。
效果预览:

注意安装后Consolas会变成Visual Studio 2005的默认字体。
点击下载该压缩文档
转自博客园

0 Comments

As Long As You Love Me

in 音乐人生

[mp3:http://www.upfeeling.com/yl/music/om002A.mp3]

as long as u love me.只要你爱我就好
although loneliness has always been a friend of mine.孤独一直是我的朋友
i’m leaving my life in ur hands.自从你离开我的生活
people say i’m crazy that i am blind.朋友说我疯了太盲目
risking it all in a glance.激情总是短暂的
how you got me blind is still a mystery.你为何能使我如此盲目仍是个谜
i can’t get u out of my head.我就是无法忘了你
don’t care what is written in ur history.我不在乎你过去的种种
as long as u’re here with me.只要你陪在我身边

i don’t care who u are.我不在乎你是怎样个人
where u’re from.你从那里来
what u did.你做过什么
as long as u love me.只要你爱我就好
who u are.你是怎样个人
where u’re from.你从那里来
don’t care what u did.我不在乎你做过什么
as long as u love me.只要你爱我就好

every little thing that u have said done.所有你说过的话和做过的事
feels like it’s deep within me.都深深的烙印在我心里
doesn’t really matter if u’re on the run.我甚至于不在乎你是否就要逃开
it seems like we’re meant 2 be.我以为我们是一对的

i’ve tried 2 hide it so that no one knows.我试着把感情隐藏起来不让任何人知道
but i guess it shows.但我无法不流露真情
when u look in 2 my eyes.当你凝视着我时
what u did where u’re comin from.你做过什么从那里来
i don’t care,as long as u love me,baby!我不在乎,只要你爱我就好,宝贝!

i don’t care.我不在乎

0 Comments