1. 程式人生 > WINDOWS開發 >WPF 加減乘除計算器

WPF 加減乘除計算器

原文: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 計算器 { /// <summary> /// MainWindow.xaml 的互動邏輯 /// </summary> 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,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:

技術分享圖片
<Window x:Class="計算器.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">
    <Window.Resources>
        <Style TargetType="Border">
            <Style.Setters>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="BorderBrush" Value="Black"/>
            </Style.Setters>
        </Style>
        <Style TargetType="Button">
            <Style.Setters>
                <Setter Property="FontSize" Value="25"/>
            </Style.Setters>
        </Style>
        <Style TargetType="TextBlock">
            <Setter Property="MinHeight" Value="90"/>
            <Setter Property="HorizontalAlignment" Value="Right"/>
            <Setter Property="FontSize" Value="28"/>
        </Style>
       
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Border>
            <TextBlock>
                <Run  x:Name="T1"/>
              <LineBreak/>
                 <Run x:Name="T2"/>
              <LineBreak/>
                  <Run x:Name="T3"/>
              <LineBreak/>
                 <Run x:Name="T5"/>
                 <Run x:Name="T4"/>
            </TextBlock>
        </Border>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Button x:Name="Clear" Click="Clear_Click" Content="C"  Grid.Column="0" Grid.Row="0" Background="#FFE7E7E7" />
            <Button Click="Fun_Click" Content="÷" Grid.Column="1" Grid.Row="0" Background="#FFE7E7E7" />
            <Button Click="Fun_Click" Content="x"  Grid.Column="2" Grid.Row="0" Background="#FFE7E7E7"/>
            <Button Click="Back_Click" Content="" Grid.Column="3" Grid.Row="0" Background="#FFE7E7E7"/>
            <Button Click="Num_Click" Content="1" Grid.Column="0" Grid.Row="1" Background="White"/>
            <Button Click="Num_Click" Content="2" Grid.Column="1" Grid.Row="1" Background="#FFFFC6C6"/>
            <Button Click="Num_Click" Content="3" Grid.Column="2" Grid.Row="1" Background="#FFFFE7C6"/>
            <Button Click="Fun_Click" Content="-" Grid.Column="3" Grid.Row="1" Background="#FFE7E7E7"/>
            <Button Click="Num_Click" Content="4" Grid.Column="0" Grid.Row="2" Background="#FFFFFFC6"/>
            <Button Click="Num_Click" Content="5" Grid.Column="1" Grid.Row="2" Background="#FFC6FFC6"/>
            <Button Click="Num_Click" Content="6" Grid.Column="2" Grid.Row="2" Background="#FFC6FFFF"/>
            <Button Click="Fun_Click" Content="+" Grid.Column="3" Grid.Row="2" Background="#FFE7E7E7"/>
            <Button Click="Num_Click" Content="7" Grid.Column="0" Grid.Row="3" Background="#FFC6C6FF"/>
            <Button Click="Num_Click" Content="8" Grid.Column="1" Grid.Row="3" Background="#FFFFC6FF"/>
            <Button Click="Num_Click" Content="9" Grid.Column="2" Grid.Row="3" Background="#FFFF8484"/>
            <Button Click="Eq_Click" Content="=" Grid.RowSpan="2" Grid.Column="3" Grid.Row="3" Background="#FFE7E7E7"/>
            <Button Click="Fun_Click"  Content="%" Grid.Column="0" Grid.Row="4" Background="#FFFFC684"/>
            <Button Click="Num_Click" Content="0" Grid.Column="1" Grid.Row="4" Background="#FFFF8484"/>
            <Button Click="Num_Click" Content="." Grid.Column="2" Grid.Row="4" Background="#FF84FF84"/>
        </Grid>
    </Grid>
</Window>
技術分享圖片