Membuat calculator pada C#
1. Design Form seperti gambar
dibawah ini
Gambar 1.1
2. Lalu ketik program seperti
dibawah ini sesuai pada bagian sub private masing – masing
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
WindowsFormsApplication1
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
public static Double a =
0;
public static Double b =
0;
public static Double c =
0;
public static string bil = "0";
public static string
operatornya = "";
public Double hasil(string
operatornya)
{
switch
(operatornya)
{
case
" bagi": c = a / b;
break;
case
"kali": c = a * b;
break;
case
"tambah": c = a + b;
break;
default:
c = a - b;
break;
}
return
c;
}
private
void btntambah_Click(object
sender, EventArgs e)
{
if
(a == 0)
{
a = Double.Parse(bil);
operatornya = "tambah";
bil = null;
this.WIN.Text
= "0";
}
else
{
b = Double.Parse(bil);
a = hasil("tambah");
operatornya = "tambha";
bil = null;
this.WIN.Text
= "0";
}
}
private
void btn1_Click(object
sender, EventArgs e)
{
if
(bil != "0")
{
bil = bil + "1";
this.WIN.Text
= bil;
}
else
{
bil = "1";
this.WIN.Text
= bil;
}
}
private
void btn2_Click(object
sender, EventArgs e)
{
if
(bil != "0")
{
bil = bil + "2";
this.WIN.Text
= bil;
}
else
{
bil = "2";
this.WIN.Text
= bil;
}
}
private
void btn3_Click(object
sender, EventArgs e)
{
if
(bil != "0")
{
bil = bil + "3";
this.WIN.Text
= bil;
}
else
{
bil = "3";
this.WIN.Text
= bil;
}
}
private
void btn4_Click(object
sender, EventArgs e)
{
if
(bil != "0")
{
bil = bil + "4";
this.WIN.Text
= bil;
}
else
{
bil = "4";
this.WIN.Text
= bil;
}
}
private
void btn5_Click(object
sender, EventArgs e)
{
if
(bil != "0")
{
bil = bil + "5";
this.WIN.Text
= bil;
}
else
{
bil = "5";
this.WIN.Text
= bil;
}
}
private
void btn6_Click(object
sender, EventArgs e)
{
if
(bil != "0")
{
bil = bil + "6";
this.WIN.Text
= bil;
}
else
{
bil = "6";
this.WIN.Text
= bil;
}
}
private
void btn7_Click(object
sender, EventArgs e)
{
if
(bil != "0")
{
bil = bil + "7";
this.WIN.Text
= bil;
}
else
{
bil = "7";
this.WIN.Text
= bil;
}
}
private
void btn8_Click(object
sender, EventArgs e)
{
if
(bil != "0")
{
bil = bil + "8";
this.WIN.Text
= bil;
}
else
{
bil = "8";
this.WIN.Text
= bil;
}
}
private
void btn9_Click(object
sender, EventArgs e)
{
if
(bil != "0")
{
bil = bil + "9";
this.WIN.Text
= bil;
}
else
{
bil = "9";
this.WIN.Text
= bil;
}
}
private
void btn0_Click(object
sender, EventArgs e)
{
if
(bil != "0")
{
bil = bil + "0";
this.WIN.Text
= bil;
}
else
{
bil = "0";
this.WIN.Text
= bil;
}
}
private
void btnbagi_Click(object
sender, EventArgs e)
{
if
(a == 0)
{
a = Double.Parse(bil);
operatornya = "bagi";
bil = null;
this.WIN.Text
= "0";
}
else
{
b = Double.Parse(bil);
a = hasil("bagi");
operatornya = "bagi";
bil = null;
this.WIN.Text
= "0";
}
}
private
void btnkali_Click(object
sender, EventArgs e)
{
if
(a == 0)
{
a = Double.Parse(bil);
operatornya = "kali";
bil = null;
this.WIN.Text
= "0";
}
else
{
b = Double.Parse(bil);
a = hasil("kali");
operatornya = "kali";
bil = null;
this.WIN.Text
= "0";
}
}
private
void btnmin_Click(object
sender, EventArgs e)
{
if (a ==
0)
{
a = Double.Parse(bil);
operatornya = "kurang";
bil = null;
this.WIN.Text
= "0";
}
else
{
b = Double.Parse(bil);
a = hasil("kurang");
operatornya = "kurang";
bil = null;
this.WIN.Text
= "0";
}
}
private
void btnhasil_Click(object
sender, EventArgs e)
{
b = Double.Parse(bil);
this.WIN.Text
= hasil(operatornya).
ToString();
bil = "0";
a = 0;
b = 0;
}
private
void btnc_Click(object
sender, EventArgs e)
{
a = 0;
b = 0;
bil = "0";
this.WIN.Text
= "0";
}
private
void btnback_Click(object
sender, EventArgs e)
{
int
jml = bil.Length;
bil = bil.Substring(0, jml - 1);
this.WIN.Text=
bil;
}
private
void btnplus_Click(object
sender, EventArgs e)
{
double
bilMinus= Double.Parse(bil) * -1;
bil = bilMinus.ToString();
this.WIN.Text
= bil;
}
{
if
(bil != "0")
{
bil = bil + ",";
this.WIN.Text
= bil;
}
else
{
bil = "0";
this.WIN.Text
= bil;
}
}
private
void btnTambah_Click_1(object
sender, EventArgs e)
{
}
private
void Form1_Load(object
sender, EventArgs e)
{
}
}
}
Komentar ini telah dihapus oleh pengarang.
BalasHapus