Manage different parser configurations by switching the default instance.

ID: 367

				
					using uCalcSoftware;

var uc = new uCalc();
// Setup a "Scientific" configuration
var scientificCalc = new uCalc();
scientificCalc.DefineFunction("sqrt(x) = x^0.5");
scientificCalc.DefineVariable("pi = 3.14159");

// Setup a "Financial" configuration
var financialCalc = new uCalc();
financialCalc.DefineFunction("tax(amount, rate) = amount * (rate/100)");

// Set the scientific calculator as the default
scientificCalc.IsDefault = true;
Console.WriteLine($"Current default is scientific? {scientificCalc.IsDefault}");

// Components that rely on the default instance now use the scientific setup.
uCalc.Expression expr1 = "2 * pi";
Console.WriteLine($"2 * pi = {expr1.Evaluate()}");

// Now, switch the default to the financial calculator
financialCalc.IsDefault = true;
Console.WriteLine($"Current default is scientific? {scientificCalc.IsDefault}"); // Should be false now
Console.WriteLine($"Current default is financial? {financialCalc.IsDefault}");

// New components will use the financial setup.
uCalc.Expression expr2 = "tax(50000, 20)";
Console.WriteLine($"Tax on 50000 at 20% = {expr2.Evaluate()}");
				
			
Current default is scientific? True
2 * pi = 6.28318
Current default is scientific? False
Current default is financial? True
Tax on 50000 at 20% = 10000
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

#define tf(IsTrue) ((IsTrue) ? "True" : "False")

int main() {
   uCalc uc;
   // Setup a "Scientific" configuration
   uCalc scientificCalc;
   scientificCalc.DefineFunction("sqrt(x) = x^0.5");
   scientificCalc.DefineVariable("pi = 3.14159");

   // Setup a "Financial" configuration
   uCalc financialCalc;
   financialCalc.DefineFunction("tax(amount, rate) = amount * (rate/100)");

   // Set the scientific calculator as the default
   scientificCalc.IsDefault(true);
   cout << "Current default is scientific? " << tf(scientificCalc.IsDefault()) << endl;

   // Components that rely on the default instance now use the scientific setup.
   uCalc::Expression expr1 = "2 * pi";
   cout << "2 * pi = " << expr1.Evaluate() << endl;

   // Now, switch the default to the financial calculator
   financialCalc.IsDefault(true);
   cout << "Current default is scientific? " << tf(scientificCalc.IsDefault()) << endl; // Should be false now
   cout << "Current default is financial? " << tf(financialCalc.IsDefault()) << endl;

   // New components will use the financial setup.
   uCalc::Expression expr2 = "tax(50000, 20)";
   cout << "Tax on 50000 at 20% = " << expr2.Evaluate() << endl;
}
				
			
Current default is scientific? True
2 * pi = 6.28318
Current default is scientific? False
Current default is financial? True
Tax on 50000 at 20% = 10000
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Setup a "Scientific" configuration
      Dim scientificCalc As New uCalc()
      scientificCalc.DefineFunction("sqrt(x) = x^0.5")
      scientificCalc.DefineVariable("pi = 3.14159")
      
      '// Setup a "Financial" configuration
      Dim financialCalc As New uCalc()
      financialCalc.DefineFunction("tax(amount, rate) = amount * (rate/100)")
      
      '// Set the scientific calculator as the default
      scientificCalc.IsDefault = true
      Console.WriteLine($"Current default is scientific? {scientificCalc.IsDefault}")
      
      '// Components that rely on the default instance now use the scientific setup.
      Dim expr1 As uCalc.Expression = "2 * pi"
      Console.WriteLine($"2 * pi = {expr1.Evaluate()}")
      
      '// Now, switch the default to the financial calculator
      financialCalc.IsDefault = true
      Console.WriteLine($"Current default is scientific? {scientificCalc.IsDefault}") '// Should be false now
      Console.WriteLine($"Current default is financial? {financialCalc.IsDefault}")
      
      '// New components will use the financial setup.
      Dim expr2 As uCalc.Expression = "tax(50000, 20)"
      Console.WriteLine($"Tax on 50000 at 20% = {expr2.Evaluate()}")
   End Sub
End Module
				
			
Current default is scientific? True
2 * pi = 6.28318
Current default is scientific? False
Current default is financial? True
Tax on 50000 at 20% = 10000