diff --git a/MAUICalculator/AppShell.xaml.cs b/MAUICalculator/AppShell.xaml.cs
index 3c4318a..ea09f91 100644
--- a/MAUICalculator/AppShell.xaml.cs
+++ b/MAUICalculator/AppShell.xaml.cs
@@ -6,5 +6,7 @@ public AppShell()
{
InitializeComponent();
}
+
+
}
}
diff --git a/MAUICalculator/MainPage.xaml b/MAUICalculator/MainPage.xaml
index 8de1788..8c6b258 100644
--- a/MAUICalculator/MainPage.xaml
+++ b/MAUICalculator/MainPage.xaml
@@ -27,7 +27,8 @@
-
+
+
diff --git a/MAUICalculator/MainPage.xaml.cs b/MAUICalculator/MainPage.xaml.cs
index 26c61c1..5c49fc2 100644
--- a/MAUICalculator/MainPage.xaml.cs
+++ b/MAUICalculator/MainPage.xaml.cs
@@ -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 = "";
@@ -31,7 +34,6 @@ private void OnNumberClicked(object sender, EventArgs e)
isResult = false;
}
- // 将数字追加到显示屏,并更新当前输入的数字
displayLabel.Text += number;
currentNumber = double.Parse(displayLabel.Text);
}
@@ -39,12 +41,19 @@ private void OnNumberClicked(object sender, EventArgs e)
// 定义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();
@@ -52,20 +61,20 @@ private void OnOperatorClicked(object sender, EventArgs e)
}
else
{
- // 否则,就将当前输入的数字赋值给上一次计算的结果
+ // 否则,将当前输入的数字赋值给上一次计算的结果
lastNumber = currentNumber;
displayLabel.Text = "0";
isResult = false;
}
- // 将当前选择的运算符赋值给变量,并清空当前输入的数字
+ // 更新当前选择的运算符
currentOperator = op;
}
+
// 定义OnEqualClicked方法来处理等号按钮点击事件
private void OnEqualClicked(object sender, EventArgs e)
{
- // 如果当前选择的运算符不为空,就执行上一次选择的运算,并显示结果
if (currentOperator != "")
{
Calculate();
@@ -75,7 +84,7 @@ private void OnEqualClicked(object sender, EventArgs e)
}
}
- // 定义OnEqualClicked方法来处理等号按钮点击事件
+ // 定义OnClearClicked方法来处理清除按钮点击事件
private void OnClearClicked(object sender, EventArgs e)
{
currentNumber = 0;
@@ -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 "+":
@@ -110,5 +152,4 @@ private void Calculate()
currentNumber = lastNumber;
}
}
-
}
diff --git a/MAUICalculator/SubPage.xaml b/MAUICalculator/SubPage.xaml
index 3e8a7a8..13a812b 100644
--- a/MAUICalculator/SubPage.xaml
+++ b/MAUICalculator/SubPage.xaml
@@ -1,12 +1,54 @@
-
-
-
-
\ No newline at end of file
+ x:Class="MAUICalculator.SubPage">
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/MAUICalculator/SubPage.xaml.cs b/MAUICalculator/SubPage.xaml.cs
index 0f9a3b9..62ee136 100644
--- a/MAUICalculator/SubPage.xaml.cs
+++ b/MAUICalculator/SubPage.xaml.cs
@@ -1,9 +1,209 @@
-namespace MAUICalculator;
-
-public partial class SubPage : ContentPage
+namespace MAUICalculator
{
- public SubPage()
- {
- InitializeComponent();
- }
-}
\ No newline at end of file
+ public partial class SubPage : ContentPage
+ {
+ private double currentNumber;
+ private double lastNumber;
+ private string currentOperator;
+ private bool isResult;
+ private MainPage _mainPage;
+ public SubPage(MainPage mainPage)
+ {
+ InitializeComponent();
+ // ҳʱMainPage״̬
+ _mainPage = mainPage;
+ currentNumber = _mainPage.currentNumber;
+ lastNumber = _mainPage.lastNumber;
+ currentOperator = _mainPage.currentOperator;
+ isResult = _mainPage.isResult;
+ displayLabel.Text = _mainPage.GetDisplayText();
+ }
+
+ private void OnNumberClicked(object sender, EventArgs e)
+ {
+ var button = sender as Button;
+ var number = button.Text;
+
+ if (isResult || displayLabel.Text == "0")
+ {
+ displayLabel.Text = "";
+ 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 != "")
+ {
+ 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;
+ }
+
+ private void Calculate()
+ {
+ switch (currentOperator)
+ {
+ case "+":
+ lastNumber += currentNumber;
+ break;
+ case "-":
+ lastNumber -= currentNumber;
+ break;
+ case "*":
+ lastNumber *= currentNumber;
+ break;
+ case "/":
+ lastNumber /= currentNumber;
+ break;
+ }
+ lastNumber = Math.Round(lastNumber, 4);
+ currentNumber = lastNumber;
+ }
+
+ private void OnPowerClicked(object sender, EventArgs e)
+ {
+ lastNumber = Math.Pow(lastNumber, currentNumber);
+ displayLabel.Text = lastNumber.ToString();
+ isResult = true;
+ }
+
+ private void OnSqrtClicked(object sender, EventArgs e)
+ {
+ lastNumber = Math.Sqrt(currentNumber);
+ displayLabel.Text = lastNumber.ToString();
+ isResult = true;
+ }
+
+ private void OnLogClicked(object sender, EventArgs e)
+ {
+ lastNumber = Math.Log10(currentNumber);
+ displayLabel.Text = lastNumber.ToString();
+ isResult = true;
+ }
+
+ private void OnLnClicked(object sender, EventArgs e)
+ {
+ lastNumber = Math.Log(currentNumber);
+ displayLabel.Text = lastNumber.ToString();
+ isResult = true;
+ }
+
+ private void OnFactorialClicked(object sender, EventArgs e)
+ {
+ lastNumber = Factorial((int)currentNumber);
+ displayLabel.Text = lastNumber.ToString();
+ isResult = true;
+ }
+
+ private double Factorial(int n)
+ {
+ if (n <= 1) return 1;
+ return n * Factorial(n - 1);
+ }
+
+ private void OnSinClicked(object sender, EventArgs e)
+ {
+ lastNumber = Math.Sin(currentNumber);
+ displayLabel.Text = lastNumber.ToString();
+ isResult = true;
+ }
+
+ private void OnCosClicked(object sender, EventArgs e)
+ {
+ lastNumber = Math.Cos(currentNumber);
+ displayLabel.Text = lastNumber.ToString();
+ isResult = true;
+ }
+
+ private void OnTanClicked(object sender, EventArgs e)
+ {
+ lastNumber = Math.Tan(currentNumber);
+ displayLabel.Text = lastNumber.ToString();
+ isResult = true;
+ }
+
+ private void OnPiClicked(object sender, EventArgs e)
+ {
+ currentNumber = Math.PI;
+ displayLabel.Text = currentNumber.ToString();
+ }
+
+ private void OnEClicked(object sender, EventArgs e)
+ {
+ currentNumber = Math.E;
+ displayLabel.Text = currentNumber.ToString();
+ }
+
+ private void OnEqualClicked(object sender, EventArgs e)
+ {
+ if (currentOperator != "")
+ {
+ Calculate();
+ displayLabel.Text = lastNumber.ToString();
+ isResult = true;
+ currentOperator = "";
+ }
+ }
+
+ private void OnDelClicked(object sender, EventArgs e)
+ {
+ if (isResult)
+ {
+ displayLabel.Text = "0";
+ isResult = false;
+ return;
+ }
+
+ if (!string.IsNullOrEmpty(displayLabel.Text))
+ {
+ displayLabel.Text = displayLabel.Text.Substring(0, displayLabel.Text.Length - 1);
+
+ if (string.IsNullOrEmpty(displayLabel.Text))
+ {
+ displayLabel.Text = "0";
+ }
+
+ if (double.TryParse(displayLabel.Text, out double number))
+ {
+ currentNumber = number;
+ }
+ else
+ {
+ currentNumber = 0;
+ }
+ }
+ }
+
+ private void OnClearClicked(object sender, EventArgs e)
+ {
+ currentNumber = 0;
+ lastNumber = 0;
+ currentOperator = "";
+ isResult = false;
+ displayLabel.Text = "0";
+ }
+ }
+}