1 class Program 2 { 3 delegate double ProcessDelegate(double param1, double param2); 4 5 static double Multiply(double param1, double param2) 6 { 7 return param1*param2; 8 } 9 10 static double Divide(double param1, double param2) 11 { 12 return param1/param2; 13 } 14 15 static void Main(string[] args) 16 { 17 ProcessDelegate process; 18 Console.WriteLine("Enter 2 number separated with a comma:"); 19 string input = Console.ReadLine(); 20 int commaPos = input.IndexOf(‘,‘); 21 double param1 = Convert.ToDouble(input.Substring(0,commaPos)); 22 double param2 = Convert.ToDouble(input.Substring(commaPas+1,input.Length - commaPos-1)); 23 Console.WriteLine("Enter M or D:"); 24 input = Console>readLine(); 25 if(input == "M") 26 process = new ProcessDelegate(Multiply); 27 else 28 process = new ProcessDelegate(Divide); 29 Console.WriteLine("Result:{0}",process(param1 ,param2 )); 30 } 31 }
时间: 2024-10-09 09:32:35