diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..191f717
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "FusionCalculator"]
+ path = FusionCalculator
+ url = https://github.com/Timerix22/FusionCalculator.git
diff --git a/FusionCalculator b/FusionCalculator
new file mode 160000
index 0000000..5907457
--- /dev/null
+++ b/FusionCalculator
@@ -0,0 +1 @@
+Subproject commit 5907457907b864c084199929b8f04d3d5fa1f3cf
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..8c5cfe5
--- /dev/null
+++ b/README.md
@@ -0,0 +1,19 @@
+# SharpCalculator
+A calculator application written in C# and Fusion.
+Using Avalonia to draw GUI.
+
+## Building
+1. ensure that [FusionCalculator](https://github.com/Timerix22/FusionCalculator) submodule is up-to-date.
+ ```shell
+ git submodule init && git submodule update
+ ```
+2. Translate FusionCalculator to C#
+ ```shell
+ cd FusionCalculator
+ ./build_cs.sh --translate-only
+ cd ..
+ ```
+3. Build graphical application
+ ```shell
+ dotnet build -c release SharpCalculator.Avalonia/SharpCalculator.Avalonia.csproj
+ ```
diff --git a/SharpCalculator.Avalonia/MainWindow.axaml b/SharpCalculator.Avalonia/MainWindow.axaml
index 37cbd60..ef684f4 100644
--- a/SharpCalculator.Avalonia/MainWindow.axaml
+++ b/SharpCalculator.Avalonia/MainWindow.axaml
@@ -9,34 +9,41 @@
FontSize="24">
4* 1* 4*
-
- = 0
+
+ =
+
* * * *
* * * * *
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
-
+
+
+
+
-
-
+
+
+
\ No newline at end of file
diff --git a/SharpCalculator.Avalonia/MainWindow.axaml.cs b/SharpCalculator.Avalonia/MainWindow.axaml.cs
index 11d21b1..3a808bd 100644
--- a/SharpCalculator.Avalonia/MainWindow.axaml.cs
+++ b/SharpCalculator.Avalonia/MainWindow.axaml.cs
@@ -1,8 +1,9 @@
using System;
+using System.Diagnostics;
using System.Globalization;
-using System.Text;
using Avalonia.Controls;
using Avalonia.Interactivity;
+using FusionCalculator;
namespace SharpCalculator.Avalonia;
@@ -10,80 +11,37 @@ public partial class MainWindow : Window
{
public MainWindow()
{
- _currentOperation = _initialOperation;
InitializeComponent();
}
- private double _prevNumber;
- private StringBuilder _currentNumberB = new();
-
- private double ParseCurrentNumber() => double.Parse(_currentNumberB.ToString(), CultureInfo.InvariantCulture);
-
- readonly Func _initialOperation = (_, firstN) => firstN;
- private Func _currentOperation;
-
- private void NewOperation(Func operation, Func operationText)
- {
- // operation replacement
- if (_currentNumberB.Length == 0)
- Input.Redo();
- else
- {
- _prevNumber = _currentOperation( _prevNumber, ParseCurrentNumber());
- Output.Text = _prevNumber.ToString(CultureInfo.InvariantCulture);
- _currentNumberB.Clear();
- }
- Input.Text += operationText();
- _currentOperation = operation;
- }
-
- private void NumberButton_OnClick(object? sender, RoutedEventArgs e)
+ private void MathButton_OnClick(object? sender, RoutedEventArgs e)
{
if (sender is not Button button)
throw new Exception();
-
string text = button.Content!.ToString()!;
Input.Text += text;
- _currentNumberB.Append(text);
- Output.Text = "= " + _currentOperation(_prevNumber, ParseCurrentNumber()).ToString(CultureInfo.InvariantCulture);
- }
-
- private void OperationButton_OnClick(object? sender, RoutedEventArgs e)
- {
- if (sender is not Button button)
- throw new Exception();
- string text = button.Content?.ToString()!;
- switch (text)
- {
- case "+":
- NewOperation((a, b) => a + b, () => text);
- break;
- case "-":
- NewOperation((a, b) => a - b, () => text);
- break;
- case "*":
- NewOperation((a, b) => a * b, () => text);
- break;
- case "/":
- NewOperation((a, b) => a / b, () => text);
- break;
- case "^":
- NewOperation(Math.Pow, () => text);
- break;
- case "=":
- NewOperation((_, newNumber) => newNumber, () => $"={_prevNumber}\n");
- break;
- default:
- throw new Exception("incorrect button text: ");
- }
}
private void ClearButton_OnClick(object? sender, RoutedEventArgs e)
{
- _prevNumber = 0;
- _currentOperation = _initialOperation;
- _currentNumberB.Clear();
Input.Text = "";
- Output.Text = "= 0";
+ Output.Text = "";
+ }
+
+ private void Input_OnTextChanged(object? sender, TextChangedEventArgs e)
+ {
+ if(Input.Text == null)
+ return;
+ string exprStr = Input.Text;
+ try
+ {
+ double rezult = Calculator.Calculate(exprStr);
+ if(!double.IsNaN(rezult))
+ Output.Text = rezult.ToString(CultureInfo.InvariantCulture);
+ }
+ catch (Exception exception)
+ {
+ Debug.WriteLine(exception.ToString());
+ }
}
}
\ No newline at end of file
diff --git a/SharpCalculator.Avalonia/Program.cs b/SharpCalculator.Avalonia/Program.cs
index 85f1a38..84edef4 100644
--- a/SharpCalculator.Avalonia/Program.cs
+++ b/SharpCalculator.Avalonia/Program.cs
@@ -1,5 +1,6 @@
using Avalonia;
using System;
+using System.Globalization;
namespace SharpCalculator.Avalonia;
@@ -9,8 +10,12 @@ class Program
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
- public static void Main(string[] args) => BuildAvaloniaApp()
- .StartWithClassicDesktopLifetime(args);
+ public static void Main(string[] args)
+ {
+ CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
+ BuildAvaloniaApp()
+ .StartWithClassicDesktopLifetime(args);
+ }
// Avalonia configuration, don't remove; also used by visual designer.
private static AppBuilder BuildAvaloniaApp()
diff --git a/SharpCalculator.Avalonia/SharpCalculator.Avalonia.csproj b/SharpCalculator.Avalonia/SharpCalculator.Avalonia.csproj
index 5b6e471..8e1831e 100644
--- a/SharpCalculator.Avalonia/SharpCalculator.Avalonia.csproj
+++ b/SharpCalculator.Avalonia/SharpCalculator.Avalonia.csproj
@@ -10,9 +10,13 @@
-
-
-
-
+
+
+
+
+
+
+
+
diff --git a/SharpCalculator.sln b/SharpCalculator.sln
index 353d5f3..a1c997e 100644
--- a/SharpCalculator.sln
+++ b/SharpCalculator.sln
@@ -2,7 +2,14 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpCalculator.Avalonia", "SharpCalculator.Avalonia\SharpCalculator.Avalonia.csproj", "{93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpCalculator", "SharpCalculator\SharpCalculator.csproj", "{F41EEE99-5319-4AAF-BB10-C82FE4E0557B}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FusionCalculator", "FusionCalculator\FusionCalculator.csproj", "{8EA39217-DEBD-493B-A19C-5505B58E03FA}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution_items", "solution_items", "{381D7A67-4F32-4BE7-AA83-65E338E1819F}"
+ ProjectSection(SolutionItems) = preProject
+ .gitignore = .gitignore
+ .gitmodules = .gitmodules
+ README.md = README.md
+ EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -14,9 +21,9 @@ Global
{93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}.Release|Any CPU.Build.0 = Release|Any CPU
- {F41EEE99-5319-4AAF-BB10-C82FE4E0557B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {F41EEE99-5319-4AAF-BB10-C82FE4E0557B}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {F41EEE99-5319-4AAF-BB10-C82FE4E0557B}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {F41EEE99-5319-4AAF-BB10-C82FE4E0557B}.Release|Any CPU.Build.0 = Release|Any CPU
+ {8EA39217-DEBD-493B-A19C-5505B58E03FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {8EA39217-DEBD-493B-A19C-5505B58E03FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {8EA39217-DEBD-493B-A19C-5505B58E03FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {8EA39217-DEBD-493B-A19C-5505B58E03FA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
diff --git a/SharpCalculator/IExpression.cs b/SharpCalculator/IExpression.cs
deleted file mode 100644
index 44bf625..0000000
--- a/SharpCalculator/IExpression.cs
+++ /dev/null
@@ -1,6 +0,0 @@
-namespace SharpCalculator;
-
-public interface IExpression
-{
- public double Calculate();
-}
\ No newline at end of file
diff --git a/SharpCalculator/NumericExpression.cs b/SharpCalculator/NumericExpression.cs
deleted file mode 100644
index 204300a..0000000
--- a/SharpCalculator/NumericExpression.cs
+++ /dev/null
@@ -1,13 +0,0 @@
-namespace SharpCalculator;
-
-public class NumericExpression : IExpression
-{
- private double n;
-
- public NumericExpression(double n)
- {
- this.n = n;
- }
-
- public double Calculate() => n;
-}
\ No newline at end of file
diff --git a/SharpCalculator/SharpCalculator.csproj b/SharpCalculator/SharpCalculator.csproj
deleted file mode 100644
index e1fafe4..0000000
--- a/SharpCalculator/SharpCalculator.csproj
+++ /dev/null
@@ -1,10 +0,0 @@
-
-
-
- netstandard2.0
- 12
- disable
- enable
-
-
-
diff --git a/nuget.config b/nuget.config
deleted file mode 100644
index 42f932e..0000000
--- a/nuget.config
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file