2015-12-07 10 views
10

बनाना मुझे एक UI बनाने की आवश्यकता है जो उपयोगकर्ता एक सूत्र तैयार करेगा। अर्थात्:गतिशील सूत्र

एक आइटम सूत्र के लिए है:

लागत * आइटम/100

एक और आइटम के लिए:

मद * 5/100

मैं उपयोगकर्ता के लिए सक्षम होना चाहते हैं वेब UI के माध्यम से सूत्र उत्पन्न करें।

तब जब उपयोगकर्ता वेरिएबल्स में प्रवेश करता है तो मैं परिणाम की गणना करना चाहता हूं।

क्या ऐसा करने के लिए कोई पैकेज या प्लगइन हैं?

धन्यवाद।

+0

स्प्रेडशीट, शायद? – Graham

+0

ठीक है मेरे ऐप में स्प्रेडशीट कैसे है? – DarthVader

+0

क्या आप यूआई केंद्रित पुस्तकालय या गणना इंजन या दोनों की तलाश में हैं? –

उत्तर

8

डार्थवडर!

यहां कुछ विकल्प हैं और यह आपकी आवश्यकताओं पर निर्भर करता है और क्या आपको कुछ जटिल या कुछ समझने और विस्तार करने के लिए बस कुछ सरल (शैक्षणिक उद्देश्यों के लिए) की आवश्यकता है।

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 आवेदन में ऊपर वर्ग का उपयोग कर एक उदाहरण है किसी भी सी # आवेदन)

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

यह एक पुस्तकालय के लिए खासतौर पर बनाया गया है वह और यह कई सूत्रों का समर्थन करता है! कुछ उदाहरण देखने और समझने में कुछ समय लग सकता है कि यह कैसे काम करता है लेकिन इसे बिना किसी काम के काम करना चाहिए।

आशा है कि यह मदद करता है,

लुइस हेनरिक Goll।

2

चेक this fiddle आप सूत्र सुधार कर सकते हैं आप की तरह चाहते हैं

एचटीएमएल

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

सीएसएस

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 के साथ टैग किया गया था, मुझे लगता है कि यह एक वेब अनुप्रयोग है। जब तक सर्वर पर सूत्र पोस्ट करने की आवश्यकता न हो और इसे वैनिला जावास्क्रिप्ट का उपयोग करके वहां मूल्यांकन किया जाए, तो आपके जीवन को इतना आसान बनाना चाहिए। जावास्क्रिप्ट एक गतिशील, untyped और व्याख्या की गई भाषा है जिसका अर्थ है कि आप एक स्ट्रिंग में गतिशील रूप से अपना सूत्र बना सकते हैं और फिर इसे अपने ब्राउज़र के जावास्क्रिप्ट इंजन द्वारा मूल्यांकन किया गया है।

w3cscools.com से कोड के उदाहरण का अनुसरण:

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

200.

का मूल्यांकन यदि फिर भी आप सर्वर साइड पर सूत्र का मूल्यांकन करने की जरूरत है, जाँच में व्याख्या चलाने पर आप क्या विकल्प होता है सी # पर भाषाएं। जावा में एक जावास्क्रिप्ट रनटाइम (Nashorn) जेवीएम में समर्थित है ताकि कोई सर्वर पक्ष पर आसानी से जावास्क्रिप्ट अभिव्यक्ति का मूल्यांकन कर सके।

संबंधित मुद्दे