Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 Calculator
- {
- public partial class Calculator : Form
- {
- public Calculator()
- {
- InitializeComponent();
- }
- bool Clear = true; //determines if the label should be cleared for new input
- double firstInput; // first input
- double secondInput; // second input
- double result = 0; // result of first and second input
- string operatorSymbol; // holds the operator symbol for each calculation
- private void btn0_Click(object sender, EventArgs e)
- {
- lblDisplay.Text = lblDisplay.Text + "0";
- }
- private void btn1_Click(object sender, EventArgs e)
- {
- lblDisplay.Text = lblDisplay.Text + "1";
- }
- private void btn2_Click(object sender, EventArgs e)
- {
- lblDisplay.Text = lblDisplay.Text + "2";
- }
- private void btn3_Click(object sender, EventArgs e)
- {
- lblDisplay.Text = lblDisplay.Text + "3";
- }
- private void btn4_Click(object sender, EventArgs e)
- {
- lblDisplay.Text = lblDisplay.Text + "4";
- }
- private void btn5_Click(object sender, EventArgs e)
- {
- lblDisplay.Text = lblDisplay.Text + "5";
- }
- private void btn6_Click(object sender, EventArgs e)
- {
- lblDisplay.Text = lblDisplay.Text + "6";
- }
- private void btn7_Click(object sender, EventArgs e)
- {
- lblDisplay.Text = lblDisplay.Text + "7";
- }
- private void btn8_Click(object sender, EventArgs e)
- {
- lblDisplay.Text = lblDisplay.Text + "8";
- }
- private void btn9_Click(object sender, EventArgs e)
- {
- lblDisplay.Text = lblDisplay.Text + "9";
- }
- private void btnDecimal_Click(object sender, EventArgs e)
- {
- lblDisplay.Text = lblDisplay.Text + ".";
- btnDecimal.Enabled = false;
- }
- private void btnBackspace_Click(object sender, EventArgs e)
- {
- if (lblDisplay.Text.Length >= 2)
- {
- lblDisplay.Text = lblDisplay.Text.Remove(lblDisplay.Text.Length - 1);
- }
- else
- {
- lblDisplay.Text = "0";
- btnDecimal.Enabled = true;
- }
- }
- private void btnClear_Click(object sender, EventArgs e)
- {
- lblDisplay.Text = "0";
- btnDecimal.Enabled = true;
- firstInput = 0;
- secondInput = 0;
- }
- private void btnPlus_Click(object sender, EventArgs e)
- {
- firstInput = double.Parse(lblDisplay.Text);
- operatorSymbol = "+";
- lblDisplay.Text = "0";
- }
- private void btnMinus_Click(object sender, EventArgs e)
- {
- firstInput = double.Parse(lblDisplay.Text);
- operatorSymbol = "-";
- lblDisplay.Text = "0";
- }
- private void btnMultiply_Click(object sender, EventArgs e)
- {
- firstInput = double.Parse(lblDisplay.Text);
- operatorSymbol = "*";
- lblDisplay.Text = "0";
- }
- private void btnDivide_Click(object sender, EventArgs e)
- {
- firstInput = double.Parse(lblDisplay.Text);
- operatorSymbol = "/";
- lblDisplay.Text = "0";
- }
- private void btnEquals_Click(object sender, EventArgs e)
- {
- secondInput = double.Parse(lblDisplay.Text);
- switch (operatorSymbol)
- {
- case "+":
- result = firstInput + secondInput;
- operatorSymbol = "";
- break;
- case "-":
- result = firstInput - secondInput;
- operatorSymbol = "";
- break;
- case "*":
- result = firstInput * secondInput;
- operatorSymbol = "";
- break;
- case "/":
- result = firstInput / secondInput;
- operatorSymbol = "";
- break;
- }
- lblDisplay.Text = result.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment