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 #8

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
66 changes: 60 additions & 6 deletions MAUICalculator/SubPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,64 @@
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 RowSpacing="6" ColumnSpacing="6" Margin="20">
<!-- 设置七行四列的网格 -->
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<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 x:Name="displayLabel" Text="0" FontSize="Large" HorizontalTextAlignment="End" Grid.Row="0" Grid.ColumnSpan="4" />

<!-- 第一行:AC、DEL、切换回主页面、除法 -->
<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="Main" Background="LightSkyBlue" Clicked="OnMainPageClicked" Grid.Row="1" Grid.Column="2"/>
<Button Text="/" Background="LightSkyBlue" Clicked="OnOperatorClicked" Grid.Row="1" Grid.Column="3"/>

<!-- 第二行:7、8、9、乘法 -->
<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"/>

<!-- 第三行:4、5、6、减法 -->
<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"/>

<!-- 第四行:1、2、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"/>

<!-- 第五行:0、小数点、等号 -->
<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="x^y" Background="LightSkyBlue" Clicked="OnAdvancedOperatorClicked" Grid.Row="6" Grid.Column="0"/>
<Button Text="lg" Background="LightSkyBlue" Clicked="OnAdvancedOperatorClicked" Grid.Row="6" Grid.Column="1"/>
<Button Text="√" Background="LightSkyBlue" Clicked="OnAdvancedOperatorClicked" Grid.Row="6" Grid.Column="2"/>
<Button Text="x!" Background="LightSkyBlue" Clicked="OnAdvancedOperatorClicked" Grid.Row="6" Grid.Column="3"/>

</Grid>

</ContentPage>
219 changes: 214 additions & 5 deletions MAUICalculator/SubPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,217 @@ namespace MAUICalculator;

public partial class SubPage : ContentPage
{
public SubPage()
{
InitializeComponent();
}
}
private double currentNumber = 0;
private double lastNumber = 0;
private string currentOperator = "";
private bool isResult = false;

public SubPage()
{
InitializeComponent();
}

private void OnNumberClicked(object sender, EventArgs e)
{
var button = sender as Button;
var number = button.Text;

if (isResult || displayLabel.Text == "0")
{
displayLabel.Text = "";
if (number == ".")
displayLabel.Text = "0";
isResult = false;
}

displayLabel.Text += number;
currentNumber = double.Parse(displayLabel.Text);
}

private void OnOperatorClicked(object sender, EventArgs e)
{
var button = sender as Button;
var op = button.Text;

if (isResult && currentOperator == "")
{
isResult = false;
displayLabel.Text = "0";
}

if (currentOperator != "")
{
if (!isResult)
{
Calculate();
displayLabel.Text = lastNumber.ToString();
isResult = true;
}
}
else
{
lastNumber = currentNumber;
displayLabel.Text = "0";
isResult = false;
}

currentOperator = op;
}

private void OnEqualClicked(object sender, EventArgs e)
{
if (currentOperator != "")
{
Calculate();
displayLabel.Text = lastNumber.ToString();
isResult = true;
currentOperator = "";
}
}

private void OnClearClicked(object sender, EventArgs e)
{
currentNumber = 0;
lastNumber = 0;
currentOperator = "";
isResult = false;
displayLabel.Text = "0";
}

private void OnDeleteClicked(object sender, EventArgs e)
{
if (displayLabel.Text.Length > 0)
{
if (displayLabel.Text == lastNumber.ToString() && isResult)
{
displayLabel.Text = "0";
isResult = false;
}
else
{
displayLabel.Text = displayLabel.Text.Substring(0, displayLabel.Text.Length - 1);
if (displayLabel.Text.Length == 0 || displayLabel.Text == "-")
displayLabel.Text = "0";
}

currentNumber = double.TryParse(displayLabel.Text, out double parsedValue) ? parsedValue : 0;
}
}

private async void OnMainPageClicked(object sender, EventArgs e)
{
await Shell.Current.GoToAsync("//MainPage");
}

private void OnAdvancedOperatorClicked(object sender, EventArgs e)
{
var button = sender as Button;
var op = button.Text;

switch (op)
{
case "x^y":
if (currentOperator == "x^y" && !isResult)
{
// ���֮ǰ�Ѿ�ѡ���˳˷����������һ�û�еõ����
lastNumber = Math.Pow(lastNumber, currentNumber);
displayLabel.Text = lastNumber.ToString();
isResult = true;
}
else
{
// ���浱ǰ����Ļ���
lastNumber = currentNumber;
currentOperator = "x^y"; // ������
isResult = false;
displayLabel.Text = "0"; // ׼������ָ��
}
break;
case "lg":
if (currentNumber <= 0)
{
displayLabel.Text = "Error";
}
else
{
lastNumber = Math.Log10(currentNumber);
displayLabel.Text = lastNumber.ToString();
}
isResult = true;
break;
case "��":
if (currentNumber < 0)
{
displayLabel.Text = "Error";
}
else
{
lastNumber = Math.Sqrt(currentNumber);
displayLabel.Text = lastNumber.ToString();
}
isResult = true;
break;
case "x!":
if (currentNumber < 0 || currentNumber != (int)currentNumber)
{
displayLabel.Text = "Error";
}
else
{
lastNumber = Factorial((int)currentNumber);
displayLabel.Text = lastNumber.ToString();
}
isResult = true;
break;
default:
break;
}
}

private void Calculate()
{
switch (currentOperator)
{
case "+":
lastNumber += currentNumber;
break;
case "-":
lastNumber -= currentNumber;
break;
case "*":
lastNumber *= currentNumber;
break;
case "/":
if (currentNumber == 0)
{
displayLabel.Text = "Error";
return;
}
lastNumber /= currentNumber;
break;
case "x^y":
lastNumber = Math.Pow(lastNumber, currentNumber);
break;
default:
break;
}
lastNumber = Math.Round(lastNumber, 4);
currentNumber = lastNumber;
}

private double Factorial(int n)
{
if (n < 0)
return double.NaN; // Return NaN for negative inputs
if (n == 0 || n == 1)
return 1;
double result = 1;
for (int i = 2; i <= n; i++)
{
result *= i;
}
return result;
}
}