码迷,mamicode.com
首页 > Web开发 > 详细

计算器软件的代码实现 (策略模式+asp.net)

时间:2015-11-29 16:25:28      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

一 策略模式代码的编写

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///Class1 的摘要说明
/// </summary>

   public interface Calculator //声明一个计算的接口
    {
        double Cal(double a,double  b);
    }
   public class Add : Calculator   //接口实现加法运算
   {
     public double Cal(double a, double b)
     {
         double result = 0;
         result = a + b;
         return result;
     }
   }
   public class Sub : Calculator //接口实现减法运算
 {
     public double Cal(double a, double b)
     {
         double result = 0;
         result = a - b;
         return result;
     }
 }
   public class Mul : Calculator  //接口实现乘法运算
 {
     public double Cal(double a, double b)
     {
         double result = 0;
         result = a * b;
        return result;
    }
 }
   public class Div : Calculator  //接口实现除法运算
 {
    public  double Cal(double a, double b)
    {
        double result = 0;
        result = a / b;
        return result;
     }
 }
 public class  Environment        //定义那个需要动态改变算法的对象   
 {
     private Calculator calculate;
     public Environment(Calculator calculate)
     {
        this.calculate = calculate;    
     }
     public double Cal(double a, double b, String m) //返回运算结果
     {
         return this.calculate.Cal(a, b);
    }
    }

二 asp.net后台代码的书写,并且引用策略模式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class yunsuan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void bianji_Click(object sender, EventArgs e)
    {
            Environment environment = null;
            double a = Convert.ToDouble(left.Text); //为相关的变量赋值
            double b = Convert.ToDouble(right.Text);
            string m = fuhao.Text;
            switch (m)
            {
                case "+":
                    environment = new Environment(new Add()); //策略模式的引用
                    break;
                case "-":
                    environment = new Environment(new Sub());

                    break;
                case "*":
                    environment = new Environment(new Mul());

                    break;
                case "/":
                    environment = new Environment(new Div());

                    break;
                default:
                    break;
            }

                string answer = environment.Cal(a, b, m).ToString();
               
                if (TextBox4.Text == answer.ToString())
                {
                    Response.Write("回答正确");  //判断用户回答是否正确,并且给出相关的提示
                   
                }
                else
                {
                    Response.Write("回答错误");
                    
                }
                ListBox1.Items.Add(left.Text + fuhao.Text + right.Text + Label1.Text + answer + "\n"); //式子保存到listbox控件中,并且给出正确答案
                left.Text = "";
                fuhao.Text = "";
                right.Text = "";
                TextBox4.Text="";
            }

   
}
    

三  界面的运行效果

技术分享技术分享技术分享

计算器软件的代码实现 (策略模式+asp.net)

标签:

原文地址:http://www.cnblogs.com/wyh19941210/p/5004824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!