Git Product home page Git Product logo

rpn's Introduction

RPN - Обратная польская нотация

This is a simple library that can convert string in Func using RPN.

   Func<double, double> func = Expression.ToDelegate("-log(x,2)*sin(x)");

and func - representation of the "-log(x,2)*sin(x)" as function of x.

2016-12-22

  • variable length of arguments in functions. Ex: "count_arguments(1,2,3,4,5,6)", "count_arguments(1,2)" // 2
  • lists. Ex: "list_length({1,2,3,4,5,6})" // 6
class Program {
        [STAThread]
        static void Main( string[] args ) {
            //Expression rpn = Expression.ToPolishNotation(Expression.ToExpresion("log(12)+123+rotate(1,2,3,9-1)"));
            //Expression rpn = Expression.ToPolishNotation(Expression.ToExpresion("rotate(rnd(),1,2,3)+log(12, rnd(4,5,6) )"));
            //Expression rpn = Expression.ToPolishNotation(Expression.ToExpresion("rotate(rnd(),2,3,4)+rotate(11)+log(10,11,12)+sin(0.5)"));
            string str_formula = "-(((count_arguments(1,2,3, rnd(4) )))-log(10,10))";
            //str_formula = "((rnd(1,2,3,4)))";
            //str_formula = "-(-5)";
            try {
                Console.WriteLine("Формула: " + str_formula);
                Expression rpn = Expression.ToPolishNotation(Expression.ToExpresion(str_formula));
                Console.WriteLine("rpn: " + rpn.ToString());
                Console.WriteLine("Результат: " + Expression.Calculate(rpn));
                Console.WriteLine("\n\n Расчёт по формуле:");
                // Проверка формулы как функции:
                str_formula = "-(-x)*3-x";
                rpn = Expression.ToPolishNotation(Expression.ToExpresion(str_formula));
                Console.WriteLine("rpn: " + rpn.ToString());
                Func<double, double> func = Expression.ToDelegate(str_formula);
                double res = func(10);
                Console.WriteLine("Формула: " + str_formula);
                Console.WriteLine("результат: "+res);
            }catch(Exception _ex) {
                Console.WriteLine(str_formula);
                string ex_info = Utilities.getExceptionInfo(_ex);
                //Clipboard.SetText(ex_info);
                Console.WriteLine(ex_info); // _ex.Message);

            }
            Console.ReadKey();
        }
    }

Результат:

Пришлось оставить скобки в стеке обратной польской нотации, т.к. без них нельзя было считать функции с переменным числом аргументов.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.