From 1e915f5d49f229339f9057de2d79d6fb11dcc531 Mon Sep 17 00:00:00 2001 From: terryd Date: Thu, 9 Mar 2023 14:35:50 +0100 Subject: [PATCH] Add project files. --- Calculators.sln | 25 +++ SimpleCalculator/App.xaml | 23 +++ SimpleCalculator/App.xaml.cs | 14 ++ SimpleCalculator/AssemblyInfo.cs | 10 ++ SimpleCalculator/CalculatorMV.cs | 131 ++++++++++++++ SimpleCalculator/MainWindow.xaml | 208 +++++++++++++++++++++++ SimpleCalculator/MainWindow.xaml.cs | 24 +++ SimpleCalculator/SimpleCalculator.csproj | 14 ++ 8 files changed, 449 insertions(+) create mode 100644 Calculators.sln create mode 100644 SimpleCalculator/App.xaml create mode 100644 SimpleCalculator/App.xaml.cs create mode 100644 SimpleCalculator/AssemblyInfo.cs create mode 100644 SimpleCalculator/CalculatorMV.cs create mode 100644 SimpleCalculator/MainWindow.xaml create mode 100644 SimpleCalculator/MainWindow.xaml.cs create mode 100644 SimpleCalculator/SimpleCalculator.csproj diff --git a/Calculators.sln b/Calculators.sln new file mode 100644 index 0000000..3b7d3bf --- /dev/null +++ b/Calculators.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.33424.131 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCalculator", "SimpleCalculator\SimpleCalculator.csproj", "{700D1536-C47F-4849-BD33-C264C318C06C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {700D1536-C47F-4849-BD33-C264C318C06C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {700D1536-C47F-4849-BD33-C264C318C06C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {700D1536-C47F-4849-BD33-C264C318C06C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {700D1536-C47F-4849-BD33-C264C318C06C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7021B25D-E767-48F5-8D9B-0C58C2FAC2D6} + EndGlobalSection +EndGlobal diff --git a/SimpleCalculator/App.xaml b/SimpleCalculator/App.xaml new file mode 100644 index 0000000..4f2ba58 --- /dev/null +++ b/SimpleCalculator/App.xaml @@ -0,0 +1,23 @@ + + + + + + + diff --git a/SimpleCalculator/App.xaml.cs b/SimpleCalculator/App.xaml.cs new file mode 100644 index 0000000..58d2e4b --- /dev/null +++ b/SimpleCalculator/App.xaml.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace SimpleCalculator; +/// +/// Interaction logic for App.xaml +/// +public partial class App : Application { +} diff --git a/SimpleCalculator/AssemblyInfo.cs b/SimpleCalculator/AssemblyInfo.cs new file mode 100644 index 0000000..8b5504e --- /dev/null +++ b/SimpleCalculator/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/SimpleCalculator/CalculatorMV.cs b/SimpleCalculator/CalculatorMV.cs new file mode 100644 index 0000000..7d29a5f --- /dev/null +++ b/SimpleCalculator/CalculatorMV.cs @@ -0,0 +1,131 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; + +namespace SimpleCalculator; + +public partial class CalculatorMV : ObservableObject { + + private enum Action { + None, + Add, + Subtract, + Multiply, + Divide, + } + + private double accumulator = 0; + private (Action, double) currentAction = (Action.None, 0); + private bool isDecimal = false; + private bool isNegative = false; + private bool isResult = false; + + + [ObservableProperty] + private string resultText = "0"; + + + [RelayCommand] + private void numberClicked(string value) { + if (this.isResult) { + this.ResultText = "0"; + this.isResult = false; + this.accumulator = double.Parse(value); + } + if (value == "0" && this.ResultText == "0") + return; + if (this.ResultText == "0") + this.ResultText = ""; + else if (this.ResultText == "-0") + this.ResultText = "-"; + this.ResultText += value; + } + + [RelayCommand] + private void acClicked() { + this.accumulator = 0; + this.currentAction = (Action.None, 0); + this.isDecimal = false; + this.isNegative = false; + this.ResultText = "0"; + } + + [RelayCommand] + private void invertClicked() { + if (this.isNegative) { + this.isNegative = false; + this.ResultText = this.ResultText.Substring(1); + } else { + this.isNegative = true; + this.ResultText = "-" + this.ResultText; + } + } + + [RelayCommand] + private void percentileClicked() { + this.accumulator = double.Parse(this.ResultText); + this.accumulator *= 0.01; + this.ResultText = this.accumulator.ToString(); + } + + [RelayCommand] + private void divideClicked() { + var value = double.Parse(this.ResultText); + this.currentAction = (Action.Divide, value); + this.accumulator = value; + this.isResult = true; + } + + [RelayCommand] + private void multiplyClicked() { + var value = double.Parse(this.ResultText); + this.currentAction = (Action.Multiply, value); + this.accumulator = value; + this.isResult = true; + } + + [RelayCommand] + private void subtractClicked() { + var value = double.Parse(this.ResultText); + this.currentAction = (Action.Subtract, value); + this.accumulator = value; + this.isResult = true; + } + + [RelayCommand] + private void addClicked() { + var value = double.Parse(this.ResultText); + this.currentAction = (Action.Add, value); + this.accumulator = value; + this.isResult = true; + } + + [RelayCommand] + private void equalClicked() { + var (action, value) = this.currentAction; + switch (action) { + case Action.None: + break; + case Action.Add: + this.accumulator = value + this.accumulator; + break; + case Action.Subtract: + this.accumulator = value - this.accumulator; + break; + case Action.Multiply: + this.accumulator = value * this.accumulator; + break; + case Action.Divide: + this.accumulator = value / this.accumulator; + break; + } + this.ResultText = this.accumulator.ToString(); + this.isResult = true; + } + + [RelayCommand] + private void decimalPointClicked() { + this.isDecimal = true; + this.ResultText += ","; + } + +} diff --git a/SimpleCalculator/MainWindow.xaml b/SimpleCalculator/MainWindow.xaml new file mode 100644 index 0000000..c4a4c51 --- /dev/null +++ b/SimpleCalculator/MainWindow.xaml @@ -0,0 +1,208 @@ + + + + + + + + + + + + + + + + + + + +