Friends familiar with javascript may not be unfamiliar with Eval () functions. We can use it to execute dynamic code. I even wrote a webpage dedicated to calculating arithmetic expressions, the computing power is better than that of google and baidu calculators, at least the accuracy is higher, but if... syntaxHighlighter. all ();
Friends familiar with javascript may not be unfamiliar with Eval () functions. We can use it to execute dynamic code. I even wrote a webpage dedicated to calculating arithmetic expressions, compared with google and baidu
The calculator should be better, at least with a high accuracy. However, if there are more than four arithmetic operations, the expression format will be very complicated. For example, the example given by Baidu:
Log (5 + 5) ^ 2)-3 + pi needs to be written as Math. log (Math. pow (5 + 5, 2) * Math. LOG10E-3 + Math. PI can use Eval for computation. I have not thought of an ideal solution for this. Well, this is not the subject of this Article. Let's let it go.
The following code has been used in the blog, at least in the code form:
[Csharp]
// Csc.exe noname1.cs/r: C: \ WINDOWS \ Microsoft. NET \ Framework \ v1.1.4322 \ Microsoft. JScript. dll
// Note: the Microsoft. JScript and Microsoft. Vsa namespaces must be added.
Public class Class1
{
Static void Main (string [] args)
{
System. Console. WriteLine ("Hello World ");
String Expression = "var result: int = 0; result = 1? \ "Success \": \ "Failure \"";
Microsoft. JScript. Vsa. VsaEngine ve = Microsoft. JScript. Vsa. VsaEngine. CreateEngine ();
Console. WriteLine (Microsoft. JScript. Eval. JScriptEvaluate (Expression, ve ));
}
}
However, the compilation environment now provides the following warning: 'Microsoft. JScript. vsa. vsaEngine 'is obsolete: 'Use of this type is not recommended because it is being deprecated in Visual Studio 2005;
There will be no replacement for this feature. Please see the ICodeCompiler documentation for additional help. 'Of course, the code can be compiled and executed normally.
Next I will provide another method to directly use the javascript Eval function. With the help of the com component, the Reference Path is % SystemRoot % \ system32 \ msscript. ocx, I paste the complete code directly.
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Diagnostics;
Namespace ScriptProgramming
{
Class Program
{
Static void Main (string [] args)
{
String strExpression = "1 + 2*3 ";
String strResult = Eval (strExpression );
Console. WriteLine (strExpression + "=" + strResult );
Console. WriteLine ("Press any key to continue .");
Console. ReadKey ();
}
///
/// Reference the com component Microsoft Script Control
/// % SystemRoot % \ system32 \ msscript. ocx
/// This function is used to dynamically execute code
///
///
///
Public static string Eval (string Expression)
{
String strResult = null;
Try
{
MSScriptControl. ScriptControlClass jscript = new MSScriptControl. ScriptControlClass ();
Jscript. Language = "JScript ";
StrResult = jscript. Eval (Expression). ToString ();
}
Catch (Exception ex)
{
Debug. Fail (ex. Message );
}
Return strResult;
}
}
}
From if one day...