WPF 加减乘除计算器

2021-03-07 10:26

阅读:552

标签:parse   main   string   his   img   lock   交互   ima   event   

原文:WPF 加减乘除计算器

技术图片

小玩意,毫无任何难度。

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 计算器
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private double n1, n2, n3;

        private double CM(double n1, double n2, string fun)
        {
            double n3 = 0;

            switch (fun)
            {
                case "+":
                    n3 = n1 + n2;
                    break;
                case "-":
                    n3 = n1 - n2;
                    break;
                case "x":
                    n3 = n1 * n2;
                    break;
                case "÷":
                    n3 = n1 / n2;
                    break;
                case "%":
                    n3 = n1 % n2;
                    break;
            }

            return n3;
        }

        private void Num_Click(object sender, RoutedEventArgs e)
        {
            var num = (sender as Button).Content;
            if (!string.IsNullOrWhiteSpace(T4.Text))
            {
                ClearFun();
            }
            if (!string.IsNullOrEmpty(T2.Text))
            {
                var d = (T3.Text + num).Replace(" ", string.Empty);

                T3.Text = d;
                n2 = double.Parse(T3.Text);
                return;
            }
            var c = (T1.Text + num).Replace(" ", string.Empty);
            T1.Text = c;
            n1 = double.Parse(T1.Text);

        }

        private void Fun_Click(object sender, RoutedEventArgs e)
        {
            var fun = (sender as Button).Content;
            if (!string.IsNullOrEmpty(T4.Text))
            {
                ClearFun();
                return;
            }
            if(!string.IsNullOrEmpty(T1.Text))
            T2.Text = fun.ToString();

        }

        private void Eq_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(T2.Text) || string.IsNullOrEmpty(T1.Text) || string.IsNullOrEmpty(T3.Text))
                return;
            n3 = CM(n1, n2, T2.Text);
            T4.Text = n3.ToString();
            T5.Text = "=";
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            ClearFun();

        }
        private void Back_Click(object sender,RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(T4.Text))
                return;
            if (!string.IsNullOrEmpty(T2.Text))
            {
                var d = T3.Text;
                T3.Text = d.Substring(0,d.Length-1);
                T3.Text = T3.Text == "" ? "0" : T3.Text;
                n2 = double.Parse(T3.Text == "" ? "0" : T3.Text);
                return;
            }
            var c = T1.Text;
            T1.Text = c.Substring(0, c.Length - 1);
            T1.Text = T1.Text == "" ? "0" : T1.Text;
            n1 = double.Parse(T1.Text);
        }

        private void ClearFun()
        {
            T1.Text = string.Empty;
            T2.Text = string.Empty;
            T3.Text = string.Empty;
            T4.Text = string.Empty;
            T5.Text = string.Empty;
        }
    }
}
技术图片

 

 

xaml:

技术图片
"计算器.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:计算器"
        mc:Ignorable="d"
        Title="MainWindow" Height="500" Width="400">
    

"auto"/>
            "*"/>
        "T1"/>
              "T2"/>
              "T3"/>
              "T5"/>
                 "T4"/>
            "1">
            "*"/>
                "*"/>
                "*"/>
                "*"/>
            "*"/>
                "*"/>
                "*"/>
                "*"/>
                "*"/>
            
技术图片

 

WPF 加减乘除计算器

标签:parse   main   string   his   img   lock   交互   ima   event   

原文地址:https://www.cnblogs.com/lonelyxmas/p/12833911.html


评论


亲,登录后才可以留言!