License: MIT

一个 Dart 包,可帮助您将中缀数学表达式解析和计算为其前缀和后缀表示法。

特点

  • 将中缀表达式转换为前缀和后缀表示法
  • 评估数学表达式
  • 支持基本数学运算

用法

您可以将 `infix-expression-parser` 用作库或 CLI 工具。以下是一些示例,解释了如何在每种情况下使用该包。

作为库

该包公开了两个主要类

  • InfixExpressionConverter:当您有一个中缀表达式并希望将其转换为后缀或前缀表示法时,请使用此类。
  • ExpressionEvaluator:当您已经有后缀或前缀表达式并希望将其计算为结果时,请使用此类。与 `InfixExpressionConverter` 结合使用,还可以轻松评估中缀表达式。请注意,为了返回值,您需要提供一个 **上下文**,您可以在其中为表达式中的每个符号赋予数值。

示例 #1:转换中缀表达式

import 'package:infix_expression_parser/infix_expression_converter.dart';

void main() {
  // Provide an infix expression
  const infixExpression = '(a-b+c)*(d+e*f)';

  // Create an instance of InfixExpressionConverter
  final converter = InfixExpressionConverter(expression: infixExpression);

  final postfixExpression = converter.toPostfixNotation();
  final prefixExpression = converter.toPrefixNotation();

  print(postfixExpression); // ab-c+def*+*
  print(prefixExpression); // *+-abc+d*ef
}

示例 #2:评估后缀表达式

import 'package:infix_expression_parser/expression_evaluator.dart';

void main() {
  // Define a context map
  const context = {'a': 2, 'b': 3, 'c': 4, 'd': 5};

  // Provide a postfix expression
  const postfixExpression = 'abc+*d/';

  // Use the static method provided by the ExpressionEvaluator class and pass both the expression and the context
  final value = ExpressionEvaluator.evaluatePostfix(expression: postfixExpression, context: context);

  print(value); // 2.8
}

示例 #3:评估前缀表达式

import 'package:infix_expression_parser/expression_evaluator.dart';

void main() {
  // Define a context map
  const context = {'a': 2, 'b': 3, 'c': 4, 'd': 5, 'e': 6};

  // Provide a postfix expression
  const prefixExpression = '*-a/bc-/ade';

  // Use the static method provided by the ExpressionEvaluator class and pass both the expression and the context
  final value = ExpressionEvaluator.evaluatePrefix(expression: prefixExpression, context: context);

  print(value); // -7.0
}

示例 #4:评估中缀表达式

import 'package:infix_expression_parser/infix_expression_converter.dart';
import 'package:infix_expression_parser/expression_evaluator.dart';

void main() {
  // Provide an infix expression
  const infixExpression = 'a-b+c-d*e';

  // Convert it to either its postfix or prefix notation
  final converter = InfixExpressionConverter(expression: infixExpression);
  final postfixExpression = converter.toPostfixNotation();

  // Define a context map
  const context = {'a': 2, 'b': 3, 'c': 4, 'd': 5, 'e': 6};

  // Use the static method provided by the ExpressionEvaluator class and pass both the expression and the context
  final value = ExpressionEvaluator.evaluatePostfix(expression: postfixExpression, context: context);

  print(value); // -27.0
}

作为 CLI 工具

该包公开了一个 `infix-parser` 命令,如下所述

A CLI tool for evaluating expressions in prefix or postfix notations or converting infix expressions.

Usage: infix-parser <command> [arguments]

Global options:
-h, --help    Print this usage information.

Available commands:
  convert   Converts an expression to its prefix or postfix notation

Run "infix-parser help <command>" for more information about a command.

您可以使用 `convert` 子命令解析中缀表达式。

示例 #1:将中缀表达式转换为前缀表达式

pub run infix_expression_parser:main convert '(a-b/c)*(a/d-e)'  --prefix
*-a/bc-/ade

示例 #2:将中缀表达式转换为后缀表达式

pub run infix_expression_parser:main convert '(a-b/c)*(a/d-e)'  --postfix
abc/-ad/e-*

未来改进

  • 命令行评估表达式的子命令可能会在此包的未来版本中出现
  • 对其他数学运算、函数和符号的支持可能会在此包的未来版本中出现

GitHub

查看 Github