[PBKK-2] Kalkulator Sederhana

Pada pertemuan kedua mata kuliah PBKK kami mendapat tugas untuk membuat aplikasi desktop sederhana menggunakan .NET Framework. .NET Framework adalah platform perangkat lunak yang dikembangkan oleh Microsoft. Frmaework ini digunakan untuk membangun dan menjalankan berbagai jenis aplikasi perangkat lunak di lingkungan sistem operasi Windows. 

Aplikasi  pertama yang saya buat adalah sebuah kalkulator sederhana. Kalkulator merupakan alat atau perangkat yang digunakan untuk melakukan perhitungan matematika. Kalkulator sederhana yang saya buat kali ini dapat melakukan operasi matematika dasar seperti penjumlahan, pengurangan, perkalian, dan pembagian. 

Source code: GitHub

Berikut adalah potongan kodenya dalam bahasa C# beserta dokumentasinya:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace WindowsFormsApp1
  13. {
  14. public partial class Form1 : Form
  15. {
  16. Double resultValue = 0;
  17. String operationPerformed = "";
  18. bool isOperationPerformed = false;
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24. private void button_click(object sender, EventArgs e)
  25. {
  26. if ((textBox_Result.Text == "0") || (isOperationPerformed))
  27. textBox_Result.Clear();
  28.  
  29. isOperationPerformed = false;
  30. Button button = (Button)sender;
  31.  
  32. if (button.Text == ".")
  33. {
  34. if (!textBox_Result.Text.Contains("."))
  35. textBox_Result.Text = textBox_Result.Text + button.Text;
  36.  
  37. }
  38. else
  39. textBox_Result.Text = textBox_Result.Text + button.Text;
  40.  
  41. }
  42.  
  43. private void operator_click(object sender, EventArgs e)
  44. {
  45. Button button = (Button)sender;
  46.  
  47. if (resultValue != 0)
  48. {
  49. buttonResult.PerformClick();
  50. operationPerformed = button.Text;
  51. labelCurrentOperation.Text = resultValue + " " + operationPerformed;
  52. isOperationPerformed = true;
  53. }
  54. else
  55. {
  56.  
  57. operationPerformed = button.Text;
  58. resultValue = Double.Parse(textBox_Result.Text);
  59. labelCurrentOperation.Text = resultValue + " " + operationPerformed;
  60. isOperationPerformed = true;
  61. }
  62. }
  63.  
  64. private void buttonCE_click(object sender, EventArgs e)
  65. {
  66. textBox_Result.Text = "0";
  67. }
  68.  
  69. private void buttonC_click(object sender, EventArgs e)
  70. {
  71. textBox_Result.Text = "0";
  72. resultValue = 0;
  73. }
  74.  
  75. private void buttonResult_click(object sender, EventArgs e)
  76. {
  77. switch (operationPerformed)
  78. {
  79. case "+":
  80. textBox_Result.Text = (resultValue + Double.Parse(textBox_Result.Text)).ToString();
  81. break;
  82. case "-":
  83. textBox_Result.Text = (resultValue - Double.Parse(textBox_Result.Text)).ToString();
  84. break;
  85. case "*":
  86. textBox_Result.Text = (resultValue * Double.Parse(textBox_Result.Text)).ToString();
  87. break;
  88. case "/":
  89. textBox_Result.Text = (resultValue / Double.Parse(textBox_Result.Text)).ToString();
  90. break;
  91. default:
  92. break;
  93. }
  94. resultValue = Double.Parse(textBox_Result.Text);
  95. labelCurrentOperation.Text = "";
  96. }
  97. }
  98. }
  99.  






Referensi: 












  

Komentar

Postingan Populer