1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace sum
 7 {
 8     class Class1
 9     {
10         public interface Calculator        //声明计算接口
11         {
12             double Cal(double x, double y);
13         }
14         private double x;                //定义x变量;
15 
16         public double X                  //封装字段
17         {
18             get { return x; }
19             set { x = value; }
20 
21         }
22         private double y;               //定义y变量
23 
24 
25         public double Y                  //封装字段
26         {
27             get { return y; }
28             set { y = value; }
29 
30         }
31         public class Add : Calculator            //接口法运算
32         {
33             public double Cal(double x, double y)
34             {
35                 double result = 0;
36                 result = x + y;
37                 return result;
38             }
39         }
40         public class Sub : Calculator 
41         {
42             public double Cal(double x, double y)
43             {
44                 double result = 0;
45                 result = x - y;
46                 return result;
47             }
48         }
49         public class Mul : Calculator 
50         {
51             public double Cal(double x, double y)
52             {
53                 double result = 0;
54                 result = x * y;
55                 return result;
56             }
57         }
58         public class Div : Calculator  
59         {
60             public double Cal(double x, double y)
61             {
62                 double result = 0;
63                 result = x / y;
64                 return result;
65             }
66         }
67         public class Opear                  //定义运算符  
68         {
69             private Calculator calculate;
70             public Opear(Calculator calculate)
71             {
72                 this.calculate = calculate;
73             }
74             public double Cal(double x, double y, String op)  //返回运算结果
75             {
76                 return this.calculate.Cal(x, y);
77             }
78         }
79     }
80 }

策略模式引用:

 1  private void textBox8_KeyDown(object sender, KeyEventArgs e)
 2         {
 3             sum.Class1.Opear opear = null;
 4             double x = Convert.ToDouble(textBox5.Text); //为相关的变量赋值
 5             double y = Convert.ToDouble(textBox7.Text);
 6             string op = textBox6.Text;
 7             switch (op)
 8             {
 9                 case "+":
10                      opear = new  sum.Class1.Opear(new sum.Class1.Add()); //策略模式的引用
11                     break;
12                 case "-":
13                     opear = new sum.Class1.Opear(new sum.Class1.Sub());
14 
15                     break;
16                 case "*":
17                     opear = new sum.Class1.Opear(new sum.Class1.Mul());
18 
19                     break;
20                 case "/":
21                     opear = new sum.Class1.Opear(new sum.Class1.Div());
22 
23                     break;
24                 default:
25                     break;
26             }
27             
28             if (e.KeyCode == Keys.Enter)
29             {
30                 string answer = opear.Cal(x, y, op).ToString();
31                 if (textBox8.Text ==answer.ToString())
32                 {
33                     MessageBox.Show("回答正确");
34                     Count++;
35                     Right++;
36                 }
37                 else
38                 {
39                     MessageBox.Show("回答错误");
40                     Count++;
41                 }

 

运行结果:

保存出好的题

 

 

 

 

 

 

 

 

posted on 2015-11-29 15:37  紫色蔷薇ˇ  阅读(257)  评论(0编辑  收藏  举报