Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 9th, 2013  |  syntax: None  |  size: 2.30 KB  |  views: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. 2900 = 1 * T * ((52 + 6) / 12)
  2.        
  3. T = 2900/(52 + 6) * 12 / 1
  4.        
  5. T = a/(b + c) * d / e
  6.        
  7. double T(double a, double b, double b, double c, double d, double e) {
  8.  return  a/(b + c) * d / e;
  9. }
  10.        
  11. double T = T(2900, 52, 6, 12, 1)
  12.        
  13. using Microsoft.CSharp;
  14. using System;
  15. using System.CodeDom.Compiler;
  16. using System.Linq;
  17. using System.Reflection;
  18.  
  19. namespace EquationSolver
  20. {
  21.     public class EquationSolver
  22.     {
  23.         MethodInfo meth;
  24.         double ExpectedResult;
  25.         public EquationSolver(string equation)
  26.         {
  27.         var codeProvider = new CSharpCodeProvider();            
  28.         var splitted = equation.Split(new[] {'='});
  29.  
  30.         ExpectedResult = double.Parse(splitted[0]);
  31.  
  32.         var SourceString = "using System; namespace EquationSolver { public static class Eq { public static double Solve(double T) { return "+
  33.             splitted[1] + ";}}}";
  34.  
  35.         System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
  36.         parameters.GenerateInMemory = true;
  37.  
  38.  
  39.         CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, SourceString);
  40.  
  41.         var cls = results.CompiledAssembly.GetType("EquationSolver.Eq");
  42.         meth = cls.GetMethod("Solve", BindingFlags.Static | BindingFlags.Public);
  43.  
  44.     }
  45.  
  46.     public double Evaluate(double T)
  47.     {            
  48.         return (double)meth.Invoke(null, new[] { (object)T });
  49.     }
  50.  
  51.     public double SearchT(double start, double end, double tolerance)
  52.     {            
  53.         do
  54.         {
  55.             var results = Enumerable.Range(0, 4).Select(x => start + (end - start) / 3 * x).Select(x => new Tuple<double, double>(
  56.                 x, Evaluate(x))).ToArray();
  57.             foreach (var result in results)
  58.             {
  59.                 if (Math.Abs(result.Item2 - ExpectedResult) <= tolerance)
  60.                 {
  61.                     return result.Item1;
  62.                 }
  63.             }
  64.             if (Math.Abs(results[2].Item2 - ExpectedResult) > Math.Abs(results[1].Item2 - ExpectedResult))
  65.             {
  66.                 end -= (end - start) / 3;                    
  67.             }
  68.             else
  69.             {
  70.                 start += (end - start) / 3;
  71.             }
  72.         } while (true);
  73.     }
  74.   }
  75.  
  76.  
  77. }
  78.        
  79. var eq = new EquationSolver( "2900 = 1 * T * ((52 + 6.0) / 12)");
  80. var r = eq.SearchT(int.MinValue,int.MaxValue,0.001);