小言_互联网的博客

C#课堂篇3_WPF实现一个简单的计算器

208人阅读  评论(0)

写在前面

使用WPF实现一个简单的小计算器,包括 基本的 四则运算 ,程序界面:


需要注意的是:

  • 开始默认显示0,当第一次输入第一个操作数,如果是非零的,就显示此数(注意需要把默认的0去掉,即输入123不能显示0123),如果第一次输入的是0,则还是显示一个0(不能是000)
  • 需要对操作数更新显示,即输入完成第一个操作数后,接着按下 运算符,然后 输入 第二个操作数的时候,需要清空当前显示的第一个操作数,而显示第二个操作数;当第二个操作数完成,按下 = 之后,需要将 第二个操作数 显示清空,换成 结果数 的显示

环境:Visual Studio 2019


代码

  • MainWindows.xaml
<Window x:Class="WPFDemo1.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:WPFDemo1"
        mc:Ignorable="d"
        Title="简单计算器" Height="450" Width="450">
    <Grid>
        <Grid.RowDefinitions >
            <RowDefinition  Height="60" ></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>

        </Grid.RowDefinitions>
        <!--显示-->
        <StackPanel Grid.Row="0"  Orientation="Horizontal" >
            <TextBox Name="resultText" Width="330" Height="60" Background="Yellow" HorizontalContentAlignment="Right" IsReadOnly="True" FontSize="36" FontWeight="Bold"></TextBox>
            <Button Width="100" Height="50" Content="归零" Click="buttClear_Click" FontSize="20" Background="red"></Button>
        </StackPanel>
        
        <!--各行-->

        <StackPanel Grid.Row="1" Orientation="Horizontal"  Width="450">
            <Button Width="50" Height="50" Margin="20" Content="9" Click="buttNum_Click"></Button>
            <Button Width="50" Height="50" Margin="20"  Content="8" Click="buttNum_Click"/>
            <Button Width="50" Height="50" Margin="20" Content="7" Click="buttNum_Click"></Button>
            <Button Width="50" Height="50" Margin="10" Content="+" Click="buttOper_Click"></Button>
        </StackPanel>

        <StackPanel Grid.Row="2"  Width="450" Orientation="Horizontal">
            <Button Width="50" Height="50" Margin="20" Content="6" Click="buttNum_Click"></Button>
            <Button Width="50" Height="50" Margin="20" Content="5" Click="buttNum_Click"></Button>
            <Button Width="50" Height="50" Margin="20" Content="4" Click="buttNum_Click"></Button>
            <Button Width="50" Height="50" Margin="10" Content="-" Click="buttOper_Click"></Button>
        </StackPanel>
        <StackPanel Grid.Row="3" Width="450" Orientation="Horizontal">
            <Button Width="50" Height="50" Margin="20" Content="3" Click="buttNum_Click"></Button>
            <Button Width="50" Height="50" Margin="20" Content="2" Click="buttNum_Click"></Button>
            <Button Width="50" Height="50" Margin="20" Content="1" Click="buttNum_Click"></Button>
            <Button Width="50" Height="50" Margin="10" Content="*" Click="buttOper_Click"></Button>
        </StackPanel>

        <StackPanel Grid.Row="4" Width="450" Orientation="Horizontal">
            <Button Width="50" Height="50" Margin="20" Content="0" Click="buttNum_Click"></Button>
            <Button Width="50" Height="50" Margin="20"  Content="." Click="buttPoint_Click"></Button>
            <Button Width="50" Height="50" Margin="20" Content="/" Click="buttOper_Click"></Button>
            <Button Width="50" Height="50" Margin="10"  Background="pink" Content="=" Click="resultButton"></Button>
        </StackPanel>


    </Grid>
</Window>

  • MainWindows.xaml.cs
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 WPFDemo1
{
   
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
   

        //操作数、操作符
        double db1;
        double db2;
        string strOper;


        //更新显示的数


        int type = 0;
        //拼接操作数

        bool isFirst = true;


        public MainWindow()
        {
   

            InitializeComponent();
            resultText.Text = "0";
        }



        private void buttNum_Click(object sender, RoutedEventArgs e)
        {
   
            Button btn = (Button)sender;
            //开始的时候需要默认显示 数字 0
            //如果只有一位数字,并且是 0 ,再次 点击 0 按钮,不做任何操作
            if (resultText.Text.Length == 1 && resultText.Text == "0" && btn.Content.ToString() == "0")
            {
   
                return;
            }
            //如果 第一个 输入的 是 非0数字,就将显示的默认数字0 清除,显示输入的数字
            else if (isFirst)
            {
   
                resultText.Text = "";
                isFirst = false;
            }
            //输入一个数 点击 运算 按钮,第一个数消失,存入db1中,方便接下来的操作
            //type == 1 的时候,显示的数字需要更新,当前数字需要记录
            if (type == 1)
            {
   


                //这里需要一个判断,防止 未输入 第一个数字,直接输入运算符引发报错

                if (resultText.Text != "")
                {
   
                    db1 = Convert.ToDouble(resultText.Text);
                    resultText.Text = "";
                    type = 0;

                }
                    

                


            }

            resultText.Text += btn.Content.ToString();

        }

        //记录运算符,然后改变 更新数字的标记 就是输入过 运算符后,就该显示第二个数字了
        private void buttOper_Click(object sender, RoutedEventArgs e)
        {
   
            Button btn = (Button)sender;
            strOper = btn.Content.ToString();
            type = 1;
        }

        private void resultButton(object sender, RoutedEventArgs e)
        {
   
            db2 = Convert.ToDouble(resultText.Text);
            switch (strOper)
            {
   
                case "+":
                    resultText.Text = (db1 + db2).ToString();
                    break;
                case "-":
                    resultText.Text = (db1 - db2).ToString();
                    break;
                case "*":
                    resultText.Text = (db1 * db2).ToString();
                    break;
                case "/":
                    resultText.Text = (db1 / db2).ToString();
                    break;
                    


            }

        }

        private void buttPoint_Click(object sender, RoutedEventArgs e)
        {
   
            resultText.Text += ".";
            isFirst = false;
        }

        private void buttClear_Click(object sender, RoutedEventArgs e)
        {
   
            resultText.Text = "0";
           
            db1 = 0.0;
            db2 = 0.0;

            //不要忘了
            isFirst = true;


        }
    }
}


转载:https://blog.csdn.net/qq_24654501/article/details/109861398
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场