Skip to main content

Simple Calculator C#





Code :
 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 Form1 : Form  
   {  
     string input = string.Empty;  
     string operand1 = string.Empty;  
     string operand2 = string.Empty;  
     char operation;  
     double result = 0.000;  
     public Form1()  
     {  
       InitializeComponent();  
     }  
     private void zero_Click(object sender, EventArgs e)  
     {  
       this.hasil.Text = "";  
       input += "0";  
       this.hasil.Text += input;  
     }  
     private void dot_Click(object sender, EventArgs e)  
     {  
       this.hasil.Text = "";  
       input += ".";  
       this.hasil.Text += input;  
     }  
     private void cancel_Click(object sender, EventArgs e)  
     {  
       this.hasil.Text = "";  
       this.input = string.Empty;  
       this.operand1 = string.Empty;  
       this.operand2 = string.Empty;  
     }  
     private void equal_Click(object sender, EventArgs e)  
     {  
       operand2 = input;  
       double num1, num2;  
       double.TryParse(operand1, out num1);  
       double.TryParse(operand2, out num2);  
       if (operation == '+')  
       {  
         result = num1 + num2;  
         hasil.Text = result.ToString();  
       }  
       else if (operation == '-') {  
         result = num1 - num2;  
         hasil.Text = result.ToString();  
       }  
       else if (operation == '*') {  
         result = num1 * num2;  
         hasil.Text = result.ToString();  
       }  
       else if (operation == '/') {  
         if (num2 != 0)  
         {  
           result = num1 / num2;  
           hasil.Text = result.ToString();  
         }  
         else  
           hasil.Text = "DIV/Zero!";  
       }  
     }  
     private void minus_Click(object sender, EventArgs e)  
     {  
       operand1 = input;  
       operation = '-';  
       input = string.Empty;  
     }  
     private void plus_Click(object sender, EventArgs e)  
     {  
       operand1 = input;  
       operation = '+';  
       input = string.Empty;  
     }  
     private void multiply_Click(object sender, EventArgs e)  
     {  
       operand1 = input;  
       operation = '*';  
       input = string.Empty;  
     }  
     private void divide_Click(object sender, EventArgs e)  
     {  
       operand1 = input;  
       operation = '/';  
       input = string.Empty;  
     }  
     private void one_Click(object sender, EventArgs e)  
     {  
       this.hasil.Text = "";  
       input += "1";  
       this.hasil.Text += input;  
     }  
     private void two_Click(object sender, EventArgs e)  
     {  
       this.hasil.Text = "";  
       input += "2";  
       this.hasil.Text += input;  
     }  
     private void three_Click(object sender, EventArgs e)  
     {  
       this.hasil.Text = "";  
       input += "3";  
       this.hasil.Text += input;  
     }  
     private void four_Click(object sender, EventArgs e)  
     {  
       this.hasil.Text = "";  
       input += "4";  
       this.hasil.Text += input;  
     }  
     private void five_Click(object sender, EventArgs e)  
     {  
       this.hasil.Text = "";  
       input += "5";  
       this.hasil.Text += input;  
     }  
     private void six_Click(object sender, EventArgs e)  
     {  
       this.hasil.Text = "";  
       input += "6";  
       this.hasil.Text += input;  
     }  
     private void seven_Click(object sender, EventArgs e)  
     {  
       this.hasil.Text = "";  
       input += "7";  
       this.hasil.Text += input;  
     }  
     private void eight_Click(object sender, EventArgs e)  
     {  
       this.hasil.Text = "";  
       input += "8";  
       this.hasil.Text += input;  
     }  
     private void nine_Click(object sender, EventArgs e)  
     {  
       this.hasil.Text = "";  
       input += "9";  
       this.hasil.Text += input;  
     }  
     private void result_TextChanged(object sender, EventArgs e)  
     {  
     }  
   }  
 }  


Comments