Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Calculator Multiple Operations? (http://www.programmingforums.org/showthread.php?t=6380)

jl13 Oct 11th, 2005 7:07 PM

Calculator Multiple Operations?
 
I'm trying to program a basic calculator and have gotten all the operations working. Yet my problem is getting the calculator to perform multiple operations without having to hit the equal button in between each seperate operation. I'm listing the operations, and equal button code along with the global variables I used. If any one could help it would be greatly appreciated. Thanks.

:

                string operation ="";
                bool newNum = false;
                double numMemory;
                bool decimalPushed =false;

                void ButtonAddClick(object sender, System.EventArgs e)
                {
                        operation ="+";
                        newNum = true;
                        numMemory = Convert.ToDouble(Display.Text);
                }
               
                void ButtonMultiplyClick(object sender, System.EventArgs e)
                {
                        operation ="x";
                        newNum = true;
                        numMemory = Convert.ToDouble(Display.Text);
                }
               
                void ButtonDivideClick(object sender, System.EventArgs e)
                {
                        operation ="/";
                        newNum = true;
                        numMemory = Convert.ToDouble(Display.Text);
                }
               
                void ButtonMinusClick(object sender, System.EventArgs e)
                {
                        operation ="-";
                        newNum = true;
                        numMemory = Convert.ToDouble(Display.Text);
                }
                void ButtonEqClick(object sender, System.EventArgs e)
                {
                        if (operation == "+")
                        {
                                if (newNum)
                                {
                                        numMemory = Convert.ToDouble(Display.Text);
                                        newNum = false;
                                }
                                double displayValue = Convert.ToDouble(Display.Text);
                                displayValue = numMemory + displayValue;
                                Display.Text = displayValue.ToString();
                        }
                        if (operation == "-")
                        {
                                        if (newNum)
                        {
                                numMemory = Convert.ToDouble(Display.Text);
                                newNum = false;
                        }
                                double displayValue = Convert.ToDouble(Display.Text);
                                displayValue = numMemory - displayValue;
                                Display.Text = displayValue.ToString();
                        }
                        if (operation == "x")
                        {
                                if (newNum)
                                {
                                        numMemory = Convert.ToDouble(Display.Text);
                                        newNum = false;
                                }
                                double displayValue = Convert.ToDouble(Display.Text);
                                displayValue = numMemory * displayValue;
                                Display.Text = displayValue.ToString();
                        }
                        if (operation == "/")
                        {
                                if (newNum)
                                {
                                        numMemory = Convert.ToDouble(Display.Text);
                                        newNum = false;
                                }
                                double displayValue = Convert.ToDouble(Display.Text);
                                displayValue = numMemory / displayValue;
                                Display.Text = displayValue.ToString();
                        }
                }


Dameon Oct 11th, 2005 8:37 PM

If an operation button is pressed without entering a value, calculate the answer to the previous operation (don't display it) and treat it as if the user inputted it manually.

Silvanus Oct 11th, 2005 8:56 PM

On an only semi-related note, you'll probably want to use a switch statement instead of your multiple ifs.

jl13 Oct 11th, 2005 9:08 PM

Thank you. I think I understand what you mean by calculate the answer to the previous operation without entering a value. Yet my problem now is with operations such as 1+2+4-5/2*3 operations like this will only perform the last operation I enter.

pr0gm3r Oct 11th, 2005 10:02 PM

I think you need to create an instance object that will stores the total value (answer). And the total value should always be display after the equal button is press.

every the calculator performs calculations add the answer from the previous answer, but do not display it. [Just like Dameon said].

display the final answer on TextBox or Lable instance object that you created;
The best idea to implement the final output in the "equal button event handler." but
add the answer everytime it perform a calculations (added in the "x,+,-" event handeler that you created).

I hope help.

Good luck...

if there any mistake on my post, please advise or correct me.

Thanks,

Dameon Oct 12th, 2005 4:32 PM

If you want to handle precedence properly, then things will get more complicated.


All times are GMT -5. The time now is 12:33 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC