zoukankan      html  css  js  c++  java
  • 匿名方法示例之计算器(委托作为参数和返回值)

     1using System;
     2using System.Collections.Generic;
     3using System.Linq;
     4using System.Text;
     5
     6namespace Calculator
     7{
     8    public delegate double MathFunction(double a,double b);
     9    //主程序代码
    10    class CalTest
    11    {
    12        static void Main()
    13        {
    14            Console .Write ("请输入数值a :");
    15            Double a=double .Parse (Console .ReadLine ());
    16            Console .Write ("请输入数值b :");
    17            Double b=double .Parse (Console .ReadLine ());
    18            Console.WriteLine("请在下面的列表中选择函数:");
    19            Console .WriteLine ("加 (1) \n减 (2) \n 乘 (3) \n除 (4) \n求余 (5) \n乘方 (6) \n开方 (7) \n对数 (8) ");
    20            int iMethod=int.Parse (Console .ReadLine ());
    21            MathFunction myFunc=PrimaryFunc.AFunc (iMethod );
    22            Console .WriteLine ("计算结果为:{0}",myFunc (a ,b ));
    23            Console .ReadLine ();
    24        }
             
    25    }

    26    public class PrimaryFunc
    27    {
    28        public static MathFunction AFunc(int i)
    29        {
    30            switch (i)
    31            {
    32                case 1:
    33                    return delegate(double a, double b) return a + b; };
    34                case 2:
    35                    return delegate(double a, double b) return a - b; };
    36                case 3:
    37                    return delegate(double a, double b) return a * b; };
    38                case 4:
    39                    return delegate(double a, double b) return a / b; };
    40                case 5:
    41                    return delegate(double a, double b) return a % b; };
    42                case 6:
    43                    return delegate(double a, double b) return Math .Pow (a ,b ); };
    44                case 7:
    45                    return delegate(double a, double b) return Math.Pow(a,1/b); };
    46                case 8:
    47                    return delegate(double a, double b) return Math.Log (a, b); };
    48                default:
    49                    return delegate(double a, double b) return 0; };
    50            }

    51        }

    52    }

    53
    54}

    55
    56
    运行结果如下:
  • 相关阅读:
    android studio解决微信登录,百度地图等调试问题
    Android studio初入的各种坑
    react native环境配置及各种坑
    单例模式序列化后反序列化单例失效的问题
    react native 初识react native
    Fragment 点击事件的穿透和重叠bug
    转载:android——eclipse如何去除Ctrl+shift+R组合键查找到的.class文件
    分布式队列Celery
    vue-生命周期
    vue-实现倒计时功能
  • 原文地址:https://www.cnblogs.com/markhe/p/1554710.html
Copyright © 2011-2022 走看看