calculate.cs

来自「《ajax编程技术与实例》的所有的案例」· CS 代码 · 共 85 行

CS
85
字号
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;


/// <summary>
/// Summary description for Calculate
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Calculate : System.Web.Services.WebService {

    private double left;
    private double right;

    public Calculate () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    /* 将请求中的参数转换成double */
    private bool convertToDouble(String one, String two)
    {
        string input1 = Server.HtmlEncode(one);
        string input2 = Server.HtmlDecode(two);
        if (!String.IsNullOrEmpty(input1) && !String.IsNullOrEmpty(input2))
        {
            left = Double.Parse(input1);
            right = Double.Parse(input2);

        }
        else
        {
            return false;
        }

        return true;

    }

    /* 加法 */
    [WebMethod]
    public string add(String one, String two) {

        convertToDouble(one, two);

        return this.left + this.right + "";
    }

    /* 乘法 */
    [WebMethod]
    public string multiply(String one, String two)
    {
        convertToDouble(one, two);

        return this.left * this.right + "";
    }

    /* 减法 */
    [WebMethod]
    public string minus(String one, String two)
    {
        convertToDouble(one, two);

        return this.left - this.right + "";
    }

    /* 除法 */
    [WebMethod]
    public string divide(String one, String two)
    {
        convertToDouble(one, two);

        if (this.right == 0)
            return "undefine operation";

        return this.left / this.right + "";
    }
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?