Guest User

Better calculator in c#

a guest
Aug 29th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace CalculatorThatIsBetter
  5. {
  6.     public partial class Form1 : Form
  7.     {
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.         decimal firstNumber=0, secondNumber=0, thirdNumber=0;
  13.         byte counter = 0;
  14.  
  15.  
  16.         private void buttonCompute_Click(object sender, EventArgs e)
  17.         {
  18.             if(counter == 0) {
  19.                 if (decimal.TryParse(textBoxInput.Text, out firstNumber)==false)
  20.                 {
  21.                     ShowError_Parse(); } counter++; labelFirstNum.Text = firstNumber.ToString("N2");
  22.                 textBoxInput.Text = "";
  23.             }
  24.             else if(counter == 1) {
  25.                 if (decimal.TryParse(textBoxInput.Text, out secondNumber) == false)
  26.                 {
  27.                     ShowError_Parse(); } counter++; labelSecondNum.Text = secondNumber.ToString("N2");
  28.                 textBoxInput.Text = "";
  29.             }
  30.             else if (counter > 1)
  31.             {
  32.                 // do math
  33.                 if (labelOperator.Text=="+") { thirdNumber = firstNumber + secondNumber; }
  34.                 if (labelOperator.Text == "-") { thirdNumber = firstNumber - secondNumber; }
  35.                 if(labelOperator.Text == "*") { thirdNumber = firstNumber * secondNumber; }
  36.                 if (labelOperator.Text == "/"&&secondNumber!=0) { thirdNumber = firstNumber / secondNumber; }
  37.                 if (labelOperator.Text=="/"&&secondNumber==0) { MessageBox.Show("You cannot divide by zero","Error"); }
  38.                 MessageBox.Show("Result: "+thirdNumber,"Your Answer");
  39.                 counter = 0;
  40.             }
  41.            
  42.         }
  43.  
  44.         private void buttonAdd_Click(object sender, EventArgs e)
  45.         {
  46.             labelOperator.Text = "+";
  47.         }
  48.  
  49.         private void buttonMultiply_Click(object sender, EventArgs e)
  50.         {
  51.             labelOperator.Text = "*";
  52.         }
  53.  
  54.         private void buttonSubtract_Click(object sender, EventArgs e)
  55.         {
  56.             labelOperator.Text = "-";
  57.         }
  58.  
  59.         private void buttonDivide_Click(object sender, EventArgs e)
  60.         {
  61.             labelOperator.Text = "/";
  62.         }
  63.  
  64.         private void button1_Click(object sender, EventArgs e)
  65.         {
  66.             textBoxInput.Text += "1";
  67.         }
  68.  
  69.         private void button2_Click(object sender, EventArgs e)
  70.         {
  71.             textBoxInput.Text += "2";
  72.         }
  73.  
  74.         private void button3_Click(object sender, EventArgs e)
  75.         {
  76.             textBoxInput.Text += "3";
  77.         }
  78.  
  79.         private void button4_Click(object sender, EventArgs e)
  80.         {
  81.             textBoxInput.Text += "4";
  82.         }
  83.  
  84.         private void button5_Click(object sender, EventArgs e)
  85.         {
  86.             textBoxInput.Text += "5";
  87.         }
  88.  
  89.         private void button6_Click(object sender, EventArgs e)
  90.         {
  91.             textBoxInput.Text += "6";
  92.         }
  93.  
  94.         private void button7_Click(object sender, EventArgs e)
  95.         {
  96.             textBoxInput.Text += "7";
  97.         }
  98.  
  99.         private void button8_Click(object sender, EventArgs e)
  100.         {
  101.             textBoxInput.Text += "8";
  102.         }
  103.  
  104.         private void button9_Click(object sender, EventArgs e)
  105.         {
  106.             textBoxInput.Text += "9";
  107.         }
  108.  
  109.         private void buttonDecimal_Click(object sender, EventArgs e)
  110.         {
  111.             textBoxInput.Text += ".";
  112.         }
  113.  
  114.         private void buttonClear_Click(object sender, EventArgs e)
  115.         {
  116.             counter = 0;
  117.             firstNumber = 0;
  118.             secondNumber = 0;
  119.             thirdNumber = 0;
  120.             labelFirstNum.Text = firstNumber.ToString("N2");
  121.             labelOperator.Text = "+";
  122.             labelSecondNum.Text = secondNumber.ToString("N2");
  123.         }
  124.  
  125.         private void button10_Click(object sender, EventArgs e)
  126.         {
  127.             textBoxInput.Text += "0";
  128.         }
  129.         public void ShowError_Parse()
  130.         {
  131.             MessageBox.Show("Failed to parse","Error");
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment