using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _11._1._3
{
class Program
{
delegate int MyDelegate(int p, int q);
static void Main( )
{
MyDelegate arithMethod = null;
string myOperate = null;
Console.WriteLine("选择对3和4执行的算术操作:");
Console.WriteLine("按“+”键执行加法操作;");
Console.WriteLine("按“-”键执行减法操作;");
Console.WriteLine("按“m”键求两个数的最大值。");
char choice = (char) Console.Read( );
switch(choice)
{
case '+':
arithMethod = new MyDelegate(Add);
myOperate = "加法";
break;
case '-':
arithMethod = new MyDelegate(Subtract);
myOperate = "减法";
break;
case 'm':
arithMethod = new MyDelegate(Max);
myOperate = "求最大值";
break;
default :
arithMethod = new MyDelegate(Add);
myOperate = "加法";
break;
}
int r = arithMethod(3, 4);
Console.WriteLine("\n对3和4执行{0}操作后的结果是:{1} ",myOperate, r);
Console.ReadLine();
Console.ReadLine();
}
static int Add(int a, int b)
{
return a + b;
}
static int Subtract(int a, int b)
{
return a - b;
}
static int Max(int a, int b)
{
if(a > b)
{
return a;
}
else
{
return b;
}
}
}
}