Tugas 2 PBKK A

Nama : William Zefanya Maranatha
NRP : 5025201167
Kelas : A

    Program kalkulator sederhana ini memiliki beberapa operasi dasar, termasuk penambahan, pengurangan, perkalian, dan pembagian. Untuk memulai, buatlah proyek baru dalam aplikasi Visual Studio dan pilih templat Windows Forms App. Pastikan untuk memberi nama ulang tombol dan teks yang Anda buat dalam aplikasi ini.


LINK GITHUB : https://github.com/ALMAGEST12/PBKK-2
SC:
  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
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         Double resultValue = 0;
  16.         String operationPerformed = "";
  17.         bool isOperationPerformed = false;
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void button_Click(object sender, EventArgs e)
  24.         {
  25.             if((textBox_Result.Text == "0") || (isOperationPerformed))
  26.                 textBox_Result.Clear();
  27.             isOperationPerformed = false;
  28.             Button button = (Button)sender;
  29.             if(button.Text == ".")
  30.             {
  31.                 if(!textBox_Result.Text.Contains("."))
  32.                     textBox_Result.Text = textBox_Result.Text + button.Text;
  33.             }
  34.             else
  35.             textBox_Result.Text = textBox_Result.Text + button.Text;
  36.         }
  37.  
  38.         private void operator_click(object sender, EventArgs e)
  39.         {
  40.             Button button = (Button)sender;
  41.             if(resultValue!=0)
  42.             {
  43.                 button15.PerformClick();
  44.                 operationPerformed = button.Text;
  45.                 labelCurrentOperation.Text = resultValue + " " + operationPerformed;
  46.                 isOperationPerformed = true;
  47.             }
  48.             else
  49.             {
  50.             operationPerformed = button.Text;
  51.             resultValue = Double.Parse(textBox_Result.Text);
  52.             labelCurrentOperation.Text = resultValue + " " + operationPerformed;
  53.             isOperationPerformed = true;
  54.             }
  55.         }
  56.  
  57.         private void button4_Click(object sender, EventArgs e)
  58.         {
  59.             textBox_Result.Text = "0";
  60.         }
  61.  
  62.         private void button5_Click(object sender, EventArgs e)
  63.         {
  64.             textBox_Result.Text = "0";
  65.             resultValue = 0;
  66.         }
  67.  
  68.         private void button15_Click(object sender, EventArgs e)
  69.         {
  70.             switch(operationPerformed)
  71.             {
  72.                 case "+":
  73.                     textBox_Result.Text = (resultValue + Double.Parse(textBox_Result.Text)).ToString();
  74.                     break;
  75.                 case "-":
  76.                     textBox_Result.Text = (resultValue - Double.Parse(textBox_Result.Text)).ToString();
  77.                     break;
  78.                 case "*":
  79.                     textBox_Result.Text = (resultValue * Double.Parse(textBox_Result.Text)).ToString();
  80.                     break;
  81.                 case "/":
  82.                     textBox_Result.Text = (resultValue / Double.Parse(textBox_Result.Text)).ToString();
  83.                     break;
  84.                 default:
  85.                     break;
  86.  
  87.             }
  88.             resultValue = Double.Parse(textBox_Result.Text);
  89.             labelCurrentOperation.Text = "";
  90.         }
  91.     }
  92. }

Komentar

Postingan populer dari blog ini

Quiz 1 PBKK

FP PBKK A