【问题标题】:convert logical expression string into string array, based on parenthesis基于括号将逻辑表达式字符串转换为字符串数组
【发布时间】:2015-02-20 10:37:58
【问题描述】:

我有一个令我困惑的问题,我不知道如何在 C# 中做到这一点。

假设,我有这个公式:((true or false) and (false or true))

这只是一个示例公式,但它可能更复杂,例如:((((true or ((((true or false) and (false or ((true or false) and (false or ((((true or false) and (false or ((true or false) and (false or true)))) or not ((true or false) and (false or ((true or false) and (false or true)))))))))) or not ((true or false) and (false or ((true or false) and (false or true))))))) and (false or ((true or false) and (false or ((((true or false) and (false or ((true or false) and (false or true)))) or not ((true or false) and (false or ((true or false) and (false or true)))))))))) or not ((true or false) and (false or ((true or false) and (false or true)))))) 等。

我想把它分成三个部分

  1. (true or false)
  2. and
  3. (false or true)

以另一种方式,我想将这个公式分解为它的两个部分公式和它的操作数。现在,我有两个二元运算符 (or,and) 和一个一元运算符 (not)。

我该如何解决这个问题?你能帮我解决这个问题吗?我尝试过使用 Grouping Constructs 的正则表达式,但我不是正则表达式专家。

谢谢, 奥托

【问题讨论】:

  • 这可能不是使用正则表达式,而是解析器。
  • 数组不是存储此类数据的最佳结构,因为它实际上是表达式树,而不是平面列表。此外,正则表达式不是解析此类表达式的最佳工具,而且此类解析本身可能是一项复杂的任务,因此您可能应该看看一些词法分析器。
  • 据我所知,如果您在 .Net 中使用 Grouping Constructs 进行正则表达式,则可以使用正则表达式。

标签: c# regex logic


【解决方案1】:

这个问题有简单的解决方案。 Reverse Polish Notation (RPN) 将您的字符串转换为 RNP,然后执行它。

【讨论】:

  • 谢谢,这就是我要找的!
【解决方案2】:

Otto,不知道你为什么要这样做我不知道字符串数组是否是最好的方法;问题是,如果你只有一个“(真或假)”等数组,你就会失去原始表达式的结构。我的解决方案使用从 0 开始的整数作为占位符来显示从表达式中提取字符串的位置。希望这可以帮助!格雷格

using System;
using System.Collections.Generic;

namespace ParseParentheses
{
    class Program
    {
        static List<string> wordList = new List<string>();
        static int wordIndex = -1;

    static void Main(string[] args)
    {
        GetChunks("(true AND (true OR false)) OR ((true AND false) OR false)");
        // Now we know how many items we have, convert the List to an array,
        // as this is what the solution specified.
        string[] words = wordList.ToArray();
        for (int i = 0; i < words.Length; i++)
        {
            Console.WriteLine(i + ":\t" + words[i]);
        }
    }

    private static void GetChunks(string text)
    {
        int start;
        int end = text.IndexOf(')');
        if (end > -1)
        {
            start = text.LastIndexOf('(', end - 1);
            if (start == -1)
            {
                throw new ArgumentException("Mismatched parentheses", text);
            }
            wordList.Add(text.Substring(start, end - start + 1));
            wordIndex++;
            text = text.Substring(0, start)
                + wordIndex.ToString()
                + text.Substring(end + 1);

            GetChunks(text);
        }
        else
        {
        // no more ) in text, just add what's left to the List.
            wordList.Add(text);
        }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2019-01-24
    • 1970-01-01
    • 2014-04-04
    相关资源
    最近更新 更多