A dart package to help you parse and evaluate infix mathematical expressions into their prefix and postfix notations.

Overview

License: MIT

A dart package to help you parse and evaluate infix mathematical expressions into their prefix and postfix notations.

Features

  • Convert an infix expression into its prefix and postfix notations
  • Evaluate a mathematical expression
  • Support for basic mathematical operations

Usage

You can use infix-expression-parser as a library or as a CLI tool. Below are some examples explaining how to use the package in each case.

As a library

The package exposes two main classes:

  • InfixExpressionConverter: Use this class when you have an infix expression and want to convert it either to its postfix or infix notation.
  • ExpressionEvaluator: Use this class when you already have a postfix or prefix expression and want to evaluate it to a result. Use in combination with InfixExpressionConverter to also evaluate infix expressions easily. Note that in order to return a value, you need to provide a context in which you give each symbol in the expression a numeric value.

Example #1: Converting an infix expression

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
}

Example #2: Evaluating a postfix expression

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
}

Example #3: Evaluating a prefix expression

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
}

Example #4: Evaluating an infix expression

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
}

As a CLI tool

The package expose a infix-parser command explained below:

" for more information about a command.">
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 " for more information about a command.

You can use the convert sub-command to parse infix expressions.

Example #1: Converting an infix expression to a prefix expression

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

Example #2: Converting an infix expression to a postfix expression

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

Future improvements

  • A sub-command for evaluating expressions in the command line may come in future iterations of this package
  • Support for other mathematical operations, functions and symbols may come come in future iterations of this package
You might also like...

A flutter package with classes to help testing applications using the canvas

Canvas test helpers MockCanvas is a utility class for writing tests for canvas operations. It supports the same API as the regular Canvas class from d

Jan 31, 2022

A simple package to help does handler with countries in Flutter

A simple package to help does handler with countries in Flutter

It's a simple package to help does handler with countries in [Dart] language and [Flutter] Framework. Usage Countries class Can access all countries s

Feb 20, 2022

A CLI tool to help generate dart classes from json returned from API

Json 2 Dart Command line utility Important note There is already a package called json2dart so this package will be called json2dartc ! This project w

Oct 5, 2022

A package that lets you include a cool, nice looking and validated Password TextFormField in your app to enhance user experience. The package is fully & easily modifiable.

A package that lets you include a cool, nice looking and validated Password TextFormField in your app to enhance user experience. The package is fully & easily modifiable.

A package that lets you include a cool, nice looking and validated Password TextFormField in your app to enhance user experience. The package is fully

Jun 7, 2022

Some examples for how to build dart code using package:analyzer and package:code_builder only (i.e. no build_runner or build.yaml files)

What is this? Some examples for how to build dart code from another dart code us

Apr 17, 2022

A Dart package which supports checking if a current package is up-to-date.

pub_updater A Dart package which enables checking whether packages are up to date and supports updating them. Intended for use in CLIs for prompting u

Oct 27, 2022

Chuanying - what you see is what you get. 传影--所见即所得

Chuanying - what you see is what you get. 传影--所见即所得

传影--所见即所得 简介 从前 想要制作证件照,需要先把图片用QQ传到电脑,再用PS手动抠图; 看到一句喜欢的话,想要记到电脑里,需要一个字一个字用键盘打出来; 看到黑板上老师写的公式,想要记到Word里,需要用MathType一点点打出来; 但是有没有可能,只用手机拍照,就能搞定上面所有的事,一步

Apr 8, 2022

Official Git of flutter code-push made by Chimera inc. If you want to get more info or seek for biz corporation, you can contact [email protected].

中文版 Chimera Flutter Code Push Chimera is a Dart compiler developed by ourselves, which generates interpretable and executable bytecode to implement co

Oct 6, 2022

A CLI tool to help batch renaming files.

batch_rename A CLI tool to enable batch renaming of files. Installation Clone the repo and add bin/batch_rename.exe to PATH: gh repo clone POWRFULCOW8

Nov 3, 2021
Owner
Miguel Manjarres
Passionate about technology. Always open to learn new things.
Miguel Manjarres
A Dart library to parse Portable Executable (PE) format

pefile A Dart library to parse Portable Executable (PE) format Usage A simple usage example: var pe = pefile.parse('C:\\Windows\\System32\\notepad.exe

null 4 Sep 12, 2022
Parse and compose Magnet URI extension (BEP53) ranges.

bep53-range Parse and compose Magnet URI extension (BEP53) ranges. Dart port of https://github.com/webtorrent/bep53-range Usage parse Parse Magnet URI

Chiziaruhoma Ogbonda 2 Feb 7, 2022
Parse cron string to schedule and generate previous or next schedule item

Parse cron string to schedule and generate previous or next schedule item

Pokhodyun Alexander 2 Apr 17, 2022
A mobile map based application to help people everywhere around the world get help

Wonder This is a mobile application made by flutter. The application is called "Wonder" because it will help people everywhere around the world to get

Sara Nersisian 1 Dec 2, 2021
Flutter package to help you lazily load and display pages of items as the user scrolls down your screen.

Flutter package to help you lazily load and display pages of items as the user scrolls down your screen.

Edson Bueno 425 Dec 13, 2022
Library for help you make userbot or bot telegram and support tdlib telegram database and only support nodejs dart and google-apps-script

To-Do telegram client dart ✅️ support multi token ( bot / userbot ) ✅️ support bot and userbot ✅️ support telegram-bot-api local server ✅️ support tel

Azka Full Snack Developer:) 73 Jan 7, 2023
library to help you create database on local memory, support json local database inspired by lowdb

Licensed Licensed under the MIT License <http://opensource.org/licenses/MIT>. SPDX-License-Identifier: MIT Copyright (c) 2021 Azkadev <http://github.c

Azka Full Snack Developer:) 35 Oct 17, 2022
Integrate easily the Paygate Global Platform into your Flutter app

Integrate easily the Paygate Global Platform into your Flutter app Features Implement payment with the Paygate Global Platform. Support for two paymen

Kokou AGBAVON 7 Dec 15, 2022