注意:需要有.net Framework 2.0才可以运行

.net Framework 2.0下载地址(如果你的电脑没有请在这儿下载):
http://www.gougou.com/search?search=.net%20Framework%202.0&id=0

学分绩点计算器下载地址:
http://download.csdn.net/source/973619
(要有CSDN账号才可以下载)

运行效果(示例而已):

效果

——————————————分割线下面是类关系图、代码———————-

阅读全文 »

遍历只做了先序,递归实现的,中序和后序都类似.代码比较简单,就不写注释,直接贴出来了

代码:

TreeNode:结点类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Text;

namespace 二叉树的建立和遍历
{
class TreeNode
{
public char data;
public TreeNode left, right;

public TreeNode(char c, TreeNode l, TreeNode r)
{
data = c;
left = l;
right = r;
}

public TreeNode()
{
left = right = null;
}
}
}

Tree:树类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Text;

namespace 二叉树的建立和遍历
{
class Tree
{
public TreeNode root;
Stack<TreeNode> stack = new Stack<TreeNode>();

public void CreateTree(String description)
{
bool left = true;
char[] descriptionarray = description.ToCharArray();
root = new TreeNode();
root.data = descriptionarray[0];
TreeNode temp = root;
for (int i = 1; i <= descriptionarray.Length - 1; i++)
{
if (descriptionarray[i] == '(')
{
left = true;
stack.Push(temp);
}
else if (descriptionarray[i] == ',')
{
left = false;
}
else if (descriptionarray[i] == ')')
{
stack.Pop();
}
else
{
temp = new TreeNode();
temp.data = descriptionarray[i];
if (left == true)
{
stack.Peek().left = temp;
}
else
{
stack.Peek().right = temp;
}
}
}
}

public void PreOrder(TreeNode t, String sign)
{
if (t != null)
{
Console.WriteLine(sign + t.data);
sign += sign;
if (t.left != null)
{
PreOrder(t.left, sign);
}

if (t.right != null)
{
PreOrder(t.right, sign);
}
}
}
}
}
阅读全文 »

题目内容:使用的排队现象,通过仿真手法评估其营业状况。
*基本要求:设某理发馆有N把理发椅,可同时为N位顾客进行理发。
*当顾客进门时,若有空椅,则可以立即坐下理发,否则需要依次排队等候。
*一旦有顾客理完发离去时,排在队头的顾客便可开始理发。
*若理发馆每天连续营业T小时,求一天内顾客在理发馆内的平均逗留时间
*顾客排队等候的队列平均长度

N和T在运行的时候输入

用C#写的,有注释,很混乱,请高人指教~~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace 队列应用
{
class Seat
{
public bool IsFree;
public Customer cus = null;

public Seat(bool b)
{
IsFree = b;
}
}

class Customer
{
public int cometime;
public int timetogo;
public int cost = 30 - new Random().Next(10); //理发需要20~30分钟

public Customer()
{
}
}

class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("每天营业多少小时?");
int workinghours = int.Parse(Console.ReadLine());
System.Console.WriteLine("有多少个椅子?");
int seats = int.Parse(Console.ReadLine());
Process(seats, workinghours);
Console.ReadLine();
}

static void Process(int num, int time)
{
//------------准备变量------------------------
Queue q = new Queue(); //队列
Seat[] S = new Seat[num]; //所有的椅子
for (int i = 0; i < num; i++) //初始化椅子
{
S[i] = new Seat(true);
S[i].cus = null;
}

int somebodycome = 1; //第一个顾客来的时间
//int count = 0;//顾客计数器
List<Customer> cuslist = new List<Customer>();
int st = num; //椅子数
int Qlen = 0; //队列长度
int Qchangetime = 0; //队列长度改变次数
//------------准备变量--------------------------
for (int t = 1; t <= time * 60; t++) //时间从第一分钟开始流逝,每分钟检查状态
{
//--------检查现在有没有人需要离开--------------
CheckLeave(S, t);
//--------检查现在有没有人需要离开--------------
//-------检查排队的人是否可以找到座位---------
CheckSeat(q, S, ref Qlen, ref Qchangetime, t);
//-------检查排队的人是否可以找到座位---------
//-------------如果来人了,有座位就坐下,没座位就排队-------------------
if (t == somebodycome)
{
bool IsSitted = false; //当前刚来的顾客是否找到了座位
Customer c = new Customer();
c.cometime = t;
cuslist.Add(c);
foreach (Seat s in S)
{
if (s.IsFree == true)
{
s.IsFree = false;
s.cus = c;
s.cus.timetogo = t + s.cus.cost;
IsSitted = true;
break;
}
}

if (IsSitted == false)
{
q.Enqueue(c);
Qlen += q.Count;
Qchangetime++;
}
else
{
IsSitted = false;
}

somebodycome += 10 - new Random().Next(5); //下一个顾客来的时间,假设5~10分钟之内会有一个
}

//------如果来人了,有座位就坐下,没座位就排队---------
}

//---------加班----------
bool Inseat = true;
//bool InQ = true;
bool KeepWorking = true;
int curtime = time * 60 + 1;
while (KeepWorking)
{
CheckLeave(S, curtime);
CheckSeat(q, S, ref Qlen, ref Qchangetime, curtime);
foreach (Seat s in S)
{
if (s.IsFree == false)
{
Inseat = true;
break;
}
else
{
Inseat = false;
}
}

KeepWorking = Inseat;
curtime++;
}

//---------加班----------
//--------------
int no = 1;
foreach (Customer c in cuslist)
{
Console.Write("第{0}个顾客  ", no);
Console.Write("来的时间:" + c.cometime);
Console.WriteLine("  走的时间:" + c.timetogo);
no++;
} //------------------

//--------打印结果-------------
int totalstaytime = 0;
foreach (Customer c in cuslist)
{
int staytime = c.timetogo - c.cometime;
totalstaytime += staytime;
}

int averragestay = totalstaytime / cuslist.Count;
System.Console.WriteLine("平均逗留时间:" + averragestay);
System.Console.WriteLine("顾客数量:" + cuslist.Count);
if (Qchangetime != 0)
{
int averagelen = Qlen / Qchangetime;
Console.WriteLine("队列平均长度:" + averagelen);
}
else
{
Console.WriteLine("椅子充足,不用排队");
}

Console.WriteLine("加班时间:" + (curtime - time * 60) + "分钟");
//--------打印结果------------
}

private static void CheckSeat(Queue q, Seat[] S, ref int Qlen, ref int Qchangetime, int t)
{
if (q.Count != 0) //如果有人排队
{
foreach (Seat s in S)
{
if (s.IsFree == true)
{
s.IsFree = false;
s.cus = (Customer) q.Dequeue();
Qlen += q.Count;
Qchangetime++;
s.cus.timetogo = t + s.cus.cost; //
}
}
}
}

private static void CheckLeave(Seat[] S, int t)
{
foreach (Seat s in S)
{
if (s.cus != null)
{
if (s.cus.timetogo == t)
{
s.IsFree = true;
}
}
}
}
}
}

官方即将出版中文版,看图:

本系列预计将在官方中文版available之后停止翻译

以后大家可以看质量有保证的了~~

但在正版出现之前,我将继续更新,希望大家继续关注,谢谢~~~~~~~~~

功能全面的文字竖排工具。

实现功能:截图、设置字体、繁体转换、文字竖排、设置行列数实现分页、输入原文的时候同步显示竖排文字、一键复制到剪切板等。

注意:C#编写,需要.net framework 2.0

下载地址:
http://download.csdn.net/source/686787

发现缺陷、不足、bug,请告知我,我会尽快修改

ps:没有混淆,可以用reflector看代码

阅读全文 »

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import  javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HomeWork extends MIDlet implements CommandListener {
Display d = Display.getDisplay(this);
FormattedFloatingDecimal.Form f = new Form("主窗口");
Command back = new Command("返回", Command.BACK, 1);
Command exit = new Command("退出", Command.EXIT, 1);
Command drs = new Command("绘制字符串", Command.OK, 1);
Command drc = new Command("画圆", Command.OK, 1);
Command drn = new Command("火柴棍拼字", Command.OK, 1);

//用于画圆的
public class CircleCanvas extends Canvas {
int pointX, pointY, len;

public void paint(Graphics g) {
g.setColor(255, 0, 0);
g.fillArc(pointX, pointY, len, len, 0, 360);
g.drawRect(pointX, pointY, len, len); //框住了就代表对了
}

public void drawCircle(int x, int y, int r) {
pointX = x - r;
pointY = y - r;
len = 2 * r;
}
}

//用于在指定位置绘制字符串
public class MyCanvas extends Canvas {
String printword = "";
int[] myenum = {
0, 0, Graphics.LEFT | Graphics.TOP,
getWidth() / 2, 0, Graphics.HCENTER | Graphics.TOP,
getWidth(), 0, Graphics.RIGHT | Graphics.TOP,
0, getHeight() / 2, Graphics.LEFT | Graphics.TOP, getWidth() / 2,
getHeight() / 2, Graphics.HCENTER | Graphics.TOP,
getWidth(), getHeight() / 2, Graphics.RIGHT | Graphics.TOP, 0,
getHeight(), Graphics.LEFT | Graphics.BOTTOM,
getWidth() / 2,
getHeight(), Graphics.HCENTER | Graphics.BOTTOM,
getWidth(), getHeight(), Graphics.RIGHT | Graphics.BOTTOM
};

int[] position = new int[3];

public void paint(Graphics g) {
g.setColor(255, 0, 0);
g.drawString(printword, position[0], position[1], position[2]);
}

public void DrawMyStrin(String s, int pos) {
printword = s;
position[0] = myenum[pos * 3 - 3];
position[1] = myenum[pos * 3 - 2];
position[2] = myenum[pos * 3 - 1];
}
}

//用火柴棍拼字
public class MatchNum extends Canvas {
int[][] lines =

{
{
100, 100, 120, 100
}, //0
{
100, 120, 120, 120
}, //1
{
100, 140, 120, 140
}, //2 //三个横
{
100, 100, 100, 120
}, //3
{
100, 120, 100, 140
}, //4//左边的两竖
{
120, 100, 120, 120
}, //5
{
120, 120, 120, 140
}
}; //6//右边的两竖
int[][] nums =

{
{
0, 2, 3, 4, 5, 6
}, //0
{
5, 6
}, //1
{
0, 1, 2, 5, 4
}, //2
{
0, 1, 2, 5, 6
}, //3
{
5, 6, 1, 3
}, //4
{
0, 1, 2, 3, 6
}, //5
{
3, 4, 0, 1, 2, 6
}, //6
{
0, 5, 6
}, //7
{
0, 1, 2, 3, 4, 5, 6
}, //8
{
0, 1, 3, 5, 6, 2
}, //9
};
int[] temp;

public void drawMyNum(int n) {
temp = nums[n];
}

public void paint(Graphics g) {
g.setColor(255, 0, 0);
int i = temp.length;
for (int j = 0; j <= i - 1; j++) {
int[] a = lines[temp[j]];
g.drawLine(a[0], a[1], a[2], a[3]);
}
}

}

public void startApp() {
f.addCommand(exit);
f.addCommand(drs);
f.addCommand(drc);
f.addCommand(drn);
f.setCommandListener(this);
d.setCurrent(f);
}

public void commandAction(Command c, Displayable dis) {
if (c == exit)
destroyApp(false);
else if (c == drs) {
MyCanvas m = new MyCanvas();
m.addCommand(back);
m.setCommandListener(this);
m.DrawMyStrin("字符串", new java.util.Random().nextInt(8) + 1);
d.setCurrent(m);
} else if (c == drc) {
CircleCanvas cc = new CircleCanvas();
cc.addCommand(back);
cc.setCommandListener(this);
cc.drawCircle(60, 60, 40);
d.setCurrent(cc);
} else if (c == drn) {
MatchNum mn = new MatchNum();
mn.addCommand(back);
mn.setCommandListener(this);
mn.drawMyNum(new java.util.Random().nextInt(9));
d.setCurrent(mn);
} else if (c == back) {
d.setCurrent(f);
}
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
delegate long DoMath(long num);

static void Main(string[] args)
{
Program p = new Program();
p.GetFactorial(26);
DoMath d = new DoMath(p.HowManyZero);
d += p.HowManyZero2;
d(26);
Console.Read();
}

public long HowManyZero(long num) //第一种解法
{
long result = 0;
for (long i = 1; i <= num; i++)
{
long j = i;
while (j % 5 == 0)
{
result++; //检查每一个数可以分解因式出多少个5
j /= 5;
}
}

Console.WriteLine(result);
return result;
}

public long HowManyZero2(long num) //第二种
{
long ret = 0;
while (num != 0)
{
ret += num / 5; //可以被5整除,然后是25、125、625......
num /= 5;
}

Console.WriteLine(ret);
return ret;
}

public decimal GetFactorial(long num) //获得阶乘
{
decimal ret = 1;
for (long i = 1; i <= num; i++)
{
ret *= i;
}

Console.WriteLine(ret);
return ret;
}
}
}

书上只标了2颗星,我怎么觉得这么费劲….
PS:decimal都算不到30的阶乘啊……

具体讲解书上有

顺便复习一下delegate~~~~~~~

编程之美—1的个数C#笨(效率低)的方法实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Program
{
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine(p.count_1_in_a_bounch_of_nums(13));
Console.Read();
}

public int count_1_in_a_num(int num)
{
int count = 0;
while (num != 0)
{
count += (num % 10) == 1 ? 1 : 0;
num /= 10;
}

return count;
}

public int count_1_in_a_bounch_of_nums(int end)
{
int count = 0;
for (int i = 1; i <= end; i++)
{
count += count_1_in_a_num(i);
}

return count;
}
}

internal值根据配置调整

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const double SPLIT = 0.01;
const int COUNT = 200;
const double PI = 3.14159265;
const int INTERVAL = 100;
double[] busySpan = new double [COUNT]; //array of busy times
double[] idleSpan = new double [COUNT]; //array of idle times
int half = INTERVAL / 2;
double radian = 0.0;
for (int i = 0; i < COUNT; i++)
{
busySpan[i] = (double) (half + (Math.Sin(PI * radian) * half));
idleSpan[i] = INTERVAL - busySpan[i];
radian += SPLIT;
}

double startTime = 0;
int j = 0;
while (true)
{
j = j % COUNT;
startTime = Environment.TickCount;
while ((Environment.TickCount - startTime) <= busySpan[j]) ;
System.Threading.Thread.Sleep((int) idleSpan[j]);
j++;
}
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Program
{
static void Main(string[] args)
{
int[] person = {0, 1, 1, 1, 1, 1, 15}; //总共六层楼,没有0层
int needed = 0;
int min = 0;
int target = 0;
for (int i = 1; i <= 6; i++)
{
needed = 0;
for (int j = 1; j < i; j++)
{
//目的在i层以下的
needed += person[j] * (i - j);
}

for (int j = i + 1; j <= 6; j++)
{
//目的在i层上面的
needed += person[j] * (j - i);
}

if (min == 0 || min >= needed)
{
min = needed;
target = i;
}
}

Console.WriteLine(target);
Console.Read();
}

用new修饰的方法,在编译期按变量类型调用

所以:

Number number = new IntNumber();
number.ShowInfo();

会显示”base class—“

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Number
{
public static int i = 123;

public virtual void ShowInfo()
{
Console.WriteLine("base class---");
}

public virtual void ShowNumber()
{
Console.WriteLine(i.ToString());
}
}

class IntNumber : Number
{
new public static int i = 456;

public new virtual void ShowInfo()
{
Console.WriteLine("Derived class---");
}

public override void ShowNumber()
{
Console.WriteLine("Base number is {0}", Number.i.ToString());
Console.WriteLine("New number is {0}", i.ToString());
}
}

class Test_Number
{
public static void Main() //Main_6_1_1
{
Number num = new Number();
num.ShowNumber();
IntNumber intNum = new IntNumber();
intNum.ShowNumber();
intNum.ShowInfo();
Number number = new IntNumber();
//究竟调用了谁?
number.ShowInfo();
//究竟调用了谁?
number.ShowNumber();
Console.Read();
}
}

C# Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void Main3_3_3()
{
int x = 10;

int y = 6;
if (x > y)
{
Console.WriteLine(x);
}
else
{
Console.WriteLine(y);
}

Console.Read();
}

对应MSIL及其注释:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
.method  public  hidebysig  static  void  Main3_3_3() cil managed 
{
.maxstack 2
.locals init (
[0] int32 x,
[1] int32 y,
[2] bool CS$4$0000) //定义三个变量
L_0000: nop
L_0001: ldc.i4.s 10 //把整型值10载入堆栈
L_0003: stloc.0 //把刚才载入堆栈的10赋值给第一个local变量,也就是int x=10;
L_0004: ldc.i4.6 //把整型值6载入堆栈
L_0005: stloc.1 //把刚才载入堆栈的6赋值给第二个local变量,也就是int y=6;
L_0006: ldloc.0
L_0007: ldloc.1 //这两句,把x,y两个local变量载入堆栈
L_0008: cgt //比较x,y的大小,结果会保存在堆栈最上方(结果以0或1表示true ,false)
L_000a: ldc.i4.0 //把0载入堆栈
L_000b: ceq //比较0和cgt的运算结果是否相等,结果会保存在堆栈最上方(结果以0或1表示true ,false)
L_000d: stloc.2 //ceq的运算结果保存入bool变量中
L_000e: ldloc.2 //再把ceq运算结果载入
L_000f: brtrue.s L_001a //判断跳转,若跳转显式Y,不跳转显式X
L_0011: ldloc.0
L_0012: callvoid [mscorlib]System.Console::WriteLine(int32)
L_0017: nop
L_0018: br.s L_0021
L_001a: ldloc.1
L_001b: callvoid [mscorlib]System.Console::WriteLine(int32)
L_0020: nop
L_0021: call int32 [mscorlib]System.Console::Read()
L_0026: pop
L_0027: ret
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
abstract class Element
{
public abstract void Accept(Visitor v);
}

class Elem1 : Element
{
public override void Accept(Visitor v)
{
v.VisitElem1(this);
}
}

class Elem2 : Element
{
public override void Accept(Visitor v)
{
v.VisitElem2(this);
}
}

abstract class Visitor
{
public abstract void VisitElem1(Elem1 e1);
public abstract void VisitElem2(Elem2 e2);
}

class Visitor1 : Visitor
{
public override void VisitElem1(Elem1 e1)
{
Console.WriteLine(this.GetType().Name + "  " + e1.GetType().Name);
}

public override void VisitElem2(Elem2 e2)
{
Console.WriteLine(this.GetType().Name + "  " + e2.GetType().Name);
}
}

class Visitor2 : Visitor
{
public override void VisitElem1(Elem1 e1)
{
Console.WriteLine(this.GetType().Name + "  " + e1.GetType().Name);
}

public override void VisitElem2(Elem2 e2)
{
Console.WriteLine(this.GetType().Name + "  " + e2.GetType().Name);
}
}

class ObjStructure
{
private List<Element> elems = new List<Element>();

public void add(Element e)
{
elems.Add(e);
}

public void visit(Visitor v)
{
foreach (Element e in elems)
{
e.Accept(v);
}
}
}

class Client
{
public static void Main()
{
ObjStructure o = new ObjStructure();
o.add(new Elem1());
o.add(new Elem2());
o.visit(new Visitor1());
o.visit(new Visitor2());
Console.Read();
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
abstract class Expretion
{
public void Translate(Words w)
{
string s = w.MyWords.Substring(0, 1);
w.MyWords = w.MyWords.Substring(2);
Excute(s);
}

public abstract void Excute(string s);
}

class EnglishExpretion : Expretion
{
public override void Excute(string s)
{
switch (s)
{
case "e":
Console.WriteLine("对方说的英语");
break;
}
}
}

class GermanExpretion : Expretion
{
public override void Excute(string s)
{
switch (s)
{
case "g":
Console.WriteLine("对方说的德语");
break;
}
}
}

class Words
{
private string words;

public string MyWords
{
get { return words; }
set { words = value; }
}
}

class Client
{
public static void Main()
{
Words w = new Words();
w.MyWords = "e g e e g ";
Expretion e = null;
while (w.MyWords.Length > 0)
{
switch (w.MyWords.Substring(0, 1))
{
case "e":
e = new EnglishExpretion();
break;
case "g":
e = new GermanExpretion();
break;
}

e.Translate(w);
}

Console.Read();
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
class MovieTheater
{
private Hashtable movies = new Hashtable();

public void GiveMeAMovie(string name)
{
Movie m;
if (movies.ContainsKey(name))
{
m = (Movie) movies[name];
}
else
{
m = new ConcreteMovie(name);
movies.Add(name, m);
}

m.ShowTheMovie();
}

public void HowManyMoviesHaveWeOrdered()
{
Console.WriteLine(movies.Count);
}
}

abstract class Movie
{
protected string name;

public Movie(string n)
{
this.name = n;
}

public abstract void ShowTheMovie();
}

class ConcreteMovie : Movie
{
public ConcreteMovie(string n)
: base(n)
{
}

public override void ShowTheMovie()
{
Console.WriteLine(name);
}
}

class Client
{
public static void Main()
{
MovieTheater mth = new MovieTheater();
mth.GiveMeAMovie("Hell Boy");
mth.GiveMeAMovie("Wanted");
mth.GiveMeAMovie("Wall-E");
mth.GiveMeAMovie("Hell Boy");
mth.GiveMeAMovie("Hell Boy");
mth.GiveMeAMovie("Wanted");
mth.GiveMeAMovie("Wall-E");
mth.GiveMeAMovie("Hell Boy");
mth.HowManyMoviesHaveWeOrdered();
Console.Read();
}
}
}

设计模式---中介者模式(打出来是“终结者.......”)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
abstract class FBIAgent
{
public abstract void SendMessage(string m);

public void ReadMessage(string m)
{
Console.WriteLine(m);
}
}

class AgentInRussia : FBIAgent
{
private FBICenter center;

public AgentInRussia(FBICenter f)
{
center = f;
}

public override void SendMessage(string m)
{
center.Send(this, m);
}
}

class AgentInEngland : FBIAgent
{
private FBICenter center;

public override void SendMessage(string m)
{
center.Send(this, m);
}

public AgentInEngland(FBICenter f)
{
center = f;
}
}

abstract class FBICenter
{
public abstract void Send(FBIAgent f, string m);
}

class RealFBICenter : FBICenter
{
private FBIAgent agentinrussia;
private FBIAgent agentinengland;

public FBIAgent russia
{
set { agentinrussia = value; }
}

public FBIAgent england
{
set { agentinengland = value; }
}

public override void Send(FBIAgent f, string m)
{
if (f == agentinengland)
{
agentinrussia.ReadMessage(m);
}
else
{
agentinengland.ReadMessage(m);
}
}
}

class Client
{
public static void Main()
{
RealFBICenter fc = new RealFBICenter();
AgentInEngland eng = new AgentInEngland(fc);
AgentInRussia rus = new AgentInRussia(fc);
fc.england = eng;
fc.russia = rus;
eng.SendMessage("我在英国搞情报,hia~hia~");
rus.SendMessage("我在俄罗斯搞情报,xia~ixa~");
Console.Read();
}
}
}

设计模式—命令模式

d1fc651edbf349ec1ad5765a.jpg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
abstract class Order
{
protected General general;

public Order(General g)
{
this.general = g;
}

public abstract void Excute();
}

class Fire : Order
{
public Fire(General g)
: base(g)
{
}

public override void Excute()
{
general.ExcuteFire();
}
}

class Retreat : Order
{
public Retreat(General g)
: base(g)
{
}

public override void Excute()
{
general.ExcuteRetreat();
}
}

class Soldier
{
private String name;

public Soldier(string n)
{
this.name = n;
}

public void ExcuteFire()
{
Console.WriteLine(name + "开火");
}

public void ExcuteRetreat()
{
Console.WriteLine(name + "撤退");
}
}

class General
{
private Soldier soldier;

public void ExcuteFire()
{
soldier.ExcuteFire();
}

public void SetSoldier(Soldier s)
{
this.soldier = s;
}

public void ExcuteRetreat()
{
soldier.ExcuteRetreat();
}
}

class Client
{
public static void Main()
{
General g = new General();
Soldier s = new Soldier("张三");
g.SetSoldier(s);
Order o = new Fire(g);
o.Excute();
o = new Retreat(g);
o.Excute();
g.SetSoldier(new Soldier("李四"));
o.Excute();
Console.Read();
}
}
}