当前位置: 首页 > news >正文

WPF实战案例 | C# WPF实现计算器源码

在这里插入图片描述
在这里插入图片描述

WPF实战案例 | C# WPF实现计算器源码

  • 一、设计来源
    • 计算器应用程序讲解
    • 1.1 主界面
    • 1.2 计算界面
  • 二、效果和源码
    • 2.1 界面设计(XAML)
    • 2.2 代码逻辑(C#)
    • 2.3 实现步骤总结
  • 源码下载
  • 更多优质源码分享

作者:xcLeigh
文章地址:https://blog.csdn.net/weixin_43151418/article/details/145280091


WPF实战案例 | C# WPF实现计算器源码, C# WPF实现计算器源码,这只是一个简单的实现,你可以根据需要添加更多的功能,如处理负数、添加更多的运算符(如平方根、百分比等)、使用更复杂的布局或引入 MVVM 架构以更好地分离逻辑和界面。如果你对其中的某个部分有具体的问题,或者想对这个计算器进行扩展,可以随时问我。这个源码让你快速搭建自己的应用程序,注释完整,代码规范,各种风格都有,代码上手简单,代码独立,可以直接用程序打开,运行使用。也可直接点击EXE运行程序。

一、设计来源

本文章是分类专栏【WPF 从入门到精通
】下的里面的一篇,专栏里面包括网站,窗体应用程序的源码,技术点解析等案列源码,让你更加掌握WPF。持续更新中,欢迎大家关注,一起学习交流。

✂ 点击快速进入专栏

计算器应用程序讲解

说明:

  • 数字输入

通常使用数字键(0-9)来输入参与计算的数值。可以通过多次点击数字键输入多位数,计算器会将输入的数字按照从左到右的顺序依次组合。

  • 运算符输入

加(+):用于执行加法运算,将前后输入的两个数相加。例如,输入 2,再点击 +,接着输入 3,最后点击 =,结果为 5。

减(-):用于执行减法运算,用前一个数减去后一个数。例如,输入 5,点击 -,再输入 2,点击 =,结果为 3。

乘(× 或 *):执行乘法运算,将前后两个数相乘。如输入 4,点击 × 或 *,输入 3,点击 =,结果为 12。

除(÷ 或 /):进行除法运算,用前一个数除以后一个数。例如,输入 8,点击 ÷ 或 /,输入 2,点击 =,结果为 4。

1.1 主界面

        计算器窗体主界面,展示自己的logo和标题(这里可以自定义,可以扩展自己想要的风格),通常使用数字键(0-9)来输入参与计算的数值。可以通过多次点击数字键输入多位数,计算器会将输入的数字按照从左到右的顺序依次组合。目前实现了基本的加减乘除,小数,清空,回退一步等功能。

在这里插入图片描述

1.2 计算界面

        计算器窗体计算界面,展示自己的logo和标题(这里可以自定义,可以扩展自己想要的风格),通常使用数字键(0-9)来输入参与计算的数值。可以通过多次点击数字键输入多位数,计算器会将输入的数字按照从左到右的顺序依次组合。目前实现了基本的加减乘除,小数,清空,回退一步等功能。

在这里插入图片描述

二、效果和源码

2.1 界面设计(XAML)

<Window x:Class="WpfCalculator.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="Calculator" Height="450" Width="350"><Grid><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><TextBox Grid.Row="0" x:Name="resultTextBox" IsReadOnly="True" TextAlignment="Right" FontSize="20"/><Grid Grid.Row="1"><Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Button Grid.Row="0" Grid.Column="0" Content="7" Click="Button_Click"/><Button Grid.Row="0" Grid.Column="1" Content="8" Click="Button_Click"/><Button Grid.Row="0" Grid.Column="2" Content="9" Click="Button_Click"/><Button Grid.Row="0" Grid.Column="3" Content="+" Click="Operator_Click"/><Button Grid.Row="1" Grid.Column="0" Content="4" Click="Button_Click"/><Button Grid.Row="1" Grid.Column="1" Content="5" Click="Button_Click"/><Button Grid.Row="1" Grid.Column="2" Content="6" Click="Button_Click"/><Button Grid.Row="1" Grid.Column="3" Content="-" Click="Operator_Click"/><Button Grid.Row="2" Grid.Column="0" Content="1" Click="Button_Click"/><Button Grid.Row="2" Grid.Column="1" Content="2" Click="Button_Click"/><Button Grid.Row="2" Grid.Column="2" Content="3" Click="Button_Click"/><Button Grid.Row="2" Grid.Column="3" Content="*" Click="Operator_Click"/><Button Grid.Row="3" Grid.Column="0" Content="0" Click="Button_Click"/><Button Grid.Row="3" Grid.Column="1" Content="." Click="Button_Click"/><Button Grid.Row="3" Grid.Column="2" Content="=" Click="Equal_Click"/><Button Grid.Row="3" Grid.Column="3" Content="/" Click="Operator_Click"/><Button Grid.Row="4" Grid.Column="0" Content="C" Click="Clear_Click" Grid.ColumnSpan="2"/><Button Grid.Row="4" Grid.Column="2" Content="AC" Click="AllClear_Click" Grid.ColumnSpan="2"/></Grid></Grid>
</Window>

说明:
    使用 Grid 布局来排列 TextBox(显示结果)和多个 Button(数字键、运算符键、清除键等)。

    为 Button 元素绑定相应的 Click 事件处理程序。

2.2 代码逻辑(C#)

using System;
using System.Windows;namespace WpfCalculator
{public partial class MainWindow : Window{private string currentInput = "";private string operatorValue = "";private double firstOperand = 0;private bool isOperatorClicked = false;public MainWindow(){InitializeComponent();}private void Button_Click(object sender, RoutedEventArgs e){if (isOperatorClicked){resultTextBox.Text = "";isOperatorClicked = false;}Button button = (Button)sender;currentInput += button.Content.ToString();resultTextBox.Text = currentInput;}private void Operator_Click(object sender, RoutedEventArgs e){if (double.TryParse(currentInput, out double operand)){firstOperand = operand;}operatorValue = ((Button)sender).Content.ToString();isOperatorClicked = true;}private void Equal_Click(object sender, RoutedEventArgs e){if (double.TryParse(currentInput, out double secondOperand)){switch (operatorValue){case "+":resultTextBox.Text = (firstOperand + secondOperand).ToString();break;case "-":resultTextBox.Text = (firstOperand - secondOperand).ToString();break;case "*":resultTextBox.Text = (firstOperand * secondOperand).ToString();break;case "/":if (secondOperand!= 0){resultTextBox.Text = (firstOperand / secondOperand).ToString();}else{MessageBox.Show("Cannot divide by zero");}break;}currentInput = resultTextBox.Text;}}private void Clear_Click(object sender, RoutedEventArgs e){currentInput = "";resultTextBox.Text = "";}private void AllClear_Click(object sender, RoutedEventArgs e){currentInput = "";operatorValue = "";firstOperand = 0;isOperatorClicked = false;resultTextBox.Text = "";}}
}

说明:

  • currentInput:存储当前输入的数字字符串。
  • operatorValue:存储当前选择的运算符。
  • firstOperand:存储第一个操作数。
  • isOperatorClicked:标志是否已经点击了运算符。
  • Button_Click 方法:将按钮的内容添加到 currentInput 中,并更新 - resultTextBox 的显示。
  • Operator_Click 方法:将当前输入转换为操作数存储在 firstOperand 中,并存储当前运算符,设置 isOperatorClicked 为 true。
  • Equal_Click 方法:将当前输入转换为第二个操作数,根据存储的运算符进行相应计算,并处理除以 0 的情况,更新结果显示。
  • Clear_Click 方法:清除当前输入。
  • AllClear_Click 方法:清除所有输入和操作状态。

2.3 实现步骤总结

    在 XAML 中设计计算器的界面布局,包括 TextBox 和 Button 元素,并为 Button 绑定 Click 事件。

    在 C# 代码中实现事件处理程序,处理数字输入、运算符输入、等于操作和清除操作。

    存储和更新操作数和运算符,根据用户输入进行相应的计算操作。


源码下载

注:源码下载在文章头部也可以点击下载,跟这里的是一样的

WPF实战案例 | C# WPF实现计算器源码(源码) 点击下载
在这里插入图片描述

更多优质源码分享

  • 【百篇源码模板】html5各行各业官网模板源码下载

  • 【模板源码】html实现酷炫美观的可视化大屏(十种风格示例,附源码)

  • 【VUE系列】VUE3实现个人网站模板源码

  • 【HTML源码】HTML5小游戏源码

  • 【C#实战案例】C# Winform贪吃蛇小游戏源码


--------------- 业精于勤,荒于嬉 ---------------

请添加图片描述

--------------- 行成于思,毁于随 ---------------

     💢 关注博主 带你实现畅游前后端

     🏰 大屏可视化 带你体验酷炫大屏

     💯 神秘个人简介 带你体验不一样得介绍

     💘 为爱表白 为你那个TA,体验别致的浪漫惊喜

     🎀 酷炫邀请函 带你体验高大上得邀请


     ① 🉑提供云服务部署(有自己的阿里云);
     ② 🉑提供前端、后端、应用程序、H5、小程序、公众号、大作业等相关业务;
     如🈶合作请联系我,期待您的联系。
    :本文撰写于CSDN平台,作者:xcLeigh(所有权归作者所有),https://blog.csdn.net/weixin_43151418,如果相关下载没有跳转,请查看这个地址,相关链接没有跳转,皆是抄袭本文,转载请备注本文原地址。


     亲,码字不易,动动小手,欢迎 点赞 ➕ 收藏,如 🈶 问题请 留言(私信或评论),博主看见后一定及时给您答复,💌💌💌


原文地址:https://blog.csdn.net/weixin_43151418/article/details/145280091(防止抄袭,原文地址不可删除)

http://www.lryc.cn/news/524474.html

相关文章:

  • AutoGen入门——快速实现多角色、多用户、多智能体对话系统
  • LeetCode 热题 100_全排列(55_46_中等_C++)(递归(回溯))
  • 将 AzureBlob 的日志通过 Azure Event Hubs 发给 Elasticsearch(1.标准版)
  • pthread_exit函数
  • 1月21日星期二今日早报简报微语报早读
  • 【2024年终总结】我与CSDN的一年
  • openssl 正确生成v3带SAN的证书
  • Golang Gin系列-5:数据模型和数据库
  • 比简单工厂更好的 - 工厂方法模式(Factory Method Pattern)
  • 分布式搜索引擎02
  • 阿里云安装mikrotik7配置内网互通
  • Docker网段和服务器ip冲突导致无法访问网络的解决方法
  • Kubernetes 集群中安装和配置 Kubernetes Dashboard
  • Android开发之Spinner
  • 【c++继承篇】--继承之道:在C++的世界中编织血脉与传承
  • 分布式系统通信解决方案:Netty 与 Protobuf 高效应用
  • 计算机网络 (54)系统安全:防火墙与入侵检测
  • stack底层实现细节
  • 工业相机 SDK 二次开发-Halcon 插件
  • map和set的使用(一)详解
  • ARP 表、MAC 表、路由表、跨网段 ARP
  • 37.构造回文字符串问题|Marscode AI刷题
  • ssm-mybatisPlus学习笔记
  • 【算法学习笔记】35:扩展欧几里得算法求解线性同余方程
  • 线性规划:机器学习中的优化利器
  • Ubuntu开发中的问题
  • MAC 地址转换为标准大写格式
  • 使用插件SlideVerify实现滑块验证
  • 深入探索 Nginx 的高级用法:解锁 Web 服务器的强大潜能
  • (01)搭建开发环境