首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

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

  • 25-02-14 15:40
  • 3654
  • 5510
blog.csdn.net

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

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

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

作者:xcLeigh
文章地址:http://iyenn.com/rec/1635714.html


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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

说明:
    使用 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 = "";
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

说明:

  • 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,如果相关下载没有跳转,请查看这个地址,相关链接没有跳转,皆是抄袭本文,转载请备注本文原地址。


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


原文地址:http://iyenn.com/rec/1635714.html(防止抄袭,原文地址不可删除)

接商务合作/毕业设计等,欢迎!
微信名片
注:本文转载自blog.csdn.net的xcLeigh的文章"https://blog.csdn.net/weixin_43151418/article/details/145280091"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

111
大数据
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top