2015-12-07 8 views
10

そのユーザーが数式を作成する必要があります。すなわち:動的数式を作成する

つの項目の式の場合である:

コスト*アイテム/他の項目については

100:

項目※5/100

私は、ユーザーができるようにしたいですWeb UIを介して数式を生成します。

次に、ユーザーが変数を入力すると、結果を計算したいと思います。

これを行うパッケージまたはプラグインはありますか?

ありがとうございました。

+0

スプレッドシート、多分? – Graham

+0

アプリでスプレッドシートを使用するにはどうすればよいですか? – DarthVader

+0

UI中心ライブラリ、計算エンジン、またはその両方を探していますか? –

答えて

8

DarthVader!

ここにはいくつかのオプションがあり、必要に応じて、非常に複雑なものが必要なのか、理解して拡張するのが簡単なのか(学術目的のためか)によって異なります。

1)簡単、簡単、カスタマイズ可能で始めましょう。私はあなたの投稿に指定したニーズを満たすクラスを作成しましたが、それは非常に生のものであり、それ以上のテストや修正なしに商用プロジェクトで使用しないでください。あなたが必要とするものを達成するための簡単な方法を示しています。コードはきちんと動作しますが、数学の優先順位(かっこや*以上)は考慮していません。それはそれはコメントし、うまくいけば自分が説明され、そうするために適応させる必要がある... コードは以下の通りです:ここでは

public class DynamicFormula 
{ 
    /// <summary> 
    /// This simply stores a variable name and its value so when this key is found in a expression it gets the value accordingly. 
    /// </summary> 
    public Dictionary<string, double> Variables { get; private set; } 

    /// <summary> 
    /// The expression itself, each value and operation must be separated with SPACES. The expression does not support PARENTHESES at this point. 
    /// </summary> 
    public string Expression { get; set; } 

    public DynamicFormula() 
    { 
     this.Variables = new Dictionary<string, double>(); 
    } 

    public double CalculateResult() 
    { 
     if (string.IsNullOrWhiteSpace(this.Expression)) 
      throw new Exception("An expression must be defined in the Expression property."); 

     double? result = null; 
     string operation = string.Empty; 

     //This will be necessary for priorities operations such as parentheses, etc... It is not being used at this point. 
     List<double> aux = new List<double>(); 

     foreach (var lexema in Expression.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)) 
     { 
      //If it is an operator 
      if (lexema == "*" || lexema == "/" || lexema == "+" || lexema == "-") 
      { 
       operation = lexema; 
      } 
      else //It is a number or a variable 
      { 
       double value = double.MinValue; 
       if (Variables.ContainsKey(lexema.ToLower())) //If it is a variable, let's get the variable value 
        value = Variables[lexema.ToLower()]; 
       else //It is just a number, let's just parse 
        value = double.Parse(lexema); 

       if (!result.HasValue) //No value has been assigned yet 
       { 
        result = value; 
       } 
       else 
       { 
        switch (operation) //Let's check the operation we should perform 
        { 
         case "*": 
          result = result.Value * value; 
          break; 
         case "/": 
          result = result.Value/value; 
          break; 
         case "+": 
          result = result.Value + value; 
          break; 
         case "-": 
          result = result.Value - value; 
          break; 
         default: 
          throw new Exception("The expression is not properly formatted."); 
        } 
       } 
      } 
     } 

     if (result.HasValue) 
      return result.Value; 
     else 
      throw new Exception("The operation could not be completed, a result was not obtained."); 
    } 
    /// <summary> 
    /// Add variables to the dynamic math formula. The variable should be properly declared. 
    /// </summary> 
    /// <param name="variableDeclaration">Should be declared as "VariableName=VALUE" without spaces</param> 
    public void AddVariable(string variableDeclaration) 
    {    
     if (!string.IsNullOrWhiteSpace(variableDeclaration)) 
     { 
      var variable = variableDeclaration.ToLower().Split('='); //Let's make sure the variable's name is LOWER case and then get its name/value 
      string variableName = variable[0]; 
      double variableValue = 0; 

      if (double.TryParse(variable[1], out variableValue)) 
       this.Variables.Add(variableName, variableValue); 
      else 
       throw new ArgumentException("Variable value is not a number"); 
     } 
     else 
     { 
      //Could throw an exception... or just ignore as it not important... 
     } 
    } 
} 

は、WPFアプリケーションで上記のクラスを使用した例である(で使用することができますあなたはより堅牢で、最も現実の生活状況のために何かが必要な場合は、あなたが決定的FLEEチェックアウトする必要があります任意のC#アプリケーション)

private void btCalculate_Click(object sender, RoutedEventArgs e) 
    { 
     string expression = tboxExpression.Text; //"cost * item/100" (IT MUST BE SEPARATED WITH SPACES!) 
     string variable1 = tboxVariable1.Text;  //"item=10" 
     string variable2 = tboxVariable2.Text;  //"cost=2.5" 

     DynamicFormula math = new DynamicFormula(); 
     math.Expression = expression; //Let's define the expression 
     math.AddVariable(variable1); //Let's add the first variable 
     math.AddVariable(variable2); //Let's add the second variable 

     try 
     { 
      double result = math.CalculateResult(); //In this scenario the result is 0,25... cost * item/100 = (2.5 * 10/100) = 0,25 
      //Console.WriteLine("Success: " + result); 
      tboxResult.Text = result.ToString(); 
     } 
     catch(Exception ex) 
     { 
      //Console.WriteLine(ex.Message); 
      tboxResult.Text = ex.Message; 
     } 
    } 

2): http://flee.codeplex.com/wikipage?title=Examples&referringTitle=Home

これがために特別に作られたライブラリですそれはいくつかの数式をサポートしています! いくつかの例を見て、それがどのように機能するかを理解することは時々ありますが、多くの作業をせずに仕事を終わらせるべきです。

希望します。

LuísHenrique Goll。

2

チェックthis fiddleあなたは

HTML

<form id="ula"> 
    <h1>Insert your formula</h1> 
    <input type="text" placeholder="Es: a(b+c)/2" /> 
    <input type="submit" value="Create form" /> 
</form> 

CSS

body{font-family:arial,sans-serif;text-align:center} 
input{padding:6px;border:1p solid #999;margin:10px auto} 

JSをしたいのような式を向上させることができます

$('form').on('submit',function(e){ 
    e.preventDefault(); 
    $(this).hide(); 
    $('body').append($('<div />').hide().fadeIn(800)); 
    var labDiv=$('div:first'); 
    var varNames = []; 
    var formula=$('input').val().toString(); 
    var varStr=formula.replace(/[^a-zA-Z]+/g, ""); 
    $.each(varStr.split(''), function(i, el) { 
     if ($.inArray(el, varNames) === -1){ 
      varNames.push(el); 
      labDiv.append('<input name="'+el+'" placeholder="'+el+' value.." /><br />'); 
     } 
    }); 
    labDiv.prepend('<h1>'+formula+'</h1>'); 
    labDiv.append('<button id="newFormula">New formula</button><button id="calculate">Calculate</button>') 
    $('#calculate').on('click',function(e){ 
     e.preventDefault(); 
     var result=formula.replace(/\(/g,'*(').replace(RegExp(':','g'),'/'); 
     for(var ct=0;ct<varNames.length;ct++){ 
      result=result.replace(new RegExp(varNames[ct], 'g'),$('input[name='+varNames[ct]+']').val()); 
      console.log(result) 
     }; 
     labDiv.append('<h2>'+result.replace(/\*\(/g,'(')+'= <b>'+eval(result.replace(',','.'))+'</b></h2>'); 
    }); 
    $('#newFormula').one('click',function(e){ 
     e.preventDefault(); 
     labDiv.remove(); 
     $('form#ula input:first').val(''); 
     $('form#ula').fadeIn(); 
    }); 
}) 
1

質問にjQueryというタグが付いていたので、私はこれがウェブアプリケーションであると仮定しています。サーバーに数式を投稿し、バニラJavaScriptを使用して数式を評価する必要がある場合を除き、あなたの人生をはるかに簡単にする必要があります。 JavaScriptはで、動的、型なし、および解釈済みの言語です。これは、動的にあなたの数式を文字列で構成し、ブラウザのJavaScriptエンジンで評価できることを意味します。

w3cscools.comから次のコード例は:

var x = 10; 
var y = 20; 
var a = eval("x * y") 

は、サーバー側で式を評価する必要があるしかし、もし、あなたが実行している上で解釈したものをオプションチェック200

に評価されますC#の言語。 Javaでは、JVMにバックアップされたJavascriptランタイム(Nashorn)があるため、サーバー側でJavaScript式を簡単に評価できます。

関連する問題