Add project files.

This commit is contained in:
terryd 2023-03-09 14:35:50 +01:00
parent 02515450a5
commit 1e915f5d49
8 changed files with 449 additions and 0 deletions

25
Calculators.sln Normal file
View File

@ -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

23
SimpleCalculator/App.xaml Normal file
View File

@ -0,0 +1,23 @@
<Application x:Class="SimpleCalculator.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SimpleCalculator"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="Button" x:Key="NumberButton">
<Setter Property="FontSize" Value="32" />
<Setter Property="Margin" Value="5" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="#333" />
</Style>
<Style TargetType="Button" x:Key="OperatorButton">
<Setter Property="FontSize" Value="32" />
<Setter Property="Margin" Value="5" />
<Setter Property="Background" Value="Orange" />
</Style>
<Style TargetType="Button" x:Key="SpecialButton">
<Setter Property="FontSize" Value="32" />
<Setter Property="Margin" Value="5" />
</Style>
</Application.Resources>
</Application>

View File

@ -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;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application {
}

View File

@ -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)
)]

View File

@ -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 += ",";
}
}

View File

@ -0,0 +1,208 @@
<Window x:Class="SimpleCalculator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:SimpleCalculator"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="400">
<Window.Resources>
<vm:CalculatorMV x:Key="ViewModel" />
</Window.Resources>
<Grid DataContext="{StaticResource ViewModel}">
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
Grid.Row="0"
Grid.ColumnSpan="4"
Content="{Binding ResultText}"
Margin="10"
BorderBrush="Black"
BorderThickness="5"
Background="Green"
FontSize="56"
FontFamily="Consolas"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Right"
/>
<Button
Grid.Column="0"
Grid.Row="1"
Grid.ColumnSpan="1"
Content="AC"
Style="{StaticResource SpecialButton}"
Command="{Binding acClickedCommand}"
/>
<Button
Grid.Column="1"
Grid.Row="1"
Grid.ColumnSpan="1"
Content="±"
Style="{StaticResource SpecialButton}"
Command="{Binding invertClickedCommand}"
/>
<Button
Grid.Column="2"
Grid.Row="1"
Grid.ColumnSpan="1"
Content="%"
Style="{StaticResource SpecialButton}"
Command="{Binding percentileClickedCommand}"
/>
<Button
Grid.Column="3"
Grid.Row="1"
Grid.ColumnSpan="1"
Content="÷"
Style="{StaticResource OperatorButton}"
Command="{Binding divideClickedCommand}"
/>
<Button
Grid.Column="3"
Grid.Row="2"
Grid.ColumnSpan="1"
Content="×"
Style="{StaticResource OperatorButton}"
Command="{Binding multiplyClickedCommand}"
/>
<Button
Grid.Column="3"
Grid.Row="3"
Grid.ColumnSpan="1"
Content="-"
Style="{StaticResource OperatorButton}"
Command="{Binding subtractClickedCommand}"
/>
<Button
Grid.Column="3"
Grid.Row="4"
Grid.ColumnSpan="1"
Content="+"
Style="{StaticResource OperatorButton}"
Command="{Binding addClickedCommand}"
/>
<Button
Grid.Column="3"
Grid.Row="5"
Grid.ColumnSpan="1"
Content="="
Style="{StaticResource OperatorButton}"
Command="{Binding equalClickedCommand}"
/>
<Button
Grid.Column="0"
Grid.Row="2"
Grid.ColumnSpan="1"
Command="{Binding numberClickedCommand}"
CommandParameter="7"
Content="7"
Style="{StaticResource NumberButton}"
/>
<Button
Grid.Column="1"
Grid.Row="2"
Grid.ColumnSpan="1"
Command="{Binding numberClickedCommand}"
CommandParameter="8"
Content="8"
Style="{StaticResource NumberButton}"
/>
<Button
Grid.Column="2"
Grid.Row="2"
Grid.ColumnSpan="1"
Command="{Binding numberClickedCommand}"
CommandParameter="9"
Content="9"
Style="{StaticResource NumberButton}"
/>
<Button
Grid.Column="0"
Grid.Row="3"
Grid.ColumnSpan="1"
Command="{Binding numberClickedCommand}"
CommandParameter="4"
Content="4"
Style="{StaticResource NumberButton}"
/>
<Button
Grid.Column="1"
Grid.Row="3"
Grid.ColumnSpan="1"
Command="{Binding numberClickedCommand}"
CommandParameter="5"
Content="5"
Style="{StaticResource NumberButton}"
/>
<Button
Grid.Column="2"
Grid.Row="3"
Grid.ColumnSpan="1"
Command="{Binding numberClickedCommand}"
CommandParameter="6"
Content="6"
Style="{StaticResource NumberButton}"
/>
<Button
Grid.Column="0"
Grid.Row="4"
Grid.ColumnSpan="1"
Command="{Binding numberClickedCommand}"
CommandParameter="1"
Content="1"
Style="{StaticResource NumberButton}"
/>
<Button
Grid.Column="1"
Grid.Row="4"
Grid.ColumnSpan="1"
Command="{Binding numberClickedCommand}"
CommandParameter="2"
Content="2"
Style="{StaticResource NumberButton}"
/>
<Button
Grid.Column="2"
Grid.Row="4"
Grid.ColumnSpan="1"
Command="{Binding numberClickedCommand}"
CommandParameter="3"
Content="3"
Style="{StaticResource NumberButton}"
/>
<Button
Grid.Column="0"
Grid.Row="5"
Grid.ColumnSpan="2"
Command="{Binding numberClickedCommand}"
CommandParameter="0"
Content="0"
Style="{StaticResource NumberButton}"
/>
<Button
Grid.Column="2"
Grid.Row="5"
Grid.ColumnSpan="1"
Content=","
Style="{StaticResource NumberButton}"
Command="{Binding decimalPointClickedCommand}"
/>
</Grid>
</Window>

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SimpleCalculator;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
</ItemGroup>
</Project>