Defines a custom `sum_to` operator to calculate the sum of a numeric range, demonstrating a single-word operator.
ID: 1224
See: Dynamic Syntax
using uCalcSoftware;
var uc = new uCalc();
// Define the variables that the operator's expression will use.
uc.DefineVariable("i");
uc.DefineVariable("total");
// Define a new 'sum_to' operator at runtime, with precedence level of 50.
// It uses the built-in ForLoop function to sum numbers into the 'total' variable.
uc.DefineOperator("{start} sum_to {end} = total = 0; ForLoop(i, start, end, 1, total = total + i)", 50);
// Use the new operator. The result is stored in the 'total' variable.
uc.Eval("1 sum_to 5");
Console.WriteLine($"The sum from 1 to 5 is: {uc.Eval("total")}");
The sum from 1 to 5 is: 15 using uCalcSoftware; var uc = new uCalc(); // Define the variables that the operator's expression will use. uc.DefineVariable("i"); uc.DefineVariable("total"); // Define a new 'sum_to' operator at runtime, with precedence level of 50. // It uses the built-in ForLoop function to sum numbers into the 'total' variable. uc.DefineOperator("{start} sum_to {end} = total = 0; ForLoop(i, start, end, 1, total = total + i)", 50); // Use the new operator. The result is stored in the 'total' variable. uc.Eval("1 sum_to 5"); Console.WriteLine($"The sum from 1 to 5 is: {uc.Eval("total")}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Define the variables that the operator's expression will use.
uc.DefineVariable("i");
uc.DefineVariable("total");
// Define a new 'sum_to' operator at runtime, with precedence level of 50.
// It uses the built-in ForLoop function to sum numbers into the 'total' variable.
uc.DefineOperator("{start} sum_to {end} = total = 0; ForLoop(i, start, end, 1, total = total + i)", 50);
// Use the new operator. The result is stored in the 'total' variable.
uc.Eval("1 sum_to 5");
cout << "The sum from 1 to 5 is: " << uc.Eval("total") << endl;
}
The sum from 1 to 5 is: 15 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Define the variables that the operator's expression will use. uc.DefineVariable("i"); uc.DefineVariable("total"); // Define a new 'sum_to' operator at runtime, with precedence level of 50. // It uses the built-in ForLoop function to sum numbers into the 'total' variable. uc.DefineOperator("{start} sum_to {end} = total = 0; ForLoop(i, start, end, 1, total = total + i)", 50); // Use the new operator. The result is stored in the 'total' variable. uc.Eval("1 sum_to 5"); cout << "The sum from 1 to 5 is: " << uc.Eval("total") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Define the variables that the operator's expression will use.
uc.DefineVariable("i")
uc.DefineVariable("total")
'// Define a new 'sum_to' operator at runtime, with precedence level of 50.
'// It uses the built-in ForLoop function to sum numbers into the 'total' variable.
uc.DefineOperator("{start} sum_to {end} = total = 0; ForLoop(i, start, end, 1, total = total + i)", 50)
'// Use the new operator. The result is stored in the 'total' variable.
uc.Eval("1 sum_to 5")
Console.WriteLine($"The sum from 1 to 5 is: {uc.Eval("total")}")
End Sub
End Module
The sum from 1 to 5 is: 15 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Define the variables that the operator's expression will use. uc.DefineVariable("i") uc.DefineVariable("total") '// Define a new 'sum_to' operator at runtime, with precedence level of 50. '// It uses the built-in ForLoop function to sum numbers into the 'total' variable. uc.DefineOperator("{start} sum_to {end} = total = 0; ForLoop(i, start, end, 1, total = total + i)", 50) '// Use the new operator. The result is stored in the 'total' variable. uc.Eval("1 sum_to 5") Console.WriteLine($"The sum from 1 to 5 is: {uc.Eval("total")}") End Sub End Module