Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows.Forms;
- namespace CalculatorThatIsBetter
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- decimal firstNumber=0, secondNumber=0, thirdNumber=0;
- byte counter = 0;
- private void buttonCompute_Click(object sender, EventArgs e)
- {
- if(counter == 0) {
- if (decimal.TryParse(textBoxInput.Text, out firstNumber)==false)
- {
- ShowError_Parse(); } counter++; labelFirstNum.Text = firstNumber.ToString("N2");
- textBoxInput.Text = "";
- }
- else if(counter == 1) {
- if (decimal.TryParse(textBoxInput.Text, out secondNumber) == false)
- {
- ShowError_Parse(); } counter++; labelSecondNum.Text = secondNumber.ToString("N2");
- textBoxInput.Text = "";
- }
- else if (counter > 1)
- {
- // do math
- if (labelOperator.Text=="+") { thirdNumber = firstNumber + secondNumber; }
- if (labelOperator.Text == "-") { thirdNumber = firstNumber - secondNumber; }
- if(labelOperator.Text == "*") { thirdNumber = firstNumber * secondNumber; }
- if (labelOperator.Text == "/"&&secondNumber!=0) { thirdNumber = firstNumber / secondNumber; }
- if (labelOperator.Text=="/"&&secondNumber==0) { MessageBox.Show("You cannot divide by zero","Error"); }
- MessageBox.Show("Result: "+thirdNumber,"Your Answer");
- counter = 0;
- }
- }
- private void buttonAdd_Click(object sender, EventArgs e)
- {
- labelOperator.Text = "+";
- }
- private void buttonMultiply_Click(object sender, EventArgs e)
- {
- labelOperator.Text = "*";
- }
- private void buttonSubtract_Click(object sender, EventArgs e)
- {
- labelOperator.Text = "-";
- }
- private void buttonDivide_Click(object sender, EventArgs e)
- {
- labelOperator.Text = "/";
- }
- private void button1_Click(object sender, EventArgs e)
- {
- textBoxInput.Text += "1";
- }
- private void button2_Click(object sender, EventArgs e)
- {
- textBoxInput.Text += "2";
- }
- private void button3_Click(object sender, EventArgs e)
- {
- textBoxInput.Text += "3";
- }
- private void button4_Click(object sender, EventArgs e)
- {
- textBoxInput.Text += "4";
- }
- private void button5_Click(object sender, EventArgs e)
- {
- textBoxInput.Text += "5";
- }
- private void button6_Click(object sender, EventArgs e)
- {
- textBoxInput.Text += "6";
- }
- private void button7_Click(object sender, EventArgs e)
- {
- textBoxInput.Text += "7";
- }
- private void button8_Click(object sender, EventArgs e)
- {
- textBoxInput.Text += "8";
- }
- private void button9_Click(object sender, EventArgs e)
- {
- textBoxInput.Text += "9";
- }
- private void buttonDecimal_Click(object sender, EventArgs e)
- {
- textBoxInput.Text += ".";
- }
- private void buttonClear_Click(object sender, EventArgs e)
- {
- counter = 0;
- firstNumber = 0;
- secondNumber = 0;
- thirdNumber = 0;
- labelFirstNum.Text = firstNumber.ToString("N2");
- labelOperator.Text = "+";
- labelSecondNum.Text = secondNumber.ToString("N2");
- }
- private void button10_Click(object sender, EventArgs e)
- {
- textBoxInput.Text += "0";
- }
- public void ShowError_Parse()
- {
- MessageBox.Show("Failed to parse","Error");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment