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_胡纪亨_无35 #9

Open
wants to merge 11 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
75 changes: 75 additions & 0 deletions MAUICalculator/AdvancedCalculatorPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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="Calculator.AdvancedCalculatorPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!-- Display Label -->
<Label x:Name="displayLabel"
Grid.Row="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="End"
FontSize="32"
TextColor="Black"
BackgroundColor="LightGray"
Padding="10"
HorizontalTextAlignment="End"
VerticalTextAlignment="Center"/>

<!-- Button Grid -->
<Grid Grid.Row="1"
ColumnSpacing="10"
RowSpacing="10"
Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<!-- Number Buttons -->
<Button Text="7" Grid.Row="0" Grid.Column="0" Clicked="OnNumberClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="8" Grid.Row="0" Grid.Column="1" Clicked="OnNumberClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="9" Grid.Row="0" Grid.Column="2" Clicked="OnNumberClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="/" Grid.Row="0" Grid.Column="3" Clicked="OnOperatorClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>

<Button Text="4" Grid.Row="1" Grid.Column="0" Clicked="OnNumberClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="5" Grid.Row="1" Grid.Column="1" Clicked="OnNumberClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="6" Grid.Row="1" Grid.Column="2" Clicked="OnNumberClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="*" Grid.Row="1" Grid.Column="3" Clicked="OnOperatorClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>

<Button Text="1" Grid.Row="2" Grid.Column="0" Clicked="OnNumberClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="2" Grid.Row="2" Grid.Column="1" Clicked="OnNumberClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="3" Grid.Row="2" Grid.Column="2" Clicked="OnNumberClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="-" Grid.Row="2" Grid.Column="3" Clicked="OnOperatorClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>

<Button Text="0" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Clicked="OnNumberClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="." Grid.Row="3" Grid.Column="2" Clicked="OnDotClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="+" Grid.Row="3" Grid.Column="3" Clicked="OnOperatorClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>

<!-- Additional Buttons -->
<Button Text="AC" Grid.Row="4" Grid.Column="0" Clicked="OnACClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="DEL" Grid.Row="4" Grid.Column="1" Clicked="OnDELClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="=" Grid.Row="4" Grid.Column="2" Grid.ColumnSpan="2" Clicked="OnEqualsClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>

<!-- Advanced Operations -->
<Button Text="lg" Grid.Row="5" Grid.Column="0" Clicked="OnAdvancedOperatorClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="sqrt" Grid.Row="5" Grid.Column="1" Clicked="OnAdvancedOperatorClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="!" Grid.Row="5" Grid.Column="2" Clicked="OnAdvancedOperatorClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
<Button Text="π" Grid.Row="5" Grid.Column="3" Clicked="OnAdvancedOperatorClicked" BackgroundColor="#ADD8E6" TextColor="Black" CornerRadius="5" Padding="10"/>
</Grid>
</Grid>
</ContentPage>
176 changes: 176 additions & 0 deletions MAUICalculator/AdvancedCalculatorPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using System;
using Microsoft.Maui.Controls;

namespace Calculator
{
public partial class AdvancedCalculatorPage : ContentPage
{
private string _currentInput = "";
private string _lastOperator = "";
private double _lastNumber = 0;
private bool _isLastInputOperator = false;

public AdvancedCalculatorPage()
{
InitializeComponent();
}

private void OnNumberClicked(object sender, EventArgs e)
{
var button = sender as Button;
if (button != null)
{
if (_isLastInputOperator)
{
_currentInput = "";
_isLastInputOperator = false;
}
_currentInput += button.Text;
UpdateDisplay();
}
}

private void OnOperatorClicked(object sender, EventArgs e)
{
var button = sender as Button;
if (button != null)
{
if (_currentInput != "")
{
if (_lastNumber != 0 && !_isLastInputOperator)
{
CalculateResult();
}
else
{
_lastNumber = double.Parse(_currentInput);
}
_lastOperator = button.Text;
_currentInput = "";
_isLastInputOperator = true;
}
}
}

private void OnAdvancedOperatorClicked(object sender, EventArgs e)
{
var button = sender as Button;
if (button != null && _currentInput != "")
{
double result = 0;
try
{
if (button.Text == "lg")
{
result = Math.Log10(double.Parse(_currentInput));
}
else if (button.Text == "sqrt")
{
result = Math.Sqrt(double.Parse(_currentInput));
}
else if (button.Text == "!")
{
result = Factorial(double.Parse(_currentInput));
}
else if (button.Text == "π")
{
result = Math.PI;
}
_currentInput = result.ToString();
UpdateDisplay();
}
catch (Exception ex)
{
DisplayAlert("Error", ex.Message, "OK");
}
}
}

private void OnACClicked(object sender, EventArgs e)
{
_currentInput = "";
_lastNumber = 0;
_lastOperator = "";
_isLastInputOperator = false;
UpdateDisplay();
}

private void OnDELClicked(object sender, EventArgs e)
{
if (_currentInput.Length > 0)
{
_currentInput = _currentInput.Substring(0, _currentInput.Length - 1);
UpdateDisplay();
}
}

private void OnEqualsClicked(object sender, EventArgs e)
{
if (_currentInput != "")
{
CalculateResult();
_lastOperator = "";
_currentInput = _lastNumber.ToString();
_lastNumber = 0;
UpdateDisplay();
}
}

private void OnDotClicked(object sender, EventArgs e)
{
if (!_currentInput.Contains("."))
{
_currentInput += ".";
UpdateDisplay();
}
}

private void UpdateDisplay()
{
displayLabel.Text = _currentInput;
}

private void CalculateResult()
{
double result = 0;
try
{
double currentNumber = double.Parse(_currentInput);
switch (_lastOperator)
{
case "+":
result = _lastNumber + currentNumber;
break;
case "-":
result = _lastNumber - currentNumber;
break;
case "*":
result = _lastNumber * currentNumber;
break;
case "/":
result = _lastNumber / currentNumber;
break;
}
_lastNumber = result;
_currentInput = result.ToString();
_isLastInputOperator = false;
}
catch (Exception ex)
{
DisplayAlert("Error", ex.Message, "OK");
}
}

private double Factorial(double number)
{
if (number < 0) throw new ArgumentException("Factorial is not defined for negative numbers.");
if (number == 0 || number == 1) return 1;
double result = 1;
for (int i = 2; i <= number; i++)
{
result *= i;
}
return result;
}
}
}
6 changes: 3 additions & 3 deletions MAUICalculator/App.xaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MAUICalculator"
x:Class="MAUICalculator.App">
xmlns:local="clr-namespace:Calculator"
x:Class="Calculator.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
2 changes: 1 addition & 1 deletion MAUICalculator/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MAUICalculator
namespace Calculator
{
public partial class App : Application
{
Expand Down
50 changes: 27 additions & 23 deletions MAUICalculator/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="MAUICalculator.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MAUICalculator"
Shell.FlyoutBehavior="Disabled"
Title="MAUICalculator">
<TabBar>
<Tab Title="Basic Calculator">
<ShellContent
Title="Basic Calculator"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />
</Tab>
<Tab Title="Advanced Calculator">
<ShellContent
Title="Advanced Calculator"
ContentTemplate="{DataTemplate local:SubPage}"
Route="SubPage" />
</Tab>
</TabBar>
</Shell>
<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Calculator"
x:Class="Calculator.AppShell">

<!-- Define the Flyout Menu -->
<Shell.FlyoutHeader>
<Label Text="Calculator App" FontSize="24" HorizontalOptions="Center" Padding="10"/>
</Shell.FlyoutHeader>

<!-- Define Flyout Items (Menu) -->
<ShellItem Route="main">
<ShellContent Title="Basic Calculator" ContentTemplate="{DataTemplate local:MainPage}" />
</ShellItem>

<ShellItem Route="advanced">
<ShellContent Title="Advanced Calculator" ContentTemplate="{DataTemplate local:AdvancedCalculatorPage}" />
</ShellItem>

<!-- Add a Flyout Menu with navigation -->
<FlyoutItem Title="Navigation">
<ShellContent Route="main" Title="Basic Calculator" ContentTemplate="{DataTemplate local:MainPage}" />
<ShellContent Route="advanced" Title="Advanced Calculator" ContentTemplate="{DataTemplate local:AdvancedCalculatorPage}" />
</FlyoutItem>

</Shell>
2 changes: 1 addition & 1 deletion MAUICalculator/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MAUICalculator
namespace Calculator
{
public partial class AppShell : Shell
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
Expand All @@ -14,17 +14,17 @@
<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->

<OutputType>Exe</OutputType>
<RootNamespace>MAUICalculator</RootNamespace>
<RootNamespace>Calculator</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<!-- Display name -->
<ApplicationTitle>MAUICalculator</ApplicationTitle>
<ApplicationTitle>Calculator</ApplicationTitle>

<!-- App Identifier -->
<ApplicationId>com.companyname.mauicalculator</ApplicationId>
<ApplicationId>com.companyname.calculator</ApplicationId>

<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
Expand Down Expand Up @@ -54,22 +54,23 @@

<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />

</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<Compile Include="D:\db\Calculator\Calculator\AdvancedCalculatorPage.xaml.cs" Link="AdvancedCalculatorPage.xaml.cs">
<DependentUpon>AdvancedCalculatorPage.xaml</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<Compile Update="SubPage.xaml.cs">
<DependentUpon>SubPage.xaml</DependentUpon>
</Compile>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<MauiXaml Update="SubPage.xaml">
<MauiXaml Update="AdvancedCalculatorPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
</ItemGroup>
Expand Down
Loading