计算器完成了连续地加减乘除,但是发现逻辑很不清楚,有时候会出错,不过还是那句话,我对自己要求不高,先凑合着用吧!

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 WpfApp5
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private decimal xxx;
        private char opteration;
        private uint count;
        private bool click_clear;
        public MainWindow()
        {
            xxx = (decimal)0.0;
            count = 0;
            opteration = '+';
            click_clear = false;
            InitializeComponent();
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            click_clear = true;
            TextBox textbox = tb;
            textbox.Text = "";
            label.Content = "";
            label_Copy.Content = "";
            xxx = 0;
            count = 0;
        }

        private void _0_b_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            textbox.Text += '0';
        }

        private void _1_b_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            textbox.Text += '1';
        }

        private void _2_b_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            textbox.Text += '2';
        }

        private void _3_b_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            textbox.Text += '3';
        }

        private void _4_b_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            textbox.Text += '4';
        }

        private void _5_b_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            textbox.Text += '5';
        }

        private void _6_b_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            textbox.Text += '6';
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            textbox.Text += '7';
        }

        private void _8_b_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            textbox.Text += '8';
        }

        private void _9_b_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            textbox.Text += '9';
        }

        private void _00_b_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            textbox.Text += "00";
        }

        private void Dot_b_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            if (textbox.Text.Contains('.'))
                ;
            else
                textbox.Text += '.';
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            count++;
            Label mylab = label;
            if (tb.Text.Equals(""))
            {
                mylab.Content = "";
                mylab.Content = '+';
                opteration = (char)mylab.Content;
            }
            else {
                switch (opteration)
                {
                    case '+':
                        try
                        {
                            xxx += Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                    case '-':
                        try
                        {
                            xxx -= Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                    case '*':
                        try
                        {
                            if (0 == count)
                                xxx = 1;
                            xxx *= Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                    case '/':
                        try
                        {
                            xxx /= Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                }
                mylab.Content = "";
                mylab.Content = '+';
                opteration = (char)mylab.Content;
            }
            tb.Text = "";
            label_Copy.Content = xxx.ToString();
            click_clear = false;
        }

        private void Sub_Click(object sender, RoutedEventArgs e)
        {
            count++;
            Label mylab = label;
            if (tb.Text.Equals(""))
            {
                mylab.Content = "";
                mylab.Content = '-';
                opteration = (char)mylab.Content;
            }
            else
            {
                switch (opteration)
                {
                    case '+':
                        try
                        {
                            xxx += Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                    case '-':
                        try
                        {
                            xxx -= Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                    case '*':
                        try
                        {
                            if (0 == count)
                                xxx = 1;
                            xxx *= Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                    case '/':
                        try
                        {
                            xxx /= Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                }
                mylab.Content = "";
                mylab.Content = '-';
                opteration = (char)mylab.Content;
            }
            tb.Text = "";
            label_Copy.Content = xxx.ToString();
            click_clear = false;
        }

        private void Mul_Click(object sender, RoutedEventArgs e)
        {
            count++;
            Label mylab = label;
            if (tb.Text.Equals(""))
            {
                mylab.Content = "";
                mylab.Content = '*';
                opteration = (char)mylab.Content;
            }
            else
            {
                switch (opteration)
                {
                    case '+':
                        try
                        {
                            xxx += Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                    case '-':
                        try
                        {
                            xxx -= Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                    case '*':
                        try
                        {
                            if (0 == count || click_clear)
                                xxx = 1;
                            xxx *= Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                    case '/':
                        try
                        {
                            if (0 == count || click_clear)
                                xxx = Convert.ToDecimal(tb.Text);
                            else
                                xxx /= Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                }
                mylab.Content = "";
                mylab.Content = '*';
                opteration = (char)mylab.Content;
            }
            tb.Text = "";
            label_Copy.Content = xxx.ToString();
            click_clear = false;
        }

        private void Div_Click(object sender, RoutedEventArgs e)
        {
            count++;
            Label mylab = label;
            if (tb.Text.Equals(""))
            {
                mylab.Content = "";
                mylab.Content = '/';
                opteration = (char)mylab.Content;
            }
            else
            {
                switch (opteration)
                {
                    case '+':
                        try
                        {
                            xxx += Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                    case '-':
                        try
                        {
                            xxx -= Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                    case '*':
                        try
                        {
                            if (0 == count || click_clear)
                                xxx = 1;
                            xxx *= Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                    case '/':
                        try
                        {
                            if (0 == count || click_clear)
                                xxx = Convert.ToDecimal(tb.Text); 
                            else
                                xxx /= Convert.ToDecimal(tb.Text);
                        }
                        catch (Exception ee)
                        {

                        }
                        break;
                }
                mylab.Content = "";
                mylab.Content = '/';
                opteration = (char)mylab.Content;
            }
            tb.Text = "";
            label_Copy.Content = xxx.ToString();
            click_clear = false;
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = tb;
            try { 
            if (textbox.Text.Length > 0)
                    textbox.Text = textbox.Text.Substring(0, textbox.Text.Length - 1);
            }
            catch (Exception ee) {

            }
        }

        private void Result_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Unable use!");
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39410618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值