zoukankan      html  css  js  c++  java
  • ASP.net四则运算《《《策略模式

    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     }

    运行结果:

    运行界面:

  • 相关阅读:
    使用 Fetch
    实现一个联系客服对话框的前端部分
    javascript之Object.defineProperty的奥妙
    vue之nextTick全面解析
    创建元素和删除元素
    vue.js应用开发笔记
    待字闺中之最多连续数的子集
    HDU-1212-Big Number
    虚方法【仅仅有虚方法或者抽象方法才干被子类方法重写】
    利用localStorage实现对ueditor编辑内容定时保存为草稿
  • 原文地址:https://www.cnblogs.com/thinking-star/p/5005236.html
Copyright © 2011-2022 走看看