From 5653ce71f9e04e35f7b0d82b4af02199c85d6c1d Mon Sep 17 00:00:00 2001
From: EulerOne <1572542354@qq.com>
Date: Sat, 31 Aug 2024 23:06:27 +0800
Subject: [PATCH 1/5] Update MainPage.xaml
---
MAUICalculator/MainPage.xaml | 186 +++++++++++++++++++++++++----------
1 file changed, 132 insertions(+), 54 deletions(-)
diff --git a/MAUICalculator/MainPage.xaml b/MAUICalculator/MainPage.xaml
index 8de1788..5b3aa75 100644
--- a/MAUICalculator/MainPage.xaml
+++ b/MAUICalculator/MainPage.xaml
@@ -1,54 +1,132 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+using System;
+using Microsoft.Maui.Controls;
+
+namespace MAUICalculator
+{
+ public partial class MainPage : ContentPage
+ {
+ public MainPage()
+ {
+ InitializeComponent();
+ LoadState();
+ }
+
+ private void LoadState()
+ {
+ var state = CalculatorState.Instance;
+ displayLabel.Text = state.IsResult ? state.LastNumber.ToString() : state.CurrentNumber.ToString();
+ }
+
+ 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);
+ }
+ }
+ }
+
+ private void OnNumberClicked(object sender, EventArgs e)
+ {
+ var button = sender as Button;
+ var number = button.Text;
+ var state = CalculatorState.Instance;
+
+ if (state.IsResult || displayLabel.Text == "0")
+ {
+ displayLabel.Text = "";
+ if (number == ".")
+ displayLabel.Text = "0";
+ state.IsResult = false;
+ }
+
+ displayLabel.Text += number;
+ state.CurrentNumber = double.Parse(displayLabel.Text);
+ }
+
+ private void OnOperatorClicked(object sender, EventArgs e)
+ {
+ var button = sender as Button;
+ var op = button.Text;
+ var state = CalculatorState.Instance;
+
+ if (state.CurrentOperator != "")
+ {
+ if (!state.IsResult)
+ {
+ Calculate();
+ displayLabel.Text = state.LastNumber.ToString();
+ state.IsResult = true;
+ }
+ }
+ else
+ {
+ state.LastNumber = state.CurrentNumber;
+ displayLabel.Text = "0";
+ state.IsResult = false;
+ }
+
+ state.CurrentOperator = op;
+ }
+
+ private void OnEqualClicked(object sender, EventArgs e)
+ {
+ var state = CalculatorState.Instance;
+ if (state.CurrentOperator != "")
+ {
+ Calculate();
+ displayLabel.Text = state.LastNumber.ToString();
+ state.CurrentOperator = "";
+ state.IsResult = true;
+ }
+ }
+
+ private void OnClearClicked(object sender, EventArgs e)
+ {
+ var state = CalculatorState.Instance;
+ state.CurrentNumber = 0;
+ state.LastNumber = 0;
+ state.CurrentOperator = "";
+ state.IsResult = false;
+ displayLabel.Text = state.LastNumber.ToString();
+ }
+
+ private void Calculate()
+ {
+ var state = CalculatorState.Instance;
+ switch (state.CurrentOperator)
+ {
+ case "+":
+ state.LastNumber += state.CurrentNumber;
+ break;
+ case "-":
+ state.LastNumber -= state.CurrentNumber;
+ break;
+ case "*":
+ state.LastNumber *= state.CurrentNumber;
+ break;
+ case "/":
+ state.LastNumber /= state.CurrentNumber;
+ break;
+ }
+ state.LastNumber = Math.Round(state.LastNumber, 4);
+ state.CurrentNumber = state.LastNumber;
+ }
+
+ private async void OnNavigateToComplexModeClicked(object sender, EventArgs e)
+ {
+ await Navigation.PushAsync(new SubPage());
+ }
+ }
+}
From 90acf96e001584c0e2c7e04c7090bb07a16af540 Mon Sep 17 00:00:00 2001
From: EulerOne <1572542354@qq.com>
Date: Sat, 31 Aug 2024 23:07:36 +0800
Subject: [PATCH 2/5] Update MainPage.xaml
---
MAUICalculator/MainPage.xaml | 186 ++++++++++-------------------------
1 file changed, 54 insertions(+), 132 deletions(-)
diff --git a/MAUICalculator/MainPage.xaml b/MAUICalculator/MainPage.xaml
index 5b3aa75..c85cb0e 100644
--- a/MAUICalculator/MainPage.xaml
+++ b/MAUICalculator/MainPage.xaml
@@ -1,132 +1,54 @@
-using System;
-using Microsoft.Maui.Controls;
-
-namespace MAUICalculator
-{
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- LoadState();
- }
-
- private void LoadState()
- {
- var state = CalculatorState.Instance;
- displayLabel.Text = state.IsResult ? state.LastNumber.ToString() : state.CurrentNumber.ToString();
- }
-
- 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);
- }
- }
- }
-
- private void OnNumberClicked(object sender, EventArgs e)
- {
- var button = sender as Button;
- var number = button.Text;
- var state = CalculatorState.Instance;
-
- if (state.IsResult || displayLabel.Text == "0")
- {
- displayLabel.Text = "";
- if (number == ".")
- displayLabel.Text = "0";
- state.IsResult = false;
- }
-
- displayLabel.Text += number;
- state.CurrentNumber = double.Parse(displayLabel.Text);
- }
-
- private void OnOperatorClicked(object sender, EventArgs e)
- {
- var button = sender as Button;
- var op = button.Text;
- var state = CalculatorState.Instance;
-
- if (state.CurrentOperator != "")
- {
- if (!state.IsResult)
- {
- Calculate();
- displayLabel.Text = state.LastNumber.ToString();
- state.IsResult = true;
- }
- }
- else
- {
- state.LastNumber = state.CurrentNumber;
- displayLabel.Text = "0";
- state.IsResult = false;
- }
-
- state.CurrentOperator = op;
- }
-
- private void OnEqualClicked(object sender, EventArgs e)
- {
- var state = CalculatorState.Instance;
- if (state.CurrentOperator != "")
- {
- Calculate();
- displayLabel.Text = state.LastNumber.ToString();
- state.CurrentOperator = "";
- state.IsResult = true;
- }
- }
-
- private void OnClearClicked(object sender, EventArgs e)
- {
- var state = CalculatorState.Instance;
- state.CurrentNumber = 0;
- state.LastNumber = 0;
- state.CurrentOperator = "";
- state.IsResult = false;
- displayLabel.Text = state.LastNumber.ToString();
- }
-
- private void Calculate()
- {
- var state = CalculatorState.Instance;
- switch (state.CurrentOperator)
- {
- case "+":
- state.LastNumber += state.CurrentNumber;
- break;
- case "-":
- state.LastNumber -= state.CurrentNumber;
- break;
- case "*":
- state.LastNumber *= state.CurrentNumber;
- break;
- case "/":
- state.LastNumber /= state.CurrentNumber;
- break;
- }
- state.LastNumber = Math.Round(state.LastNumber, 4);
- state.CurrentNumber = state.LastNumber;
- }
-
- private async void OnNavigateToComplexModeClicked(object sender, EventArgs e)
- {
- await Navigation.PushAsync(new SubPage());
- }
- }
-}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
From 2882f9ec47169a2ad59852ae03802255dd3e636e Mon Sep 17 00:00:00 2001
From: EulerOne <1572542354@qq.com>
Date: Sat, 31 Aug 2024 23:08:06 +0800
Subject: [PATCH 3/5] Update MainPage.xaml.cs
---
MAUICalculator/MainPage.xaml.cs | 120 ++++++++++++++++++--------------
1 file changed, 69 insertions(+), 51 deletions(-)
diff --git a/MAUICalculator/MainPage.xaml.cs b/MAUICalculator/MainPage.xaml.cs
index 26c61c1..5b3aa75 100644
--- a/MAUICalculator/MainPage.xaml.cs
+++ b/MAUICalculator/MainPage.xaml.cs
@@ -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());
+ }
+ }
}
From c0d16c76baa76dbd146074eff72bd670a710e202 Mon Sep 17 00:00:00 2001
From: EulerOne <1572542354@qq.com>
Date: Sat, 31 Aug 2024 23:08:26 +0800
Subject: [PATCH 4/5] Update SubPage.xaml
---
MAUICalculator/SubPage.xaml | 59 ++++++++++++++++++++++++++++++++-----
1 file changed, 52 insertions(+), 7 deletions(-)
diff --git a/MAUICalculator/SubPage.xaml b/MAUICalculator/SubPage.xaml
index 3e8a7a8..4e0f41b 100644
--- a/MAUICalculator/SubPage.xaml
+++ b/MAUICalculator/SubPage.xaml
@@ -3,10 +3,55 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MAUICalculator.SubPage"
Title="Advanced Calculator">
-
-
-
-
\ No newline at end of file
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
From 2bff361282ab10c200381f4e53da64516947150d Mon Sep 17 00:00:00 2001
From: EulerOne <1572542354@qq.com>
Date: Sat, 31 Aug 2024 23:08:40 +0800
Subject: [PATCH 5/5] Update SubPage.xaml.cs
---
MAUICalculator/SubPage.xaml.cs | 209 +++++++++++++++++++++++++++++++--
1 file changed, 202 insertions(+), 7 deletions(-)
diff --git a/MAUICalculator/SubPage.xaml.cs b/MAUICalculator/SubPage.xaml.cs
index 0f9a3b9..3a2bd0e 100644
--- a/MAUICalculator/SubPage.xaml.cs
+++ b/MAUICalculator/SubPage.xaml.cs
@@ -1,9 +1,204 @@
-namespace MAUICalculator;
+using System;
+using Microsoft.Maui.Controls;
-public partial class SubPage : ContentPage
+namespace MAUICalculator
{
- public SubPage()
- {
- InitializeComponent();
- }
-}
\ No newline at end of file
+ public partial class SubPage : ContentPage
+ {
+ public SubPage()
+ {
+ InitializeComponent();
+ LoadState();
+ }
+
+ private void LoadState()
+ {
+ var state = CalculatorState.Instance;
+ displayLabel.Text = state.IsResult ? state.LastNumber.ToString() : state.CurrentNumber.ToString();
+ }
+
+ 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
+ {
+ if (displayLabel.Text.Length > 0)
+ {
+ displayLabel.Text = displayLabel.Text.Remove(displayLabel.Text.Length - 1);
+ if (displayLabel.Text == "") displayLabel.Text = "0";
+ state.CurrentNumber = double.Parse(displayLabel.Text);
+ }
+ }
+ }
+ }
+
+ private void OnNumberClicked(object sender, EventArgs e)
+ {
+ var button = sender as Button;
+ var number = button.Text;
+ var state = CalculatorState.Instance;
+
+ if (state.IsResult || displayLabel.Text == "0")
+ {
+ displayLabel.Text = "";
+ if (number == ".")
+ displayLabel.Text = "0";
+ state.IsResult = false;
+ }
+
+ displayLabel.Text += number;
+ state.CurrentNumber = double.Parse(displayLabel.Text);
+ }
+
+ private void OnOperatorClicked(object sender, EventArgs e)
+ {
+ var button = sender as Button;
+ var op = button.Text;
+ var state = CalculatorState.Instance;
+
+ if (state.CurrentOperator != "")
+ {
+ if (!state.IsResult)
+ {
+ Calculate();
+ displayLabel.Text = state.LastNumber.ToString();
+ state.IsResult = true;
+ }
+ }
+ else
+ {
+ state.LastNumber = state.CurrentNumber;
+ displayLabel.Text = "0";
+ state.IsResult = false;
+ }
+
+ state.CurrentOperator = op;
+ }
+
+ private void OnEqualClicked(object sender, EventArgs e)
+ {
+ var state = CalculatorState.Instance;
+ if (state.CurrentOperator != "")
+ {
+ Calculate();
+ displayLabel.Text = state.LastNumber.ToString();
+ state.CurrentOperator = "";
+ state.IsResult = true;
+ }
+ }
+
+ private void OnClearClicked(object sender, EventArgs e)
+ {
+ var state = CalculatorState.Instance;
+ state.CurrentNumber = 0;
+ state.LastNumber = 0;
+ state.CurrentOperator = "";
+ state.IsResult = false;
+ displayLabel.Text = state.LastNumber.ToString();
+ }
+
+ private void Calculate()
+ {
+ var state = CalculatorState.Instance;
+ switch (state.CurrentOperator)
+ {
+ case "+":
+ state.LastNumber += state.CurrentNumber;
+ break;
+ case "-":
+ state.LastNumber -= state.CurrentNumber;
+ break;
+ case "*":
+ state.LastNumber *= state.CurrentNumber;
+ break;
+ case "/":
+ state.LastNumber /= state.CurrentNumber;
+ break;
+ case "x^y":
+ state.LastNumber = Math.Pow(state.LastNumber, state.CurrentNumber);
+ break;
+ case "sqrt":
+ state.LastNumber = Math.Sqrt(state.CurrentNumber);
+ break;
+ case "lg":
+ state.LastNumber = Math.Log10(state.CurrentNumber);
+ break;
+ case "ln":
+ state.LastNumber = Math.Log(state.CurrentNumber);
+ break;
+ case "!":
+ state.LastNumber = Factorial((int)state.CurrentNumber);
+ break;
+ case "sin":
+ state.LastNumber = Math.Sin(state.CurrentNumber);
+ break;
+ case "cos":
+ state.LastNumber = Math.Cos(state.CurrentNumber);
+ break;
+ case "tan":
+ state.LastNumber = Math.Tan(state.CurrentNumber);
+ break;
+ }
+ state.LastNumber = Math.Round(state.LastNumber, 4);
+ state.CurrentNumber = state.LastNumber;
+ }
+
+ private void OnComplexOperatorClicked(object sender, EventArgs e)
+ {
+ var button = sender as Button;
+ var op = button.Text;
+ var state = CalculatorState.Instance;
+
+ if (state.CurrentOperator != "")
+ {
+ Calculate();
+ displayLabel.Text = state.LastNumber.ToString();
+ }
+ else
+ {
+ state.LastNumber = state.CurrentNumber;
+ displayLabel.Text = "0";
+ }
+
+ state.CurrentOperator = op;
+ }
+
+ private void OnComplexNumberClicked(object sender, EventArgs e)
+ {
+ var button = sender as Button;
+ var number = button.Text;
+ var state = CalculatorState.Instance;
+
+ if (number == "pi")
+ {
+ state.CurrentNumber = Math.PI;
+ }
+ else if (number == "e")
+ {
+ state.CurrentNumber = Math.E;
+ }
+
+ displayLabel.Text = state.CurrentNumber.ToString();
+ state.IsResult = true;
+ }
+
+ private double Factorial(int number)
+ {
+ if (number < 0) throw new ArgumentException("Number must be non-negative.");
+ double result = 1;
+ for (int i = 2; i <= number; i++)
+ {
+ result *= i;
+ }
+ return result;
+ }
+ }
+}