-1

I am making a simple calculator. Till now I have been successfully able to implement some basic features in my calculator. Take a look at the code

public double num1 { get; set; }
        public double num2 { get; set; }
        public string op { get; set; }
       // public bool checker { get; set; }

        private void ButtonBase_OnClick1(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Button1.Content.ToString();
            ShowTextBlock.Text += Button1.Content.ToString();
        }

        private void ButtonBase_OnClick2(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Button2.Content.ToString();
            ShowTextBlock.Text += Button2.Content.ToString();
        }

        private void ButtonBase_OnClick3(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Button3.Content.ToString();
            ShowTextBlock.Text += Button3.Content.ToString();
        }

        private void ButtonBase_OnClick4(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Button4.Content.ToString();
            ShowTextBlock.Text += Button4.Content.ToString();
        }

        private void ButtonBase_OnClick5(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Button5.Content.ToString();
            ShowTextBlock.Text += Button5.Content.ToString();
        }

        private void ButtonBase_OnClick6(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Button6.Content.ToString();
            ShowTextBlock.Text += Button6.Content.ToString();
        }

        private void ButtonBase_OnClick7(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Button7.Content.ToString();
            ShowTextBlock.Text += Button7.Content.ToString();
        }

        private void ButtonBase_OnClick8(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Button8.Content.ToString();
            ShowTextBlock.Text += Button8.Content.ToString();
        }

        private void ButtonBase_OnClick9(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Button9.Content.ToString();
            ShowTextBlock.Text += Button9.Content.ToString();
        }

        private void ButtonBase_OnClick0(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Button0.Content.ToString();
            ShowTextBlock.Text += Button0.Content.ToString();
        }

        private void ButtonBase_OnClickdot(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Buttondot.Content.ToString();
            ShowTextBlock.Text += Buttondot.Content.ToString();
        }

        private void ButtonBase_OnClickobrac(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Buttonobrac.Content;
            ShowTextBlock.Text += Buttonobrac.Content.ToString();
        }

        private void ButtonBase_OnClickcbrac(object sender, RoutedEventArgs e)
        {
            CalculateTextBlock.Text += Buttoncbrac.Content;
            ShowTextBlock.Text += Buttoncbrac.Content.ToString();
        }



        //private void ButtonBase_OnClickinf(object sender, RoutedEventArgs e)
        //{
        //    CalculateTextBlock.Text += ("0/0").ToString();
        //    ShowTextBlock.Text += "Inf.";
        //}

        private void ButtonBase_OnClickplus(object sender, RoutedEventArgs e)
        {
            //num1 += double.Parse(CalculateTextBlock.Text);
            num1 = Convert.ToDouble(CalculateTextBlock.Text);
            op = "plus";
            CalculateTextBlock.Text = "";
            ShowTextBlock.Text += "+";
        }

        private void ButtonBase_OnClickminus(object sender, RoutedEventArgs e)
        {
            num1 += double.Parse(CalculateTextBlock.Text);
            op = "sub";
            CalculateTextBlock.Text = "";
            ShowTextBlock.Text += "-";
        }

        private void ButtonBase_OnClickmul(object sender, RoutedEventArgs e)
        {
            num1 +=  double.Parse(CalculateTextBlock.Text);
            op = "mul";
            CalculateTextBlock.Text = "";
            ShowTextBlock.Text += "*";
        }

        private void ButtonBase_OnClickdiv(object sender, RoutedEventArgs e)
        {
            num1 += double.Parse(CalculateTextBlock.Text);
            op = "div";
            CalculateTextBlock.Text = "";
            ShowTextBlock.Text += "/";
        }

        private void ButtonBase_OnClickequal(object sender, RoutedEventArgs e)
        {
            switch (op)
            {
                case "plus" :
                    //num2 = num1 + double.Parse(CalculateTextBlock.Text);
                    num2 = num1 + Convert.ToDouble(CalculateTextBlock.Text);
                    break;

                case "sub":
                    num2 = num1 - double.Parse(CalculateTextBlock.Text);
                    break;

                case "mul":
                    num2 = num1*double.Parse(CalculateTextBlock.Text);
                    break;

                case "div":
                    num2 = num1/double.Parse(CalculateTextBlock.Text);
                    break;
            }
            CalculateTextBlock.Text = num2.ToString();
            num1 = 0;
            ShowTextBlock.Text = "";

        }

        private void ButtonBase_OnClickclear(object sender, RoutedEventArgs e)
        {
            num1 = 0;
            num2 = 0;
            ShowTextBlock.Text = "";
            CalculateTextBlock.Text = "";
        }

The problem occurs when I insert brackets into the calculatetextbox. It throws a FormatException at the parsing. Is there any way I can implement BIDMAS here?

Note: I have tried both parsing and converting methods {int.parse and convert}

Any help is appreciated. Thanks

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Prajjwal
  • 49
  • 2
  • 10
  • Are all those codes really necessary? I think you should show only the parts that are neccessary to get a good answer. – Mohayemin May 17 '15 at 05:10
  • @Mohayemin They are only the calculation part of the calculator. For anyone who is seeing this code for first time the will understand what I am trying to do here easily. Only the numeric buttons part is not essentially required.. – Prajjwal May 17 '15 at 05:14
  • 4
    BIDMAS requires more complex code. What you might need to do is parse your string to calculate for pairs of brackets, from the most external to the most internal. Then, if brackets are found, you evaluate the contents (which might contain more bracktes inside), you repeat this recursively until no brackets are found, and at that point you return the result of the evaluation. – Jorge Torres May 17 '15 at 05:19

4 Answers4

1

If you need to implement BIDMAS rule and parenthesis, you should first parse the input and make tokens for each expression. Once you have each token then do the calculation as per the rule.

Other than that this question is answered here: dynamic expression evaluation

Another post on stack overflow also refers to this

stackoverflow post to answer the same question

Community
  • 1
  • 1
0

Seems like it's just missing .ToString() conversion of your Bottonobrac.Content in which you have it in your Buttondot.Content

private void ButtonBase_OnClickdot(object sender, RoutedEventArgs e)
    {
        CalculateTextBlock.Text += Buttondot.Content.ToString(); //You have it here
        ShowTextBlock.Text += Buttondot.Content.ToString();
    }

    private void ButtonBase_OnClickobrac(object sender, RoutedEventArgs e)
    {
        CalculateTextBlock.Text += Buttonobrac.Content; //Add .ToString() 
        ShowTextBlock.Text += Buttonobrac.Content.ToString();
    }

This should solve your issue. Meanwhile you can always test what's missing in your code using compile and execute c# online.

F.Lee
  • 1
  • 2
  • I tried tostring method previously. It just throws the FormatException. That's why I tried removing "ToString" Method. – Prajjwal May 17 '15 at 05:49
-1

Try this:-

<script type="text/javascript">
        function Calculate() {

            var expression = document.getElementById('<%=this.TextBox1.ClientID%>').value;
            var result = eval(expression);
            var hidden = document.getElementById('<%=this.hdfResult.ClientID%>');
            hidden.value = result;

        }
    </script>

and on button click event write this OnClientClick="Calculate(); i.e call script. For more information go through this Link

OR

Try this

 <form id="Calc" runat="server">
    <div>

    <table border= "4">
    <tr>
    <td>
    <input type="text" name="Input" size="16" \>
    <br />
    </td>
    </tr>
    <tr>
    <td>
    <input type="button" name="one" value=" 1 "  onclick ="Calc.Input.value += '1'" \>
    <input type="button" name="two" value=" 2 " onclick="Calc.Input.value += '2'" \>
    <input type="button" name="three" value=" 3 " onclick="Calc.Input.value += '3'" \>
    <input type="button" name="plus" value=" + " onclick="Calc.Input.value += ' + '" \>
    <br />
    <input type="button" name="four" value=" 4 " onclick="Calc.Input.value += '4'" \>
    <input type="button" name="five" value=" 5 " onclick="Calc.Input.value += '5'" \> 
    <input type="button" name="six" value=" 6 " onclick="Calc.Input.value += '6'" \>
    <input type="button" name="minus" value=" - " onclick="Calc.Input.value += ' - '" \>
    <br />
    <input type="button" name="seven" value=" 7 " onclick="Calc.Input.value += '7'" \>
    <input type="button" name="eight" value=" 8 " onclick="Calc.Input.value += '8'" \>
    <input type="button" name="nine" value=" 9 " onclick="Calc.Input.value += '9'" \>
    <input type="button" name="times" value=" x " onclick="Calc.Input.value += ' * '" \>
    <br />
    <input type="button" name="clear" value=" c " onclick="Calc.Input.value = ''" \>
    <input type="button" name="zero" value=" 0 " onclick="Calc.Input.value += '0'" \>
    <input type="button" name="DoIt" value=" = " onclick="Calc.Input.value = eval(Calc.Input.value)" \>
    <input type="button" name="div" value=" / " onclick="Calc.Input.value += ' / '" \>
    <br />
    </td>
    </tr>
    </table>    </div>
       </form>
Pradnya Bolli
  • 1,915
  • 1
  • 19
  • 37
-1

@Prajjwal I have also made a simple calculator on Microsoft Visual Studio Express 2013 for Web. Here is my code. Take a look! Hopes it helps:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void addButton_Click(object sender, EventArgs e)
    {
        double firstValue = double.Parse(firstTextBox.Text);
        double secondValue = double.Parse(secondTextBox.Text);
        double addValue = firstValue + secondValue;
        resultLabel.Text = addValue.ToString();
    }

    protected void subtractButton_Click(object sender, EventArgs e)
    {
        double firstValue = double.Parse(firstTextBox.Text);
        double secondValue = double.Parse(secondTextBox.Text);
        double addValue = firstValue - secondValue;
        resultLabel.Text = addValue.ToString();
    }

    protected void multiplicationButton_Click(object sender, EventArgs e)
    {
        double firstValue = double.Parse(firstTextBox.Text);
        double secondValue = double.Parse(secondTextBox.Text);
        double addValue = firstValue * secondValue;
        resultLabel.Text = addValue.ToString();
    }

    protected void divideButton_Click(object sender, EventArgs e)
    {
        double firstValue = double.Parse(firstTextBox.Text);
        double secondValue = double.Parse(secondTextBox.Text);
        double addValue = firstValue / secondValue;
        resultLabel.Text = addValue.ToString();
    }
}
Gagan
  • 93
  • 3