首页 > Web开发 > 详细

ASP.net四则运算《《《策略模式

时间:2015-11-29 21:00:47      阅读:163      评论:0      收藏:0      [点我收藏+]

Calculator.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5   /// <summary>
 6   ///Calculator 的摘要说明
 7   /// </summary>
 8 public abstract class Calculator
 9 {
10     public abstract double Cal(double x, double y);
11 }
12 
13     public class Add : Calculator                //接口法运算
14     {
15         public override double Cal(double x, double y)     //重写
16         {
17             double result = 0;
18             result = x + y;
19             return result;
20         }
21     }
22     public class Sub : Calculator
23     {
24         public override double Cal(double x, double y)
25         {
26             double result = 0;
27             result = x - y;
28             return result;
29         }
30     }
31     public class Mul : Calculator
32     {
33         public override double Cal(double x, double y)
34         {
35             double result = 0;
36             result = x * y;
37             return result;
38         }
39     }
40     public class Div : Calculator
41     {
42         public override double Cal(double x, double y)
43         {
44             double result = 0;
45             result = x / y;
46             return result;
47         }
48     }
49     public class Opear           //定义运算符
50     {
51         private Calculator calculate = null;          //实例化一个基类的引用对象
52         public Opear(Calculator calculator)         //calculator为派生类的一个对象
53         {
54             this.calculate = calculator;                  //把派生类的对象赋给基类的引用对象
55         }
56         public double Cal(double x, double y, String op)
57         {
58             return this.calculate.Cal(x, y);         //返回计算结果
59         }
60     }

 

ASP.net后台代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 public partial class Default2 : System.Web.UI.Page
 8 {
 9     protected void Page_Load(object sender, EventArgs e)
10     {
11 
12     }
13     protected void Button1_Click(object sender, EventArgs e)
14     { 
15          string op = DropDownList1.SelectedItem.ToString();
16          double x = Convert.ToDouble(TextBox1.Text); 
17          double y = Convert.ToDouble(TextBox2.Text);
18         
19          Opear opear =null;
20          if (DropDownList1.SelectedIndex == 0)
21          {
22              opear = new Opear(new Add());    
23          }
24          else if (DropDownList1.SelectedIndex == 1)
25          {
26            opear  = new Opear(new Sub());    
27          }
28          else if (DropDownList1.SelectedIndex == 2)
29          {
30              opear = new Opear(new Mul());
31          }
32          else if (DropDownList1.SelectedIndex == 3)
33          {
34              opear = new Opear(new Div());
35          }
36          string answer = opear.Cal(x, y, op).ToString(); 
37          string result = TextBox1.Text + DropDownList1.SelectedItem.ToString() + TextBox2.Text;
38          if (TextBox4.Text == answer)                                 
39          {
40              Response.Write("<script>alert(‘回答正确!‘)</script>");          
41              ListBox1.Items.Add(result + "=" + TextBox4.Text.Trim());
42          }
43  
44          else                                                       
45          {
46              Response.Write("<script>alert(‘答题错误!‘)</script>");          
47              ListBox1.Items.Add(result + "=" + TextBox4.Text.Trim());  
48          }
49          TextBox1.Text = "";             
50          TextBox2.Text = "";
51          TextBox4.Text = "";  
52      }
53     }

 

运行结果:

运行界面:

技术分享

技术分享技术分享

 

ASP.net四则运算《《《策略模式

原文:http://www.cnblogs.com/thinking-star/p/5005236.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!