有沒有辦法給操作員代碼轉換沒有做對"+", "-", "*", "/"
檢查,然後傳遞價值?
using System;
namespace ConsoleApplication1
{
class Program
{
private static void Main(string[] args)
{
string operator = "+";
int inputOne = 2;
int inputTwo = 5;
Console.WriteLine(Result(inputOne, inputTwo, operator));
Console.ReadLine();
}
public static string Result(int one, int two, string computeOperator)
{
if (computeOperator == "+")
{
return (one + two).ToString();
}
if (computeOperator == "-")
{
return (one - two).ToString();
}
//and so on...
return "0";
}
}
}
找到一個很好的數學表達式解析器。 – Oded
你可以傳遞一個委託,比如'Func'。 –
Servy
請參閱:http://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net – SQLMason