Kalkulator Sederhana

 Kalkulator Sederhana

Nama : Fadhil Dimas Sucahyo
NRP : 05111940000212

Untuk Pertemuan ini, saya telah membuat aplikasi kalkulator sederhana menggunakan .NET Framework.


Aplikasi ini memiliki fungsi untuk melakukan penambahan, pengurangan, perkalian, dan pembagian. Aplikasi ini juga memiliki fungsi untuk membersihkan input dan perhitungan.

Langkah pertama kita membuka project baru dalam aplikasi Visual Studio dan pilih Windows Forms App (.NET). Lalu kita dapat membuat beberapa pengaturan terhadap tampilan aplikasi, seperti menambahkan button, textbox, font, allignment, warna, dan lain-lainnya. Jangan lupa untuk merename button dan text yang dibuat menyesuaikan source code yang akan dibuat.

Berikut tampilan aplikasi yang telah dibuat.


Dan ini source code untuk aplikasi Kalkulator Sederhana.

  1. namespace SoapCalculator
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         Double resultValue = 0;
  6.         String operationPerformed = "";
  7.         bool isOperationPerformed = false;
  8.        
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.  
  14.         private void button_Click(object sender, EventArgs e)
  15.         {
  16.             if ((textBox_Result.Text == "0") || (isOperationPerformed))
  17.                 textBox_Result.Clear();
  18.  
  19.             isOperationPerformed = false;
  20.             Button button = (Button)sender;
  21.             if (button.Text == ".")
  22.             {
  23.                 if(!textBox_Result.Text.Contains("."))
  24.                     textBox_Result.Text = textBox_Result.Text + button.Text;
  25.             }else
  26.             textBox_Result.Text = textBox_Result.Text + button.Text;
  27.         }
  28.  
  29.         private void operator_Click(object sender, EventArgs e)
  30.         {
  31.             Button button = (Button)sender;
  32.             if (resultValue != 0)
  33.             {
  34.                 button11.PerformClick();
  35.                 operationPerformed = button.Text;
  36.                 labelCurrentOperation.Text = resultValue + " " + operationPerformed;
  37.                 isOperationPerformed = true;
  38.             }
  39.             else
  40.             {
  41.                 operationPerformed = button.Text;
  42.                 resultValue = Double.Parse(textBox_Result.Text);
  43.                 labelCurrentOperation.Text = resultValue + " " + operationPerformed;
  44.                 isOperationPerformed = true;
  45.             }
  46.         }
  47.  
  48.         private void button5_Click(object sender, EventArgs e)
  49.         {
  50.             textBox_Result.Text = "0";
  51.         }
  52.  
  53.         private void button6_Click(object sender, EventArgs e)
  54.         {
  55.             textBox_Result.Text = "0";
  56.             resultValue = 0;
  57.         }
  58.  
  59.         private void button11_Click(object sender, EventArgs e)
  60.         {
  61.             switch(operationPerformed)
  62.             {
  63.                 case "+":
  64.                     textBox_Result.Text = (resultValue + Double.Parse(textBox_Result.Text)).ToString();
  65.                     break;
  66.                 case "-":
  67.                     textBox_Result.Text = (resultValue - Double.Parse(textBox_Result.Text)).ToString();
  68.                     break;
  69.                 case "*":
  70.                     textBox_Result.Text = (resultValue * Double.Parse(textBox_Result.Text)).ToString();
  71.                     break;
  72.                 case "/":
  73.                     textBox_Result.Text = (resultValue / Double.Parse(textBox_Result.Text)).ToString();
  74.                     break;
  75.                 default:
  76.                     break;
  77.             }
  78.             resultValue = Double.Parse(textBox_Result.Text);
  79.             labelCurrentOperation.Text = "";
  80.         }
  81.     }
  82. }
Contoh Hasil






Comments

Popular posts from this blog

ETS PBKK A