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

【C#】WPF实现简易计算器_第1张图片
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
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    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 做的一个简单的计算器

你可能感兴趣的:(C#)