-1

I am trying to continuously get this calculator to show the inputs, such as 1 + 1 = 2, I believe the code is correct, yet when I run into + - / * it needs parsed. I do not have the closest ideas how to do this.

I have tried for quite some time, so I char.Parse and if so where?

public partial class Form1 : Form
{
    char c;
    double num1;
    double num2;

    public Form1()
    {
        InitializeComponent();
    }
    private void btn0_Click(object sender, EventArgs e)
    {
        txtBox.Text += 0;
    }
    private void btn1_Click(object sender, EventArgs e)
    {
        txtBox.Text += 1;
    }
    private void btn2_Click(object sender, EventArgs e)
    {
        txtBox.Text += 2;
    }
    private void btn3_Click(object sender, EventArgs e)
    {
        txtBox.Text += 3;
    }
    private void btn4_Click(object sender, EventArgs e)
    {
        txtBox.Text += 4;
    }
    private void btn5_Click(object sender, EventArgs e)
    {
        txtBox.Text += 5;
    }
    private void btn6_Click(object sender, EventArgs e)
    {
        txtBox.Text += 6;
    }
    private void btn7_Click(object sender, EventArgs e)
    {
        txtBox.Text += 7;
    }
    private void btn8_Click(object sender, EventArgs e)
    {
        txtBox.Text += 8;
    }
    private void btn9_Click(object sender, EventArgs e)
    {
        txtBox.Text += 9;
    }
    private void btnDecimal_Click(object sender, EventArgs e)
    {
        if (!txtBox.Text.Contains('.'))
            txtBox.Text += '.';
    }
    private void btnAddition_Click(object sender, EventArgs e)
    {
        c = '+';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty; // txtBox.Text += c;
    }
    private void btnSubtraction_Click(object sender, EventArgs e)
    {
        c = '-';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnMultiplication_Click(object sender, EventArgs e)
    {
        c = '*';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnDivision_Click(object sender, EventArgs e)
    {
        c = '/';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnClear_Click(object sender, EventArgs e)
    {
        txtBox.Clear();
    }
    private void btnEqual_Click(object sender, EventArgs e)
    {
        double result;
        num2 = double.Parse(txtBox.Text);
        switch (c)
        {
            case '+':
                result = num1 + num2;
                txtBox.Text = result.ToString(); // txtBox.Text += result.ToString();
                break;
            case '-':
                result = num1 - num2;
                txtBox.Text = result.ToString();
                break;
            case '/':
                if (num2 != 0)
                {
                    result = num1 / num2;
                    txtBox.Text = result.ToString();
                }
                else
                {
                    txtBox.Text = "You can't divide by zero... sign up for Math 100 please =)";
                }
                break;
            case '*':
                result = num1 * num2;
                txtBox.Text = result.ToString();
                break;
        }
    }
}

}

Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
Zoro
  • 695
  • 2
  • 7
  • 23
  • 3
    What do you mean by "parse a char"? "parse" means to discover internal structure of a string - there is no structure in `"+"`, it's a single character, and you even know what it is. – Amadan Sep 03 '13 at 00:50

3 Answers3

2

The logic in your code is entirely flawed.

You can do three things:

Firstly, you can scrap what you're doing and go with a library that does this for you.. such as NCalc.

Or, you can scrap what you're doing and look up an algorithm that does this.. such as the Shunting Yard algorithm.

Or, you can keep trying to refine what you have.. but it might be best to look up other ways of doing this.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
0

I don't know if this suites your needs but perhaps you could try something like this:

Is there a string math evaluator in .NET?

Community
  • 1
  • 1
Neaox
  • 1,933
  • 3
  • 18
  • 29
0

From looking at your code, your problematic line of code is:

txtBox.Text = result.ToString(); // txtBox.Text += result.ToString();

You're setting the textbox's text to the result of your computation (a + b, a - b, a * b, or a / b) rather than the expression and result that you desire.

As a result, I think you're looking for something like

txtBox.Text = num1 + " + " + num2 + " = " + result.ToString();
Warty
  • 7,237
  • 1
  • 31
  • 49