如何使用文本文件中的公式?

问题描述 投票:0回答:4

我有一个桌面 C# 应用程序,它使用公式根据用户的输入计算值。

我在想是否有一种方法可以从文本文件中获取公式并使用它而不是对其进行硬编码,这样一旦程序编译后就可以轻松更改公式。

问题是将文本文件中的字符串转换为代码中可用的公式。

谢谢你

c# winforms text-files formula type-conversion
4个回答
1
投票

是的,这是个好主意。但是如果您从文本文件中获取数据类型,则会出现问题。您还需要对此进行良好的验证。

根据我的建议,你必须使用 XML。

它易于处理,您可以选择更准确的数据作为比较文本文件


1
投票

我想你可能想尝试直接解析公式字符串来计算结果。然而,使用

XML
来存储你的公式也是一个好主意(尽管你必须自己输入从实际公式转换而来的公式,为此,你必须了解一些用于表达公式的 XML 结构不用担心,很简单)。首先我谈谈表达公式的 XML 结构。它有两种类型的节点:

  1. 操作数节点:该节点的名称为

    operand
    ,有 1 个名为
    value
    的属性,并且没有任何子元素。它的
    value
    可以是数字或变量(占位符),以便您可以在运行时传递一些值。例子:

    <operand value="1234"/>
    <operand value="x"/>
    
  2. 算子节点:该节点与其支持的算子具有相似的名称。为简单起见,我假设您的公式仅支持 4 个运算符:

    +
    -
    *
    /
    。加法由标签
    <add>
    表示,减法由标签
    <sub>
    表示,乘法由标签
    <mul>
    ,除法由标签表示
    <div>
    。每个运算符节点有 2 个元素(因为运算符是二元的)。每个元素都是一个操作数。这并不严格限于上面提到的
    operand
    节点,它可以是另一个
    operator
    节点。这就是有关 XML 结构的全部知识。例子:

    //expression of (x + y)
    <add>
      <operand value="x"/>
      <operand value="y"/>
    </add>
    

以下 XML 是公式

f(x,y) = (x + y - x/y) * (x-y) / 5
的示例。该公式需要在运行时计算 2 个变量。

<mul>
  <sub>
     <add>
        <operand value="x"/>
        <operand value="y"/>
     </add>
     <div>
        <operand value="x"/>
        <operand value="y"/>
     </div>
  </sub>
  <div>
     <sub>
        <operand value="x"/>
        <operand value="y"/>
     </sub>
     <operand value="5"/>
  </div>
</mul>

这是处理它的代码:

public class ArgumentInfo
{
   public string Name { get; set; }
   public double Value { get; set; }
}
public double ComputeNode(XElement node, params ArgumentInfo[] args)
{
        var operands = node.Elements().ToList();
        if (operands.Count == 0)
        {
            if (node.Name != "operand") throw new ArgumentException("XML formula error! Please check it");
            ArgumentInfo o = args.FirstOrDefault(x => x.Name == node.Attribute("value").Value);
            return o == null ? double.Parse(node.Attribute("value").Value) : o.Value;
        }    
        if (operands.Count != 2) throw new ArgumentException("XML formula error! Please check it");    
        var a = ComputeNode(operands[0], args);
        var b = ComputeNode(operands[1], args);
        if (node.Name == "add") return a + b;
        else if (node.Name == "sub") return a - b;
        else if (node.Name == "mul") return a * b;
        else return a / b;            
}
public double Compute(string xmlFormula, params ArgumentInfo[] args)
{
        XDocument doc = XDocument.Parse(xmlFormula);
        return ComputeNode(doc.Root, args);
}
public double ComputeFormulaFromPath(string xmlFormulaPath, params ArgumentInfo[] args)
{
        XDocument doc = XDocument.Load(xmlFormulaPath);
        return ComputeNode(doc.Root, args);
}
//Example f(x,y) = (x + y - x/y) * (x-y) / 5 with (x,y) = (10,20)
var result = ComputeFormulaFromPath(@"E:\yourFormula.xml", new ArgumentInfo {Name = "x", Value = 10}, 
                                                           new ArgumentInfo {Name="y",Value=20});
//The result is -59

您还可以定义一些名为

<formula>
的标签来表达公式,并且您可以在同一个
xml
文件中定义多个公式。


0
投票

我认为您不能按照您的方法动态使用公式。我并不是说这是不可能的,但要使其成为可能,您的应用程序中需要更多额外的填充。

举个更简单的例子

您的文本文件有几个简单的公式,格式如下:

Add : a+b
Simple Interest : (P*R*T)/100
Progression = 1+2+3... n+ (n+1)+(n+2)....

那么你能告诉我如何选择公式中的动态部分,即上面示例中的

P,R,T,n,a,b

AFAIK,如果您将公式放在代码中,这会很容易。是的,你可以做一件常见的事情。创建一个类文件,其中包含程序所需的所有公式作为方法的通用方法,并在需要时调用它们。

见下文

public static class MathematicalBench
{
    public static double SimpleInterest(double principal, double rate, double time)
    {
       return (principal*rate*time)/100;
    }
}

如有需要请致电

Class A:
    private void SomeMethod1()
    {
       // Your logic 
       var interest=  MathematicalBench.SimpleInterest(128765.98,3.6,16);
    }

Another Class B:
    private void SomeNewMethod1()
    {
       // Your logic 
       var interest=  MathematicalBench.SimpleInterest(6523898,6.2,10);
    }

这当然会更加灵活和简单。


0
投票

如果您用波兰表示法编写公式,则实现起来会更简单 [https://en.wikipedia.org/wiki/Polish_notation]

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.