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_王一涵_无38 #11

Open
wants to merge 5 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
6 changes: 3 additions & 3 deletions MAUICalculator/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<?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">
Expand Down Expand Up @@ -27,9 +27,9 @@
<Label x:Name="displayLabel" Text="0" FontSize="Large" HorizontalTextAlignment="End" Grid.Row="0" Grid.ColumnSpan="4" />

<!-- 使用Button创建键盘按钮,并占据特定的Grid位置,设置Clicked事件处理程序 -->
<Button Text="AC" Background="LightSkyBlue" Clicked="OnClearClicked" Grid.Row="1" Grid.Column="0" Grid.RowSpan="1" Grid.ColumnSpan="3"/>
<Button Text="AC" Background="LightSkyBlue" Clicked="OnClearClicked" Grid.Row="1" Grid.Column="0" Grid.RowSpan="1" Grid.ColumnSpan="2"/>
<Button Text="DEL" Background="LightSkyBlue" Clicked="OnDeleteClicked" Grid.Row="1" Grid.Column="2"/>
<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"/>
Expand Down
120 changes: 69 additions & 51 deletions MAUICalculator/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,114 +1,132 @@
namespace MAUICalculator
using System;
using Microsoft.Maui.Controls;

namespace MAUICalculator
{
public partial class MainPage : ContentPage
{
int count = 0;

public MainPage()
{
InitializeComponent();
LoadState();
}

private void LoadState()
{
var state = CalculatorState.Instance;
displayLabel.Text = state.IsResult ? state.LastNumber.ToString() : state.CurrentNumber.ToString();
}

// 定义一些变量来存储当前输入的数字,当前选择的运算符,以及上一次计算的结果
private double currentNumber = 0;
private double lastNumber = 0;
private string currentOperator = "";
private bool isResult = false;
private void OnDeleteClicked(object sender, EventArgs e)
{
var state = CalculatorState.Instance;
if (state.IsResult) displayLabel.Text = "";
else
{
if (state.CurrentOperator != "")
{
state.CurrentOperator = "";
displayLabel.Text = state.LastNumber.ToString();
}
else
{
displayLabel.Text = displayLabel.Text.Remove(displayLabel.Text.Length - 1);
if (displayLabel.Text == "") displayLabel.Text += "0";
state.CurrentNumber = double.Parse(displayLabel.Text);
}
}
}

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

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

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

// 定义OnOperatorClicked方法来处理运算符按钮点击事件
private void OnOperatorClicked(object sender, EventArgs e)
{
// 获取按钮的文本值
var button = sender as Button;
var op = button.Text;
var state = CalculatorState.Instance;

// 如果当前的运算符不为空,就执行上一次选择的运算,并显示结果
if (currentOperator != "")
if (state.CurrentOperator != "")
{
Calculate();
displayLabel.Text = lastNumber.ToString();
isResult = true;
if (!state.IsResult)
{
Calculate();
displayLabel.Text = state.LastNumber.ToString();
state.IsResult = true;
}
}
else
{
// 否则,就将当前输入的数字赋值给上一次计算的结果
lastNumber = currentNumber;
state.LastNumber = state.CurrentNumber;
displayLabel.Text = "0";
isResult = false;
state.IsResult = false;
}

// 将当前选择的运算符赋值给变量,并清空当前输入的数字
currentOperator = op;
state.CurrentOperator = op;
}

// 定义OnEqualClicked方法来处理等号按钮点击事件
private void OnEqualClicked(object sender, EventArgs e)
{
// 如果当前选择的运算符不为空,就执行上一次选择的运算,并显示结果
if (currentOperator != "")
var state = CalculatorState.Instance;
if (state.CurrentOperator != "")
{
Calculate();
displayLabel.Text = lastNumber.ToString();
isResult = true;
currentOperator = "";
displayLabel.Text = state.LastNumber.ToString();
state.CurrentOperator = "";
state.IsResult = true;
}
}

// 定义OnEqualClicked方法来处理等号按钮点击事件
private void OnClearClicked(object sender, EventArgs e)
{
currentNumber = 0;
lastNumber = 0;
currentOperator = "";
isResult = false;
displayLabel.Text = lastNumber.ToString();
var state = CalculatorState.Instance;
state.CurrentNumber = 0;
state.LastNumber = 0;
state.CurrentOperator = "";
state.IsResult = false;
displayLabel.Text = state.LastNumber.ToString();
}

// 定义Calculate方法来执行运算逻辑
private void Calculate()
{
// 根据当前选择的运算符,对上一次计算的结果和当前输入的数字进行相应的运算,并更新上一次计算的结果
switch (currentOperator)
var state = CalculatorState.Instance;
switch (state.CurrentOperator)
{
case "+":
lastNumber += currentNumber;
state.LastNumber += state.CurrentNumber;
break;
case "-":
lastNumber -= currentNumber;
state.LastNumber -= state.CurrentNumber;
break;
case "*":
lastNumber *= currentNumber;
state.LastNumber *= state.CurrentNumber;
break;
case "/":
lastNumber /= currentNumber;
break;
default:
state.LastNumber /= state.CurrentNumber;
break;
}
lastNumber = Math.Round(lastNumber, 4);
currentNumber = lastNumber;
state.LastNumber = Math.Round(state.LastNumber, 4);
state.CurrentNumber = state.LastNumber;
}
}

private async void OnNavigateToComplexModeClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new SubPage());
}
}
}
59 changes: 52 additions & 7 deletions MAUICalculator/SubPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,55 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MAUICalculator.SubPage"
Title="Advanced Calculator">
<VerticalStackLayout>
<Label
Text="Implement your advanced calculaor here!"
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ContentPage>

<Grid RowSpacing="0" ColumnSpacing="0" Padding="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<!-- Display label -->
<Label x:Name="displayLabel" Text="0" FontSize="Large" HorizontalTextAlignment="End" VerticalOptions="Center" Grid.Row="0" Grid.ColumnSpan="4" />

<!-- Function buttons -->
<Grid Grid.Row="1" VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<!-- Row 1 -->
<Button Text="x^y" Background="LightSkyBlue" Clicked="OnComplexOperatorClicked" Grid.Row="0" Grid.Column="0" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Button Text="sqrt" Background="LightSkyBlue" Clicked="OnComplexOperatorClicked" Grid.Row="0" Grid.Column="1" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Button Text="lg" Background="LightSkyBlue" Clicked="OnComplexOperatorClicked" Grid.Row="0" Grid.Column="2" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Button Text="ln" Background="LightSkyBlue" Clicked="OnComplexOperatorClicked" Grid.Row="0" Grid.Column="3" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>

<!-- Row 2 -->
<Button Text="sin" Background="LightSkyBlue" Clicked="OnComplexOperatorClicked" Grid.Row="1" Grid.Column="0" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Button Text="cos" Background="LightSkyBlue" Clicked="OnComplexOperatorClicked" Grid.Row="1" Grid.Column="1" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Button Text="tan" Background="LightSkyBlue" Clicked="OnComplexOperatorClicked" Grid.Row="1" Grid.Column="2" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Button Text="pi" Background="LightSkyBlue" Clicked="OnComplexNumberClicked" Grid.Row="1" Grid.Column="3" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>

<!-- Row 3 -->
<Button Text="e" Background="LightSkyBlue" Clicked="OnComplexNumberClicked" Grid.Row="2" Grid.Column="0" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Button Text="!" Background="LightSkyBlue" Clicked="OnComplexOperatorClicked" Grid.Row="2" Grid.Column="1" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Button Text="0" Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="2" Grid.Column="2" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Button Text="=" Background="LightSkyBlue" Clicked="OnEqualClicked" Grid.Row="2" Grid.Column="3" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>

<!-- Row 4 -->
<Button Text="AC" Background="LightSkyBlue" Clicked="OnClearClicked" Grid.Row="3" Grid.Column="0" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Button Text="DEL" Background="LightSkyBlue" Clicked="OnDeleteClicked" Grid.Row="3" Grid.Column="1" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Button Text="+" Background="LightSkyBlue" Clicked="OnOperatorClicked" Grid.Row="3" Grid.Column="2" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Button Text="-" Background="LightSkyBlue" Clicked="OnOperatorClicked" Grid.Row="3" Grid.Column="3" Margin="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
</Grid>
</Grid>
</ContentPage>
Loading