基本信息
源码名称:C# 简易计算器 示例源码
源码大小:0.18M
文件格式:.7z
开发语言:ASP
更新时间:2017-11-10
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

namespace JSQ
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            btn_0.Click = new EventHandler(Button_Click);
            btn_1.Click = new EventHandler(Button_Click);
            btn_2.Click = new EventHandler(Button_Click);
            btn_3.Click = new EventHandler(Button_Click);
            btn_4.Click = new EventHandler(Button_Click);
            btn_5.Click = new EventHandler(Button_Click);
            btn_6.Click = new EventHandler(Button_Click);
            btn_7.Click = new EventHandler(Button_Click);
            btn_8.Click = new EventHandler(Button_Click);
            btn_9.Click = new EventHandler(Button_Click);
            btn_point.Click = new EventHandler(Button_Click);

            btn_add.Click = new EventHandler(Btn_operation_Click);
            btn_cut.Click = new EventHandler(Btn_operation_Click);
            btn_multiply.Click = new EventHandler(Btn_operation_Click);
            btn_divide.Click = new EventHandler(Btn_operation_Click);
        }

        float temp1, temp2;//记录第一个数字

        string pos;//记录符号
        string s;//搜集字符串
        string x;//搜集前一个数据
        string m = "";//搜集符号

        bool bo = false;//判断等号健的点击的个数并依次处理不同动作
        bool fh = false;//符号开关,控制是否可以在没有数字时显示符号
        bool v = false;//符号改变控制开关

        /*
         *处理数字按钮的事件 
         */
        private void Button_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;

            s = s b.Text.ToString();
            textBox1.Text = m s;
            lal.Text = s;
            x = s;

            fh = true;
        }

        /*
         * 运算符号处理
         */
        private void Btn_operation_Click(object sender, EventArgs e)
        {
            pos = "";
            Button oper = (Button)sender;
            pos = oper.Text;

            if (v)
            {
                try
                {
                    textBox1.Text = m.Remove(m.Length - 1).ToString() oper.Text.ToString();
                    m = textBox1.Text;
                }
                catch { }
            }

            if (fh == true)
            {
                temp1 = TryFloat(x, temp1);
                textBox1.Text = x oper.Text.ToString();

                m = textBox1.Text;
                s = "";

                fh = false;
                v = true;
                bo = true;
            }
        }

        /*
         * 单击运算处理及清零处理
         */
        private void Btn_amount_Click(object sender, EventArgs e)
        {

            temp2 = TryFloat(s, temp2);

            if (bo)
            {
                textBox1.Text = textBox1.Text "\r\n" "= " getCalculate(pos, temp1, temp2);
                label1.Text = "双击 ^_^ 归零喲";
                lal.Text = getCalculate(pos, temp1, temp2).ToString();
                x = getCalculate(pos, temp1, temp2).ToString();
                temp1 = TryFloat(lal.Text, temp1);
                bo = false;
            }
            else
            {
                change();
            }
        }


        /*
         * 将字符串转换为float类型数字
         */
        public static float TryFloat(string str, float temp)
        {
            float re_f = temp;
            try { re_f = float.Parse(str); }
            catch { }
            return re_f;
        }

        /*
         * 计算并返回计算结果
         */
        private float getCalculate(string p, float a, float b)
        {

            float n = 0;
            switch (p)
            {
                case "+":
                    return n = (a b);

                case "-":
                    return n = (a - b);

                case "×":
                    return n = (a * b);

                case "÷":
                    return n = (a / b);
            }
            return n;

        }

        /*
         * 初始化计算器,归零所有参数
         */
        public void change()
        {

            temp1 = 0;
            temp2 = 0;
            textBox1.Text = "";
            label1.Text = "";
            lal.Text = "";
            s = "";
            x = "";
            m = "";
            fh = false;
        }
    }
}