Kalkulator Sederhana menggunakan C#
Kali ini saya akan membuat sebuah aplikasi kalkulator sederhana. Aplikasi kalkulator sederhana ini menggunakan bahasa C# dengan bantuan framework .NET. Berikut adalah tampilan code dari aplikasi tersebut :
Berikut adalah source code dari aplikasi tersebut :
using System.Reflection.Emit;
namespace Calculator
{
public partial class Form1 : Form
{
Double resultValue = 0;
String operationPerformed = "";
bool isOperationPerformed = false;
public Form1()
{
InitializeComponent();
}
private void button_click(object sender, EventArgs e)
{
if ((Result.Text == "0") || (isOperationPerformed))
Result.Clear();
isOperationPerformed = false;
Button button = (Button)sender;
if (button.Text == ".")
{
if (!Result.Text.Contains("."))
Result.Text = Result.Text + button.Text;
}
Result.Text = Result.Text + button.Text;
}
private void buttonClear_Click(object sender, EventArgs e)
{
Result.Text = "0";
resultValue = 0;
}
private void buttonOperator_click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (resultValue != 0)
{
button15.PerformClick();
operationPerformed = button.Text;
label1.Text = resultValue + " " + operationPerformed;
isOperationPerformed = true;
}
else
{
operationPerformed = button.Text;
resultValue = double.Parse(Result.Text);
label1.Text = resultValue + " " + operationPerformed;
isOperationPerformed = true;
}
}
private void buttonEqual_Click(object sender, EventArgs e)
{
switch (operationPerformed)
{
case "+":
Result.Text = (resultValue + Double.Parse(Result.Text)).ToString();
break;
case "-":
Result.Text = (resultValue - Double.Parse(Result.Text)).ToString();
break;
case "X":
Result.Text = (resultValue * Double.Parse(Result.Text)).ToString();
break;
case "/":
Result.Text = (resultValue / Double.Parse(Result.Text)).ToString();
break;
default:
break;
}
resultValue = Double.Parse(Result.Text);
label1.Text = "";
}
}
}
Komentar
Posting Komentar