標籤:使用 os io cti div ar line new
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
weituo
{
class
Program
{
static
void
Main(
string
[] args)
{
// 聲明委託變數
ProcessDelegate process;
Console.WriteLine(
"請輸入用逗號分隔的兩個數字:"
);
string
input = Console.ReadLine();
int
commaPos = input.IndexOf(
‘,‘
);
double
param1 = Convert.ToDouble(input.Substring(0, commaPos));
double
param2 = Convert.ToDouble(input.Substring(commaPos + 1,input.Length - commaPos -1));
Console.WriteLine(
"輸入M乘法D除法"
);
input =Console.ReadLine();
// 初始化委託變數
if
(input ==
"M"
)
process =
new
ProcessDelegate(Multiply);
//注釋:此處也可以寫process = Multiply
else
process =
new
ProcessDelegate(Divide);
// 使用委託調用函數
double
result = process(param1,param2);
Console.WriteLine(
"結果:{0}"
,result);
Console.ReadKey();
}
// 聲明委託
delegate
double
ProcessDelegate(
double
param1,
double
param2);
static
double
Multiply(
double
param1,
double
param2)
{
return
param1 * param2;
}
static
double
Divide(
double
param1,
double
param2)
{
return
param1 / param2;
}
}
}