-3

I'm developing a calculator in C#, and I have gotten pretty far except for the fact that when I am adding numbers, it only succeeds if the addends are the same number (e.g. 9 + 9 = 18, 6 + 9 = 12). So I'm pretty confused. Will someone help me please?

private void button5_Click(object sender, EventArgs e)
{
    b = false;
    number = decimal.Parse(richTextBox1.Text);
    richTextBox1.Text = number + " + ";
    number2 = decimal.Parse(richTextBox1.Text);
    addReady = true;
}

private void button17_Click(object sender, EventArgs e)
{
    if (addReady == true)
    {
        answer = number + number2;
        richTextBox1.Text = Convert.ToString(answer);
        b = true;
    }
}

Also, when I put this in, my positive/negative button suddenly started malfunctioning, converting everything to 0. What's up with that?

decimal neg;
neg = number * 2;
number = number - neg;
richTextBox1.Text = Convert.ToString(number);
user3587709
  • 89
  • 1
  • 6
  • 7
    Come on... you're reading the same textbox twice... have you even tried debugging? You'd be able to find it by yourself in half a time you spent writing the question... – MarcinJuraszek May 12 '14 at 02:28
  • 1
    You are definitely confused, since when does 6 + 9 = 12? – JK. May 12 '14 at 02:30
  • What typo? That's what I'm trying to find out! – user3587709 May 12 '14 at 02:38
  • @JK I could not stop laughing... OP, just like MarcinJuraszek said, you're parsing the same TextBox twice. So, you're extracting the first decimal two times in the row. – B.K. May 12 '14 at 02:41
  • I get that, except I don't know how to fix it. – user3587709 May 12 '14 at 02:43
  • @B.K. I completely understand, but how to I fix it? – user3587709 May 12 '14 at 02:45
  • @user3587709: what's the name of your second textbox? Put that on the 6th line instead of richTextBox1. – siride May 12 '14 at 02:46
  • @siride I don't have a second textbox. – user3587709 May 12 '14 at 02:47
  • Important rule of thumb not followed here that's probably fueling your confusion: You should always give your UI elements a meaningful name. For example, what the hell is Button17 supposed to do?? You should go back and refactor your UI names and have much more clarity. – Paul Sasik May 12 '14 at 03:04
  • You're trying to write an expression parser! That's hard. You need to download a library to do that for you. Don't try to do your own parsing. – Enigmativity May 12 '14 at 03:09

1 Answers1

3

Alright. What you're trying to do is parse the string from your text box two times in the row. So, both times you'll get the exact same number. It doesn't cycle the way you wrote it. On top of that, you're not extracting the expression. To make your life easier, I suggest using Calculator.NET third party library. It parses mathematical expressions:

http://weblogs.asp.net/pwelter34/archive/2007/05/05/calculator-net-calculator-that-evaluates-math-expressions.aspx

Then there's NCalc (just like Calculator.NET, this library is unbelievably simple to use):

http://ncalc.codeplex.com/

You may also take advantage of the Microsoft Script Control, as demonstrated in this answer:

https://stackoverflow.com/a/392355/2006048

Just in case the link doesn't work I'll copy the code here for demonstration (all credit to the original poster):

MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
sc.Language = "VBScript";
string expression = "1 + 2 * 7";
object result = sc.Eval(expression);            
MessageBox.Show(result.ToString());
Community
  • 1
  • 1
B.K.
  • 9,982
  • 10
  • 73
  • 105