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_孙鑫礼_工物研2 #7

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
11 changes: 11 additions & 0 deletions MAUICalculator/CalculatorState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

namespace MAUICalculator
{
public static class CalculatorState
{
public static double currentNumber { get; set; } = 0;
public static double lastNumber { get; set; } = 0;
public static string currentOperator { get; set; } = "";
public static bool isResult { get; set; }=false;
}
}
4 changes: 3 additions & 1 deletion MAUICalculator/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +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"/>
Expand Down
161 changes: 119 additions & 42 deletions MAUICalculator/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public MainPage()
}

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

// 定义OnNumberClicked方法来处理数字按钮点击事件
private void OnNumberClicked(object sender, EventArgs e)
Expand All @@ -23,17 +23,17 @@ private void OnNumberClicked(object sender, EventArgs e)
var number = button.Text;

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

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

// 定义OnOperatorClicked方法来处理运算符按钮点击事件
Expand All @@ -43,72 +43,149 @@ private void OnOperatorClicked(object sender, EventArgs e)
var button = sender as Button;
var op = button.Text;

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

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

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

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

// 定义Calculate方法来执行运算逻辑
private void Calculate()
{
// 根据当前选择的运算符,对上一次计算的结果和当前输入的数字进行相应的运算,并更新上一次计算的结果
switch (currentOperator)
try
{
switch (CalculatorState.currentOperator)
{
case "ln":
if (CalculatorState.currentNumber <= 0)
throw new ArgumentOutOfRangeException("Logarithm of non-positive numbers is undefined.");
CalculatorState.lastNumber = Math.Log(CalculatorState.currentNumber);
break;

case "x^y":
CalculatorState.lastNumber = Math.Pow(CalculatorState.lastNumber, CalculatorState.currentNumber);
break;

case "sin":
CalculatorState.lastNumber = Math.Sin(CalculatorState.currentNumber);
break;

case "cos":
CalculatorState.lastNumber = Math.Cos(CalculatorState.currentNumber);
break;

case "+":
CalculatorState.lastNumber += CalculatorState.currentNumber;
break;
case "-":
CalculatorState.lastNumber -= CalculatorState.currentNumber;
break;
case "*":
CalculatorState.lastNumber *= CalculatorState.currentNumber;
break;
case "/":
CalculatorState.lastNumber /= CalculatorState.currentNumber;
break;

default:
throw new InvalidOperationException("Invalid operator");
}

CalculatorState.lastNumber = Math.Round(CalculatorState.lastNumber, 4);
CalculatorState.currentNumber = CalculatorState.lastNumber;
}
catch (Exception ex)
{
CalculatorState.lastNumber = -99999;
CalculatorState.currentNumber = 0;
}
}

/// <summary>
/// 定义DEL按键所执行的命令
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnDeleteClicked(object sender, EventArgs e)
{
// displayLabel.Text = $@"
//currentN = {currentNumber},lastN = {lastNumber},currentOp = {currentOperator},isResult = {isResult}
//";
// 当上一个字符为`=`时,清空显示屏但不改变`lastNumber`的值
if (CalculatorState.isResult) {
displayLabel.Text = "";
}
else
{
case "+":
lastNumber += currentNumber;
break;
case "-":
lastNumber -= currentNumber;
break;
case "*":
lastNumber *= currentNumber;
break;
case "/":
lastNumber /= currentNumber;
break;
default:
break;
// 简化考虑:
// (1) 上一个字符为运算符 <=> 屏幕显示为"0"
// (2) 上一个字符为数字 <=> 屏幕显示为非零数字
if (displayLabel.Text == "0")
{
CalculatorState.currentOperator = "";
}
else if (displayLabel.Text != "")
{
// 若上一个字符为数字 从屏幕显示字符串中删除最后一个字符
displayLabel.Text = displayLabel.Text.Remove(
displayLabel.Text.Length - 1, 1
);
// 然后转化为浮点数 更新currentNumber
if (displayLabel.Text != "")
CalculatorState.currentNumber = double.Parse(displayLabel.Text);
else
CalculatorState.currentNumber = 0;
}

}
lastNumber = Math.Round(lastNumber, 4);
currentNumber = lastNumber;
}


// 切换时保证显示的内容的一致
protected override void OnAppearing()
{
base.OnAppearing();
displayLabel.Text = CalculatorState.currentNumber.ToString(); // 或根据需要显示的内容更新
}

}

}
57 changes: 51 additions & 6 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>
<!-- 使用Grid作为根布局 -->
<Grid RowSpacing="6" ColumnSpacing="6" Margin="20">

<!-- 设置六行四列的网格 -->
<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>

<!-- 使用Label显示计算结果,并占据第一行四列 -->
<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" />
<Button Text="DEL" Background="LightSkyBlue" Clicked="OnDeleteClicked" Grid.Row="1" Grid.Column="1"/>
<Button Text="pi" Background="LightSkyBlue" Clicked="OnNumberClicked" Grid.Row="1" Grid.Column="2"/>
<Button Text="e" Background="LightSkyBlue" Clicked="OnNumberClicked" 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="x^y" 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="sin" 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="ln" 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"/>

</Grid>

</ContentPage>
Loading