.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 2024-12-24T19:25:53Z by OpenModelicaOpenModelica 1.24.3 using GenerateDoc.mos