Winfrom编写简单的计算器

来源:互联网 发布:ubuntu怎么安装deb包 编辑:程序博客网 时间:2024/06/06 02:48

简单的计算器

基本布局:


代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Calculator
{
    public partial class calculator : Form
    {
        public calculator()
        {
            InitializeComponent();
            lb_1.Text = null;
        }


        private void calculator_Load(object sender, EventArgs e)
        {


        }


        private string FuHao;
        private float? Mun;
        private float? Num;
        private bool IsCounting = false;
        private bool IsHaving = false;
        private bool IsJiSuan = false;


        private void ShuRu(string shu)
        {
            if(!IsCounting)
            {
                txtShuchu.Text = txtShuchu.Text + shu;
            }
            else
            {
                txtShuchu.Text = "";
                txtShuchu.Text = txtShuchu.Text + shu;
                IsCounting = false;
            }
        }


        private void XianShi(string fuhao)
        {
            if (!IsHaving)
            {
                Mun = float.Parse(txtShuchu.Text);
                IsHaving = true;
            }
            else
            {
                Num = float.Parse(txtShuchu.Text);
            }
            if((lb_1.Text.EndsWith("+")|| lb_1.Text.EndsWith("-")|| lb_1.Text.EndsWith("×") || lb_1.Text.EndsWith("÷")||lb_1.Text.EndsWith("="))&&IsCounting)
            {
                lb_1.Text = lb_1.Text.Remove(lb_1.Text.Length - 1, 1);
            }
            else
            {
                lb_1.Text +=txtShuchu.Text;
                if(IsJiSuan)
                {
                    Mun=JiSuan(Mun, Num);
                    txtShuchu.Text = Mun.ToString();
                }  
            }
            FuHao = fuhao;
            lb_1.Text += FuHao;
            IsCounting = true;
            IsJiSuan = true; 
        }
        private float? JiSuan(float? a,float? b)
        {
            float? c=0;
            switch(FuHao)
            {
                case "+":
                    c = a + b;
                    break;
                case "-":
                    c = a - b;
                    break;
                case "×":
                    c = a * b;
                    break;
                case "÷":
                    if (b == 0)
                    {
                        MessageBox.Show("被除数不能为0");
                    }
                    else
                    {
                        c = a / b;
                    }
                    break;
                case "=":
                    c = a;
                    break;
                default:
                    break;
            }
            return c;
        }
#region btn_click_munber
        private void btn_1_Click(object sender, EventArgs e)
        {
            ShuRu(btn_1.Text);
        }


        private void btn_2_Click(object sender, EventArgs e)
        {
            ShuRu(btn_2.Text);
        }


        private void btn_3_Click(object sender, EventArgs e)
        {
            ShuRu(btn_3.Text);
        }


        private void btn_4_Click(object sender, EventArgs e)
        {
            ShuRu(btn_4.Text);
        }


        private void btn_5_Click(object sender, EventArgs e)
        {
            ShuRu(btn_5.Text);
        }


        private void btn_6_Click(object sender, EventArgs e)
        {
            ShuRu(btn_6.Text);
        }


        private void btn_7_Click(object sender, EventArgs e)
        {
            ShuRu(btn_7.Text);
        }


        private void btn_8_Click(object sender, EventArgs e)
        {
            ShuRu(btn_8.Text);
        }


        private void btn_9_Click(object sender, EventArgs e)
        {
            ShuRu(btn_9.Text);
        }


        private void btn_0_Click(object sender, EventArgs e)
        {
            ShuRu(btn_0.Text);
        }
        #endregion
#region btn_click_zhiling
        private void btn_jia_Click(object sender, EventArgs e)
        {
            XianShi(btn_jia.Text);
        }


        private void btn_jian_Click(object sender, EventArgs e)
        {
            XianShi(btn_jian.Text);
        }


        private void btn_cheng_Click(object sender, EventArgs e)
        {
            XianShi(btn_cheng.Text);
        }


        private void btn_chu_Click(object sender, EventArgs e)
        {
            XianShi(btn_chu.Text);
        }


        private void btn_deng_Click(object sender, EventArgs e)
        {
            XianShi(btn_deng.Text);
        }
        private void btn_clear_Click(object sender, EventArgs e)
        {
            lb_1.Text = "";
            txtShuchu.Text = "";
            Mun = null;
            Num = null;
            IsCounting = false;
            IsHaving = false;
            IsJiSuan = false;
        }
    }
#endregion
}
总结:
长时间的没有整体的写一写自己东西,一直感觉自己在原地踏步,这次的实验虽然有点简单,写的也并不是很好,但是证明了这么些时间大量的读代码还是有用的。
没什么难度。希望以后看到这些东西的时候能给自己一个狗屎的评价。

原创粉丝点击