Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculator_桂昕_无34 #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions MAUICalculator/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@
x:Class="MAUICalculator.App">
<Application.Resources>
<ResourceDictionary>

<Color x:Key="ButtonLight">#6FFF</Color>
<Color x:Key="ButtonLight2">White</Color>
<Color x:Key="ButtonDark">#2777</Color>
<Color x:Key="ButtonDark2">#4777</Color>
<Color x:Key="EqualDark">#58CEFF</Color>

<Style TargetType="Button">
<Setter Property="Background" Value="{AppThemeBinding Light={StaticResource ButtonLight}, Dark={StaticResource ButtonDark}}" />
<Setter Property="FontSize" Value="Small" />
</Style>

<Style TargetType="Button" Class="Number">
<Setter Property="Background" Value="{AppThemeBinding Light={StaticResource ButtonLight2}, Dark={StaticResource ButtonDark2}}" />
</Style>

<Style TargetType="Button" Class="Operator">
<Setter Property="FontFamily" Value="NCMMathBook" />
</Style>

<Style TargetType="Button" Class="Equal">
<Setter Property="Background" Value="{AppThemeBinding Light={StaticResource Magenta}, Dark={StaticResource EqualDark}}" />
<Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource White}, Dark={StaticResource PrimaryDarkText}}" />
<Setter Property="FontFamily" Value="NCMMathBook" />
</Style>

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
Expand Down
16 changes: 16 additions & 0 deletions MAUICalculator/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,21 @@ public App()

MainPage = new AppShell();
}

protected override Window CreateWindow(IActivationState? activationState)
{
var window = base.CreateWindow(activationState);

if (MainPage != null)
{
var width = (int)MainPage.WidthRequest;
var height = (int)MainPage.HeightRequest;

window.Width = width;
window.Height = height;
}

return window;
}
}
}
12 changes: 7 additions & 5 deletions MAUICalculator/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MAUICalculator"
Shell.FlyoutBehavior="Disabled"
Title="MAUICalculator">
Title="MAUICalculator"
WidthRequest="420"
HeightRequest="540"
Shell.TabBarBackgroundColor="Transparent"
BackgroundColor="Transparent">
<TabBar>
<Tab Title="Basic Calculator">
<Tab Title="Basic">
<ShellContent
Title="Basic Calculator"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />
</Tab>
<Tab Title="Advanced Calculator">
<Tab Title="Advanced">
<ShellContent
Title="Advanced Calculator"
ContentTemplate="{DataTemplate local:SubPage}"
Route="SubPage" />
</Tab>
Expand Down
21 changes: 21 additions & 0 deletions MAUICalculator/Data.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MAUICalculator
{
public static class CalculatorData
{
// 定义一些变量来存储当前输入的数字,当前选择的运算符,以及上一次计算的结果
public static double currentNumber = 0;
public static bool currentNumberDied = false;
public static double lastNumber = 0;
public static string currentOperator = "";
public static bool isResult = false;
public static string displayText = "0";
public static string historyText = "";

}
}
5 changes: 4 additions & 1 deletion MAUICalculator/MAUICalculator.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
Expand Down Expand Up @@ -56,6 +55,10 @@
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
<None Remove="Resources\Fonts\NewCMMath-Book-Modified.ttf" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
Expand Down
58 changes: 33 additions & 25 deletions MAUICalculator/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MAUICalculator.MainPage">
x:Class="MAUICalculator.MainPage"
Background="Transparent"
BackgroundColor="Transparent">

<!-- 使用Grid作为根布局 -->
<Grid RowSpacing="6" ColumnSpacing="6" Margin="20">
<Grid RowSpacing="2" ColumnSpacing="2" Margin="4">

<!-- 设置六行四列的网格 -->
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
Expand All @@ -24,30 +26,36 @@
</Grid.ColumnDefinitions>

<!-- 使用Label显示计算结果,并占据第一行四列 -->
<Label x:Name="displayLabel" Text="0" FontSize="Large" HorizontalTextAlignment="End" Grid.Row="0" Grid.ColumnSpan="4" />
<VerticalStackLayout Grid.Row="0" Grid.ColumnSpan="4" Margin="0,10,10,0">
<Label x:Name="historyLabel" Text=" " FontSize="Micro" MinimumHeightRequest="25"
TextColor="{StaticResource Gray400}"
HorizontalTextAlignment="End" />
<Label x:Name="displayLabel" Text="0" FontSize="44" HorizontalTextAlignment="End" FontAttributes="Bold" LineHeight="1" />
</VerticalStackLayout>

<!-- 使用Button创建键盘按钮,并占据特定的Grid位置,设置Clicked事件处理程序 -->
<Button Text="AC" Background="LightSkyBlue" Clicked="OnClearClicked" Grid.Row="1" Grid.Column="0" Grid.RowSpan="1" Grid.ColumnSpan="3"/>
<Button Text="/" Background="LightSkyBlue" Clicked="OnOperatorClicked" Grid.Row="1" Grid.Column="3"/>

<Button Text="7" Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="2" Grid.Column="0"/>
<Button Text="8" Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="2" Grid.Column="1"/>
<Button Text="9" Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="2" Grid.Column="2"/>
<Button Text="*" Background="LightSkyBlue" Clicked="OnOperatorClicked" Grid.Row="2" Grid.Column="3"/>

<Button Text="4" Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="3" Grid.Column="0"/>
<Button Text="5" Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="3" Grid.Column="1"/>
<Button Text="6" Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="3" Grid.Column="2"/>
<Button Text="-" Background="LightSkyBlue" Clicked="OnOperatorClicked" Grid.Row="3" Grid.Column="3"/>

<Button Text="1" Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="4" Grid.Column="0"/>
<Button Text="2" Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="4" Grid.Column="1"/>
<Button Text="3" Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="4" Grid.Column="2"/>
<Button Text="+" Background="LightSkyBlue" Clicked="OnOperatorClicked" Grid.Row="4" Grid.Column="3"/>

<Button Text="0" Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2"/>
<Button Text="." Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="5" Grid.Column="2"/>
<Button Text="=" Background="LightSkyBlue" Clicked="OnEqualClicked" Grid.Row="5" Grid.Column="3"/>
<Button Text="AC" Clicked="OnClearClicked" FontSize="Micro" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"/>
<Button Text="DEL" Clicked="OnDeleteClicked" FontSize="Micro" Grid.Row="1" Grid.Column="2"/>
<Button Text="÷" Clicked="OnOperatorClicked" Grid.Row="1" Grid.Column="3" StyleClass="Operator"/>

<Button Text="7" Clicked="OnNumberClicked" Grid.Row="2" Grid.Column="0" StyleClass="Number"/>
<Button Text="8" Clicked="OnNumberClicked" Grid.Row="2" Grid.Column="1" StyleClass="Number"/>
<Button Text="9" Clicked="OnNumberClicked" Grid.Row="2" Grid.Column="2" StyleClass="Number"/>
<Button Text="×" Clicked="OnOperatorClicked" Grid.Row="2" Grid.Column="3" StyleClass="Operator"/>

<Button Text="4" Clicked="OnNumberClicked" Grid.Row="3" Grid.Column="0" StyleClass="Number"/>
<Button Text="5" Clicked="OnNumberClicked" Grid.Row="3" Grid.Column="1" StyleClass="Number"/>
<Button Text="6" Clicked="OnNumberClicked" Grid.Row="3" Grid.Column="2" StyleClass="Number"/>
<Button Text="−" Clicked="OnOperatorClicked" Grid.Row="3" Grid.Column="3" StyleClass="Operator"/>

<Button Text="1" Clicked="OnNumberClicked" Grid.Row="4" Grid.Column="0" StyleClass="Number"/>
<Button Text="2" Clicked="OnNumberClicked" Grid.Row="4" Grid.Column="1" StyleClass="Number"/>
<Button Text="3" Clicked="OnNumberClicked" Grid.Row="4" Grid.Column="2" StyleClass="Number"/>
<Button Text="+" Clicked="OnOperatorClicked" Grid.Row="4" Grid.Column="3" StyleClass="Operator"/>

<Button Text="0" Clicked="OnNumberClicked" Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" StyleClass="Number"/>
<Button Text="." Clicked="OnNumberClicked" Grid.Row="5" Grid.Column="2" StyleClass="Number"/>
<Button Text="=" Clicked="OnEqualClicked" Grid.Row="5" Grid.Column="3" StyleClass="Equal"/>

</Grid>

Expand Down
112 changes: 93 additions & 19 deletions MAUICalculator/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,78 @@
{
public partial class MainPage : ContentPage
{
int count = 0;

public static MainPage? Current;

public MainPage()
{
InitializeComponent();
Current = this;
}

~MainPage()
{
if (Current == this)
Current = null;
}

// 定义一些变量来存储当前输入的数字,当前选择的运算符,以及上一次计算的结果
private double currentNumber = 0;
private bool currentNumberDied = false;
private double lastNumber = 0;
private string currentOperator = "";
private bool isResult = false;

public void LoadFromData()
{
// 同步两个页面的数据
currentNumber = CalculatorData.currentNumber;
currentNumberDied = CalculatorData.currentNumberDied;
lastNumber = CalculatorData.lastNumber;
currentOperator = CalculatorData.currentOperator;
isResult = CalculatorData.isResult;
displayLabel.Text = CalculatorData.displayText;
historyLabel.Text = CalculatorData.historyText;

}

protected override void OnDisappearing()
{
base.OnDisappearing();

// 同步两个页面的数据
CalculatorData.currentNumber = currentNumber;
CalculatorData.currentNumberDied = currentNumberDied;
CalculatorData.lastNumber = lastNumber;
CalculatorData.currentOperator = currentOperator;
CalculatorData.isResult = isResult;
CalculatorData.displayText = displayLabel.Text;
CalculatorData.historyText = historyLabel.Text;

SubPage.Current?.LoadFromData();

}

// 定义OnNumberClicked方法来处理数字按钮点击事件
private void OnNumberClicked(object sender, EventArgs e)
{
// 获取按钮的文本值
var button = sender as Button;
var number = button.Text;

// 如果当前显示的是结果,或者是0,就清空显示屏
if (isResult || displayLabel.Text == "0")
// 如果当前显示的是结果,或者是0,或者是旧输入的数字,就清空显示屏
if (isResult || displayLabel.Text == "0" || currentNumberDied)
{
displayLabel.Text = "";
if (number == ".")
displayLabel.Text = "0";
isResult = false;
currentNumberDied = false;
}

// 将数字追加到显示屏,并更新当前输入的数字
displayLabel.Text += number;
//currentOperator = "";
currentNumber = double.Parse(displayLabel.Text);
}

Expand All @@ -43,46 +84,63 @@ private void OnOperatorClicked(object sender, EventArgs e)
var button = sender as Button;
var op = button.Text;

// 如果当前的运算符不为空,就执行上一次选择的运算,并显示结果
// 如果当前的运算符不为空
if (currentOperator != "")
{
Calculate();
displayLabel.Text = lastNumber.ToString();
isResult = true;
// 如果未输入数字,只改变运算符
if (isResult || displayLabel.Text == "0" || currentNumberDied)
{
// Do nothing.
}
// 如果已输入数字,就执行上一次选择的运算,并显示结果
else
{
Calculate();
currentNumber = lastNumber;
}
}
else

// 如果当前的运算符为空,就将当前输入的数字赋值给上一次计算的结果
if (currentOperator == "")
{
// 否则,就将当前输入的数字赋值给上一次计算的结果
lastNumber = currentNumber;
displayLabel.Text = "0";
isResult = false;
lastNumber = currentNumber;
isResult = false;

}

// 将当前选择的运算符赋值给变量,并清空当前输入的数字
// 将当前选择的运算符赋值给变量,并预备清空当前输入的数字
currentOperator = op;
currentNumberDied = true;

// 更新historyLabel显示当前的计算过程
historyLabel.Text = $"{lastNumber} {currentOperator} ";
}

// 定义OnEqualClicked方法来处理等号按钮点击事件
private void OnEqualClicked(object sender, EventArgs e)
{

// 如果当前选择的运算符不为空,就执行上一次选择的运算,并显示结果
if (currentOperator != "")
{
// 更新historyLabel显示当前的计算过程
historyLabel.Text = $"{lastNumber} {currentOperator} {currentNumber} = ";

Calculate();
displayLabel.Text = lastNumber.ToString();
isResult = true;
currentOperator = "";
}
}

// 定义OnEqualClicked方法来处理等号按钮点击事件
// 定义OnClearClicked方法来处理清除按钮点击事件
private void OnClearClicked(object sender, EventArgs e)
{
currentNumber = 0;
lastNumber = 0;
currentOperator = "";
isResult = false;
displayLabel.Text = lastNumber.ToString();
historyLabel.Text = "";
}

// 定义Calculate方法来执行运算逻辑
Expand All @@ -94,20 +152,36 @@ private void Calculate()
case "+":
lastNumber += currentNumber;
break;
case "-":
case "":
lastNumber -= currentNumber;
break;
case "*":
case "×":
lastNumber *= currentNumber;
break;
case "/":
case "÷":
lastNumber /= currentNumber;
break;
default:
break;
}
lastNumber = Math.Round(lastNumber, 4);
currentNumber = lastNumber;
// currentNumber = lastNumber;
}

// 定义OnDeleteClicked方法来处理删除按钮点击事件
private void OnDeleteClicked(object sender, EventArgs e)
{
// 当上一个字符为"="时,清空显示屏但不改变所存储的计算结果
if (isResult)
{
currentNumber = double.Parse(displayLabel.Text);
historyLabel.Text = displayLabel.Text;
currentOperator = "";
isResult = false;
}
else
currentNumber = 0;
displayLabel.Text = "0";
}
}

Expand Down
Loading