#include #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; cout << "---- Simple function def ----" << endl; uc.DefineFunction("f(x) = x ^ 2 + 5"); cout << uc.Eval("f(10)") << endl; cout << "---- Function overloading ----" << endl; // Overloading based on parameter type or number of parameters uc.DefineFunction("MyOverload(x As Double) = x + x"); uc.DefineFunction("MyOverload(x As String) As String = x + x"); uc.DefineFunction("MyOverload(x As String, y As String) As String = x + y"); uc.DefineFunction("MyOverload(x, y) = x + y"); cout << uc.EvalStr("MyOverload(5)") << endl; cout << uc.EvalStr("MyOverload('Ha')") << endl; cout << uc.EvalStr("MyOverload('Hello ', 'world!')") << endl; cout << uc.EvalStr("MyOverload(5, 10)") << endl; cout << "---- Definition hiding/un-hiding ----" << endl; // Shadowing (hiding) definitions uc.DefineFunction("h(x) = x * 10"); cout << uc.Eval("h(3)") << endl; auto hFunc = uc.DefineFunction("h(x) = x * 100"); // hides previous def cout << uc.Eval("h(3)") << endl; hFunc.Release(); // Releasing this restores previous def cout << uc.Eval("h(3)") << endl; cout << "---- Optional parameters ----" << endl; uc.DefineFunction("Opt(x, y = 5, z As String = 'Hello') = x + y + Length(z)"); cout << uc.Eval("Opt(10)") << endl; cout << uc.Eval("Opt(10, 20)") << endl; cout << uc.Eval("Opt(10, 20, 'Just a test 123')") << endl; cout << "---- Recursion ----" << endl; uc.DefineFunction("Factorial(x) = iif(x > 1, x * Factorial(x - 1), 1)"); uc.DefineFunction("Fib(n) = IIf(n < 2, n, Fib(n - 1) + Fib(n - 2))"); cout << uc.Eval("Factorial(5)") << endl; cout << uc.Eval("Fib(10)") << endl; // Bootstrap - builds new def on top of existing one cout << "------ Bootstrapping -------" << endl; cout << uc.EvalStr("Hex(123)") << endl; // uses "built-in" version of Hex() auto MyHex = uc.DefineFunction("Hex(number As Int) As String = '0x' + UCase(Hex(number))", 0, uCalc::DataType::Empty, true); cout << uc.EvalStr("Hex(123)") << endl; MyHex.Release(); cout << uc.EvalStr("Hex(123)") << endl; // Overwrite - useful for spreadsheet-like functionality cout << "------ Overwrite -------" << endl; uc.DefineFunction("SpreadsheetCell_A1() = 5", 0, uCalc::DataType::Empty, false, true); uc.DefineFunction("SpreadsheetCell_B2() = SpreadsheetCell_A1() * 10", 0, uCalc::DataType::Empty, false, true); uc.DefineFunction("SpreadsheetCell_C3() = SpreadsheetCell_A1() + SpreadsheetCell_B2()", 0, uCalc::DataType::Empty, false, true); cout << uc.Eval("SpreadsheetCell_A1()") << endl; cout << uc.Eval("SpreadsheetCell_B2()") << endl; cout << uc.Eval("SpreadsheetCell_C3()") << endl; // SpreadsheetCell_C3() will be affected by the definition changes of SpreadsheetCell_A1() and SpreadsheetCell_B3() uc.DefineFunction("SpreadsheetCell_B2() = SpreadsheetCell_A1() * 100", 0, uCalc::DataType::Empty, false, true); uc.DefineFunction("SpreadsheetCell_A1() = 25", 0, uCalc::DataType::Empty, false, true); cout << "-------" << endl; // Note: Empty parenthesis are optional for functions with no parameters cout << uc.Eval("SpreadsheetCell_A1") << endl; cout << uc.Eval("SpreadsheetCell_B2") << endl; cout << uc.Eval("SpreadsheetCell_C3") << endl; // See Define() topic for more }