From 24198b2e0911e2cd4f7c282287bdc3d01a53c468 Mon Sep 17 00:00:00 2001 From: zsyg <3872006562@qq.com> Date: Sun, 29 Jun 2025 09:46:39 +0800 Subject: [PATCH] Add files via upload --- tools/Calculator.cs | 34 +++++++++++ tools/CalculatorForm.cs | 118 ++++++++++++++++++++++++++++++++++++ tools/CalculatorToolCard.cs | 38 ++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 tools/Calculator.cs create mode 100644 tools/CalculatorForm.cs create mode 100644 tools/CalculatorToolCard.cs diff --git a/tools/Calculator.cs b/tools/Calculator.cs new file mode 100644 index 0000000..a6e018e --- /dev/null +++ b/tools/Calculator.cs @@ -0,0 +1,34 @@ +using System; + +namespace AppStore +{ + /// + /// 提供基本数学运算功能的工具类 + /// + public static class Calculator + { + /// + /// 加法运算 + /// + public static double Add(double a, double b) => a + b; + + /// + /// 减法运算 + /// + public static double Subtract(double a, double b) => a - b; + + /// + /// 乘法运算 + /// + public static double Multiply(double a, double b) => a * b; + + /// + /// 除法运算 + /// + public static double Divide(double a, double b) + { + if (b == 0) throw new DivideByZeroException("除数不能为零"); + return a / b; + } + } +} diff --git a/tools/CalculatorForm.cs b/tools/CalculatorForm.cs new file mode 100644 index 0000000..d00cfcf --- /dev/null +++ b/tools/CalculatorForm.cs @@ -0,0 +1,118 @@ +using System; +using System.Drawing; +using System.Windows.Forms; +using AppStore; + +namespace AppStore +{ + public class CalculatorForm : Form + { + private TextBox display = new TextBox(); + private double firstNumber = 0; + private string operation = ""; + + public CalculatorForm() + { + this.Text = "计算器"; + this.Size = new Size(300, 400); + this.StartPosition = FormStartPosition.CenterScreen; + CreateControls(); + } + + private void CreateControls() + { + // 显示框 + display = new TextBox(); + display.ReadOnly = true; + display.TextAlign = HorizontalAlignment.Right; + display.Font = new Font("Microsoft YaHei", 14); + display.Size = new Size(260, 40); + display.Location = new Point(20, 20); + this.Controls.Add(display); + + // 按钮布局 + string[] buttonLabels = { + "7", "8", "9", "/", + "4", "5", "6", "*", + "1", "2", "3", "-", + "0", ".", "=", "+" + }; + + for (int i = 0; i < buttonLabels.Length; i++) + { + var btn = new Button(); + btn.Text = buttonLabels[i]; + btn.Size = new Size(60, 50); + btn.Location = new Point(20 + (i % 4) * 65, 70 + (i / 4) * 55); + btn.Font = new Font("Microsoft YaHei", 12); + btn.Click += ButtonClickHandler; + this.Controls.Add(btn); + } + + // 清除按钮 + var clearBtn = new Button(); + clearBtn.Text = "C"; + clearBtn.Size = new Size(260, 40); + clearBtn.Location = new Point(20, 300); + clearBtn.Font = new Font("Microsoft YaHei", 12); + clearBtn.Click += (s, e) => { + display.Text = ""; + firstNumber = 0; + operation = ""; + }; + this.Controls.Add(clearBtn); + } + + private void ButtonClickHandler(object sender, EventArgs e) + { + var button = sender as Button; + if (button == null) return; + + switch (button.Text) + { + case "+": + case "-": + case "*": + case "/": + if (!string.IsNullOrEmpty(display.Text)) + { + firstNumber = double.Parse(display.Text); + operation = button.Text; + display.Text = ""; + } + break; + + case "=": + if (!string.IsNullOrEmpty(operation) && !string.IsNullOrEmpty(display.Text)) + { + double secondNumber = double.Parse(display.Text); + double result = 0; + + try + { + result = operation switch + { + "+" => AppStore.Calculator.Add(firstNumber, secondNumber), + "-" => AppStore.Calculator.Subtract(firstNumber, secondNumber), + "*" => AppStore.Calculator.Multiply(firstNumber, secondNumber), + "/" => AppStore.Calculator.Divide(firstNumber, secondNumber), + _ => 0 + }; + + display.Text = result.ToString(); + } + catch (DivideByZeroException) + { + MessageBox.Show("除数不能为零", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + operation = ""; + } + break; + + default: + display.Text += button.Text; + break; + } + } + } +} diff --git a/tools/CalculatorToolCard.cs b/tools/CalculatorToolCard.cs new file mode 100644 index 0000000..4044524 --- /dev/null +++ b/tools/CalculatorToolCard.cs @@ -0,0 +1,38 @@ +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace AppStore +{ + public class CalculatorToolCard : ToolCard + { + public CalculatorToolCard() + { + ToolName = "计算器"; + try + { + string iconPath = Path.Combine(Application.StartupPath, "img", "resource", "png", "Calculator.png"); + if (File.Exists(iconPath)) + { + ToolIcon = Image.FromFile(iconPath); + } + else + { + ToolIcon = SystemIcons.Application.ToBitmap(); + } + } + catch + { + ToolIcon = SystemIcons.Application.ToBitmap(); + } + this.ToolCardClicked += OnCalculatorCardClicked; + UpdateDisplay(); + } + + private void OnCalculatorCardClicked(object sender, EventArgs e) + { + var calculatorForm = new CalculatorForm(); + calculatorForm.ShowDialog(); + } + } +}