C# 仿Windows10计算器

简介:

        利用WPF技术,模仿Windows10系统自带的计算机逻辑和样式(针对标准模式),开发出一个类似的计算器。功能上在普通计算机运算逻辑基础上,加上了Windows10自带计算器简易版的记忆功能,可以对算式结果进行存储和二次运用。

样式设计:

        导入了WPF的UI框架MahApps.Metro,尽量模仿Windows10自带计算机的样式:

主要功能: 

MS:记忆当前显示的数字(Memory Save)

MC:清除记忆的数字(Memory Clean)

MR:显示记忆的数字(Memory Recall)

M-:记忆的数字减去当前数字,并保存

M+:记忆的数字加上当前数字,并保存

CE:清除主文本框的内容

C:清除算式文本框与主文本框的内容

%:对主文本框中操作数取百分数

√x:对主文本框中的数进行平方根操作

1/x:对主文本框中的数取倒数

x²:对主文本框中的数进行平方运算

±:对主文本框中的数进行取相反数操作

       另外两个图片按钮分别完成对主文本框进行删除一位的操作以及清空历史记录的操作,其余按钮则则负责完成基本的数值输入与运算逻辑。

       加入了历史记录以及记忆两个栈面板分别完成记录历史操作以及进行记忆存储与操作的功能。

 程序代码:

App.xmal:

<Application x:Class="WpfApp3.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp3"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <!-- Accent and AppTheme setting -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindows.xmal:

<Window x:Class="WpfApp3.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:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="计算器" Height="524" Width="818" Background="#FFF7F7F7">
    <Grid Height="485" VerticalAlignment="Top" Background="#FFF7F7F7">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="395*"/>
            <ColumnDefinition Width="14*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="0*"/>
            <RowDefinition Height="0*"/>
        </Grid.RowDefinitions>
        <Button  Click="Button_Click_Recall" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="50" Margin="420,190,0,0" VerticalAlignment="Top" Width="140" Background="White">
            <WrapPanel >
                <Image Source="Resource/Image/delete.png"  Width="60" Height="25"/>
            </WrapPanel>
        </Button>
        <Button x:Name="B_opposite" Click="Button_Click_Opposite" Content="±" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="0,436,0,0" VerticalAlignment="Top" Width="140" FontSize="22" BorderBrush="#FFCCCCCC" />
        <Button x:Name="B_percent" Click="Button_Click_Percent" Content="%" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="0,191,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_reciprocal" Click="Button_Click_Recipraocal" Content="1/x" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="0,240,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_7" Click="Button_Click_Num" Content="7"  Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="0,289,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_4" Click="Button_Click_Num" Content="4"  Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="0,338,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_1" Click="Button_Click_Num" Content="1"  Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="0,387,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_divide" Click="Button_Click_Operate" Content="÷" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="420,240,0,0" VerticalAlignment="Top" Width="140" FontSize="22" Background="White"/>
        <Button x:Name="B_clear" Click="Button_Click_C" Content="C" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="280,191,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_sqrt" Click="Button_Click_Sqrt" Content="√x" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="280,240,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_clearE" Click="Button_Click_CE" Content="CE" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="140,191,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_square" Click="Button_Click_Square" Content="x²" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="50" Margin="140,239,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_multiply" Click="Button_Click_Operate" Content="×" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="420,289,0,0" VerticalAlignment="Top" Width="140" FontSize="22" Background="White"/>
        <Button x:Name="B_9" Click="Button_Click_Num" Content="9" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="280,289,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_8" Click="Button_Click_Num" Content="8" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="140,289,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_sub" Click="Button_Click_Operate" Content="-" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="420,338,0,0" VerticalAlignment="Top" Width="140" FontSize="22" Background="White"/>
        <Button x:Name="B_6" Click="Button_Click_Num" Content="6" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="280,338,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_5" Click="Button_Click_Num" Content="5" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="140,338,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_add" Click="Button_Click_Operate" Content="+" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="420,387,0,0" VerticalAlignment="Top" Width="140" FontSize="22" Background="White"/>
        <Button x:Name="B_3" Click="Button_Click_Num" Content="3" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="280,387,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_2" Click="Button_Click_Num" Content="2" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="140,387,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_equal" Click="Button_Click_Operate" Content="=" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="420,436,0,0" VerticalAlignment="Top" Width="140" FontSize="22" Background="#FFCECECE"/>
        <Button x:Name="B_point" Click="Button_Click_Num" Content="." Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Margin="280,436,0,0" Width="140" Height="49" VerticalAlignment="Top" FontSize="22"/>
        <Button x:Name="B_0" Click="Button_Click_Num" Content="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Height="49" Margin="140,436,0,0" VerticalAlignment="Top" Width="140" FontSize="20" Background="White"/>
        <Button x:Name="B_memoryClear" Content="MC" Click="Button_Click_MC" HorizontalAlignment="Left" Margin="0,164,0,0" VerticalAlignment="Top" Width="100" Height="27" BorderThickness="0"  Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"/>
        <Button x:Name="B_memoryRead" Content="MR" Click="Button_Click_MR" HorizontalAlignment="Left" Margin="100,164,0,0" VerticalAlignment="Top" Width="100" Height="27" BorderThickness="0"  Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"/>
        <Button x:Name="B_memoryAdd" Content="M+" Click="Button_Click_MAdd" HorizontalAlignment="Left" Margin="200,164,0,0" VerticalAlignment="Top" Width="100" Height="27" BorderThickness="0"  Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"/>
        <Button x:Name="B_memorySub" Content="M-" Click="Button_Click_MSub" HorizontalAlignment="Left" Margin="280,164,0,0" VerticalAlignment="Top" Width="100" Height="27" BorderThickness="0"  Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"/>
        <Button x:Name="B_memorySave" Content="MS" Click="Button_Click_MS" HorizontalAlignment="Left" Margin="380,164,0,0" VerticalAlignment="Top" Width="100" Height="27" BorderThickness="0"  Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"/>
        <TabControl Margin="560,0,0,-10" Grid.RowSpan="3" Background="#FFF7F7F7" SelectionChanged="TabControl_SelectionChanged" Grid.ColumnSpan="2">
            <TabItem  Header="历史记录" FontWeight="Normal" FontFamily="Arial Black" Height="38" VerticalAlignment="Top">
                <Grid Background="#FFF7F7F7" Margin="0,0,-3,0" Height="435">
                    <ScrollViewer HorizontalScrollBarVisibility="Auto" Margin="0,0,0,25">
                        <StackPanel x:Name="historyPanel">
                        </StackPanel>
                    </ScrollViewer>
                    <Button Click="Button_Click_ClearHistory" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Left" Margin="194,398,0,0" VerticalAlignment="Top" Height="37" Width="43">
                        <WrapPanel >
                            <Image Source="Resource/Image/删除.png"  Width="21" Height="27"/>
                            <Image HorizontalAlignment="Left" Height="27" VerticalAlignment="Top" Width="25"/>
                        </WrapPanel>
                    </Button>
                </Grid>
            </TabItem>
            <TabItem Header="记忆">
                <Grid x:Name="memoryGrid" Background="#FFF7F7F7">
                    <ScrollViewer HorizontalScrollBarVisibility="Auto" Margin="0,0,0,25">
                        <StackPanel x:Name="memoryPanel" >
                        </StackPanel>
                    </ScrollViewer>
                </Grid>
            </TabItem>
        </TabControl>
        <TextBlock x:Name="Text_01" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Height="119" Width="560" Background="#FFF7F7F7" TextAlignment="Right" FontSize="48" FontWeight="Normal" Margin="0,45,0,0"><Run Language="zh-cn"/>0</TextBlock>
        <TextBlock x:Name="Text_02" HorizontalAlignment="Left" Text="" TextWrapping="Wrap" TextAlignment="Right" VerticalAlignment="Top" Height="35" Width="560" Margin="0,10,0,0" FontSize="20" Foreground="#FFB9B9B9" FontWeight="Normal"></TextBlock>
    </Grid>
</Window>

MainWindows.xmal.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;
// To access MetroWindow, add the following reference
using MahApps.Metro.Controls;

namespace WpfApp3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private bool cover = true;//数字是否覆盖写入
        private string lastOp = null;//前操作符
        private string num = null;//一号操作数
        private string num2 = null;//二号操作数
        private string num3 = null;//三号操作数
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_Opposite(object sender, RoutedEventArgs e)//取相反数操作
        {
            if (Text_01.Text.Length >= 1 && Text_01.Text != "0")
            {
                if (Text_01.Text[0] == '-')
                {
                    Text_01.Text = Text_01.Text.Substring(1, Text_01.Text.Length - 1);
                    num3 = Text_01.Text;
                }
                else
                {
                    Text_01.Text = "-" + Text_01.Text;
                    num3 = Text_01.Text;
                }
            }
            cover = true;
        }

        private void Button_Click_Percent(object sender, RoutedEventArgs e)//取百分数操作
        {
            if (Text_01.Text != null)
            {
                Text_01.Text = Convert.ToString(Convert.ToDouble(Text_01.Text) * 0.01);
                if (lastOp != null)
                {
                    num2 = Text_01.Text;
                    num3 = Text_01.Text;
                }
                else if (Text_02.Text.Length >= 1 && Text_02.Text[Text_02.Text.Length - 1] == '=')
                {
                    Text_02.Text = Text_01.Text;
                }
                num3 = Text_01.Text;
            }
            cover = true;
        }

        private void Button_Click_Recipraocal(object sender, RoutedEventArgs e)//取倒数操作
        {
            if (Text_01.Text != null)
            {
                if (Text_01.Text.Length >= 1 && Text_01.Text == "0")
                {
                    Text_02.Text = null;
                    Text_01.Text = "除数不能为0,请按C删除";
                }
                else
                {
                    double num4 = Convert.ToDouble(Text_01.Text);
                    Text_01.Text = Convert.ToString(1 / Convert.ToDouble(Text_01.Text));
                    if (lastOp != null)
                    {
                        num2 = Text_01.Text;
                        num3 = Text_01.Text;
                    }
                    if (Text_02.Text.Length >= 1 && (Text_02.Text[Text_02.Text.Length - 1] == '=' ||
                        Text_02.Text[0] == 's' || Text_02.Text[0] == '√' || Text_02.Text[1] == '/'))
                    {
                        Text_02.Text = "1/(" + num4 + ")";
                    }
                    else if (Text_02.Text.Length == 0)
                    {
                        Text_02.Text = "1/(" + num4 + ")";
                    }
                    num3 = Text_01.Text;
                }
            }
            cover = true;
        }

        private void Button_Click_Sqrt(object sender, RoutedEventArgs e)//取平方根操作
        {
            if (Text_01.Text != null)
            {
                if (Text_01.Text.Length >= 1 && Text_01.Text[0] == '-')
                {
                    Text_02.Text = null;
                    Text_01.Text = "无效输入,请按C清除";
                }
                else
                {
                    double num4 = Convert.ToDouble(Text_01.Text);
                    Text_01.Text = Convert.ToString(Math.Sqrt(Convert.ToDouble(Text_01.Text)));
                    if (lastOp != null)
                    {
                        num2 = Text_01.Text;
                        num3 = Text_01.Text;
                    }
                    if (Text_02.Text.Length >= 1 && (Text_02.Text[Text_02.Text.Length - 1] == '=' ||
                        Text_02.Text[0] == 's' || Text_02.Text[0] == '√' || Text_02.Text[1] == '/'))
                    {
                        Text_02.Text = "√(" + num4 + ")";
                    }
                    else if (Text_02.Text.Length == 0)
                    {
                        Text_02.Text = "√(" + num4 + ")";
                    }
                    num3 = Text_01.Text;
                }
            }
            cover = true;
        }

        private void Button_Click_Square(object sender, RoutedEventArgs e)//取平方操作
        {
            if (Text_01.Text != null)
            {
                double num4 = Convert.ToDouble(Text_01.Text);
                Text_01.Text = Convert.ToString(Convert.ToDouble(Text_01.Text) * Convert.ToDouble(Text_01.Text));
                if (lastOp != null)
                {
                    num2 = Text_01.Text;
                    num3 = Text_01.Text;
                }
                if (Text_02.Text.Length >= 1 && (Text_02.Text[Text_02.Text.Length - 1] == '=' ||
                        Text_02.Text[0] == 's' || Text_02.Text[0] == '√' || Text_02.Text[1] == '/'))
                {//上一次是完整等式、开根号式、平方式,重写副文本框
                    Text_02.Text = "sqr(" + num4 + ")";
                }
                else if (Text_02.Text.Length == 0)//副文本框算式显示
                {
                    Text_02.Text = "sqr(" + num4 + ")";
                }
                num3 = Text_01.Text;
            }
            cover = true;
        }

        private void Button_Click_ClearHistory(object sender, RoutedEventArgs e)
        {
            historyPanel.Children.Clear();
        }

        private void Button_Click_Recall(object sender, RoutedEventArgs e)
        {
            if (Text_02.Text != null)
            {
                Text_02.Text = null;
            }
            else if (Text_01.Text.Length > 1)
            {
                Text_01.Text = Text_01.Text.Substring(0, Text_01.Text.Length - 1);
            }
            else if (Text_01.Text.Length == 1)
            {
                Text_01.Text = "0";
            }
            num3 = Text_01.Text;
        }
        private void Button_Click_C(object sender, RoutedEventArgs e)
        {
            Text_01.Text = "0";
            Text_02.Text = null;
            num = null;
            num2 = null;
            num3 = null;
            lastOp = null;

        }
        private void Button_Click_CE(object sender, RoutedEventArgs e)
        {
            Text_01.Text = "0";
            num3 = null;
        }

        private void TabControl_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click_Num(object sender, RoutedEventArgs e)
        {
            Button numBtn = sender as Button;
            string numstr = numBtn.Content.ToString();
            if (Text_02.Text.Length >= 1 && (Text_02.Text[Text_02.Text.Length - 1] == '=' ||
                        Text_02.Text[0] == 's' || Text_02.Text[0] == '√' || Text_02.Text[1] == '/'))
            //如果算式文本框中表达完整表达式,或者是sqrt和sqr和倒数,则应该清空
            {
                Text_02.Text = null;
                num = null;
                num2 = null;
                num3 = null;
                lastOp = null;
            }
            if (Text_01.Text == "0" || cover)
            {
                Text_01.Text = numstr;
                cover = false;
            }
            else{ Text_01.Text += numstr; }
            num3 = Text_01.Text;
            if (lastOp == null){ num = num3; }
            else { num2 = num3;}
        }

        private void Button_Click_Operate(object sender, RoutedEventArgs e)
        {
            Button numBtn = sender as Button;
            string op = numBtn.Content.ToString();
            if (op == "=")
            {
                if (lastOp == null)
                {
                    Text_02.Text = Text_01.Text + op;
                }
                else
                {
                    if (num2 == null)
                    {
                        num2 = num3;
                    }
                    Text_02.Text = num + lastOp + num2 + op;
                    double result = 0;
                    switch (lastOp)
                    {
                        case "+":
                            result = Convert.ToDouble(num) + Convert.ToDouble(num2);
                            break;
                        case "-":
                            result = Convert.ToDouble(num) - Convert.ToDouble(num2);
                            break;
                        case "×":
                            result = Convert.ToDouble(num) * Convert.ToDouble(num2);
                            break;
                        case "÷":
                            result = Convert.ToDouble(num) / Convert.ToDouble(num2);
                            break;
                        default:
                            break;
                    }
                    num3 = Convert.ToString(result);

                    num = Convert.ToString(result);
                    Text_01.Text = Convert.ToString(result);
                }
                historyAdd(Text_02.Text, Text_01.Text);
            }
            else
            {
                if (lastOp != null)
                {
                    if (!cover)
                    {
                        //计算上一个结果
                        perOperate();
                        num = num3;
                    }
                }
                else
                {
                    num = Text_01.Text;
                }
                lastOp = op;
                Text_02.Text = num + op;
            }
            cover = true;
        }

        private void perOperate()//处理上一次的计算结果
        {
            double result = 0;
            num2 = Text_01.Text;
            switch (lastOp)
            {
                case "+":
                    result = Convert.ToDouble(num) + Convert.ToDouble(num2);
                    break;
                case "-":
                    result = Convert.ToDouble(num) - Convert.ToDouble(num2);
                    break;
                case "×":
                    result = Convert.ToDouble(num) * Convert.ToDouble(num2);
                    break;
                case "÷":
                    result = Convert.ToDouble(num) / Convert.ToDouble(num2);
                    break;
                default:
                    break;
            }
            num3 = Convert.ToString(result);
            Text_01.Text = num3;
            historyAdd(num + lastOp + num2 + "=", num3);
        }

        private void Button_Click_MC(object sender, RoutedEventArgs e)
        {
            memoryPanel.Children.Clear();
        }

        private void Button_Click_MR(object sender, RoutedEventArgs e)
        {
            if (memoryPanel.Children.Count > 0)
            {
                Border border = memoryPanel.Children[0] as Border;
                if (border != null)
                {
                    Grid grid = border.Child as Grid;
                    if (grid != null)
                    {
                        TextBlock textBlock = grid.Children[0] as TextBlock;
                        Text_01.Text = textBlock.Text;
                    }
                }
            }
            cover = true;
        }

        private void Button_Click_MAdd(object sender, RoutedEventArgs e)
        {
            if (memoryPanel.Children.Count > 0)
            {
                Border border = memoryPanel.Children[0] as Border;
                if (border != null)
                {
                    Grid grid = border.Child as Grid;
                    if (grid != null)
                    {
                        TextBlock textBlock = grid.Children[0] as TextBlock;
                        textBlock.Text = Convert.ToString(Convert.ToDouble(textBlock.Text) + Convert.ToDouble(Text_01.Text));
                    }
                }
            }
            else
            {
                memoryAdd(Text_01.Text);
            }
            Text_02.Text = null;
            num = null;
            num2 = null;
            lastOp = null;
            cover = true;
        }

        private void Button_Click_MSub(object sender, RoutedEventArgs e)
        {
            if (memoryPanel.Children.Count > 0)
            {
                Border border = memoryPanel.Children[0] as Border;
                if (border != null)
                {
                    Grid grid = border.Child as Grid;
                    if (grid != null)
                    {
                        TextBlock textBlock = grid.Children[0] as TextBlock;
                        textBlock.Text = Convert.ToString(Convert.ToDouble(textBlock.Text) - Convert.ToDouble(Text_01.Text));
                    }
                }
            }
            else
            {
                memoryAdd(Text_01.Text);
            }
            Text_02.Text = null;
            num = null;
            num2 = null;
            lastOp = null;
            cover = true;
        }

        private void Button_Click_MS(object sender, RoutedEventArgs e)
        {
            memoryAdd(Text_01.Text);
            cover = true;
            Text_02.Text = null;
            num = null;
            num2 = null;
            lastOp = null;
        }

        private void Button_Click_pMC(object sender, RoutedEventArgs e)
        {
            var button = e.OriginalSource as FrameworkElement;
            if (button == null) return;
            Grid grid = VisualTreeHelper.GetParent(button) as Grid;
            Border border = VisualTreeHelper.GetParent(grid) as Border;
            memoryPanel.Children.Remove(border);
            cover = true;
        }

        private void Button_Click_pMAdd(object sender, RoutedEventArgs e)
        {
            var button = e.OriginalSource as FrameworkElement;
            if (button == null) return;
            Grid grid = VisualTreeHelper.GetParent(button) as Grid;
            TextBlock textBlock = grid.Children[0] as TextBlock;
            textBlock.Text = Convert.ToString(Convert.ToDouble(textBlock.Text) + Convert.ToDouble(Text_01.Text));
            Text_02.Text = null;
            num = null;
            num2 = null;
            lastOp = null;
            cover = true;
        }

        private void Button_Click_pMSub(object sender, RoutedEventArgs e)
        {
            var button = e.OriginalSource as FrameworkElement;
            if (button == null) return;
            Grid grid = VisualTreeHelper.GetParent(button) as Grid;
            TextBlock textBlock = grid.Children[0] as TextBlock;
            textBlock.Text = Convert.ToString(Convert.ToDouble(textBlock.Text) - Convert.ToDouble(Text_01.Text));
            Text_02.Text = null;
            num = null;
            num2 = null;
            lastOp = null;
            cover = true;
        }

        private void historyAdd(string text01, string text02)//添加历史记录
        {
            Border border = new Border();
            border.BorderBrush = new SolidColorBrush(Colors.Transparent);
            border.BorderThickness = new Thickness(1);
            border.Height = 60;


            TextBlock text1 = new TextBlock();
            text1.HorizontalAlignment = HorizontalAlignment.Left;
            text1.Margin = new Thickness(10, 10, 0, 0);
            text1.TextWrapping = TextWrapping.Wrap;
            text1.Text = text01;
            text1.VerticalAlignment = VerticalAlignment.Top;
            text1.Width = 215;
            text1.Height = 20;
            text1.TextAlignment = TextAlignment.Right;
            text1.FontSize = 12;
            text1.Foreground = new SolidColorBrush(Colors.Gray);
            text1.FontFamily = new FontFamily("Microsoft YaHei");

            TextBlock text2 = new TextBlock();
            text2.HorizontalAlignment = HorizontalAlignment.Left;
            text2.Margin = new Thickness(10, 30, 0, 0);
            text2.TextWrapping = TextWrapping.Wrap;
            text2.Text = text02;
            text2.VerticalAlignment = VerticalAlignment.Top;
            text2.Width = 215;
            text2.Height = 40;
            text2.TextAlignment = TextAlignment.Right;
            text2.FontSize = 24;
            text2.FontFamily = new FontFamily("Microsoft YaHei");

            Grid gird = new Grid();
            gird.Children.Add(text1);
            gird.Children.Add(text2);
            border.Child = gird;
            historyPanel.Children.Add(border);
        }
        private void memoryAdd(string text01)//添加记忆
        {
            Border border = new Border();
            border.BorderBrush = new SolidColorBrush(Colors.Transparent);
            border.BorderThickness = new Thickness(1);
            border.Height = 70;

            TextBlock text1 = new TextBlock();
            text1.HorizontalAlignment = HorizontalAlignment.Left;
            text1.Margin = new Thickness(10, 5, 0, 0);
            text1.TextWrapping = TextWrapping.Wrap;
            text1.Text = text01;
            text1.VerticalAlignment = VerticalAlignment.Top;
            text1.Width = 210;
            text1.Height = 40;
            text1.TextAlignment = TextAlignment.Right;
            text1.FontSize = 24;
            text1.FontFamily = new FontFamily("Microsoft YaHei");

            Button button = new Button();
            button.Content = "M-";
            Style style = (Style)Application.Current.FindResource(ToolBar.ButtonStyleKey);
            button.Style = style;
            button.Margin = new Thickness(165, 37, 5, 5);
            button.FontSize = 10;
            button.Background = new SolidColorBrush(Colors.LightGray);
            button.Click += new RoutedEventHandler(Button_Click_pMSub);

            Button button2 = new Button();
            button2.Content = "M+";
            button2.Style = style;
            button2.Margin = new Thickness(115, 37, 57, 5);
            button2.FontSize = 10;
            button2.Background = new SolidColorBrush(Colors.LightGray);
            button2.Click += new RoutedEventHandler(Button_Click_pMAdd);

            Button button3 = new Button();
            button3.Content = "MC";
            button3.Style = style;
            button3.Margin = new Thickness(65, 37, 107, 5);
            button3.FontSize = 10;
            button3.Background = new SolidColorBrush(Colors.LightGray);
            button3.Click += new RoutedEventHandler(Button_Click_pMC);

            Grid gird = new Grid();
            gird.Children.Add(text1);
            gird.Children.Add(button);
            gird.Children.Add(button2);
            gird.Children.Add(button3);
            border.Child = gird;
            memoryPanel.Children.Add(border);
        }
    }
}

运行结果 :

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值