ohbutt69

ohbuttc#

Oct 26th, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.50 KB | None | 0 0
  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.Windows.Forms;
  9.  
  10. namespace Calculator
  11. {
  12.     public partial class Calculator : Form
  13.     {
  14.         public Calculator()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         bool Clear = true; //determines if the label should be cleared for new input
  19.         double firstInput; // first input
  20.         double secondInput; // second input
  21.         double result = 0; // result of first and second input
  22.         string operatorSymbol; // holds the operator symbol for each calculation
  23.  
  24.         private void btn0_Click(object sender, EventArgs e)
  25.         {
  26.             lblDisplay.Text = lblDisplay.Text + "0";
  27.         }
  28.         private void btn1_Click(object sender, EventArgs e)
  29.         {
  30.             lblDisplay.Text = lblDisplay.Text + "1";
  31.         }
  32.         private void btn2_Click(object sender, EventArgs e)
  33.         {
  34.             lblDisplay.Text = lblDisplay.Text + "2";
  35.         }
  36.         private void btn3_Click(object sender, EventArgs e)
  37.         {
  38.             lblDisplay.Text = lblDisplay.Text + "3";
  39.         }
  40.         private void btn4_Click(object sender, EventArgs e)
  41.         {
  42.             lblDisplay.Text = lblDisplay.Text + "4";
  43.         }
  44.         private void btn5_Click(object sender, EventArgs e)
  45.         {
  46.             lblDisplay.Text = lblDisplay.Text + "5";
  47.         }
  48.         private void btn6_Click(object sender, EventArgs e)
  49.         {
  50.             lblDisplay.Text = lblDisplay.Text + "6";
  51.         }
  52.         private void btn7_Click(object sender, EventArgs e)
  53.         {
  54.             lblDisplay.Text = lblDisplay.Text + "7";
  55.         }
  56.         private void btn8_Click(object sender, EventArgs e)
  57.         {
  58.             lblDisplay.Text = lblDisplay.Text + "8";
  59.         }
  60.         private void btn9_Click(object sender, EventArgs e)
  61.         {
  62.             lblDisplay.Text = lblDisplay.Text + "9";
  63.         }
  64.         private void btnDecimal_Click(object sender, EventArgs e)
  65.         {
  66.             lblDisplay.Text = lblDisplay.Text + ".";
  67.             btnDecimal.Enabled = false;
  68.         }
  69.         private void btnBackspace_Click(object sender, EventArgs e)
  70.         {
  71.             if (lblDisplay.Text.Length >= 2)
  72.             {
  73.                 lblDisplay.Text = lblDisplay.Text.Remove(lblDisplay.Text.Length - 1);
  74.             }
  75.             else
  76.             {
  77.                 lblDisplay.Text = "0";
  78.                 btnDecimal.Enabled = true;
  79.             }
  80.         }
  81.         private void btnClear_Click(object sender, EventArgs e)
  82.         {
  83.             lblDisplay.Text = "0";
  84.             btnDecimal.Enabled = true;
  85.             firstInput = 0;
  86.             secondInput = 0;
  87.         }
  88.  
  89.         private void btnPlus_Click(object sender, EventArgs e)
  90.         {
  91.             firstInput = double.Parse(lblDisplay.Text);
  92.             operatorSymbol = "+";
  93.             lblDisplay.Text = "0";
  94.         }
  95.        
  96.         private void btnMinus_Click(object sender, EventArgs e)
  97.         {
  98.             firstInput = double.Parse(lblDisplay.Text);
  99.             operatorSymbol = "-";
  100.             lblDisplay.Text = "0";
  101.         }
  102.  
  103.         private void btnMultiply_Click(object sender, EventArgs e)
  104.         {
  105.             firstInput = double.Parse(lblDisplay.Text);
  106.             operatorSymbol = "*";
  107.             lblDisplay.Text = "0";
  108.         }
  109.  
  110.         private void btnDivide_Click(object sender, EventArgs e)
  111.         {
  112.             firstInput = double.Parse(lblDisplay.Text);
  113.             operatorSymbol = "/";
  114.             lblDisplay.Text = "0";
  115.         }
  116.  
  117.         private void btnEquals_Click(object sender, EventArgs e)
  118.         {
  119.             secondInput = double.Parse(lblDisplay.Text);
  120.             switch (operatorSymbol)
  121.             {
  122.                 case "+":
  123.                     result = firstInput + secondInput;
  124.                     operatorSymbol = "";
  125.                     break;
  126.                 case "-":
  127.                     result = firstInput - secondInput;
  128.                     operatorSymbol = "";
  129.                     break;
  130.                 case "*":
  131.                     result = firstInput * secondInput;
  132.                     operatorSymbol = "";
  133.                     break;
  134.                 case "/":
  135.                     result = firstInput / secondInput;
  136.                     operatorSymbol = "";
  137.                     break;
  138.             }
  139.             lblDisplay.Text = result.ToString();
  140.         }
  141.     }
  142. }
  143.  
  144.  
Advertisement
Add Comment
Please, Sign In to add comment