WPF學習筆記(27)科學計算器

科學計算器

  • 1. 前端界面
  • 2. 功能代碼
  • 3. 效果展示


1. 前端界面

<Window x:Class="Cal.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Cal"mc:Ignorable="d"Title="科學計算器" Height="600" Width="400"WindowStartupLocation="CenterScreen"ResizeMode="NoResize"><Window.Resources><Style TargetType="Button"><Setter Property="FontSize" Value="20"/><Setter Property="Margin" Value="5"/><Setter Property="Background" Value="#FF333333"/><Setter Property="Foreground" Value="White"/><Setter Property="BorderThickness" Value="0"/><Setter Property="BorderBrush" Value="#FF555555"/></Style></Window.Resources><Grid Background="#FF222222"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><!-- 顯示區域 --><Border Grid.Row="0" Background="#FF111111" Padding="10,10,10,10" Margin="10,10,10,10"><StackPanel><TextBlock x:Name="txtHistory" Foreground="#FFAAAAAA" FontSize="16" HorizontalAlignment="Right" Margin="0,0,0,5"/><TextBlock x:Name="txtDisplay" Foreground="White" FontSize="36" HorizontalAlignment="Right" Text="0"/></StackPanel></Border><!-- 按鈕區域 --><Grid Grid.Row="1" Margin="10"><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="*"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><!-- 第二行 --><Button x:Name="btnClear" Grid.Row="1" Grid.Column="0" Content="C" Background="#FFAA0000"/><Button x:Name="btnBack" Grid.Row="1" Grid.Column="1" Content="?"/><Button x:Name="btnPercent" Grid.Row="1" Grid.Column="2" Content="%"/><Button x:Name="btnDivide" Grid.Row="1" Grid.Column="3" Content="/" Background="#FF555555"/><Button x:Name="btnSqrt" Grid.Row="1" Grid.Column="4" Content="" Background="#FF555555"/><!-- 第一行 --><Button x:Name="btnTan" Grid.Row="0" Grid.Column="0" Content="tan" Background="#FF555555"/><Button x:Name="btnLog" Grid.Row="0" Grid.Column="1" Content="log" Background="#FF555555"/><Button x:Name="btnLn" Grid.Row="0" Grid.Column="2" Content="ln" Background="#FF555555"/><Button x:Name="btnPi" Grid.Row="0" Grid.Column="3" Content="π" Background="#FF555555"/><Button x:Name="btnFactorial" Grid.Row="0" Grid.Column="4" Content="n!" Background="#FF555555"/><!-- 第三行 --><Button x:Name="btn7" Grid.Row="2" Grid.Column="0" Content="7" /><Button x:Name="btn8" Grid.Row="2" Grid.Column="1" Content="8"/><Button x:Name="btn9" Grid.Row="2" Grid.Column="2" Content="9"/><Button x:Name="btnMultiply" Grid.Row="2" Grid.Column="3" Content="×" Background="#FF555555"/><Button x:Name="btnPower" Grid.Row="2" Grid.Column="4" Content="x^y" Background="#FF555555"/><!-- 第四行 --><Button x:Name="btn4" Grid.Row="3" Grid.Column="0" Content="4"/><Button x:Name="btn5" Grid.Row="3" Grid.Column="1" Content="5"/><Button x:Name="btn6" Grid.Row="3" Grid.Column="2" Content="6"/><Button x:Name="btnSubtract" Grid.Row="3" Grid.Column="3" Content="-" Background="#FF555555"/><Button x:Name="btnSin" Grid.Row="3" Grid.Column="4" Content="sin" Background="#FF555555"/><!-- 第五行 --><Button x:Name="btn1" Grid.Row="4" Grid.Column="0" Content="1"/><Button x:Name="btn2" Grid.Row="4" Grid.Column="1" Content="2"/><Button x:Name="btn3" Grid.Row="4" Grid.Column="2" Content="3"/><Button x:Name="btnAdd" Grid.Row="4" Grid.Column="3" Content="+" Background="#FF555555"/><Button x:Name="btnCos" Grid.Row="4" Grid.Column="4" Content="cos" Background="#FF555555"/><!-- 第六行 --><Button x:Name="btn0" Grid.Row="5" Grid.Column="0" Content="0"/><Button x:Name="btnDecimal" Grid.Row="5" Grid.Column="1" Content="."/><Button x:Name="btnPlusMinus" Grid.Row="5" Grid.Column="2" Content="±"/><Button x:Name="btnEquals" Grid.Row="5" Grid.Column="3" Grid.ColumnSpan="2" Content="=" Background="#FF007ACC"/></Grid></Grid>
</Window>

2. 功能代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace Cal
{/// <summary>/// MainWindow.xaml 的交互邏輯/// </summary>public partial class MainWindow : Window{private string currentInput = "0";//當前輸入private string previousInput = "";//之前輸入private string operation = "";//操作符private bool newInput = true;private bool operationPerformed = false;//操作已執行public MainWindow(){InitializeComponent();// 數字按鈕事件btn0.Click += NumberButton_Click;btn1.Click += NumberButton_Click;btn2.Click += NumberButton_Click;btn3.Click += NumberButton_Click;btn4.Click += NumberButton_Click;btn5.Click += NumberButton_Click;btn6.Click += NumberButton_Click;btn7.Click += NumberButton_Click;btn8.Click += NumberButton_Click;btn9.Click += NumberButton_Click;// 運算符按鈕事件btnAdd.Click += OperatorButton_Click;btnSubtract.Click += OperatorButton_Click;btnMultiply.Click += OperatorButton_Click;btnDivide.Click += OperatorButton_Click;btnEquals.Click += EqualsButton_Click;// 功能按鈕事件btnClear.Click += ClearButton_Click;btnBack.Click += BackButton_Click;btnDecimal.Click += DecimalButton_Click;btnPlusMinus.Click += PlusMinusButton_Click;btnPercent.Click += PercentButton_Click;// 科學計算按鈕事件btnSqrt.Click += ScientificButton_Click;btnPower.Click += ScientificButton_Click;btnSin.Click += ScientificButton_Click;btnCos.Click += ScientificButton_Click;btnTan.Click += ScientificButton_Click;btnLog.Click += ScientificButton_Click;btnLn.Click += ScientificButton_Click;btnPi.Click += ScientificButton_Click;btnFactorial.Click += ScientificButton_Click;}private void NumberButton_Click(object sender, RoutedEventArgs e){Button button = (Button)sender;if (currentInput == "0" || newInput){currentInput = button.Content.ToString();newInput = false;}else{currentInput += button.Content.ToString();}UpdateDisplay();}private void OperatorButton_Click(object sender, RoutedEventArgs e){Button button = (Button)sender;if (!newInput && !operationPerformed){Calculate();}operation = button.Content.ToString();previousInput = currentInput;newInput = true;operationPerformed = false;UpdateHistory();}private void EqualsButton_Click(object sender, RoutedEventArgs e){Calculate();operation = "";UpdateHistory();newInput = true;operationPerformed = true;}private void Calculate(){if (string.IsNullOrEmpty(previousInput) || string.IsNullOrEmpty(operation))return;double num1 = double.Parse(previousInput);double num2 = double.Parse(currentInput);double result = 0;switch (operation){case "+":result = num1 + num2;break;case "-":result = num1 - num2;break;case "×":result = num1 * num2;break;case "/":result = num1 / num2;break;}currentInput = result.ToString();UpdateDisplay();}private void ScientificButton_Click(object sender, RoutedEventArgs e){Button button = (Button)sender;double num = double.Parse(currentInput);double result = 0;switch (button.Content.ToString()){case "√":result = Math.Sqrt(num);break;case "x^y":// 需要額外處理冪運算previousInput = currentInput;operation = "^";newInput = true;UpdateHistory();return;case "sin":result = Math.Sin(num * Math.PI / 180); // 轉換為弧度break;case "cos":result = Math.Cos(num * Math.PI / 180);break;case "tan":result = Math.Tan(num * Math.PI / 180);break;case "log":result = Math.Log10(num);break;case "ln":result = Math.Log(num);break;case "π":currentInput = Math.PI.ToString();UpdateDisplay();return;case "n!":result = Factorial((int)num);break;}currentInput = result.ToString();UpdateDisplay();newInput = true;}private int Factorial(int n){if (n <= 1)return 1;return n * Factorial(n - 1);}private void ClearButton_Click(object sender, RoutedEventArgs e){currentInput = "0";previousInput = "";operation = "";newInput = true;UpdateDisplay();txtHistory.Text = "";}private void BackButton_Click(object sender, RoutedEventArgs e){if (currentInput.Length > 1){currentInput = currentInput.Substring(0, currentInput.Length - 1);}else{currentInput = "0";newInput = true;}UpdateDisplay();}private void DecimalButton_Click(object sender, RoutedEventArgs e){if (!currentInput.Contains(".")){currentInput += ".";UpdateDisplay();}}private void PlusMinusButton_Click(object sender, RoutedEventArgs e){if (currentInput != "0"){if (currentInput.StartsWith("-")){currentInput = currentInput.Substring(1);}else{currentInput = "-" + currentInput;}UpdateDisplay();}}private void PercentButton_Click(object sender, RoutedEventArgs e){double num = double.Parse(currentInput);currentInput = (num / 100).ToString();UpdateDisplay();}private void UpdateDisplay(){txtDisplay.Text = currentInput;}private void UpdateHistory(){//$為字符串拼接的優化,等同于string.Format(),示例如下:txtHistory.Text = $"{previousInput} {operation}";}}
}

3. 效果展示

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/88895.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/88895.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/88895.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【Linux系列】unzip file 命令

博客目錄掌握 unzip -o 命令&#xff1a;高效解壓并覆蓋文件的完整指南一、unzip 命令基礎二、-o 選項的核心作用三、典型應用場景四、與其他選項的組合使用五、注意事項與風險防范六、替代方案與高級技巧掌握 unzip -o 命令&#xff1a;高效解壓并覆蓋文件的完整指南 在日常的…

1965–2022年中國大陸高分辨率分部門用水數據集,包含:灌溉用水、工業制造用水、生活用水和火電冷卻

1965–2022年中國大陸高分辨率部門用水數據集 高質量用水數據集對推進變化環境下的水資源研究至關重要。然而&#xff0c;現有中國用水數據通常按行政區劃或流域匯總&#xff0c;時空分辨率不足&#xff0c;難以支撐人類用水模式及時空變化特征的精細化分析。為此&#xff0c;…

git中的指令解釋

在 Git 的 diff 輸出中&#xff0c;---、 和 ... 塊的含義如下&#xff1a; 1. --- a/1.py 和 b/1.py --- a/1.py&#xff1a;表示舊版本的文件路徑&#xff08;通常是工作目錄或上一次提交的版本&#xff09;。 b/1.py&#xff1a;表示新版本的文件路徑&#xff08;通常是暫存…

STM32實現四自由度機械臂(SG90舵機)多功能控制(軟件篇freertos)

書接上回的硬件篇STM32控制四自由度機械臂&#xff08;SG90舵機&#xff09;&#xff08;硬件篇&#xff09;&#xff08;簡單易復刻&#xff09;-CSDN博客 此時硬件平臺已經搭建完畢&#xff0c;軟件總共設計了三種模式&#xff0c;分別為 模式1&#xff1a;搖桿&藍牙模…

docker常用命令集(2)

接前一篇文章&#xff1a;docker常用命令集&#xff08;1&#xff09; 本文內容參考&#xff1a; Docker build 命令 | 菜鳥教程 docker基礎(二)之docker build-CSDN博客 Docker push 命令 | 菜鳥教程 Docker pull 命令 | 菜鳥教程 特此致謝&#xff01; 3. docker build …

舒爾特方格訓練小游戲流量主微信小程序開源

功能特點 游戲核心功能&#xff1a; 隨機生成55舒爾特方格 按順序點擊數字1-25 實時計時和嘗試次數統計 錯誤點擊反饋&#xff08;視覺和觸覺&#xff09; 數據統計&#xff1a; 記錄每次完成時間 保存歷史最佳成績 保存最近5次嘗試記錄 統計嘗試次數&#xff08;錯誤點擊&…

在Spring Boot 開發中 Bean 的聲明和依賴注入最佳的組合方式是什么?

在Spring Boot 開發中&#xff0c;社區和 Spring 官方已經形成了一套非常明確的最佳實踐。這個黃金組合就是&#xff1a; Bean 聲明&#xff1a;使用構造型注解&#xff08;Stereotype Annotations&#xff09;&#xff0c;如 Service, Repository, Component 等。依賴注入&…

Oxygen XML Editor 26.0編輯器

Oxygen XML Editor 26.0編輯器 歡迎使用Oxygen XML Editor 26.0編輯器準備工作安裝javajdk安裝jdk驗證Oxygen XML Editor 26.0安裝歡迎使用Oxygen XML Editor 26.0編輯器 準備工作安裝java Java官網下載地址:https://www.oracle.com/java/technologies/ Oxygen XML Editor 2…

AWS Lambda Container 方式部署 Flask 應用并通過 API Gateway 提供訪問

前言 一年前寫過一篇 Lambda 運行 Flask 應用的博文: https://lpwmm.blog.csdn.net/article/details/139756140 當時使用的是 ZIP 包方式部署應用代碼, 對于簡單的 API 開發用起來還是可以的, 但是如果需要集成到 CI/CD pipeline 里面就有點不太優雅. 本文將介紹使用容器方式…

React虛擬DOM的進化之路

引言 在Web前端開發中&#xff0c;用戶交互的流暢性和頁面性能一直是核心挑戰。早期&#xff0c;開發者直接操作真實DOM&#xff08;Document Object Model&#xff09;時&#xff0c;頻繁的重排&#xff08;reflow&#xff09;和重繪&#xff08;repaint&#xff09;導致性能…

(7)機器學習小白入門 YOLOv:機器學習模型訓練詳解

— (1)機器學習小白入門YOLOv &#xff1a;從概念到實踐 (2)機器學習小白入門 YOLOv&#xff1a;從模塊優化到工程部署 (3)機器學習小白入門 YOLOv&#xff1a; 解鎖圖片分類新技能 (4)機器學習小白入門YOLOv &#xff1a;圖片標注實操手冊 (5)機器學習小白入門 YOLOv&#xff…

初識MySQL(三)之主從配置與讀寫分離實戰

主重復制 主重復制原理master開啟二進制日志記錄slave開啟IO進程&#xff0c;從master中讀取二進制日志并寫入slave的中繼日志slave開啟SQL進程&#xff0c;從中繼日志中讀取二進制日志并進行重放最終&#xff0c;達到slave與master中數據一致的狀態&#xff0c;我們稱作為主從…

RabbitMQ面試精講 Day 2:RabbitMQ工作模型與消息流轉

【RabbitMQ面試精講 Day 2】RabbitMQ工作模型與消息流轉 開篇 歡迎來到"RabbitMQ面試精講"系列的第2天&#xff0c;今天我們將深入探討RabbitMQ的工作模型與消息流轉機制。這是面試中最常被問到的核心知識點之一&#xff0c;90%的RabbitMQ面試都會涉及消息流轉流程…

基于SpringBoot3集成Kafka集群

1. build.gradle依賴引入 implementation org.springframework.kafka:spring-kafka:3.2.02. 新增kafka-log.yml文件 在resource/config下面新增kafka-log.yml&#xff0c;配置主題與消費者組 # Kafka消費者群組 kafka:consumer:group:log-data: log-data-grouptopic:log-data: …

wpf Canvas 導出圖片

在WPF中將Canvas導出為圖片主要涉及以下關鍵步驟和注意事項: ?核心實現方法?使用RenderTargetBitmap將Canvas渲染為位圖,再通過PngBitmapEncoder保存為PNG文件。需注意臨時移除Canvas的布局變換(LayoutTransform)以避免渲染異常?1。示例代碼片段:CanvasExporter.cs pu…

lvs負載均衡實操模擬

目錄 一、配置準備 二、NET模式 修改LVS端 開啟路由 修改對內網卡 ens160 修改對外網卡 ens224 加載網卡配置文件 修改web1端 修改網卡信息 重啟網絡 檢測 配置web2 檢測 驗證配置是否正常 啟動nginx服務 驗證以上配置 添加lvs規則 驗證 三、DR模式 修改…

Spring Boot 是如何簡化 IoC 的配置的?

首先Spring Boot 并沒有發明新的 IoC 理論&#xff0c;它做的也不是替換掉 Spring IoC 容器。相反&#xff0c;Spring Boot 是 Spring IoC 思想的實踐者和簡化者。它通過**“約定優于配置”&#xff08;Convention over Configuration&#xff09;**的理念&#xff0c;將原本繁…

Go語言中的組合式接口設計模式

文章目錄Go語言中的組合式接口設計模式背景和需求組合式接口設計Go語言中的組合式接口設計模式 背景和需求 在微服務架構和復雜業務系統中&#xff0c;我們經常需要調用多個外部服務或內部模塊。傳統的做法是將所有方法都放在一個大接口中&#xff0c;但這種設計會導致接口臃…

React - createPortal

什么是createPortal&#xff1f;注意這是一個API&#xff0c;不是組件&#xff0c;他的作用是&#xff1a;將一個組件渲染到DOM的任意位置&#xff0c;跟Vue的Teleport組件類似。用法 import { createPortal } from react-dom;const App () > {return createPortal(<div…

Cursor的使用

Cursor的使用 Ctrl L 打開歷史對話記錄 Tab智能助手 1.單行/多行補全 已有代碼片段&#xff1a; //需求&#xff1a;寫一個工具類計算數組平均值 public class ArrayUtils {//按tab會完成補全 }按tab鍵- Cursor 自動生成代碼: //需求&#xff1a;寫一個工具類計算數組平均值 p…