Friday, October 4, 2013

use parentheses in c# calculator

Hello I just write a simple calculator in C# & I want to improve my program to handle parentheses.

Here is my button to add 1(digit):

 private void btnOne_Click(object sender, EventArgs e)        {            txtResult.Text += '1';        }

This is a method for my Plus button:

private void btnPlus_Click(object sender, EventArgs e)        {            lblChar.Text = "+";            num1 = float.Parse(txtResult.Text);            txtResult.Text = "";        }

And this is for to calculate final result:

private void btnEqual_Click(object sender, EventArgs e)        {    num2 = float.Parse(txtResult.Text);                if (lblChar.Text == "+")                {                    num3 = num1 + num2;                    txtResult.Text = Convert.ToString(num3);                }}

Anyone can assist me to write parentheses for my program?

You can use NCalc – Mathematical Expressions Evaluator for .NET

 Expression e = new Expression("2 + (3 + 5)*6"); var result = e.Evaluate();

No comments:

Post a Comment