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_庞炜耀_无32 #10

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions MAUICalculator/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public AppShell()
{
InitializeComponent();
}


}
}
3 changes: 2 additions & 1 deletion MAUICalculator/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
<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="MediumVioletRed" Clicked="OnClearClicked" Grid.Row="1" Grid.Column="1" Grid.RowSpan="1" Grid.ColumnSpan="2"/>
<Button Text="DEL" Background="Red" Clicked="OnDelClicked" Grid.Row="1" Grid.Column="0"/>
<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"/>
Expand Down
75 changes: 58 additions & 17 deletions MAUICalculator/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@
public partial class MainPage : ContentPage
{
int count = 0;

public double currentNumber = 0;
public double lastNumber = 0;
public string currentOperator = "";
public bool isResult = false;
//public static Label displayLabel;
public MainPage()
{
InitializeComponent();
}

public string GetDisplayText()
{
return displayLabel.Text;
}
// 定义一些变量来存储当前输入的数字,当前选择的运算符,以及上一次计算的结果
private double currentNumber = 0;
private double lastNumber = 0;
private string currentOperator = "";
private bool isResult = false;


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

// 如果当前显示的是结果,或者是0,就清空显示屏
if (isResult || displayLabel.Text == "0")
{
displayLabel.Text = "";
Expand All @@ -31,41 +34,47 @@ private void OnNumberClicked(object sender, EventArgs e)
isResult = false;
}

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

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

// 如果当前的运算符不为空,就执行上一次选择的运算,并显示结果
if (currentOperator != "")
// 如果当前显示的是结果,或者没有数字输入,不进行运算,只更新当前运算符
if (isResult || currentOperator != "")
{
currentOperator = op;
isResult = false;
return;
}

// 如果当前的运算符不为空,并且有有效的数字输入,才执行计算
if (!string.IsNullOrEmpty(currentOperator))
{
Calculate();
displayLabel.Text = lastNumber.ToString();
isResult = true;
}
else
{
// 否则,就将当前输入的数字赋值给上一次计算的结果
// 否则,将当前输入的数字赋值给上一次计算的结果
lastNumber = currentNumber;
displayLabel.Text = "0";
isResult = false;
}

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


// 定义OnEqualClicked方法来处理等号按钮点击事件
private void OnEqualClicked(object sender, EventArgs e)
{
// 如果当前选择的运算符不为空,就执行上一次选择的运算,并显示结果
if (currentOperator != "")
{
Calculate();
Expand All @@ -75,7 +84,7 @@ private void OnEqualClicked(object sender, EventArgs e)
}
}

// 定义OnEqualClicked方法来处理等号按钮点击事件
// 定义OnClearClicked方法来处理清除按钮点击事件
private void OnClearClicked(object sender, EventArgs e)
{
currentNumber = 0;
Expand All @@ -85,10 +94,43 @@ private void OnClearClicked(object sender, EventArgs e)
displayLabel.Text = lastNumber.ToString();
}

// 定义OnDelClicked方法来处理DEL按钮点击事件
private void OnDelClicked(object sender, EventArgs e)
{
// 如果显示的是结果,则清空显示但保留lastNumber
if (isResult)
{
displayLabel.Text = "0";
isResult = false;
return;
}

// 如果显示不为空,删除最后一个字符
if (!string.IsNullOrEmpty(displayLabel.Text))
{
displayLabel.Text = displayLabel.Text.Substring(0, displayLabel.Text.Length - 1);

// 如果删除后显示为空,则重置为0
if (string.IsNullOrEmpty(displayLabel.Text))
{
displayLabel.Text = "0";
}

// 更新currentNumber
if (double.TryParse(displayLabel.Text, out double number))
{
currentNumber = number;
}
else
{
currentNumber = 0;
}
}
}

// 定义Calculate方法来执行运算逻辑
private void Calculate()
{
// 根据当前选择的运算符,对上一次计算的结果和当前输入的数字进行相应的运算,并更新上一次计算的结果
switch (currentOperator)
{
case "+":
Expand All @@ -110,5 +152,4 @@ private void Calculate()
currentNumber = lastNumber;
}
}

}
60 changes: 51 additions & 9 deletions MAUICalculator/SubPage.xaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,54 @@
<?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.SubPage"
Title="Advanced Calculator">
<VerticalStackLayout>
<Label
Text="Implement your advanced calculaor here!"
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ContentPage>
x:Class="MAUICalculator.SubPage">

<Grid RowSpacing="6" ColumnSpacing="6" Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

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

<!-- Scientific Functions -->
<Button Text="x^y" Grid.Row="1" Grid.Column="0" Clicked="OnPowerClicked" />
<Button Text="√x" Grid.Row="1" Grid.Column="1" Clicked="OnSqrtClicked" />
<Button Text="lg(x)" Grid.Row="1" Grid.Column="2" Clicked="OnLogClicked" />
<Button Text="ln(x)" Grid.Row="1" Grid.Column="3" Clicked="OnLnClicked" />

<!-- Trigonometric Functions -->
<Button Text="sin" Grid.Row="2" Grid.Column="0" Clicked="OnSinClicked" />
<Button Text="cos" Grid.Row="2" Grid.Column="1" Clicked="OnCosClicked" />
<Button Text="tan" Grid.Row="2" Grid.Column="2" Clicked="OnTanClicked" />
<Button Text="x!" Grid.Row="2" Grid.Column="3" Clicked="OnFactorialClicked" />

<!-- Constants -->
<Button Text="π" Grid.Row="3" Grid.Column="0" Clicked="OnPiClicked" />
<Button Text="e" Grid.Row="3" Grid.Column="1" Clicked="OnEClicked" />

<!-- Add basic operations as well -->
<Button Text="+" Grid.Row="4" Grid.Column="0" Clicked="OnOperatorClicked" />
<Button Text="-" Grid.Row="4" Grid.Column="1" Clicked="OnOperatorClicked" />
<Button Text="*" Grid.Row="4" Grid.Column="2" Clicked="OnOperatorClicked" />
<Button Text="/" Grid.Row="4" Grid.Column="3" Clicked="OnOperatorClicked" />

<!-- Clear, Delete, Equals -->
<Button Text="DEL" Grid.Row="5" Grid.Column="0" Clicked="OnDelClicked" />
<Button Text="AC" Grid.Row="5" Grid.Column="1" Clicked="OnClearClicked" />
<Button Text="=" Grid.Row="5" Grid.Column="2" Grid.ColumnSpan="2" Clicked="OnEqualClicked" />

</Grid>
</ContentPage>
Loading