Click here to Skip to main content
15,891,694 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class calc : System.Web.UI.Page
{
    static float a, c, d;
    static char b;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_1_Click(object sender, EventArgs e)
    {
        if ((tb.Text == "+") || (tb.Text == "-") || (tb.Text == "*") || (tb.Text == "/"))

        {

            tb.Text = "";

            tb.Text = tb.Text + btn_1.Text;

        }

        else
        {

            tb.Text = tb.Text + btn_1.Text;
        }
    }

    protected void btn_2_Click(object sender, EventArgs e)
    {
        if ((tb.Text == "+") || (tb.Text == "-") || (tb.Text == "*") || (tb.Text == "/"))

        {
            tb.Text = "";

            tb.Text = tb.Text + btn_2.Text;
        }

        else
        {

            tb.Text = tb.Text + btn_2.Text;
        }
    }


    protected void btn_3_Click(object sender, EventArgs e)
    {
    if ((tb.Text == "+") || (tb.Text == "-") || (tb.Text == "*") || (tb.Text == "/"))

    {

        tb.Text = "";

        tb.Text = tb.Text + btn_3.Text;

    }

    else
    {

        tb.Text = tb.Text + btn_3.Text;
    }
}

    

 
    protected void btn_4_Click(object sender, EventArgs e)
    {
        if ((tb.Text == "+") || (tb.Text == "-") || (tb.Text == "*") || (tb.Text == "/"))

        {

            tb.Text = "";

            tb.Text = tb.Text + btn_4.Text;

        }

        else
        {

            tb.Text = tb.Text + btn_4.Text;
        }

    }

    protected void btn_5_Click(object sender, EventArgs e)
    {
        if ((tb.Text == "+") || (tb.Text == "-") || (tb.Text == "*") || (tb.Text == "/"))

        {

            tb.Text = "";

            tb.Text = tb.Text + btn_5.Text;

        }

        else
        {

            tb.Text = tb.Text + btn_5.Text;
        }
    }

    protected void btn_6_Click(object sender, EventArgs e)
    {
        if ((tb.Text == "+") || (tb.Text == "-") || (tb.Text == "*") || (tb.Text == "/"))

        {

            tb.Text = "";

            tb.Text = tb.Text + btn_6.Text;

        }

        else
        {

            tb.Text = tb.Text + btn_6.Text;
        }
    }

   

    protected void btn_7_Click(object sender, EventArgs e)
    {
        if ((tb.Text == "+") || (tb.Text == "-") || (tb.Text == "*") || (tb.Text == "/"))

        {

            tb.Text = "";

            tb.Text = tb.Text + btn_7.Text;

        }

        else
        {

            tb.Text = tb.Text + btn_7.Text;
        }
    }

    protected void btn_8_Click(object sender, EventArgs e)
    {
        if ((tb.Text == "+") || (tb.Text == "-") || (tb.Text == "*") || (tb.Text == "/"))

        {

            tb.Text = "";

            tb.Text = tb.Text + btn_8.Text;

        }

        else
        {

            tb.Text = tb.Text + btn_8.Text;
        }
    }

    protected void btn_9_Click(object sender, EventArgs e)
    {
        if ((tb.Text == "+") || (tb.Text == "-") || (tb.Text == "*") || (tb.Text == "/"))

        {

            tb.Text = "";

            tb.Text = tb.Text + btn_9.Text;

        }

        else
        {

            tb.Text = tb.Text + btn_9.Text;
        }
    }

   

    protected void btn_0_Click(object sender, EventArgs e)
    {
        if ((tb.Text == "+") || (tb.Text == "-") || (tb.Text == "*") || (tb.Text == "/"))

        {

            tb.Text = "";

            tb.Text = tb.Text + btn_0.Text;

        }

        else
        {

            tb.Text = tb.Text + btn_0.Text;
        }
    }

  

  
    protected void btn_add_Click(object sender, EventArgs e)
    {
        a = Convert.ToInt32(tb.Text);

        tb.Text = "";

        b = '+';

        tb.Text += b;

    }
    protected void btn_sub_Click(object sender, EventArgs e)
    {
        a = Convert.ToInt32(tb.Text);

        tb.Text = "";

        b = '-';

        tb.Text += b;
    }

    protected void btn_product_Click(object sender, EventArgs e)
    {
        a = Convert.ToInt32(tb.Text);

        tb.Text = "";

        b = '*';

        tb.Text += b;
    }

    protected void btn_div_Click(object sender, EventArgs e)

    {
        
        a = Convert.ToInt32(tb.Text);

        tb.Text = "";

        b = '/';

        tb.Text += b;
    }
    protected void btn_eql_Click(object sender, EventArgs e)
    {
        c = Convert.ToInt32(tb.Text);

        tb.Text = "";

        if (b == '/')

        {

            d = a / c;

            tb.Text += d;

            a = d;

        }

        else if (b == '+')

        {

            d = a + c;

            tb.Text += d;

            a = d;

        }

        else if (b == '-')

        {

            d = a - c;

            tb.Text += d;

            a = d;

        }

        else

        {

            d = a * c;

            tb.Text += d;

            a = d;

        }
    }

     protected void btn_clr_Click(object sender, EventArgs e)
    {
        tb.Text = "";
    }
}


What I have tried:

i tried to calucater it taking only from button not in keyboard
Posted
Updated 12-Mar-18 23:29pm
v2
Comments
HobbyProggy 13-Mar-18 4:46am    
As far as i can see you are lacking an "KeyDown" event which gets triggered when you press any key on your keyboard.
Richard MacCutchan 13-Mar-18 5:25am    
You could improve things greatly by getting rid of all that duplicate code. You only need a single handler for the keydown event, and a common subroutine for the numbers.

1 solution

That code is not set up for keyboard input - it works by "validating" inputs very simply:
if the textbox currently is an operator, then replace it with the digit. Otherwise, add the digit to it.

That's not necessarily a good "starter" for a keyboard input calculator: I'd want to type
12+14=
and have the expression parsed afterwards, which isn't possible with the code you have at the moment. I'd want to delete stuff as well when I made in input mistake. I'd also expect this to work:
2+3*4=
And it should give me 14, not 20. Your whole system at the moment doesn't allow for that. (And it's pretty badly written: you only need one handler for all ten number buttons, and the code can be a lot simpler as well.)

It depends on exactly what your homework assignment is asking for, but I'd suggest that you let the user enter what he wants, and then parse it into an expression before evaluating it: the simplest method to do that is to "tokenize" the input string (convert it into a set of Operator and Operand values) and then evaluate the operators via a stacking system.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900