C# wpf 做的一個簡單的計算器

閒着無聊研究了下C#的wpf,主要是學習界面,第一次學習有可能有bug或寫的不好的地方各位大佬們不要吐槽哈

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("f10");
                         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;
                         }
                         tempContent += value;
                         inputContent.Text = tempContent;
                         break;
                
                 }
            }
           
        }
       
    }
}

效果圖:

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章