C#程序设计实验11 简易计算器样式设计

<Window x:Class="Calculator.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:Calculator"

        mc:Ignorable="d"

        Title="HZF的计算器" Height="450" Width="800">

    <Grid>

        <TextBox x:Name="Box" HorizontalAlignment="Left" Margin="10,217,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="218" Height="140"/>

        <TextBox x:Name="num_Box" HorizontalAlignment="Left" Margin="10,74,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Height="41"/>

扫描二维码关注公众号,回复: 16493885 查看本文章

        <Grid Margin="266,0,0,10">

            <Grid.RowDefinitions>

                <RowDefinition Height="114*"/>

                <RowDefinition Height="311*"/>

            </Grid.RowDefinitions>

            <Button x:Name="Zero" Content="0" HorizontalAlignment="Left" Margin="59,201,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20"  Grid.Row="1" Click="Zero_Click"/>

            <Button x:Name="one" Content="1" HorizontalAlignment="Left" Margin="59,98,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20" Grid.Row="1" Click="one_Click"/>

            <Button x:Name="nine" Content="9" HorizontalAlignment="Left" Margin="254,53,0,0" VerticalAlignment="Top" Height="56" Width="66" FontSize="20" Click="nine_Click"/>

            <Button x:Name="eight" Content="8" HorizontalAlignment="Left" Margin="155,53,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20" Click="eight_Click"/>

            <Button x:Name="seven" Content="7" HorizontalAlignment="Left" Margin="59,53,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20" Click="seven_Click"/>

            <Button x:Name="six" Content="6" HorizontalAlignment="Left" Margin="255,22,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20" Grid.Row="1" Click="six_Click"/>

            <Button x:Name="five" Content="5" HorizontalAlignment="Left" Margin="155,22,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20" Grid.Row="1" Click="five_Click"/>

            <Button x:Name="four" Content="4" HorizontalAlignment="Left" Margin="59,22,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20" Grid.Row="1" Click="four_Click"/>

            <Button x:Name="three" Content="3" HorizontalAlignment="Left" Margin="255,98,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20" Grid.Row="1" Click="three_Click"/>

            <Button x:Name="two" Content="2" HorizontalAlignment="Left" Margin="155,98,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20"  RenderTransformOrigin="0.641,-0.782" Grid.Row="1" Click="two_Click"/>

            <Button x:Name="point" Content="." HorizontalAlignment="Left" Margin="255,201,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20" Grid.Row="1" Click="point_Click"/>

            <Button x:Name="div" Content="÷" HorizontalAlignment="Left" Margin="362,201,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20" Grid.Row="1" Click="div_Click"/>

            <Button x:Name="mutil" Content="×" HorizontalAlignment="Left" Margin="362,98,0,0" VerticalAlignment="Top" Height="56" Width="64" FontSize="20" Grid.Row="1" Click="mutil_Click"/>

            <Button x:Name="minus" Content="-" HorizontalAlignment="Left" Margin="362,22,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20" Grid.Row="1" RenderTransformOrigin="0.234,0.543" Click="minus_Click"/>

            <Button x:Name="add" Content="+" HorizontalAlignment="Left" Margin="362,53,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20" Click="add_Click"/>

            <Button x:Name="mo" Content="%" HorizontalAlignment="Left" Margin="155,201,0,0" VerticalAlignment="Top" Height="56" Width="65" FontSize="20" Grid.Row="1"/>

            <Button x:Name="deng" Content="=" HorizontalAlignment="Left" Margin="450,134,0,0" VerticalAlignment="Top" Height="123" Width="65" FontSize="20" Grid.Row="1" Click="deng_Click"/>

            <Button x:Name="clear" Content="CLR" HorizontalAlignment="Left" Margin="450,57,0,0" VerticalAlignment="Top" Height="123" Width="65" FontSize="20" Grid.RowSpan="2" Click="clear_Click"/>

        </Grid>

    </Grid>

</Window>

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 Calculator

{

    /// <summary>

    /// MainWindow.xaml 的交互逻辑

    /// </summary>

    public partial class MainWindow : Window

    {

        //定义两个数组,存数字和运算符

        private List<double> values = new List<double>();//储存值

        private List<int> operators = new List<int>();//储存运算符

        //将运算符初始化为不可点击,因为运算符不能出现在第一位

        private bool add_flag = false;//加+

        private bool minus_flag = false;//减-

        private bool multi_flag = false;//乘×

        private bool div_flag = false;//除÷

        private bool result_flag = false;//等于=

        private bool can_operate_flag = false;//响应=

        private bool mo_flag = false;//模%

        private bool point_flag = false;

        public MainWindow()

        {

            InitializeComponent();

        }

        private void num_down(string num)

        {

            //如果按下某一个运算符,它变为true,

            //而后当按下一个数值时会清楚上一个在文本框的值

            if (add_flag || minus_flag || div_flag || result_flag || multi_flag

                )

            {

                //如果按下等号,结束这次运算,文本框置空

                if (result_flag)

                {

                    Box.Text = "";

                }

                num_Box.Clear();

                add_flag = false;//加+

                minus_flag = false;//减-

                multi_flag = false;//乘×

                div_flag = false;//除÷

                result_flag = false;//等于=

                can_operate_flag = false;//响应=

                mo_flag = false;//模%

            }

            if ((num.Equals(".") && num_Box.Text.IndexOf(".") < 0) || !num.Equals("."))

            {

                //如果用户输入是点并且遍历文本框没有点,或者输入不是.

                num_Box.Text += num;  //文本框后追加输入内容

                Box.Text += num;

                can_operate_flag = true; //运算符禁用解除

            }

        }

        private void Zero_Click(object sender, RoutedEventArgs e)

        {

            num_down("0");

        }

        private void one_Click(object sender, RoutedEventArgs e)

        {

            num_down("1");

        }

        private void two_Click(object sender, RoutedEventArgs e)

        {

            num_down("2");

        }

        private void three_Click(object sender, RoutedEventArgs e)

        {

            num_down("3");

        }

        private void four_Click(object sender, RoutedEventArgs e)

        {

            num_down("4");

        }

        private void five_Click(object sender, RoutedEventArgs e)

        {

            num_down("5");

        }

        private void six_Click(object sender, RoutedEventArgs e)

        {

            num_down("6");

        }

        private void seven_Click(object sender, RoutedEventArgs e)

        {

            num_down("7");

        }

        private void eight_Click(object sender, RoutedEventArgs e)

        {

            num_down("8");

        }

        private void nine_Click(object sender, RoutedEventArgs e)

        {

            num_down("9");

        }

        private void point_Click(object sender, RoutedEventArgs e)

        {

            num_down(".");

        }

        private void add_Click(object sender, RoutedEventArgs e)

        {

            if (!add_flag && can_operate_flag)//防止用户输入多次符号键

            {

                result_flag = false;

                values.Add(double.Parse(num_Box.Text));//数组装载数字

                operators.Add(0);//数组装载+号

                Box.Text += "+";

                add_flag = true;

                can_operate_flag = false;//禁用计算操作

            }

        }

        private void minus_Click(object sender, RoutedEventArgs e)

        {

            if (!minus_flag && can_operate_flag)//防止用户输入多次符号键

            {

                result_flag = false;

                values.Add(double.Parse(num_Box.Text));//数组装载数字

                operators.Add(1);//数组装载-号

                Box.Text += "-";

                minus_flag = true;

                can_operate_flag = false;//禁用计算操作

            }

        }

        private void mutil_Click(object sender, RoutedEventArgs e)

        {

            if (!multi_flag && can_operate_flag)//防止用户输入多次符号键

            {

                result_flag = false;

                values.Add(double.Parse(num_Box.Text));//数组装载数字

                operators.Add(2);//数组装载*号

                Box.Text = "(" + num_Box.Text + ")" + "×";//给前面的输入加上()

                multi_flag = true;//禁用乘号

                can_operate_flag = false;//禁用计算操作

            }

        }

        private void div_Click(object sender, RoutedEventArgs e)

        {

            if (!div_flag && can_operate_flag)//防止用户输入多次符号键

            {

                result_flag = false;

                values.Add(double.Parse(num_Box.Text));//数组装载数字

                operators.Add(3);//数组装载/号

                Box.Text = "(" + Box.Text + ")" + "÷";//给前面的输入加上()

                div_flag = true;

                can_operate_flag = false;//禁用计算操作

            }

        }

        private void clear_Click(object sender, RoutedEventArgs e)

        {

            operators.Clear();//清楚操作符数组和值数组

            values.Clear();

            //初始化

            add_flag = false;//加+

            minus_flag = false;//减-

            multi_flag = false;//乘×

            div_flag = false;//除÷

            result_flag = false;//等于=

            can_operate_flag = false;//响应=

            mo_flag = false;//模%

            num_Box.Clear();

            Box.Clear ();

        }

        private void deng_Click(object sender, RoutedEventArgs e)

        {

            //数字数量>0,运算符数量>0,可等于的运算未禁用

            if (values.Count > 0 && operators.Count > 0 && can_operate_flag)

            {

                values.Add(double.Parse(num_Box.Text));//将用户点击的数字传入值数组

                double total = values[0];

                for (int i = 0; i < operators.Count; i++)

                {

                    int _operator = operators[i];//把运算符装入operators数组

                    switch (_operator)

                    {

                        //按照输入的运算符对应的数组下标进行对应操作

                        case 0:

                            total = total+ values[i + 1]; //方法加

                            break;

                        case 1:

                            total = total - values[i + 1]; //方法减

                            break;

                        case 2:

                            total = total * values[i + 1]; //方法乘

                            break;

                        case 3:

                            total = total / values[i + 1]; //方法除

                            break;

                       

                        case 4:

                            total = total % values[i + 1];

                            break;

                    }

                }

                num_Box.Text = total + "";

                Box.Text = total + "";

                operators.Clear();

                values.Clear();

                result_flag = true;

            }

        }

    }     

}

猜你喜欢

转载自blog.csdn.net/qq_62480054/article/details/131585658