1. 程式人生 > >C#實現簡單的計算器

C#實現簡單的計算器

用文字框接收點選button時的資訊來實現簡單資料兩個數之間的加減:

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 FrmCalcutor : Form
    {
        string sign;
        double num1;//第一個數
        double num2;//第二個數
        double num3;//運算結果
        bool flag = true;//判斷是否為第一個數
        public FrmCalcutor()
        {
            InitializeComponent();
        }

        private void btn1_Click(object sender, EventArgs e)
        {//數字1
            if (flag == true)
            {//未輸入運算子之前,flag設定為true,所以所鍵入的字元都是賦給第一個數字的
                Button b = (Button)sender;
                txtnum.Text += b.Text;       //連線button 鍵入的字串
                num1 = double.Parse(txtnum.Text);//將字串轉化為double型別賦給num
            }
            else
            {//輸入運算子後,flag設定為false,所以鍵入的字元都是賦給第二個字元的
                Button b = (Button)sender;     
                txtnum.Text += b.Text;
                num2 = double.Parse(txtnum.Text);
            }
        }

        private void btn2_Click(object sender, EventArgs e)
        {//數字2
            if (flag == true)
            { 
                Button b=(Button)sender;
                txtnum.Text += b.Text;
                num1 = double.Parse(txtnum.Text);
            }
            else
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num2 = double.Parse(txtnum.Text);
            }
        }

        private void btn3_Click(object sender, EventArgs e)
        {//數字3
            if (flag == true)
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num1 = double.Parse(txtnum.Text);
            }
            else
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num2 = double.Parse(txtnum.Text);
            }

        }

        private void btn4_Click(object sender, EventArgs e)
        {//數字4
            if (flag == true)
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num1 = double.Parse(txtnum.Text);
            }
            else
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num2 = double.Parse(txtnum.Text);
            }

        }

        private void btn5_Click(object sender, EventArgs e)
        {//數字5
            if (flag == true)
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num1 = double.Parse(txtnum.Text);
            }
            else
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num2 = double.Parse(txtnum.Text);
            }

        }

        private void btn6_Click(object sender, EventArgs e)
        {//數字6
            if (flag == true)
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num1 = double.Parse(txtnum.Text);
            }
            else
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num2 = double.Parse(txtnum.Text);
            }

        }

        private void btn7_Click(object sender, EventArgs e)
        {//數字7
            if (flag == true)
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num1 = double.Parse(txtnum.Text);
            }
            else
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num2 = double.Parse(txtnum.Text);
            }
        }

        private void btn8_Click(object sender, EventArgs e)
        {//數字8
            if (flag == true)
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num1 = double.Parse(txtnum.Text);
            }
            else
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num2 = double.Parse(txtnum.Text);
            }
        }

        private void btn9_Click(object sender, EventArgs e)
        {//數字9
            if (flag == true)
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num1 = double.Parse(txtnum.Text);
            }
            else
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num2 = double.Parse(txtnum.Text);
            }
        }

        private void btn0_Click(object sender, EventArgs e)
        {//數字0
            if (flag == true)
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num1 = double.Parse(txtnum.Text);
            }
            else
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num2 = double.Parse(txtnum.Text);
            }
        }

        private void btnpiont_Click(object sender, EventArgs e)
        {//小數點.
            if (flag == true)
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num1 = double.Parse(txtnum.Text);
            }
            else
            {
                Button b = (Button)sender;
                txtnum.Text += b.Text;
                num2 = double.Parse(txtnum.Text);
            }
        }

        private void btnjia_Click(object sender, EventArgs e)
        {//加號
            sign = "+";
            txtnum.Text = "";
            flag = false;
            label1.Text = sign;

        }

        private void btnjian_Click(object sender, EventArgs e)
        {//減號
            sign = "-";
            txtnum.Text = "";
            flag = false;
            label1.Text = sign;
        }

        private void btncheng_Click(object sender, EventArgs e)
        {//乘號
            sign = "*";
            txtnum.Text = "";
            flag = false;
            label1.Text = sign;
        }

        private void btnchu_Click(object sender, EventArgs e)
        {//除號
            sign = "/";
            txtnum.Text = "";
            flag = false;
            label1.Text = sign;
        }

        private void btndeng_Click(object sender, EventArgs e)
        {//等號
            
            flag = true;//點選=後,該計算過程結束,flag變為true,準備下一次計算時使用
            switch(sign)
            {
                case "+":
                    num3 = num1 + num2;
                    txtnum.Text = num3.ToString();
                    break;
                case "-":
                    num3 = num1 - num2;
                    txtnum.Text = num3.ToString();
                    break;
                case "*":
                    num3 = num1 * num2;
                    txtnum.Text = num3.ToString();
                    break;
                case "/":
                    if (num2 != 0)
                        num3 = num1 / num2;
                    else
                        txtnum.Text = "除數不能為0";
                    break;
            }
            

        }

        private void btnC_Click(object sender, EventArgs e)
        {//全部歸零
            num1 = 0;
            num2 = 0;
            num3 = 0;
            txtnum.Text = "";

        }

        private void btnCE_Click(object sender, EventArgs e)
        {//
            num2 = 0;
            num3 = 0;
            txtnum.Text = "";

        }

        private void btnclear_Click(object sender, EventArgs e)
        {//清楚上一位數字
           string s= txtnum.Text.Substring(0,txtnum.Text.Length-1);
           txtnum.Text = s;
           if (flag == true)
           
               num1 = double.Parse(s);
           
            else
               num2 = double.Parse(s);
            
        }

        private void FrmCalcutor_FormClosing(object sender, FormClosingEventArgs e)
        {
            string msg="是否要關閉計算器";
           DialogResult dr= MessageBox.Show(msg,"退出程式",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
           if (dr == DialogResult.OK)
               e.Cancel = false;
           else
               e.Cancel = true;
        }


    }
}