科学计算器

  • 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…