uCalc API Version: 5.7.0-preview.1 Released: 7/30/2026
Warning
uCalc API Preview Release Notice:This preview documentation describes the intended behavior of the API. It is not fully accurate or complete.The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.Use of the preview version in your production code is not recommended.
A step-by-step tutorial on creating a functional calculator application, using uCalc's expression parser to handle the evaluation logic.
This project brings together the core concepts of the uCalc expression parser to build a familiar, practical application: a basic calculator. You'll see how to handle user input from a simple UI and let uCalc do all the heavy lifting of parsing and calculation with a single function call.
This tutorial is UI-framework-agnostic. Whether you're using WinForms, WPF, Qt, or a web framework, the core logic for interfacing with uCalc remains the same.
Every calculator needs a basic set of components:
display_text.0 through 9.+, -, *, /.=) button to trigger the calculation.C) button to reset the display.The layout would look something like a standard pocket calculator.
The logic behind the UI is surprisingly simple. We'll manage a single string variable that represents the content of the calculator's display. The example below will simulate this process.
0-9, +, -, *, /)When a user clicks any of these buttons, the action is the same: append the button's character to the display_text string.
"123", pressing + changes it to "123+"."123+", pressing 4 changes it to "123+4".C)When the 'C' button is clicked, simply clear the display_text string to an empty string ("").
=)This is where uCalc shines. When the user presses the = button, all you need to do is:
display_text.EvalStr and set it as the new content of display_text.That's it! uCalc handles everything else:
"5 * + 3"), EvalStr will return a descriptive error message (e.g., "Syntax error"), which you can display directly to the user.How would you do this without uCalc? You would need to build a complete parsing engine yourself. This typically involves complex algorithms like:
5 + 2) into a postfix (Reverse Polish Notation) expression (5 2 +).This can be hundreds of lines of complex, error-prone code. uCalc reduces this entire complex problem to a single line: result = uc.EvalStr(expression).
The following example simulates the complete logic of the calculator's "brain". It shows how a string is built up from simulated button presses and then evaluated by uCalc.
ID: 1365
using uCalcSoftware;
var uc = new uCalc();
string display_text = "";
// Simulate user pressing buttons: 1, 2, 3, +, 4, 5, 6
display_text = display_text + "123";
Console.WriteLine($"Display: {display_text}");
display_text = display_text + "+";
Console.WriteLine($"Display: {display_text}");
display_text = display_text + "456";
Console.WriteLine($"Display: {display_text}");
// Simulate pressing '='. This is where uCalc does the work.
var result = uc.EvalStr(display_text);
display_text = result;
Console.WriteLine($"Result: {display_text}");
Console.WriteLine("");
// Simulate a new calculation with an error
display_text = "5 * / 3";
Console.WriteLine($"Display: {display_text}");
result = uc.EvalStr(display_text);
display_text = result;
Console.WriteLine($"Result: {display_text}");
Console.WriteLine("");
// Simulate clearing the display
display_text = "C"; // Let's say 'C' is a special command
if (display_text == "C") {
display_text = "";
}
Console.WriteLine($"Display after clear: '{display_text}'");
Display: 123
Display: 123+
Display: 123+456
Result: 579
Display: 5 * / 3
Result: Syntax error
Display after clear: '' using uCalcSoftware; var uc = new uCalc(); string display_text = ""; // Simulate user pressing buttons: 1, 2, 3, +, 4, 5, 6 display_text = display_text + "123"; Console.WriteLine($"Display: {display_text}"); display_text = display_text + "+"; Console.WriteLine($"Display: {display_text}"); display_text = display_text + "456"; Console.WriteLine($"Display: {display_text}"); // Simulate pressing '='. This is where uCalc does the work. var result = uc.EvalStr(display_text); display_text = result; Console.WriteLine($"Result: {display_text}"); Console.WriteLine(""); // Simulate a new calculation with an error display_text = "5 * / 3"; Console.WriteLine($"Display: {display_text}"); result = uc.EvalStr(display_text); display_text = result; Console.WriteLine($"Result: {display_text}"); Console.WriteLine(""); // Simulate clearing the display display_text = "C"; // Let's say 'C' is a special command if (display_text == "C") { display_text = ""; } Console.WriteLine($"Display after clear: '{display_text}'");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
string display_text = "";
// Simulate user pressing buttons: 1, 2, 3, +, 4, 5, 6
display_text = display_text + "123";
cout << "Display: " << display_text << endl;
display_text = display_text + "+";
cout << "Display: " << display_text << endl;
display_text = display_text + "456";
cout << "Display: " << display_text << endl;
// Simulate pressing '='. This is where uCalc does the work.
auto result = uc.EvalStr(display_text);
display_text = result;
cout << "Result: " << display_text << endl;
cout << "" << endl;
// Simulate a new calculation with an error
display_text = "5 * / 3";
cout << "Display: " << display_text << endl;
result = uc.EvalStr(display_text);
display_text = result;
cout << "Result: " << display_text << endl;
cout << "" << endl;
// Simulate clearing the display
display_text = "C"; // Let's say 'C' is a special command
if (display_text == "C") {
display_text = "";
}
cout << "Display after clear: '" << display_text << "'" << endl;
}
Display: 123
Display: 123+
Display: 123+456
Result: 579
Display: 5 * / 3
Result: Syntax error
Display after clear: '' #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; string display_text = ""; // Simulate user pressing buttons: 1, 2, 3, +, 4, 5, 6 display_text = display_text + "123"; cout << "Display: " << display_text << endl; display_text = display_text + "+"; cout << "Display: " << display_text << endl; display_text = display_text + "456"; cout << "Display: " << display_text << endl; // Simulate pressing '='. This is where uCalc does the work. auto result = uc.EvalStr(display_text); display_text = result; cout << "Result: " << display_text << endl; cout << "" << endl; // Simulate a new calculation with an error display_text = "5 * / 3"; cout << "Display: " << display_text << endl; result = uc.EvalStr(display_text); display_text = result; cout << "Result: " << display_text << endl; cout << "" << endl; // Simulate clearing the display display_text = "C"; // Let's say 'C' is a special command if (display_text == "C") { display_text = ""; } cout << "Display after clear: '" << display_text << "'" << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim display_text As String = ""
'// Simulate user pressing buttons: 1, 2, 3, +, 4, 5, 6
display_text = display_text + "123"
Console.WriteLine($"Display: {display_text}")
display_text = display_text + "+"
Console.WriteLine($"Display: {display_text}")
display_text = display_text + "456"
Console.WriteLine($"Display: {display_text}")
'// Simulate pressing '='. This is where uCalc does the work.
Dim result = uc.EvalStr(display_text)
display_text = result
Console.WriteLine($"Result: {display_text}")
Console.WriteLine("")
'// Simulate a new calculation with an error
display_text = "5 * / 3"
Console.WriteLine($"Display: {display_text}")
result = uc.EvalStr(display_text)
display_text = result
Console.WriteLine($"Result: {display_text}")
Console.WriteLine("")
'// Simulate clearing the display
display_text = "C" '// Let's say 'C' is a special command
If display_text = "C" Then
display_text = ""
End If
Console.WriteLine($"Display after clear: '{display_text}'")
End Sub
End Module
Display: 123
Display: 123+
Display: 123+456
Result: 579
Display: 5 * / 3
Result: Syntax error
Display after clear: '' Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim display_text As String = "" '// Simulate user pressing buttons: 1, 2, 3, +, 4, 5, 6 display_text = display_text + "123" Console.WriteLine($"Display: {display_text}") display_text = display_text + "+" Console.WriteLine($"Display: {display_text}") display_text = display_text + "456" Console.WriteLine($"Display: {display_text}") '// Simulate pressing '='. This is where uCalc does the work. Dim result = uc.EvalStr(display_text) display_text = result Console.WriteLine($"Result: {display_text}") Console.WriteLine("") '// Simulate a new calculation with an error display_text = "5 * / 3" Console.WriteLine($"Display: {display_text}") result = uc.EvalStr(display_text) display_text = result Console.WriteLine($"Result: {display_text}") Console.WriteLine("") '// Simulate clearing the display display_text = "C" '// Let's say 'C' is a special command If display_text = "C" Then display_text = "" End If Console.WriteLine($"Display after clear: '{display_text}'") End Sub End Module