Tugas 2 Kelas PBKK - A 2022

 Kalkulator Sederhana Menggunakan Framework .NET


Pada postingan kali ini, saya mengerjakan contoh aplikasi yang dibuat dengan Framework.NET. Yaitu kalkulator sederhana dengan menggunakan bahasa C# serta MS Visual Studio Code. Dibawah ini adalah code dari pembuatan Kalkulator Sederhana 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Calculator_APP
  12. {
  13. public partial class Form1 : Form
  14. {
  15.  
  16. string first = "";
  17. string second = "";
  18. char function;
  19. double result = 0.0;
  20. string userInput = "";
  21.  
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. }
  26.  
  27. private void angka1_Click(object sender, EventArgs e)
  28. {
  29. layar.Text = "";
  30. userInput += "1";
  31. layar.Text += userInput;
  32. }
  33.  
  34. private void angka2_Click(object sender, EventArgs e)
  35. {
  36. layar.Text = "";
  37. userInput += "2";
  38. layar.Text += userInput;
  39. }
  40.  
  41. private void angka3_Click(object sender, EventArgs e)
  42. {
  43. layar.Text = "";
  44. userInput += "3";
  45. layar.Text += userInput;
  46. }
  47.  
  48. private void angka4_Click(object sender, EventArgs e)
  49. {
  50. layar.Text = "";
  51. userInput += "4";
  52. layar.Text += userInput;
  53. }
  54.  
  55. private void angka5_Click(object sender, EventArgs e)
  56. {
  57. layar.Text = "";
  58. userInput += "5";
  59. layar.Text += userInput;
  60. }
  61.  
  62. private void angka6_Click(object sender, EventArgs e)
  63. {
  64. layar.Text = "";
  65. userInput += "6";
  66. layar.Text += userInput;
  67. }
  68.  
  69. private void angka7_Click(object sender, EventArgs e)
  70. {
  71. layar.Text = "";
  72. userInput += "7";
  73. layar.Text += userInput;
  74. }
  75.  
  76. private void angka8_Click(object sender, EventArgs e)
  77. {
  78. layar.Text = "";
  79. userInput += "8";
  80. layar.Text += userInput;
  81. }
  82.  
  83. private void angka9_Click(object sender, EventArgs e)
  84. {
  85. layar.Text = "";
  86. userInput += "9";
  87. layar.Text += userInput;
  88. }
  89.  
  90. private void tombolclear_Click(object sender, EventArgs e)
  91. {
  92. first = "";
  93. second = "";
  94. userInput = "";
  95. result = 0.0;
  96. layar.Text = "0";
  97. }
  98.  
  99. private void tombolbagi_Click(object sender, EventArgs e)
  100. {
  101. function = '/';
  102. first = userInput;
  103. userInput = "";
  104. }
  105.  
  106. private void tombolkali_Click(object sender, EventArgs e)
  107. {
  108. function = '*';
  109. first = userInput;
  110. userInput = "";
  111. }
  112.  
  113. private void tomboltambah_Click(object sender, EventArgs e)
  114. {
  115. function = '+';
  116. first = userInput;
  117. userInput = "";
  118. }
  119.  
  120. private void tombolkurang_Click(object sender, EventArgs e)
  121. {
  122. function = '-';
  123. first = userInput;
  124. userInput = "";
  125. }
  126.  
  127. private void tombolsamadengan_Click(object sender, EventArgs e)
  128. {
  129. second = userInput;
  130. double firstNum, secondNum;
  131. firstNum = Convert.ToDouble(first);
  132. secondNum = Convert.ToDouble(second);
  133.  
  134. //tambah
  135. if(function == '+')
  136. {
  137. result = firstNum + secondNum;
  138. layar.Text = result.ToString();
  139. }
  140. //kurang
  141. else if(function == '-')
  142. {
  143. result = firstNum - secondNum;
  144. layar.Text = result.ToString();
  145. }
  146. //bagi
  147. else if(function == '/')
  148. {
  149. if(secondNum == '0')
  150. {
  151. layar.Text = "wrong";
  152. }
  153. else
  154. {
  155. result = firstNum / secondNum;
  156. layar.Text = result.ToString();
  157. }
  158. }
  159. //kali
  160. else if(function == '*')
  161. {
  162. result = firstNum * secondNum;
  163. layar.Text = result.ToString();
  164. }
  165.  
  166. }
  167.  
  168. private void tombolkoma_Click(object sender, EventArgs e)
  169. {
  170. layar.Text += ".";
  171. }
  172.  
  173. private void tombolnol_Click(object sender, EventArgs e)
  174. {
  175. layar.Text = "";
  176. userInput += "0";
  177. layar.Text += userInput;
  178. }
  179. }
  180. }
  181.  



Untuk hasil bentuk kalkulator dibawah ini : 




Referensi untuk pembuatan tugas ini :






Terima kasih. Tetap semangat, tetap kuat, tetap hidup!!!

Komentar