Rabu, 12 Maret 2014

Contoh Codingan kalkulator C#

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 kalkulator
{
    public partial class kalkulator : Form
    {
        string operand1 = string.Empty;
        string operand2 = string.Empty;
        string result;
        char operation;
        int a = 0;

        public kalkulator()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e) //angka 1
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "1";
        }

        private void button2_Click(object sender, EventArgs e) // angka 2
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "2";
        }

        private void button3_Click(object sender, EventArgs e) //angka 3
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "3";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "4";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "5";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "6";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "7";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "8";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "9";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "0";
        }

        private void button15_Click(object sender, EventArgs e) // .
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += ".";
        }

        private void button13_Click(object sender, EventArgs e) //  +
        {
            operand1 = textBox1.Text;
            operation = '+';
            textBox1.Text = string.Empty;

        }

        private void button11_Click(object sender, EventArgs e) // *
        {
            operand1 = textBox1.Text;
            operation = '*';
            textBox1.Text = string.Empty;

        }

        private void button12_Click(object sender, EventArgs e) // (“/”)
        {
            operand1 = textBox1.Text;
            operation = '/';
            textBox1.Text = string.Empty;

        }

        private void button14_Click(object sender, EventArgs e) // -
        {
            operand1 = textBox1.Text;
            operation = '-';
            textBox1.Text = string.Empty;

        }

        private void button16_Click(object sender, EventArgs e) // =
        {
            operand2 = textBox1.Text;
            double opr1, opr2;
            double.TryParse(operand1, out opr1);
            double.TryParse(operand2, out opr2);

            switch (operation)
            {
                case '+':
                    a = 0;
                    result = (opr1 + opr2).ToString();
                    textBox1.Text = result.ToString();
                    break;

                case '-':
                    a = 0;
                    result = (opr1 - opr2).ToString();
                    textBox1.Text = result.ToString();
                    break;

                case '*':
                    a = 0;
                    result = (opr1 * opr2).ToString();
                    textBox1.Text = result.ToString();
                    break;

                case '/':
                    if (opr2 != 0)
                    {
                        a = 0;
                        result = (opr1 / opr2).ToString();
                        textBox1.Text = result.ToString();
                    }
                    else
                    {
                        a = 0;
                        textBox1.Text = "tidak bisa di bagi 0 !!";
                    }
                    break;
            }
        }

        private void button17_Click(object sender, EventArgs e) // C
        {
            textBox1.Text = "0";
        }
    }

}

Gambar Kalkulator


Tidak ada komentar:

Posting Komentar