C# wpf 做的一个简单的计算器

闲着无聊研究了下C#的wpf,主要是学习界面,第一次学习有可能有bug或写的不好的地方各位大佬们不要吐槽哈,只做了加减乘除,其他的算法没做不过原理很简单的用下C#的Math函数就行了,快下班了不想做了谁想做代码copy继续去实现。

Calculator.xaml文件

<Window x:Class="Calcu.Calculator"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Calculator" Height="320" Width="350"
        ResizeMode="NoResize"
        >
    <Grid>
        <StackPanel
            Orientation="Vertical"
            >
            <StackPanel
                Orientation="Horizontal"
                >
                <TextBox InputMethod.IsInputMethodEnabled="False"  KeyDown="Keydown" Name="inputContent" Margin="10,20,0,0" Width="230" Height="45" FontSize="30" FontStyle="Normal" FontFamily="SimSun" VerticalContentAlignment="Bottom" TextAlignment="Right" />
            </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="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="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="jian" 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="point" Width="50" Height="30" Margin="10,10,0,0" Content="."/>
                <Button Click="onClick" Name="jia" Width="50" Height="30" Margin="10,10,0,0" Content="+"/>
            </StackPanel>
            
            <StackPanel
                Orientation="Horizontal"
                >
                <Button Click="onClick" Name="clear" Width="110" Height="30" Margin="10,10,0,10" Content="C"/>
                <Button Click="onClick" Name="dengyu" Width="110" Height="30" Margin="10,10,0,10" Content="="/>
            </StackPanel>
        </StackPanel>
        
    </Grid>
</Window>

Calculator.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.Shapes;

namespace Calcu
{
    /// <summary>
    /// jisuanqi.xaml 的交互逻辑
    /// </summary>
    public partial class Calculator : Window
    {
        private string currentStatus = "";
        private double firstNum = -1;
        private double secondNum = -1;
        private string tempContent = "";
        private int firstNumLen = 0;
        private  const string PLUS = "+";
        private  const string SUBTRACTION = "-";
        private  const string MULTIPLICATION = "*";
        private  const string DIVISION = "/";
        private  const string EQUAL = "=";


        public Calculator()
        {
            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 = (string)btn.Content;
                 switch (value)
                 {
                     case DIVISION:
                         if (tempContent.Length == 0)
                         {
                             return;
                         }
                         firstNum = double.Parse(tempContent);
                         currentStatus = DIVISION;
                         tempContent += currentStatus;
                         inputContent.Text = tempContent;
                         firstNumLen = tempContent.Length;
                         break;

                     case MULTIPLICATION:
                         if (tempContent.Length == 0)
                         {
                             return;
                         }
                         firstNum = double.Parse(tempContent);
                         currentStatus = MULTIPLICATION;
                         tempContent += currentStatus;
                         inputContent.Text = tempContent;
                         firstNumLen = tempContent.Length;
                         break;

                     case PLUS:
                         if (tempContent.Length == 0)
                         {
                             return;
                         }
                         firstNum = double.Parse(tempContent);
                         currentStatus = PLUS;
                         tempContent += currentStatus;
                         inputContent.Text = tempContent;
                         firstNumLen = tempContent.Length;
                         break;

                     case SUBTRACTION:
                         if (tempContent.Length == 0)
                         {
                             return;
                         }
                         firstNum = double.Parse(tempContent);
                         currentStatus = SUBTRACTION;
                         tempContent += currentStatus;
                         inputContent.Text = tempContent;
                         firstNumLen = tempContent.Length;
                         break;

                     case EQUAL:
                         if (firstNum < 0 || currentStatus.Length == 0)
                         {
                             return;
                         }
                         Console.WriteLine(tempContent);
                         Console.WriteLine(firstNumLen);
                         Console.WriteLine(tempContent.Length);
                         string secondStr = tempContent.Substring(firstNumLen);
                         Console.WriteLine(secondStr);
                        secondNum = double.Parse(secondStr);
                         double result=0.0;
                         switch (currentStatus)
                         {
                             case PLUS:
                               result =  firstNum + secondNum;
                                break;

                             case SUBTRACTION:
                                result = firstNum - secondNum;
                                break;

                             case MULTIPLICATION:
                                result = firstNum * secondNum;
                                break;

                             case DIVISION:
                                result = firstNum/secondNum;
                                break;
                         }
                         string showResult = result.ToString("f2");
                         firstNum = double.Parse(showResult);
                         inputContent.Text = showResult;
                         secondNum = -1;
                         currentStatus = "";
                         firstNumLen = tempContent.Length;
                         tempContent = showResult;
                         break;

                     case "C":
                         inputContent.Text = "";
                         currentStatus = "";
                         firstNum = -1;
                         secondNum = -1;
                         tempContent = "";
                         firstNumLen = 0;
                         break;

                     default:
                         if (firstNumLen > 0 && currentStatus.Length <= 0)
                         {
                             inputContent.Text = "";
                             currentStatus = "";
                             firstNum = -1;
                             secondNum = -1;
                             tempContent = "";
                             firstNumLen = 0;
                         }
                         if (value.Equals(".") && tempContent.Length<= 0)
                         {
                             return;
                         }
                         tempContent += value;
                         inputContent.Text = tempContent;
                         break;
                
                 }
            }
           
        }
       
    }
}

效果图:

源码地址:https://download.csdn.net/download/qhs1573/11264243

  • 11
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值