Defining a new custom operator with a precedence level set relative to an existing operator.
ID: 656
See: Precedence = [int]
using uCalcSoftware;
var uc = new uCalc();
// Goal: Define a new power operator '**' with higher precedence than multiplication '*'.
var mul_precedence = uc.ItemOf("*", uCalc.Properties(ItemIs.Infix)).Precedence;
// Set the new operator's precedence to be higher than multiplication.
uc.DefineOperator("{base} ** {exp} = Pow(base, exp)", mul_precedence + 10);
// The new operator should be evaluated before multiplication and addition.
// The expression is equivalent to: 2 + (3 * (2 ** 3)) -> 2 + (3 * 8) -> 2 + 24 -> 26
Console.WriteLine(uc.Eval("2 + 3 * 2 ** 3"));
26 using uCalcSoftware; var uc = new uCalc(); // Goal: Define a new power operator '**' with higher precedence than multiplication '*'. var mul_precedence = uc.ItemOf("*", uCalc.Properties(ItemIs.Infix)).Precedence; // Set the new operator's precedence to be higher than multiplication. uc.DefineOperator("{base} ** {exp} = Pow(base, exp)", mul_precedence + 10); // The new operator should be evaluated before multiplication and addition. // The expression is equivalent to: 2 + (3 * (2 ** 3)) -> 2 + (3 * 8) -> 2 + 24 -> 26 Console.WriteLine(uc.Eval("2 + 3 * 2 ** 3"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Goal: Define a new power operator '**' with higher precedence than multiplication '*'.
auto mul_precedence = uc.ItemOf("*", uCalc::Properties(ItemIs::Infix)).Precedence();
// Set the new operator's precedence to be higher than multiplication.
uc.DefineOperator("{base} ** {exp} = Pow(base, exp)", mul_precedence + 10);
// The new operator should be evaluated before multiplication and addition.
// The expression is equivalent to: 2 + (3 * (2 ** 3)) -> 2 + (3 * 8) -> 2 + 24 -> 26
cout << uc.Eval("2 + 3 * 2 ** 3") << endl;
}
26 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Goal: Define a new power operator '**' with higher precedence than multiplication '*'. auto mul_precedence = uc.ItemOf("*", uCalc::Properties(ItemIs::Infix)).Precedence(); // Set the new operator's precedence to be higher than multiplication. uc.DefineOperator("{base} ** {exp} = Pow(base, exp)", mul_precedence + 10); // The new operator should be evaluated before multiplication and addition. // The expression is equivalent to: 2 + (3 * (2 ** 3)) -> 2 + (3 * 8) -> 2 + 24 -> 26 cout << uc.Eval("2 + 3 * 2 ** 3") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Goal: Define a new power operator '**' with higher precedence than multiplication '*'.
Dim mul_precedence = uc.ItemOf("*", uCalc.Properties(ItemIs.Infix)).Precedence
'// Set the new operator's precedence to be higher than multiplication.
uc.DefineOperator("{base} ** {exp} = Pow(base, exp)", mul_precedence + 10)
'// The new operator should be evaluated before multiplication and addition.
'// The expression is equivalent to: 2 + (3 * (2 ** 3)) -> 2 + (3 * 8) -> 2 + 24 -> 26
Console.WriteLine(uc.Eval("2 + 3 * 2 ** 3"))
End Sub
End Module
26 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Goal: Define a new power operator '**' with higher precedence than multiplication '*'. Dim mul_precedence = uc.ItemOf("*", uCalc.Properties(ItemIs.Infix)).Precedence '// Set the new operator's precedence to be higher than multiplication. uc.DefineOperator("{base} ** {exp} = Pow(base, exp)", mul_precedence + 10) '// The new operator should be evaluated before multiplication and addition. '// The expression is equivalent to: 2 + (3 * (2 ** 3)) -> 2 + (3 * 8) -> 2 + 24 -> 26 Console.WriteLine(uc.Eval("2 + 3 * 2 ** 3")) End Sub End Module