Code Calculator (tham khảo)

4 views
Skip to first unread message

anhtuan.songhong

unread,
Jul 16, 2014, 10:04:10 AM7/16/14
to ne...@googlegroups.com
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OnTap
{
    public partial class Form1 : Form
    {
        //Dung de su dung trong cac phep tinh
        private bool check;
        private double memory;
        private int op;

        private const int PLUS = 1;
        private const int MINUS = 2;
        private const int MULTI = 3;
        private const int DEVIDE = 4;

        //Dung de su dung trong day memory
        private double m;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            if (check)
                txtDisplay.Clear();
            txtDisplay.Text += 1;
            check = false;
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (check)
                txtDisplay.Clear();
            txtDisplay.Text += 2;
            check = false;
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (check)
                txtDisplay.Clear();
            txtDisplay.Text += 3;
            check = false;
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (check)
                txtDisplay.Clear(); 
            txtDisplay.Text += 4;
            check = false;
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (check)
                txtDisplay.Clear(); 
            txtDisplay.Text += 5;
            check = false;
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (check)
                txtDisplay.Clear(); 
            txtDisplay.Text += 6;
            check = false;
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if (check)
                txtDisplay.Clear(); 
            txtDisplay.Text += 7;
            check = false;
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (check)
                txtDisplay.Clear(); 
            txtDisplay.Text += 8;
            check = false;
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            if (check)
                txtDisplay.Clear(); 
            txtDisplay.Text += 9;
            check = false;
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            if (check)
                txtDisplay.Clear(); 
            txtDisplay.Text += 0;
            check = false;
        }

        private void btnResult_Click(object sender, EventArgs e)
        {
            try
            {
                double result;
                double memory2 = double.Parse(txtDisplay.Text);
                
                switch(op){
                    case PLUS:
                        result = memory + memory2;
                        txtDisplay.Text = result.ToString();
                        check = true;
                        memory = 0;
                        break;
                    case MINUS:
                        result = memory - memory2;
                        txtDisplay.Text = result.ToString();
                        check = true;
                        memory = 0;
                        break;
                    case MULTI:
                        result = memory * memory2;
                        txtDisplay.Text = result.ToString();
                        check = true;
                        memory = 0;
                        break;
                    case DEVIDE:
                        result = memory / memory2;
                        txtDisplay.Text = result.ToString();
                        check = true;
                        memory = 0;
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            try
            {
                memory += double.Parse(txtDisplay.Text);
                txtDisplay.Text = memory.ToString();
                op = PLUS;
                check = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            try
            {
                memory -= double.Parse(txtDisplay.Text);
                txtDisplay.Text = memory.ToString();
                op = MINUS;
                check = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnMulti_Click(object sender, EventArgs e)
        {
            try
            {
                if(memory==0)
                    memory = double.Parse(txtDisplay.Text);
                else
                    memory = memory * double.Parse(txtDisplay.Text);
                txtDisplay.Text = memory.ToString();
                op = MULTI;
                check = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnDivide_Click(object sender, EventArgs e)
        {
            try
            {
                if(memory==0)
                    memory = double.Parse(txtDisplay.Text);
                else
                    memory = memory/double.Parse(txtDisplay.Text);
                txtDisplay.Text = memory.ToString();
                op = DEVIDE;
                check = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnCE_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = null;
        }

        private void btnC_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = null;
            memory = 0;
        }

        private void btnMPlus_Click(object sender, EventArgs e)
        {
            m += double.Parse(txtDisplay.Text);            
        }

        private void btnMResult_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = m.ToString();
        }
    }
}

Reply all
Reply to author
Forward
0 new messages