Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi... I'm trying to generate calculator program in c#. Here the operations "=" is working only for two numbers. If three numbers were given continuously, its not working... plzzz help me.

public partial class _Default : System.Web.UI.Page
{
    static bool plus;
    static bool minus;
    static bool multiply;
    static bool division;
   // static bool equal;

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btn0_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn0.Text;
    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn1.Text;
    }
    protected void btn2_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn2.Text;
    }
    protected void btn3_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn3.Text;
    }
    protected void btn4_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn4.Text;
    }
    protected void btn5_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn5.Text;
    }
    protected void btn6_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn6.Text;
    }
    protected void btn7_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn7.Text;
    }
    protected void btn8_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn8.Text;
    }
    protected void btn9_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn9.Text;
    }
    protected void btndot_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btndot.Text;
    }
    protected void btnclr_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = string.Empty;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
     //bool plus=false  ;
    if (txtdisplay.Text == "")
    {
        return;
    }
    else
    {
        plus = true;
        minus = false;
        multiply = false;
        division = false;
        Session["firstNo"] = txtdisplay.Text;
        txtdisplay.Text = "";
    }

    }
   // bool minus = false ;
    protected void btnMinus_Click(object sender, EventArgs e)
    {
        if (txtdisplay.Text == "")
        {
            return;
        }
        else
        {
           minus = true;
           plus = false;
           multiply = false;
           division = false;
            Session["firstNo"] = txtdisplay.Text;
            txtdisplay.Text = "";
        }
    }
   // bool multiply = false ;
    protected void btnMul_Click(object sender, EventArgs e)
    {
        if (txtdisplay.Text == "")
        {
            return;
        }
        else
        {
           multiply  = true;
           plus = false;
           minus = false;
           division = false;
            Session["firstNo"] = txtdisplay.Text;
            txtdisplay.Text = "";
        }
    }
    //bool division = false;
    protected void btnDiv_Click(object sender, EventArgs e)
    {
        if (txtdisplay.Text == "")
        {
            return;
        }
        else
        {
           division = true;
           plus = false;
           minus = false;
                multiply =false ;
            Session["firstNo"] = txtdisplay.Text;
            txtdisplay.Text = "";
        }
    }
    protected void btnEqual_Click(object sender, EventArgs e)
    {
      /*  bool plus=true  ;
        bool minus=true  ;
        bool multiply=true  ;
        bool division=true ;*/
    //   bool equal = true;
        if (plus)
        {
            decimal  sum = Convert.ToDecimal (txtdisplay.Text) + Convert.ToDecimal (Session["firstNo"]);
            txtdisplay.Text = sum.ToString();
        }
        else if (minus)
        {
            decimal  diff = Convert.ToDecimal (Session["firstNo"]) - Convert.ToDecimal (txtdisplay.Text);
            txtdisplay.Text = diff.ToString();
        }
        else if (multiply)
        {
            decimal  mul = (Convert.ToDecimal (txtdisplay.Text)) * (Convert.ToDecimal (Session["firstNo"]));
            txtdisplay.Text = mul.ToString();
        }
        else if (division)
        {
          decimal  div = Convert.ToDecimal (Session["firstNo"]) / Convert.ToDecimal(txtdisplay.Text);
            txtdisplay.Text = div.ToString();
        }
        return;
    }

}


[edit]Subject: Question moved into body, subject shortened to a brief description.
Code Block added to preserve formatting
- OriginalGriff[/edit]
Posted
Updated 22-Feb-11 20:25pm
v2

Please forgive me for some off-topic: I never understood who needs all those calculators with buttons which can do almost nothing.

Want to take a look at mine? http://www.sakryukov.org/freeware/calculator/[^].

No server-side, "View Page Source" and get full source code!

No, I won't understand all your exercises…

—SA
 
Share this answer
 
v2
Comments
Member 7697543 23-Feb-11 4:25am    
ok.. Thank u...
Sergey Alexandrovich Kryukov 23-Feb-11 4:33am    
You're very welcome. Sorry, I'm not answering your question. Just wanted to share the idea.
Do you know another way to do calculators, now in C#? You can compile C# code from you C# program. However, it needs AppDomain, simply because when you compile again and again the result cannot be unloaded, the memory leaks. You can only unload AppDomain or whole process... The idea, I used it.
A matter of whole article, in fact...
--SA
Member 7697543 23-Feb-11 5:14am    
Actually im a begginer of c#..
so oly trying to do this...
but i didnt get the result correctly:-(:-(
Sergey Alexandrovich Kryukov 23-Feb-11 5:38am    
Is a simpler project an option. You're not approaching the whole idea of coding correctly. Main principle is "Don't Repeat Yourself" ("DRY"), but you repeat similar 12 times. Never ever do it, from the very beginning. Re-factor it. Make a collection of data, cycles, function, etc. Did you learn it? What are you doing right now? Better don't continue and don't search for you bug, don't waste time. Write properly first.
--SA
Member 7697543 23-Feb-11 5:54am    
now created like this...
continuosly adding the numbers
just like sub,mul and division...
but i dono how to calculate like 1+2*3=9?:-(
There are a few things you could do to improve this, and one you should do to fix your problem:

Firstly: replace all your separate button Click handlers with a common handler:

C#
protected void NumberButton_Click(object sender, EventArgs e)
    {
    Button b = sender as Button;
    if (b != null)
        {
        txtdisplay.Text = txtdisplay.Text + b.Text;
        }
    }
That makes your code a lot shorter, easier to read, and reliable!
Then, set up a routine called SetOperation:
private void SetOperation(ref bool op)
   {
    if (txtdisplay.Text != "")
       {
       multiply  = false;
       plus = false;
       minus = false;
       division = false;
       op = true;
       Session["firstNo"] = txtdisplay.Text;
       txtdisplay.Text = "";
       }
   }
And call that in each operation button press:
protected void btnAdd_Click(object sender, EventArgs e)
   {
   SetOperation(ref plus);
   }
   protected void btnMinus_Click(object sender, EventArgs e)
   {
   SetOperation(ref minus);
   }
And so on.
Secondly: When you press a operation key, you first need to check if there is a pending operation! I.e. If plus is true, or minus is true, or ...
Otherwise, you just throw away your previous work...
Hint: Could you check and call most of btnEqual_Click?
 
Share this answer
 
Comments
Member 7697543 23-Feb-11 4:20am    
ok. thanks for ur help:-)

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