.Modelica.Utilities.Examples.calculator

Information

Syntax

result = calculator(expression);

Description

This function demonstrates how a simple expression calculator can be implemented in form of a recursive decent parser using basically the Strings.scanToken(..) and Strings.scanDelimiter(..) function.

The following operations are supported (pi=3.14.. is a predefined constant):

   +, -
   *, /
   (expression)
   sin(expression)
   cos(expression)
   tan(expression)
   sqrt(expression)
   asin(expression)
   acos(expression)
   atan(expression)
   exp(expression)
   log(expression)
   pi

Example

  calculator("2+3*(4-1)");  // returns 11
  calculator("sin(pi/6)");  // returns 0.5

Interface

function calculator
  import Modelica.Utilities.Strings;
  extends Modelica.Icons.Function;
  input String string "Expression that is evaluated";
  output Real result "Value of expression";
end calculator;

Generated at 2020-06-05T07:38:22Z by OpenModelica 1.16.0~dev-420-gc007a39