从字符串中提取公式

Extracting Formula from String

我必须从公式中提取所有变量

Fiddle for below problem

eg. (FB+AB+ESI) / 12

Output {FB,AB,ESI}

到目前为止编写的代码

var length = formula.Length;
            List<string> variables = new List<string>();
            List<char> operators = new List<char> { '+', '-', '*', '/', ')', '(', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            int count = 0;
            string character = string.Empty;
            for (int i = 0; i < length; i++)
            {
                if (!operators.Contains(formula[i]))
                    character += formula[i];
                else
                {
                    if (!string.IsNullOrWhiteSpace(character))
                        variables.Add(character);
                    character = string.Empty;
                    count = i;
                }
            }
            if (!string.IsNullOrWhiteSpace(character))
                variables.Add(character);
            return variables;

Output of the Method is {FB,AB,ESI} which is correct

我的问题是 Varaible 包含数字字段,即

eg. (FB1+AB1)/100

Expected Output : {FB1,AB1}

But My method return {FB,AB}

你也可以这样做,只要优化代码就可以了。

       string ss = "(FB+AB+ESI) / 12";

        string[] spl =  ss.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
        string final = spl[0].Replace("(", "").Replace(")", "").Trim();
        string[] entries = final.Split(new char[] {'+'}, StringSplitOptions.RemoveEmptyEntries);
        StringBuilder sbFinal = new StringBuilder();
        sbFinal.Append("{");
        foreach(string en in entries)
        {
            sbFinal.Append(en + ",");
        }
        string finalString = sbFinal.ToString().TrimEnd(',');
        finalString += "}";

这是使用正则表达式的方法。

        Regex regex = new Regex(@"([A-Z])\w+");

        List<string> matchedStrings = new List<string>();
        foreach (Match match in regex.Matches("(FB1+AB1)/100"))
        {
            matchedStrings.Add(match.Value);
        }

这将创建一个包含所有匹配项的字符串列表。

如果没有正则表达式,您可以拆分实际的运算符(不是数字),然后删除以数字开头的任何项目:

public static List<string> GetVariables(string formula)
{
    if (string.IsNullOrWhitespace(formula)) return new List<string>();

    var operators = new List<char> { '+', '-', '*', '/', '^', '%', '(', ')' };

    int temp;
    return formula
        .Split(operators.ToArray(), StringSplitOptions.RemoveEmptyEntries)
        .Where(operand => !int.TryParse(operand[0].ToString(), out temp))
        .ToList();
}

如果变量名必须以开头

   letter A..Z, a..z

如果变量名可以包含

  letters     A..Z, a..z
  digits      0..1
  underscopes _

你可以使用正则表达式:

  String source = "(FB2+a_3B+EsI) / 12";

  String pattern = @"([A-Z]|[a-z])+([A-z]|[a-z]|\d|_)*";

  // output will be "{FB2,a_3B,EsI}"
  String output = "{" + String.Join(",", 
    Regex.Matches(source, pattern)
      .OfType<Match>()
      .Select(item => item.Value)) + "}";

如果你需要一个集合,比如说一个变量名称数组,只需修改Linq:

  String names[] = Regex.Matches(source, pattern)
    .OfType<Match>()
    .Select(item => item.Value)
    .ToArray();

然而,实现的只是一种简单的分词器:你必须将 "variable names" 从函数名称、class 名称中分离出来,检查它们是否被注释掉等

你要做的是翻译。

我不能给你完整的代码,但我能给你的是一个良好的开端(这需要大量的编码)。

首先,了解reverse polish notation

其次,你需要了解stacks

第三,您必须同时应用两者才能获得您想要解释的内容。

已更改您的代码以执行您要求的操作,但不确定解决方案的方法,因为未考虑括号和运算符优先级。

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string formula = "AB1+FB+100";
        var length = formula.Length;
        List<string> variables = new List<string>();
        List<char> operators = new List<char>{'+', '-', '*', '/', ')', '('};
        List<char> numerals = new List<char>{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

        int count = 0;
        string character = string.Empty;
        char prev_char = '[=10=]';

        for (int i = 0; i < length; i++)
        {
            bool is_operator = operators.Contains(formula[i]);
            bool is_numeral = numerals.Contains(formula[i]);
            bool is_variable = !(is_operator || is_numeral);
            bool was_variable = character.Contains(prev_char);

            if (is_variable || (was_variable && is_numeral) )
                character += formula[i];
            else
            {
                if (!string.IsNullOrWhiteSpace(character))
                    variables.Add(character);
                character = string.Empty;
                count = i;
            }

            prev_char = formula[i];
        }

        if (!string.IsNullOrWhiteSpace(character))
            variables.Add(character);

        foreach (var item in variables)
            Console.WriteLine(item);
        Console.WriteLine();
        Console.WriteLine();
    }
}

也许还可以考虑 Math-Expression-Evaluator (on nuget)