Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Function Plotter - Function.cs

Function.cs

Caricato da: Crybot
Scarica il programma completo

  1. using System;
  2. using System.Text;
  3. using System.CodeDom.Compiler;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Microsoft.VisualBasic;
  7.  
  8. namespace DrawGraph
  9. {
  10.     internal class Function
  11.     {
  12.         private MethodInfo _evaluateFunction;
  13.         private string _expression;
  14.  
  15.         public Function(string f)
  16.         {
  17.             Expression = f;
  18.         }
  19.  
  20.         public Function()
  21.         {
  22.         }
  23.  
  24.         public string Expression
  25.         {
  26.             get
  27.             {
  28.                 return _expression;
  29.             }
  30.  
  31.             set
  32.             {
  33.                 _expression = ConvertExpression(value);
  34.                 CreateEvaluator();
  35.             }
  36.         }
  37.  
  38.         public float Apply(float x)
  39.         {
  40.             return (float) _evaluateFunction.Invoke(null, new object[] {x});
  41.         }
  42.  
  43.         private string ConvertExpression(string expr)
  44.         {
  45.             StringBuilder converted = new StringBuilder(expr.Replace(" ", string.Empty));
  46.  
  47.             for (int i = 0; i < converted.Length; i++)
  48.             {
  49.                 if (char.IsNumber(converted[i]) && i < converted.Length - 1)
  50.                 {
  51.                     if (!IsNumberOrOperator(converted[i + 1]))
  52.                     {
  53.                         converted.Insert(i + 1, "*");
  54.                         i++;
  55.                     }
  56.                 }
  57.  
  58.                 else if (converted[i] == 'x' && i < converted.Length - 1)
  59.                 {
  60.                     if (!IsOperator(converted[i + 1]))
  61.                     {
  62.                         converted.Insert(i + 1, "*");
  63.                         i++;
  64.                     }
  65.                 }
  66.  
  67.                 else if (converted[i] == ')' && i < converted.Length - 1)
  68.                 {
  69.                     if (!IsOperator(converted[i + 1]))
  70.                     {
  71.                         converted.Insert(i + 1, "*");
  72.                         i++;
  73.                     }
  74.                 }
  75.             }
  76.  
  77.             converted = converted.Replace("sin", "Math.Sin")
  78.                                  .Replace("cos", "Math.Cos")
  79.                                  .Replace("tan", "Math.Tan")
  80.                                  .Replace("abs", "Math.Abs")
  81.                                  .Replace("log", "Math.Log")
  82.                                  .Replace("sqrt", "Math.Sqrt");
  83.  
  84.             return converted.ToString();
  85.         }
  86.  
  87.         private bool IsNumberOrOperator(char c)
  88.         {
  89.             const string operators = "+-/*^.,)";
  90.             return (char.IsNumber(c) || operators.Contains(c));
  91.         }
  92.  
  93.         private bool IsOperator(char c)
  94.         {
  95.             const string operators = "+-/*^.,)";
  96.             return (operators.Contains(c));
  97.         }
  98.  
  99.         private void CreateEvaluator()
  100.         {
  101.             // il codice compilato e` in vb.net poiche` semplifica il parsing dell'espressione
  102.             string code = string.Format(
  103.                 @"Imports System.Collections.Generic
  104.                 Imports System.Text
  105.                 Imports System
  106.                 Imports Microsoft.VisualBasic
  107.                 Class Evaluator
  108.                     Const e as Double = math.E
  109.                           Public Shared Function Evaluate(x As Single) As Single
  110.                                    Return {0}
  111.                           End Function
  112.                 End Class", Expression);
  113.  
  114.             var parameters = new CompilerParameters();
  115.             var provider = new VBCodeProvider();
  116.  
  117.             parameters.GenerateExecutable = false;
  118.             parameters.TreatWarningsAsErrors = true;
  119.             parameters.TempFiles.KeepFiles = false;
  120.             parameters.GenerateInMemory = true;
  121.  
  122.             var results = provider.CompileAssemblyFromSource(parameters, code);
  123.  
  124.             if (results.Errors.Count > 0)
  125.                 throw new Exception("Espressione non valida!");
  126.  
  127.             var asm = results.CompiledAssembly;
  128.             _evaluateFunction = asm.GetType("Evaluator").GetMethod("Evaluate");
  129.         }
  130.     }
  131. }