【C#】WPF实现简易计算器

在这里插入图片描述
MainWindow.xaml

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel 
            Orientation="Vertical"
            >
            <StackPanel
                Orientation="Horizontal"
                >
                <TextBox  Name="inputContent" KeyDown="KeyDown" FontSize="30" VerticalContentAlignment="Bottom" TextAlignment="Right" InputMethod.IsInputMethodEnabled="False" Width="230" Height="45" Margin="10,20,0,0"/>
            </StackPanel>
            <StackPanel
                Orientation="Horizontal"
                >
                <Button Click="OnClick" Name="btn7" Width="50" Height="30" Margin="10,10,0,0" Content="7"/>
                <Button Click="OnClick" Name="btn8" Width="50" Height="30" Margin="10,10,0,0" Content="8"/>
                <Button Click="OnClick" Name="btn9" Width="50" Height="30" Margin="10,10,0,0" Content="9"/>
                <Button Click="OnClick" Name="btn_chu" Width="50" Height="30" Margin="10,10,0,0" Content="÷"/>
            </StackPanel>
            <StackPanel
                Orientation="Horizontal"
                >
                <Button Click="OnClick" Name="btn4" Width="50" Height="30" Margin="10,10,0,0" Content="4"/>
                <Button Click="OnClick" Name="btn5" Width="50" Height="30" Margin="10,10,0,0" Content="5"/>
                <Button Click="OnClick" Name="btn6" Width="50" Height="30" Margin="10,10,0,0" Content="6"/>
                <Button Click="OnClick" Name="btn_cheng" Width="50" Height="30" Margin="10,10,0,0" Content="×"/>
            </StackPanel>
            <StackPanel
                Orientation="Horizontal"
                >
                <Button Click="OnClick" Name="btn1" Width="50" Height="30" Margin="10,10,0,0" Content="1"/>
                <Button Click="OnClick" Name="btn2" Width="50" Height="30" Margin="10,10,0,0" Content="2"/>
                <Button Click="OnClick" Name="btn3" Width="50" Height="30" Margin="10,10,0,0" Content="3"/>
                <Button Click="OnClick" Name="btn_jia" Width="50" Height="30" Margin="10,10,0,0" Content="+"/>
            </StackPanel>
            <StackPanel
                Orientation="Horizontal"
                >
                <Button Click="OnClick" Name="btn0" Width="110" Height="30" Margin="10,10,0,0" Content="0"/>
                <Button Click="OnClick" Name="btn_dian" Width="50" Height="30" Margin="10,10,0,0" Content="."/>
                <Button Click="OnClick" Name="btn_jian" Width="50" Height="30" Margin="10,10,0,0" Content="-"/>
            </StackPanel>
            <StackPanel
                Orientation="Horizontal"
                >
                <Button Click="OnClick" Name="btn_qingchu" Width="110" Height="30" Margin="10,10,0,0" Content="C"/>
                <Button Click="OnClick" Name="btn_dengyu" Width="110" Height="30" Margin="10,10,0,0" Content="="/>
            </StackPanel>
        </StackPanel>

    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApp2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private string tempContent = "";
        private Stack<double> st0;
        private Stack<char> st1;
        public MainWindow()
        {
            st0 = new Stack<double>();
            st1 = new Stack<char>();
            InitializeComponent();
        }
        private void KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }
        private void OnClick(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if(btn!=null)
            {
                string value = btn.Content.ToString();
                switch (value)
                {
                    case "C":
                        {
                            tempContent = "";
                            inputContent.Text = tempContent;
                            break;
                        }
                    case "+":
                        {
                            if (tempContent == "")
                            {
                                return;
                            }
                            tempContent += "+";
                            inputContent.Text = tempContent;
                            break;
                        }
                    case "-":
                        {
                            if (tempContent == "")
                            {
                                return;
                            }
                            tempContent += "-";
                            inputContent.Text = tempContent;
                            break;
                        }
                    case "×":
                        {
                            if (tempContent == "")
                            {
                                return;
                            }
                            tempContent += "×";
                            inputContent.Text = tempContent;
                            break;
                        }
                    case "÷":
                        {
                            if (tempContent == "")
                            {
                                return;
                            }
                            tempContent += "÷";
                            inputContent.Text = tempContent;
                            break;
                        }
                    case "=":
                        {
                            if (tempContent == "")
                            {
                                return;
                            }
                            //调用表达式求值函数
                            tempContent = getSum(inputContent.Text);
                            inputContent.Text = tempContent;
                            break;
                        }
                    default:
                        {
                            tempContent += btn.Content;
                            inputContent.Text = tempContent;
                            break;
                        }
                }

            }

        }

        string getSum(string s)
        {
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] != '+' && s[i] != '-' && s[i] != '×' && s[i] != '÷')
                {
                    for (int j = i; j < s.Length; j++)
                    {
                        if (s[j] == '+' || s[j] == '-' || s[j] == '×' || s[j] == '÷')
                        {
                            st0.Push(double.Parse(s.Substring(i, j - i)));
                            i = j - 1;
                            break;
                        }
                        if (j == s.Length - 1)
                        {
                            st0.Push(double.Parse(s.Substring(i, j + 1 - i)));
                            i = s.Length;
                        }
                    }
                }
                else
                {
                    if (st1.Count == 0)
                    {
                        st1.Push(s[i]);
                    }
                    else if ((s[i] == '+' || s[i] == '-') && (st1.Peek() == '×' || st1.Peek() == '÷'))
                    {
                        double a1 = 0, b1 = 0;
                        a1 = st0.Pop();
                        b1 = st0.Pop();
                        char op1 = st1.Peek();
                        switch (op1)
                        {
                            case '×':
                                {
                                    st0.Push(a1 * b1);
                                    break;
                                }
                            case '÷':
                                {
                                    st0.Push(b1 / a1);
                                    break;
                                }
                        }
                        st1.Pop();
                        st1.Push(s[i]);
                    }
                    else
                    {
                        st1.Push(s[i]);
                    }
                }
            }
            
            while (st1.Count != 0)
            {
                char op2 = st1.Pop();
                double a = 0, b = 0;
                a = st0.Pop();
                b = st0.Pop();
                switch (op2)
                {
                    case '+':
                        {
                            st0.Push(a + b);
                            break;
                        }
                    case '-':
                        {
                            st0.Push(b - a);
                            break;
                        }
                    case '×':
                        {
                            st0.Push(b * a);
                            break;
                        }
                    case '÷':
                        {
                            st0.Push(b / a);
                            break;
                        }
                }
            }

            return st0.Peek().ToString();
        }
        
    }
}

参考文章
【C#】栈——表达式求值
C# wpf 做的一个简单的计算器

以下是一个简易计算器WPF应用程序中的MainWindow类示例: ```csharp using System; using System.Windows; using System.Windows.Controls; namespace Calculator { public partial class MainWindow : Window { private double num1, num2, result; private string operation; public MainWindow() { InitializeComponent(); } private void NumberButton_Click(object sender, RoutedEventArgs e) { Button button = (Button)sender; if (operation == null) { num1 = num1 * 10 + Convert.ToDouble(button.Content.ToString()); ResultLabel.Content = num1; } else { num2 = num2 * 10 + Convert.ToDouble(button.Content.ToString()); ResultLabel.Content = num2; } } private void OperationButton_Click(object sender, RoutedEventArgs e) { Button button = (Button)sender; operation = button.Content.ToString(); } private void EqualsButton_Click(object sender, RoutedEventArgs e) { switch (operation) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": result = num1 / num2; break; } ResultLabel.Content = result; num1 = result; num2 = 0; operation = null; } private void ClearButton_Click(object sender, RoutedEventArgs e) { num1 = 0; num2 = 0; operation = null; ResultLabel.Content = "0"; } } } ``` 该类包含了按钮点击事件的处理程序,用于处理数字、运算符和清除按钮的点击。通过使用num1、num2和operation变量来追踪计算器的状态。当数字按钮被点击时,它们被添加到num1或num2中,当运算符按钮被点击时,它们被设置为operation变量,当等号按钮被点击时,通过执行适当的操作计算结果。最后,当清除按钮被点击时,计算器被复位。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值