2008年3月22日

问题:猫一叫喊 所有的老鼠都逃跑

要点:1. 联动效果,运行代码只要执行Cat.Cryed()方法。2. 对老鼠和主人进行抽象
评分标准: <1>.构造出Cat、Mouse、Master三个类,并能使程序运行(2分)
            <2>从Mouse和Master中提取抽象(5分)
            <3>联动效应,只要执行Cat.Cryed()就可以使老鼠逃跑,主人惊醒。(3分)


    public interface Observer
    {
        void Response();    //观察者的响应,如是老鼠见到猫的反映
    }
    public interface Subject
    {
        void AimAt(Observer obs);  //针对哪些观察者,这里指猫的要扑捉的对象---老鼠
    }
    public class Mouse : Observer
    {
        private string name;
        public Mouse(string name, Subject subj)
        {          
            this.name = name;
            subj.AimAt(this);
        }
        
        public void Response()
        {
            Console.WriteLine(name + " attempt to escape!");
        }
    }
    public class Master : Observer
    {  
        public Master(Subject subj)
        {          
            subj.AimAt(this);
        }
        
        public void Response()
        {
            Console.WriteLine("Host waken!");
        }  
    }
  
    public class Cat : Subject
    {
        private ArrayList observers;
        public Cat()
        {  
            this.observers = new ArrayList();
        }
        public void AimAt(Observer obs)
        {
            this.observers.Add(obs);
        }
        public void Cry()
        {
            Console.WriteLine("Cat cryed!");
            foreach (Observer obs in this.observers)
            {
                obs.Response();
            }
        }
    }
    class MainClass
    {      
        static void Main(string[] args)
        {
            Cat cat = new Cat();
            Mouse mouse1 = new Mouse("mouse1", cat);
            Mouse mouse2 = new Mouse("mouse2", cat);
            Master master = new Master(cat);
            cat.Cry();
        }
    }


//---------------------------------------------------------------------------------------------

设计方法二: 使用event -- delegate设计..
    public delegate void SubEventHandler();
    public abstract class Subject
    {
        public event SubEventHandler SubEvent;
        protected void FireAway()
        {
            if (this.SubEvent != null)
                this.SubEvent();
        }  
    }
    public class Cat : Subject
    {  
        public void Cry()
        {
            Console.WriteLine("cat cryed.");
            this.FireAway();
        }
    }
    public abstract class Observer
    {
        public Observer(Subject sub)
        {
            sub.SubEvent += new SubEventHandler(Response);
        }
        public abstract void Response();    
    }
    public class Mouse : Observer
    {
        private string name;
        public Mouse(string name, Subject sub) : base(sub)
        {  
            this.name = name;
        }
        public override void Response()
        {
            Console.WriteLine(name + " attempt to escape!");
        }
    }
    public class Master : Observer
    {
        public Master(Subject sub) : base(sub){}
        public override void Response()
        {
            Console.WriteLine("host waken");
        }
    }
    class Class1
    {
        static void Main(string[] args)
        {
            Cat cat = new Cat();
            Mouse mouse1 = new Mouse("mouse1", cat);
            Mouse mouse2 = new Mouse("mouse2", cat);
            Master master = new Master(cat);
            cat.Cry();
        }

    }

//中国移动的例子

namespace ConsoleApplication1
{
    public interface IObserver
    {
        void Reply();        }

    public interface ISub
    {
        void AddIn(IObserver ob);
    }


    public class user : IObserver
    {
        private string no;

        public string No { get { return no; } }
        //构造时被加入到chinamobile
        public user(string no, ISub sub)
        {
            this.no = no;
            //add in
            sub.AddIn(this);
        }

        public void Reply()
        {
            Console.WriteLine(this.no + " reply");
        }
    }

    public class chinamobile:ISub
    {
        private ArrayList list = new ArrayList();

        public void AddIn(IObserver ob)
        {
            list.Add(ob);
        }

        public void send()
        {
            foreach (IObserver i in list)
            {
                //Console.WriteLine("Send to :" + i.No);
                i.Reply();
            }
        }
    }   
    class MainClass
    {
        static void Main(string[] args)
        {
            chinamobile cm = new chinamobile();
            user u1 = new user("111", cm);
            user u2 = new user("222", cm);
            user u3 = new user("333", cm);
            cm.send();
        }
    }
   

 

}

posted @ 2008-03-22 13:51 Humtong 阅读(196) 评论(1) 编辑


2008年3月12日

字母全排列,即给一个fat得到fat fta atf aft tfa taf 所有排列

首先可以看出,全排列其实就是阶乘,3个字母会有3阶乘种表现,4字母会有4阶乘种
上面fat的变化是有规律的 ,我们要得到全排列的做法是: 将三个字母分别作为开头,然后
调整除了第一个字符之外的位置,即fat 将后两个调整 fta
当有4个字母时,如golf,那么分别让g,o,l,f打头,当g打头是后面是olf,那就可以回到上面fat的例子
这就是递归的表现,4个字母是四种字母打头的后面是3的全排列,3个字母又是3种字母打头的后面是2的全排列 就像第一行所示
char[] currChar=new char[100];

func(int newSize)
{
 if(newSize==1)
   return;
 for(int j=0;j<newSize;j++)//进行newSize次 下面的MoveWord会让所有字母都有机会在第一个位置
{
  func(newSize-1); //对除第一个外的字母递归 
  if(newSize==2)//表示到了newSize=2的全排列 此时执行ShowWord将显示xy,执行MoveWord 变成 yx 因为当newSize=2还有一次循环,所以再执行ShowWord 显示yx,再进行MoveWord使得其归位到xy
    ShowWord();//显示当前字符
  MoveWord(newSize);//实现对后n-1个字符进行移动 加入golf送进来 移动成glfo, gfol ,golf
}
}

posted @ 2008-03-12 12:02 Humtong 阅读(193) 评论(0) 编辑

  我一直在寻找答案,寻找自己是否真实的喜欢做软件开发这个事,是否真的最终能从中实现自我。也许某一天有个做生意的大好机会,我就受利益所趋跑去做生意了,从此不再问津软件行业。可能一般人都会对故人故事有所留恋有所依赖,但我是那种时刻放得下的人,就是后怕自己会是这样的人,这并不同于所谓的拿得起放得下,这是残酷般的抛弃,有时候对自己的这种心态很失望。就像以前很多次玩游戏的时候也是这样,练了一个暑假的游戏,突然有一天觉得有点不适,之后就再也没登陆去过。这种行为差点延续到这篇文章,一种突然死亡……  但还是往下

  其实问题的根本不是选择的是什么,而是抱着什么样的心态去接受一件事。做任何事都是有相通的地方,那就是态度的问题,我内心缺少就是一个许三多,缺少踏实和认真。在口上我都一直承认自己唯一喜欢的就是做开发,但事实上我觉得内心深处并没有口上说的那么坚决,我并有没非常投入的去专研技术,总是停留在浅浅之处,以前还给自己一个无耻的借口:“生错时代,应该早10年”,bull shit..   我很喜欢看励志片,看传记,看大牛的事迹,虽然当时热血沸腾,可是一觉之后,就是像失忆一样,并没有从那些人身上提取到一种意志,回想自己大多数时间还是在闲逛,真想自虐一番。为什么呢?这和我成长在一个平静的生活环境有关?但既然我意识到了这些我为什么不能改呢?能?不能? 我想答案是不确定的,就像一句:“永远会对你好” 往往都是骗局。 真正能否做到只要慢慢的看自己,你可以分裂你自己,变成一个思考者一个行动者,思考者做错了不要总给自己忏悔的机会,行动者应该更勇敢一些,做思考者所想之事。 突然觉得把自己分裂成多个人是件有趣的事…… 扯了~~ 

  做成大事的很少有投机者,要做软件高手,做强人,先做人,绝对更重要。当某一天,发现自己内心找到一种宁静一种满足时,而不是现在的浮躁,那时一切都会变得容易:)

p:文章写的烂源于小时候没好好读书
 

posted @ 2008-03-12 11:38 Humtong 阅读(51) 评论(0) 编辑

之前一直发现有时候用迅雷下载歌曲会突然变1MB的速度,(我是1M电信 通常110kB/s),应该是交换机上限制了速度
但该1MB速度并不是虚假的 歌曲确实是如实下了一大段

现在做个试验当在迅雷 工具->配置->速度地方限制速度为80的时候
歌曲下载被限制到了80kB

此时如果去掉限制速度 马上又反弹会了300KB 但2秒后就又回到了80KB

于是对于挂机下载而言 可以使用 按键精灵 不断重复的用脚本设置限速和去掉限速 使得速度能得到一定反弹时候提升
其实早就想搞个脚本这样做 网上没找到(可能我孤陋寡闻了) 就自己录制了一个 让我录制的冲动是 在下uboutu的时候
突然到了7.9MB/s的速度 -- 
以下就是
/Files/humtong/1111111111111.txt

如何使用按键精灵就不说了 自己找一下

让这段脚本循环执行 就可以飞快下载 绝对可信~ 人格担保

注意:只能挂着下载 最大化迅雷后 将限制速度的地方填写70~90k的样子 然后启动该脚本
一下是使用脚本时瞬间反弹后达到650KB

不过该脚本还是存在问题,你可以适当在打钩后多延时一会 。等我录个再精确一点的传上来


posted @ 2008-03-12 01:28 Humtong 阅读(3850) 评论(8) 编辑


2008年3月1日

http://profile.myspace.cn/index.cfm?fuseaction=user.viewprofile&friendid=1304163741

故事很真实~~ 值得一看!!

posted @ 2008-03-01 00:12 Humtong 阅读(505) 评论(0) 编辑


2008年2月28日

删除结点的时候考虑情况分三种 叶子节点 有一个子结点 有两个子节点
----
using System;
using System.Collections.Generic;
using System.Text;


namespace chapt10_01
{
    //线索二叉搜索树
    class Node
    {
        public Node lchild;
        public Node rchild;
        public int lthread=1;
        public int rthread=1;
        public int Value;
        public Node(int v) { Value = v; }
    }

    class ThreadBinarySearchTree : chapt10_01.IThreadBinarySearchTree
    {
        private Node head;
        //0thread 1node
        public ThreadBinarySearchTree()
        {
            head = new Node(0);
            head.lthread = 0;
            head.lchild = head;
        }

        public void find(int element, ref Node parent, ref Node currentNode)
        { }

        public void insert(int element) {

            Node newNode = new Node(element);
            Node currentNode,parent;
            //empty
            if (head.lthread == 0)
            {
                head.lthread = 1;
                head.lchild = newNode;
                newNode.lchild = head;
                newNode.rchild = head;
                newNode.lthread = 0;
                newNode.rthread = 0;
            }
            else
            {
                currentNode = head.lchild;
                parent = head;
                while (currentNode != null)
                {
                    parent = currentNode;

                    if (head.lchild == head)
                    {
                        parent = head;
                    }
                    else if (element == currentNode.Value)
                    {
                        Console.Write("{0} is exist.canot insert", element);
                        return;
                    }
                    else if (element < currentNode.Value && currentNode.lthread == 1)
                        currentNode = currentNode.lchild;
                    else if (element < currentNode.Value && currentNode.lthread == 0)
                        currentNode = null;
                    else if (element > currentNode.Value && currentNode.rthread == 1)
                        currentNode = currentNode.rchild;
                    else if (element > currentNode.Value && currentNode.rthread == 0)
                        currentNode = null;                   
                }
                newNode.lthread = 0;
                newNode.rthread = 0;
                if (parent == head)
                {
                    head.lthread = 1;
                    head.lchild = newNode;
                    newNode.lchild = head;
                    newNode.rchild = head;
                }
                else if (element < parent.Value)
                {
                    newNode.lchild = parent.lchild;
                    newNode.rchild = parent;
                    parent.lthread = 1;
                    parent.lchild = newNode;
                }
                else if (element > parent.Value)
                {
                    newNode.lchild = parent;
                    newNode.rchild = parent.rchild;
                    parent.rthread = 1;
                    parent.rchild = newNode;
                }
            }

        }

        //找到中序后继
        public Node Inorder_successor(ref Node currentNode)
        {
            //right is thred
            if (currentNode.rthread == 0)
            {
                currentNode = currentNode.rchild;
            }
            else
            {//有孩子的最左
                currentNode = currentNode.rchild;
                while (currentNode.lthread != 0)
                {
                    currentNode = currentNode.lchild;
                }
            }
            return currentNode;
        }
        //前驱
        public Node Inorder_per(ref Node currentNode)
        {
            //Node parent;
            //right is thred
            if (currentNode.lthread == 0)
            {
                currentNode = currentNode.lchild;
            }
            else
            {
                currentNode = currentNode.lchild;
                while (currentNode.rthread != 0)
                {
                    //parent = currentNode;
                    currentNode = currentNode.rchild;
                }
            }
            return currentNode;
        }
            

        public void Inorder_traversal()
        {
            if (head.lchild == head)
            {
                Console.WriteLine("Tree is empty");
                return;
            }
            Node currentNode;
            currentNode = head.lchild;
            while (currentNode.lthread == 1)
            {
                currentNode = currentNode.lchild;
            }
            Console.Write(currentNode.Value + " ");
            while (currentNode.rchild != head)
            {
                currentNode = Inorder_successor(ref currentNode);
                Console.Write(currentNode.Value + " ");
            }
        }

        public void Inorder_retraversal()
        {
            if (head.lchild == head)
            {
                Console.WriteLine("Tree is empty");
                return;
            }
            Node currentNode;
            currentNode = head.lchild;
            while (currentNode.rthread == 1)
            {
                currentNode = currentNode.rchild;
            }
            Console.Write(currentNode.Value + " ");
            while (currentNode.lchild != head)
            {
                currentNode = Inorder_per(ref currentNode);
                Console.Write(currentNode.Value + " ");
            }
        }

        public void remove(int element)
        {
            if (head.lchild == head)
            {
                Console.Write("tree is empty");
                return;
            }
            //find del node & it's parent node
            Node parent,currentNode,child=null;
            currentNode = head.lchild;
            parent = head;
            while (currentNode.Value!=element&&currentNode != null)
            {
                parent = currentNode;
                if (element < currentNode.Value && currentNode.lthread == 1)
                    currentNode = currentNode.lchild;
                else if (element < currentNode.Value && currentNode.lthread == 0)
                    currentNode = null;
                else if (element > currentNode.Value && currentNode.rthread == 1)
                    currentNode = currentNode.rchild;
                else if (element > currentNode.Value && currentNode.rthread == 0)
                    currentNode = null;               
            }
            if (currentNode == null)
            {
                Console.WriteLine("cannot find {0}", element);
                return;
            }
            while (true)
            {
                //前驱和 后继
                Node i_per;
                Node i_suc;
                //leaf
                if (currentNode.lthread == currentNode.rthread && currentNode.lthread == 0)
                {
                    if (parent == head)
                    {
                        head.lthread = 0;
                        head.lchild = head;
                    }
                    else if (currentNode == parent.lchild)
                    {
                        parent.lthread = 0;
                        parent.lchild = Inorder_per(ref currentNode);
                    }
                    else if (currentNode == parent.rchild)
                    {
                        parent.rthread = 0;
                        parent.rchild = Inorder_successor(ref currentNode);
                    }
                    break;
                }
                    //case2 has only one child
                else if ((currentNode.lthread == 0 && currentNode.rthread == 1)
                    || (currentNode.lthread == 1 && currentNode.rthread == 0))
                {
                     i_per = Inorder_per(ref currentNode);
                     i_suc = Inorder_successor(ref currentNode);
                    //has left child
                    if (currentNode.lthread == 1)
                    {
                        child = currentNode.lchild;
                    }
                    else if (currentNode.rthread == 1)
                    {
                        child = currentNode.rchild;
                    }

                    if (currentNode == parent.lchild)
                    {
                        parent.lchild = child;
                    }
                    else if (currentNode == parent.rchild)
                    {
                        parent.rchild = child;
                    }
                    //move
                    if (currentNode.rthread == 1)
                    {
                        i_suc.lchild = i_per;
                        i_suc.lthread = 0;
                    }
                    else if (currentNode.lthread == 1)
                    {
                        i_per.rchild = i_suc;
                        i_per.rthread = 0;
                    }
                    break;
                }
                else//case3 has two children
                {

                    i_suc = currentNode;
                    Inorder_successor(ref i_suc);
                    //中序后继的父节点
                    parent = i_suc.rchild;
                    //兑换当前与中续后继的值
                    currentNode.Value = i_suc.Value;
                   
                    currentNode = i_suc;
                    continue;
                }

            }
            //free
            currentNode = null;
        }

        static void Main(string[] args)
        {
            ThreadBinarySearchTree t = new ThreadBinarySearchTree();
            t.insert(65);
            t.insert(40);
            t.insert(30);           
            t.insert(50);
            t.insert(45);
            t.insert(72);
            t.insert(69);
            t.insert(80);
           
            t.Inorder_traversal();
            Console.Write("\n");
            Console.WriteLine("删除 40");
            t.remove(40);
            t.Inorder_traversal();
        }
    }
}

posted @ 2008-02-28 17:41 Humtong 阅读(114) 评论(0) 编辑


2008年2月26日

Alignment Example
Left Many years later, as he faced the firing squad, Colonel Aureliano Buendia
was to remember that distant afternoon when his father took him to discover
ice.
Right

Many years later, as he faced the firing squad, Colonel Aureliano Buendia
was to remember that distant afternoon when his father took him to discover
ice.

Center

Many years later, as he faced the firing squad, Colonel Aureliano Buendia
was to remember that distant afternoon when his father took him to discover
ice. 

Justify Many  years  later, as he faced the firing squad, Colonel Aureliano Buendia
was to remember that distant afternoon when his father took him to discover
ice.
在word和html中 可以进行格式对齐
wo就模仿上表的对齐方式编写一段代码
大致思想就是寻找在字符串中截取width长度的字符串
..t happy...
假设被截取的字串最后5个字符是"t happ"
如果该字串最后一个字符非空格('p') 且最后一个字符的后一个(在截取之外原字串中'y')也不为空
说明该字串最后一个单词无法在前一行显示全部,此时就查找到最后单词的首部的前一格,也就是"t h"之间的空格处
之后通过补空格将..t填充到wdith为止 而happ则放到下一行

left,right,center都差不多只是添加空格位置不同 justify是从左到右讲单词间的空格扩充直到该截取行到width长度

代码如下:

class Alignments
    {
        public void adjust(string pString, int pWidth,char align)
        {          
           
            int backPostion = 0;
            int clen = 0;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < pString.Length; i+=pWidth)
            {
                backPostion = i;
                if (i > 0)
                {
                    //校正开始位置
                    i -= clen;
                   
                     //排除行首的空
                    while (pString[i - pWidth] == ' ')
                        i++;

                    if (pString[i - 1] != ' '&&pString[i]!=' ')
                    {
                        //如果当前是字符回退到该单词的首部的前一格
                        backPostion =BackToLastSpace(i - 1, pString);
                        //如果回退到了行首 说明整行都是字符
                        if (i == backPostion)
                        {
                            //直接添加
                            sb.Append(pString.Substring(i - pWidth, pWidth));
                            sb.Append("\n");
                            continue;
                        }
                    }
                    //截取该行 到最后一个不完整单词前
                    clen = i - backPostion;
                    switch (align)
                    {
                        case 'l':
                            sb.Append(pString.Substring(i - pWidth, backPostion % pWidth == 0 ? pWidth : backPostion % pWidth).TrimStart());
                            //补空该不完整
                            for (int p = 0; p < clen; p++)
                                sb.Append(" ");                           
                            break;
                        case 'r':
                            for (int p = 0; p < clen; p++)
                                sb.Append(" ");
                            sb.Append(pString.Substring(i - pWidth, backPostion % pWidth == 0 ? pWidth : backPostion % pWidth).TrimStart());                                                       
                            break;
                        case 'c':
                            for (int p = 0; p < (clen+1)/2; p++)
                                sb.Append(" ");
                            sb.Append(pString.Substring(i - pWidth, backPostion % pWidth == 0 ? pWidth : backPostion % pWidth).TrimStart());
                            for (int p = 0; p < (clen + 1) / 2; p++)
                                sb.Append(" ");
                            break;
                        case 'j':
                            //获取行 对行进行空格插入
                            string lineString =pString.Substring(i - pWidth, backPostion % pWidth == 0 ? pWidth : backPostion % pWidth).TrimStart();
                            int spaceIndex = 0;
                            int pIndex=0;
                            //不到width的长度
                            int curLen = clen;        
                            //补curlen个空格
                            while (curLen>0)
                            {
                                pIndex=spaceIndex;
                                //找到空格
                                while (lineString[spaceIndex] != ' ')
                                {
                                    spaceIndex++;
                                }
                                //将之前的单词加让空格
                                sb.Append(lineString.Substring(pIndex, spaceIndex - pIndex+1));
                                //再添加空格
                                sb.Append(" ");
                                spaceIndex++;
                                curLen--;
                            }
                            sb.Append(lineString.Substring(spaceIndex));
                            break;
                        default:
                            return;
                    }
                   
                    sb.Append("\n");                   
                    //将该行不完整单词补足                   
                }
            }

 

            int lastSpace = pString.Substring(backPostion).TrimStart().Length;
            //居中 最后一行特殊对待
            if (align == 'c')
                for (int p = 0; p < (pWidth - lastSpace + 1) / 2; p++)
                    sb.Append(" ");
            sb.Append(pString.Substring(backPostion).TrimStart());
            Console.Write(sb.ToString());
        }


        //不完整单词回退
        int BackToLastSpace(int curIdx,string pS)
        {
            while (pS[curIdx]!=' ')
            {
                curIdx--;
            }
            return curIdx;
        }

        static void Main(string[] args)
        {
            Alignments a = new Alignments();
            int b = "was to remember that distant afternoon when his father took him to discover".Length;
            a.adjust("Many years later, as he faced the firing squad, Colonel Aureliano Buendia was to remember that distant afternoon when his father took him to discover ice.",
                75, 'j');
        }
    }



posted @ 2008-02-26 21:20 Humtong 阅读(202) 评论(0) 编辑


2008年2月24日

posted @ 2008-02-24 21:18 Humtong 阅读(665) 评论(0) 编辑

posted @ 2008-02-24 14:26 Humtong 阅读(579) 评论(0) 编辑

posted @ 2008-02-24 14:06 Humtong 阅读(122) 评论(0) 编辑


posts - 14, comments - 11, trackbacks - 0, articles - 0

Copyright © Humtong