存档:2007年七月

Adapter pattern 适配器模式

七月 31, 2007 | 软件工程/编程技巧/设计模式 | RSS 2.0

using System;
using System.Collections.Generic;
using System.Text;
/*****************************************************
* Copyright     : [url]www.blog.163.com/liufabin66688[/url]
* FileName      : 适配器模式
* Author        : 刘发宾
* NickName      : 小和尚
* Version       : v1.0
* Description   :
* CreatDate     : 2007-7-31
* HistoryEdit   : 历史修改记录
———- 第一次修改 ——————-     
        Author  :
        Date    :
        Summary :
————–  ————————     
*****************************************************/
namespace ConsoleApplication1
{
 
    class ChemicalCompound
    {

        protected string name;
        protected float boilingPoint;
        protected float meltingPoint;
        protected double molecularWeight;
        protected string molecularFormula;
        //构造函数
        public ChemicalCompound(string name)
        {
            this.name = name;
       
         }
        //属性
        public float BoilingPoint
        {
            get { return boilingPoint; }
       
        }
        public float MeltingPoint
        {
            get { return meltingPoint; }
       
        }
        public double MolecularWeight
        {

            get { return molecularWeight; }
        }
        public string MolecularFormula
        {
            get { return molecularFormula; }
       
        }
   
   
    }
    //适配器,母接口-化学数据银行
    class ChemicalDatabank
    {
    //取得临界点
        public float GetCriticalPoint(string compound,string point)
        {
        float temperature=0.0F;
        //熔点
           if(point==”M”)
           {
             switch(compound.ToLower())
             {
                 case “water”:temperature=0.0F;break;
                 case “benzene”:temperature=5.5F;break;
                 case “alcohol”:temperature=-114.1F;break;
            
             }       
           }
          else
           {
               switch(compound.ToLower())
               {
              
                   case “water”:temperature=100.0F;break;
                   case “benzene”:temperature=80.1F;break;
                   case “alcohol”:temperature=78.3F;break;
               }
          
           }
        return temperature;
        }
     //取得分子式
        public string GetMolecularStructure(string compound)
        {
        string structure=”";
        switch(compound.ToLower())
        {
            case “water”:structure=”H20″; break;
            case “benzene”:structure=”C6H6″;break;
            case “alcohol”:structure=”C2H602″;break;
        }
       return structure;
        }
        public double GetMolecularWeight(string compound)
        {
        double  weight=0.0;
        switch(compound.ToLower())
        {
            case “water”:weight=18.015;break;
            case “benzene”:weight=78.1134;break;
            case “alcolor”:weight=46.0688;break;
       
        }
        return weight;
       
        }
    }
    //公接口
    class Compound : ChemicalCompound
    {
        private ChemicalDatabank bank;
        public Compound(string name)
            : base(name)
        {
        //适配器,母接口化学数据银行
            bank = new ChemicalDatabank();
            boilingPoint = bank.GetCriticalPoint(name,”B”);
            meltingPoint = bank.GetCriticalPoint(name,”M”);
            molecularWeight = bank.GetMolecularWeight(name);
            molecularFormula=bank.GetMolecularStructure(name);

       
        }
        public void Dispaly()
        {
            Console.WriteLine(”化合物:{0}”,name);
            Console.WriteLine(”分子式:{0}”,MolecularFormula);
            Console.WriteLine(”分子式:{0}”,MolecularWeight);
            Console.WriteLine(”熔点:{0}”,MeltingPoint);
            Console.WriteLine(”妇点:{0}”, BoilingPoint);
       
       
        }
   
   
   
   
    }
    class Program
    {
        static void Main(string[] args)
        {  //显示取得水的特性
            Compound water = new Compound(”Water”);
            water.Dispaly();
            //显示取得笨的特性
            Compound benzene = new Compound(”Benzene”);
            benzene.Dispaly();
            Compound alcohol = new Compound(”Alcohol”);
            alcohol.Dispaly();
            Console.Read();
        }
    }
}

没有评论 »

object的学习c#

七月 29, 2007 | c/c++, 软件工程/编程技巧/设计模式 | RSS 2.0

msdn上的例子

equals

using System;

public class Sample {

    void Method() {

    Object Obj1 = new Object();

    Object Obj2 = new Object();

    Console.WriteLine(Obj1.Equals(Obj2)); //===> false

    Obj2 = Obj1;

    Console.WriteLine(Obj1.Equals(Obj2)); //===> true

    }

}

using System;

class Point: Object

{ protected int x, y; public Point() { this.x = 0; this.y = 0; } public Point(int X, int Y)

{ this.x = X; this.y = Y; }

public override bool Equals(Object obj)

 { //Check for null and compare run-time types. if (obj == null || GetType() != obj.GetType()) return false; Point p = (Point)obj; return (x == p.x) && (y == p.y); } public override int GetHashCode() { return x ^ y; }}

class Point3D: Point { int z; public Point3D(int X, int Y, int Z) { this.x = X; this.y = Y; this.z = Z; }

 public override bool Equals(Object obj) { return base.Equals(obj) && z == ((Point3D)obj).z; }

public override int GetHashCode() { return base.GetHashCode() ^ z; }}

object gethashtable

using System;

public class SomeType {

   public override int GetHashCode() {

     return 0;

   }

}

public class AnotherType {

   public override int GetHashCode() {

     return 1;

   }

}

public class LastType {

   public override int GetHashCode() {

     return 2;

   }

}

public class MyClass {

   SomeType a = new SomeType();

   AnotherType b = new AnotherType();

   LastType c = new LastType();

   public override int GetHashCode () {

     return a.GetHashCode() ^ b.GetHashCode() ^ c.GetHashCode();

   }

}

object的gettype()

using System;

public class MyBaseClass: Object {

}

public class MyDerivedClass: MyBaseClass {

}

public class Test {

   public static void Main() {

      MyBaseClass myBase = new MyBaseClass();

      MyDerivedClass myDerived = new MyDerivedClass();

      object o = myDerived;

      MyBaseClass b = myDerived;

      Console.WriteLine(”mybase: Type is {0}”, myBase.GetType());

      Console.WriteLine(”myDerived: Type is {0}”, myDerived.GetType());

      Console.WriteLine(”object o = myDerived: Type is {0}”, o.GetType());

      Console.WriteLine(”MyBaseClass b = myDerived: Type is {0}”, b.GetType());

   }

}

浅拷贝与深拷贝

using System;

class MyBaseClass {

   public static string CompanyName = “My Company”;

   public int age;

   public string name;

}

class MyDerivedClass: MyBaseClass {

   static void Main() {

  

   // Creates an instance of MyDerivedClass and assign values to its fields.

   MyDerivedClass m1 = new MyDerivedClass();

   m1.age = 42;

   m1.name = “Sam”;

   // Performs a shallow copy of m1 and assign it to m2.

   MyDerivedClass m2 = (MyDerivedClass) m1.MemberwiseClone();

   }

}

没有评论 »

贪心算法找零钱c#php版本

七月 29, 2007 | c/c++, php, 数据结构算法 | RSS 2.0

先贴出我的php的, 不过我这个已经是排好序的.就是你要找的面值是从大到小的,而且是一分为单位的,切记不要忘记了转化

<?
function zhaoqian($m,$n)
{
 $k=count($m);
 for($i=0;$i<$k;$i++)
 {
  $num[$i]=$n/$m[$i];
  $n=$n%$m[$i];  
 }
 
 return  $num;
 
 
 
 
}
$m=array(25,10,5,1);
$n=99;
$num=zhaoqian($m,$n);
echo “找钱的方案”;
for($i=0;$i<count($m);$i++)
{
 
 echo intval($num[$i]).”枚”.intval($m[$i]).”面值”;
 
}


?>

c#版本的,这两个其实都一样我写完了他再写他

using System;
using System.Collections.Generic;
using System.Text;

namespace tanxin
{
    class Program
    {  
        public static int[] zhaoqian(int[] m,int n)
        {
          int k=m.Length;
          int[] num=new int[k];
          for(int i=0;i<k;i++)
          {
           num[i]=n/m[i];
            n=n%m[i];
          }
           return num;
    }
        static void Main(string[] args)
        {
            int[] m={25,10,5,1};
            int n=99;
            int[] num = new int[m.Length];
            num = zhaoqian(m, n);
            Console.WriteLine(”{0}的找钱方案”,n);
            for (int i = 0; i < m.Length; i++)
            {
                Console.WriteLine(num[i]+”枚”+m[i]+”面值”);
           
            }
            Console.ReadLine();

        }
    }
}

没有评论 »

全排列,组合的算法c#,php版

七月 29, 2007 | c/c++, php, 数据结构算法 | RSS 2.0

c#版本的  php的当然你也可以用层for循环,不过那样效率太底

/*****************************************************
* Copyright     : [url]www.blog.163.com/liufabin66688[/url]
* FileName      : 全排列算法
* Author        : 刘发宾
* NickName      : 小和尚
* Version       : v1.0
* Description   : 全排列c#版本,输出全排列。
* CreatDate     : 2007-7-29
* HistoryEdit   : 历史修改记录
———- 第一次修改 ——————-     
        Author  :
        Date    :
        Summary :
————–  ————————     
*****************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] s = “dabced”.ToCharArray();
            quanpailie(s,0,5);
            Console.WriteLine(”总数为{0}”,resultcount);
            Console.ReadLine();

        }
        static int resultcount = 0;
        public static void quanpailie(char[]list,int start,int end)//递归的产生全排列
        {
            if (start == end)
            {
                for (int i = 0; i <= end; i++)
                {

                    Console.Write(”{0}”, list[i]);
                  
               }
              Console.WriteLine();
                resultcount++;
            }
            else
            {
                for (int i = start; i<end; i++)
                {
                swap(ref list[start],ref list[i]);//交换位置
                quanpailie(list,start+1,end);
                swap(ref list[start],ref list[i]);//交换位置
               
               
                }
           
           
           
            }
        }
       public static void  swap( ref char a,ref char b)
               {
                char temp;
                temp=a;
                a=b;
                b=temp;
   
               }
       
       
       
       
    }
}
php版本的

<?
class quanpailie
{
 function type($list,$k,$m)
 { static  $count1=0;
  if($k==$m)
  {
     for($i=0;$i<=$m;$i++)
     echo  $list[$i];
     $this->$count1=$this->$count1+1;
  echo “”;
 
  }
  
  else
  {
   for($i=$k;$i<=$m;$i++)
   {
    $this->swap($list[$k],$list[$i]);
    $this->type($list,$k+1,$m);
    
    $this->swap($list[$k],$list[$i]);
    
   }
   
   
   
  }

 }
 public  function swap(&$a,&$b)
 {
  $temp=$a;
  $a=$b;
  $b=$temp;
  
 }

}
$aa=new quanpailie();
$var=array(1,4,6,5,6);
$aa->type($var,0,4);
//echo quanpailie->count1;

?>

////////

另附加别人的一求组合的

从n个字符串中取m个字符的所有组合(无放回抽样)

数学原理

Cm=Cn-1m+Cn-1m-1

c#代码示例

using System;
using System.Collections.Generic;
using System.Text;

namespace Combination
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] Sample = new string[] { “0″, “1″, “2″, “3″, “4″, “5″,”6″,”7″,”8″,”9″,”a”,”b”,”c”,”d”,”e”,”f”,”g” };
            List<string> SampleList = new List<string>();
            SampleList.AddRange(Sample);
            List<string> result = getCombination(SampleList, 3);
            Console.Read();
        }
        static List<string> getCombination(List<string> SampleList, int m)
        {
            if (m == 1)
            {
                return SampleList;
            }
            List<string> result = new List<string>();
            if (SampleList.Count == m)
            {
                StringBuilder temp_sb = new StringBuilder();
                foreach (string s in SampleList)
                {
                    temp_sb.Append(s);
                }
                result.Add(temp_sb.ToString());
                Console.WriteLine(temp_sb.ToString());
                return result;
            }
            string temp_firstelement = SampleList[0];
            SampleList.RemoveAt(0);
            List<string> temp_samplist1 = new List<string>();
            temp_samplist1.AddRange(SampleList);
            List<string> temp_list1 = getCombination(temp_samplist1, m – 1);
            foreach (string s in temp_list1)
            {
                result.Add(temp_firstelement + s);
                Console.WriteLine(temp_firstelement + s);
            }       
            List<string> temp_samplist2 = new List<string>();
            temp_samplist2.AddRange(SampleList);
            List<string> temp_list2 = getCombination(temp_samplist2, m);
            result.AddRange(temp_list2);           
            return result;
        }
    }
}

没有评论 »

准备复习数据结构和算法设计

七月 28, 2007 | 心情杂记, 数据结构算法 | RSS 2.0

       快毕业了,准备抽空把自己专业的核心课程研究研究!包括<<计算机算法设计与分析>>王哓东,<<数据结构>>当然是经典的严蔚敏的啊!

      有机会的话会学习的同时应用到实例中!主要是用c#和php实现!

     一 递归与分治策略

   

没有评论 »

c#设计模式之单例模式

七月 28, 2007 | c/c++, 软件工程/编程技巧/设计模式 | RSS 2.0

今天终于完成了创建型模式的学习 呵呵可喜可贺

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Threading;
namespace ConsoleApplication1
{
    class LoadBalancer
    {
    private static LoadBalancer balancer;
    private ArrayList severs=new ArrayList();
    private Random random=new Random();
    protected LoadBalancer()
       {
        //列出来所有的服务器
        severs.Add(”servaer1″);
        severs.Add(”servaer2″);
        severs.Add(”servaer3″);
        severs.Add(”servaer4″);
        severs.Add(”servaer5″);
  //    severs.Add(”servaer6″);
//      severs.Add(”servaer7″);

   
   
   
       }

   public static LoadBalancer getLoadBalancer()
   {
//通过双检锁的模式支持多线程的应用
   if(balancer==null)
   {
  
   Mutex mutex=new Mutex();
   mutex.WaitOne();
       if(balancer==null)
       {
       balancer=new LoadBalancer();
       mutex.Close();
      
       }
 
   }
  
  
    return balancer;
   }
 public string Server
 {
 get
 {
 int  r= random.Next(severs.Count);
 return severs[r].ToString();
 
 }
 }
    }

  class Client
  {
  [STAThread]
      static void Main(string[] args)
  {
  LoadBalancer b1=LoadBalancer.getLoadBalancer();
  LoadBalancer b2=LoadBalancer.getLoadBalancer();
  LoadBalancer b3=LoadBalancer.getLoadBalancer();
  LoadBalancer br=LoadBalancer.getLoadBalancer();
      if(b1==b2&&b2==b3&&b3==br)
      {
      Console.WriteLine(”同一个实例”);
     
      }
  Console.WriteLine(b1.Server);
 Console.WriteLine(b2.Server);
 Console.WriteLine(b3.Server);
 Console.WriteLine(br.Server);
 Console.Read();
  }
 
 
 
  }
}

没有评论 »

c#设计模式之原型模式

七月 27, 2007 | c/c++, 软件工程/编程技巧/设计模式 | RSS 2.0

using System;
using System.Collections;
using System.Text;

namespace ColorManager
{  //定义抽象原型prototype
    abstract class ColorPrototype
    {  //克龙方法
        public abstract ColorPrototype Clone();

    }
    //具体原型
    class Color : ColorPrototype
    {
        private int red, green, blue;
        public Color(int red, int green, int blue)
        {
            this.red = red;
            this.green = green;
            this.blue = blue;
       
        }
        public override ColorPrototype Clone()
        {   //浅表拷贝
            return (ColorPrototype)this.MemberwiseClone();

            //throw new Exception(”The method or operation is not implemented.”);
        }
        public void Display()
        {
            Console.WriteLine(”RGB values are{0},{1},{2}”,red,green,blue);
       
        }
   
   
   
    }
    //原型管理者
    class ColorManager
    {

        Hashtable colors = new Hashtable();
        public ColorPrototype this[string name]
        {

            get { return (ColorPrototype)colors[name]; }
            set { colors.Add(name,value); }
       
       
       
        }
   
    }
    //客户端测试
    class Client
    {
        [STAThread]
        static void Main(string[] args)
        {
            ColorManager colormanager = new ColorManager();
            colormanager["red"]=new Color(255,0,1);
            colormanager["green"] = new Color(0,255,0);
            colormanager["blue"]=new Color(22,266,55);
            string colorName = “blue”;
            Color c1=(Color)colormanager[colorName].Clone();
            c1.Display();
            colorName=”green”;
            Color c2=(Color)colormanager[colorName].Clone();
            c2.Display();
            Console.Read();
       
       
       
       
        }
   
   
   
   
    }

}

没有评论 »

smarty模板的嵌套二级分类

七月 27, 2007 | php | RSS 2.0

今日做到商品的网站,用到二级分类

模板:

<{section name=rows loop=$data}>
    <dl><dt onclick=”toggle(this)” ><img    src=”Images/menu_minus.gif”/>  <{$data[rows].Bigname}><dt><{section name=row loop=$data[rows].topic}><dd><{$data[rows].topic[row].CatName}></dd><{/section}></dl><{/section}>;

程序”

$sql=”select * from bigcat “;
$result=$DB->fetch($sql);

for($i=0;$i<count($result);$i++)//取得大分类的小分类
{
$Sql=”select * from cartid where BigId=’”.$result[$i]['BigId'].”‘”;
$result1[$i]=$DB->fetch($Sql);
$result[$i]['topic']=$result1[$i];
}

$smarty->assign(”data”,$result);

//////

$query = “SELECT id,name,name_cn FROM di_flag ORDER BY id desc”;

$result = mysql_query($query);

while($row = mysql_fetch_array($result))

{

                $query2=”SELECT id, name, name_cn,flag

                                FROM di_sort

                                WHERE di_sort.flag =$row[id]

                                ORDER BY id desc”;

                $result2=mysql_query($query2);

                while($row2 = mysql_fetch_array($result2))

                    {

                            $post[]=array(’sid’=>$row2['id'],

                                          ’sortname’=>$row2['name'],

                                     );

                     }

                $row_array[] = array(’cid’=>$row['id'],

                                      ‘cat_name’=>$row['name'],

                                      ‘topic’=>$post                       

                               );                             

                unset($post);

}

$smarty->assign(”forum”,$row_array);

unset($row_array);

模板页面内容:

<!–{section name=sec1 loop=$forum}–><div class=”sort_list”><a href=”products.php?flag=<!–{$forum[sec1].cid}–>” title=”<!–{$forum[sec1].cat_name}–>”><!–{$forum[sec1].cat_name}–></a><!–{section name=sec2 loop=$forum[sec1].topic}–><div class=”sort_list02″><a href=”products.php?sort=<!–{$forum[sec1].topic[sec2].sid}–>” title=”<!–{$forum[sec1].topic[sec2].sortname}–>”><!–{$forum[sec1].topic[sec2].sortname}–></a></div>

<!–{/section}–></div><!–{/section}–>

没有评论 »

转c#集合学习

七月 26, 2007 | c/c++ | RSS 2.0

一.先来说说数组的不足(也可以说集合与数组的区别):

1.数组是固定大小的,不能伸缩。虽然System.Array.Resize这个泛型方法可以重置数组大小,但是该方法是重新创建新设置大小的数组,用的是旧数组的元素初始化。随后以前的数组就废弃!而集合却是可变长的

2.数组要声明元素的类型,集合类的元素类型却是object.

3.数组可读可写不能声明只读数组。集合类可以提供ReadOnly方法以只读方式使用集合。

4.数组要有整数下标才能访问特定的元素,然而很多时候这样的下标并不是很有用。集合也是数据列表却不使用下标访问。很多时候集合有定制的下标类型,对于队列和栈根本就不支持下标访问!

 

二.下面讲述6种常用集合

1.ArrayList类

using System;

using System.Collections.Generic;

using System.Text;

using System.Collections;

namespace ConsoleApplication1

…{

    class Program

    …{

        static void Main(string[] args)

        …{

            ArrayList al = new ArrayList();

            al.Add(100);//单个添加

            foreach (int number in new int[6] …{ 9, 3, 7, 2, 4, 8 })

            …{

                al.Add(number);//集体添加方法一//清清月儿 http://blog.csdn.net/21aspnet/

            }

            int[] number2 = new int[2] …{ 11,12 };

            al.AddRange(number2);//集体添加方法二

            al.Remove(3);//移除值为3的

            al.RemoveAt(3);//移除第3个

            ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取旧ArrayList一部份

            Console.WriteLine(”遍历方法一:”);

            foreach (int i in al)//不要强制转换

            …{

                Console.WriteLine(i);//遍历方法一

            }

            Console.WriteLine(”遍历方法二:”);

            for (int i = 0; i != al2.Count; i++)//数组是length

            …{

                int number = (int)al2[i];//一定要强制转换

                Console.WriteLine(number);//遍历方法二

            }

        }

    }

}

 

2.Stack类

栈,后进先出。push方法入栈,pop方法出栈。

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Collections;

namespace ConsoleApplication1

…{

    class Program

    …{

        static void Main(string[] args)

        …{

            Stack sk = new Stack();

            Stack sk2 = new Stack();

            foreach (int i in new int[4] …{ 1, 2, 3, 4 })

            …{

                sk.Push(i);//填充

                sk2.Push(i);

            }

            

            foreach (int i in sk)

            …{

                Console.WriteLine(i);//遍历

            }

            sk.Pop();

            Console.WriteLine(”Pop”);

            foreach (int i in sk)

            …{

                Console.WriteLine(i);

            }

            

            sk2.Peek();//弹出最后一项不删除//清清月儿 http://blog.csdn.net/21aspnet/

            Console.WriteLine(”Peek”);

            foreach (int i in sk2)

            …{

                Console.WriteLine(i);

            }

            while (sk2.Count != 0)

            …{

                int i = (int)sk2.Pop();//清空

                sk2.Pop();//清空

            }

            Console.WriteLine(”清空”);

            foreach (int i in sk2)

            …{

                Console.WriteLine(i);

            }

        }

    }

}

 

3.Queue类

队列,先进先出。enqueue方法入队列,dequeue方法出队列。

 

using System;

没有评论 »

C#对象群集学习

七月 25, 2007 | c/c++ | RSS 2.0

 群集用来组合对象,群集对象能够维系多个到其他类型对象的引用,群集想象为装鸡蛋的纸板,而他有的对象就是鸡蛋!

所以两个都是对象,但是属性不同,地址博

 矩形数组 double[,] data=new double[2,3];

int[,] data={{3,55,2},{53,6,3}};

锯齿数组string[][] names;

string[][]names=new string[3][]

names[0]=new string[4]

names[1]=new string[2];

names[2]=new string[3]

有序列表,集合,字典

 

没有评论 »