110,533
社区成员
发帖
与我相关
我的任务
分享
Console.WriteLine(Get_Add(double.Parse("1.23"), double.Parse("2.32")));
Console.WriteLine(Get_Add(int.Parse("1"), int.Parse("2")));
Console.WriteLine(Get_Add("1","2"));
public dynamic Get_Add(dynamic d , dynamic a)
{
return a + d;
}
像这种可以执行double,int,float,string的加法
#Region "字符串表达式计算"
Const Num As String = "(\-?\d+\.?\d*)"
Const Func1 As String = "(exp|log|log10|abs|sqr|sqrt|sin|cos|tan|asin|acos|atan|atan|round|tanh|truncate|floor|ceiling)"
Const Func2 As String = "(atan2)"
Const FuncN As String = "(min|max)"
Const Constants As String = "(e|pi)"
Private rePower As New Regex(String.Concat(Num, "\s*(\^)\s*", Num))
Private reAddSub As New Regex(String.Concat(Num, "\s*([-+])\s*", Num))
Private reMulDiv As New Regex(String.Concat(Num, "\s*([*/])\s*", Num))
Private reFunc1 As New Regex(String.Concat(Func1, "\(\s*", Num, "\s*\)"), RegexOptions.IgnoreCase)
Private reFunc2 As New Regex(String.Concat(Func2, "\(\s*", Num, "\s*,\s*", Num, "\s*\)"), RegexOptions.IgnoreCase)
Private reFuncN As New Regex(String.Concat(FuncN, "\((\s*", Num, "\s*,)+\s*", Num, "\s*\)"), RegexOptions.IgnoreCase)
Private reSign1 As New Regex("([-+/*^])\s*\+")
Private reSign2 As New Regex("\-\s*\-")
Private rePar As New Regex("(?<![A-Za-z0-9])\(\s*([-+]?\d+.?\d*)\s*\)")
Private reNum As New Regex("^\s*[-+]?\d+\.?\d*\s*$")
Private reConst As New Regex(String.Concat("\s*", Constants, "\s*"), RegexOptions.IgnoreCase)
''' <summary>
''' 字符串表达式计算.如:pi*(100+23)*1.2/35
''' 注:这个函数使用的是正则,所以速度是呵呵的,只能在前台界面.
''' (exp|log|log10|abs|sqr|sqrt|sin|cos|tan|asin|acos|atan|atan|round|tanh|truncate|floor|ceiling)
''' (atan2)
''' (min|max)
''' (e|pi)
''' 支持 括号,四则运算,EXP,LOG,LOG10,SQR,SQRT,三角函数.MIN,MAX,e,pi
''' ** 注:还没有测试线程安全,所以,先不要用在服务器上。
''' </summary>
''' <param name="expr"></param>
''' <returns></returns>
''' <remarks></remarks>
<Extension()> Public Function Evaluate(ByVal expr As String) As Double
If String.IsNullOrWhiteSpace(expr) Then Return 0
expr = reConst.Replace(expr, AddressOf DoConstants)
Do Until reNum.IsMatch(expr)
Dim saveExpr As String = expr
Do While rePower.IsMatch(expr)
expr = rePower.Replace(expr, AddressOf DoPower)
Loop
Do While reMulDiv.IsMatch(expr)
expr = reMulDiv.Replace(expr, AddressOf DoMulDiv)
Loop
Do While reFuncN.IsMatch(expr)
expr = reFuncN.Replace(expr, AddressOf DoFuncN)
Loop
Do While reFunc2.IsMatch(expr)
expr = reFunc2.Replace(expr, AddressOf DoFunc2)
Loop
Do While reFunc1.IsMatch(expr)
expr = reFunc1.Replace(expr, AddressOf DoFunc1)
Loop
expr = reSign1.Replace(expr, "$1")
expr = reSign2.Replace(expr, "+")
Do While reAddSub.IsMatch(expr)
expr = reAddSub.Replace(expr, AddressOf DoAddSub)
Loop
expr = rePar.Replace(expr, "$1")
'//表达式错误
If expr = saveExpr Then
'//Throw New SyntaxErrorException()
Return 0
End If
Loop
Return Val(expr)
End Function
Friend Function DoConstants(ByVal m As Match) As String
Dim V As String = String.Empty
Select Case m.Groups(1).Value.ToUpper
Case "PI"
V = Math.PI.ToString
Case "E"
V = Math.E.ToString
End Select
Return V
End Function
Friend Function DoPower(ByVal m As Match) As String
Dim n1 As Double = Val(m.Groups(1).Value)
Dim n2 As Double = Val(m.Groups(3).Value)
Return (n1 ^ n2).ToString
End Function
Friend Function DoMulDiv(ByVal m As Match) As String
Dim n1 As Double = Val(m.Groups(1).Value)
Dim n2 As Double = Val(m.Groups(3).Value)
Dim V As String = String.Empty
Select Case m.Groups(2).Value
Case "/"
V = (n1 / n2).ToString
Case "*"
V = (n1 * n2).ToString
End Select
Return V
End Function
Friend Function DoAddSub(ByVal m As Match) As String
Dim n1 As Double = Val(m.Groups(1).Value)
Dim n2 As Double = Val(m.Groups(3).Value)
Dim V As String = String.Empty
Select Case m.Groups(2).Value
Case "+"
V = (n1 + n2).ToString
Case "-"
V = (n1 - n2).ToString
End Select
Return V
End Function
Friend Function DoFunc1(ByVal m As Match) As String
Dim V As String = String.Empty
Dim n1 As Double = Val(m.Groups(2).Value)
Select Case m.Groups(1).Value.ToUpper
Case "EXP"
V = Math.Exp(n1).ToString
Case "LOG"
V = Math.Log(n1).ToString
Case "LOG10"
V = Math.Log10(n1).ToString
Case "ABS"
V = Math.Abs(n1).ToString
Case "SQR", "SQRT"
V = Math.Sqrt(n1).ToString
Case "SIN"
V = Math.Sin(n1).ToString
Case "COS"
V = Math.Cos(n1).ToString
Case "TAN"
V = Math.Tan(n1).ToString
Case "ASIN"
V = Math.Asin(n1).ToString
Case "ACOS"
V = Math.Acos(n1).ToString
Case "ATAN"
V = Math.Atan(n1).ToString
Case "ROUND"
V = Math.Round(n1).ToString
Case "TANH"
V = Math.Tanh(n1).ToString
Case "Truncate".ToUpper
V = Math.Truncate(n1).ToString
Case "Floor".ToUpper
V = Math.Floor(n1).ToString
Case "Ceiling".ToUpper
V = Math.Ceiling(n1).ToString
End Select
Return V
End Function
Friend Function DoFunc2(ByVal m As Match) As String
Dim V As String = String.Empty
Dim n1 As Double = Val(m.Groups(2).Value)
Dim n2 As Double = Val(m.Groups(3).Value)
Select Case m.Groups(1).Value.ToUpper
Case "ATAN2"
V = Math.Atan2(n1, n2).ToString
End Select
Return V
End Function
Friend Function DoFuncN(ByVal m As Match) As String
Dim V As String = String.Empty
Dim args As New ArrayList()
Dim i As Integer = 2
Do While m.Groups(i).Value <> String.Empty
args.Add(StrToDec(m.Groups(i).Value.Replace(","c, " "c)))
i += 1
Loop
Select Case m.Groups(1).Value.ToUpper
Case "MIN"
args.Sort()
V = args(0).ToString
Case "MAX"
args.Sort()
V = args(args.Count - 1).ToString
End Select
Return V
End Function
#End Region
public string Add(int a,int b)
{
return (a+b).ToString();
}
public string Add(int a,float b)
{
return ((float)a+b).ToString();
}
........
double Add(double op1, double op2) { return op1 + op2; }
int Add(int op1, int op2) { return op1 + op2; }
public T Add<T>(T t1, T t2)
{
if(typeof(T).IsPrimitive
&& typeof(T) != typeof(bool)
&& typeof(T) != typeof(char))
{
dynamic d1 = t1;
dynamic d2 = t2;
return (d1 + d2);
}
else
{
return default;
}
}
public string Add<T>(T a, T b) where T:struct
{
if (typeof(T) == typeof(int))
{
return (int.Parse(a + "") + int.Parse(b + "")).ToString();
}
else if (typeof(T) == typeof(double))
{
return (double.Parse(a + "") + double.Parse(b + "")).ToString();
}
//float,long.....
return default(T).ToString();
}
template <class 形参>
形参 相加(形参 a, 形参b){ return a + b; }
template <class 形参>
void 相加(形参 A, 形参 B){return A+B;}