Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 2900 = 1 * T * ((52 + 6) / 12)
- T = 2900/(52 + 6) * 12 / 1
- T = a/(b + c) * d / e
- double T(double a, double b, double b, double c, double d, double e) {
- return a/(b + c) * d / e;
- }
- double T = T(2900, 52, 6, 12, 1)
- using Microsoft.CSharp;
- using System;
- using System.CodeDom.Compiler;
- using System.Linq;
- using System.Reflection;
- namespace EquationSolver
- {
- public class EquationSolver
- {
- MethodInfo meth;
- double ExpectedResult;
- public EquationSolver(string equation)
- {
- var codeProvider = new CSharpCodeProvider();
- var splitted = equation.Split(new[] {'='});
- ExpectedResult = double.Parse(splitted[0]);
- var SourceString = "using System; namespace EquationSolver { public static class Eq { public static double Solve(double T) { return "+
- splitted[1] + ";}}}";
- System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
- parameters.GenerateInMemory = true;
- CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, SourceString);
- var cls = results.CompiledAssembly.GetType("EquationSolver.Eq");
- meth = cls.GetMethod("Solve", BindingFlags.Static | BindingFlags.Public);
- }
- public double Evaluate(double T)
- {
- return (double)meth.Invoke(null, new[] { (object)T });
- }
- public double SearchT(double start, double end, double tolerance)
- {
- do
- {
- var results = Enumerable.Range(0, 4).Select(x => start + (end - start) / 3 * x).Select(x => new Tuple<double, double>(
- x, Evaluate(x))).ToArray();
- foreach (var result in results)
- {
- if (Math.Abs(result.Item2 - ExpectedResult) <= tolerance)
- {
- return result.Item1;
- }
- }
- if (Math.Abs(results[2].Item2 - ExpectedResult) > Math.Abs(results[1].Item2 - ExpectedResult))
- {
- end -= (end - start) / 3;
- }
- else
- {
- start += (end - start) / 3;
- }
- } while (true);
- }
- }
- }
- var eq = new EquationSolver( "2900 = 1 * T * ((52 + 6.0) / 12)");
- var r = eq.SearchT(int.MinValue,int.MaxValue,0.001);
Add Comment
Please, Sign In to add comment