![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 2
Rep Power: 0
![]() |
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();
}
} |
|
|
|
|
|
#2 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
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.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Aug 2005
Location: Hiding from... them...
Posts: 110
Rep Power: 3
![]() |
On an only semi-related note, you'll probably want to use a switch statement instead of your multiple ifs.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Oct 2005
Posts: 2
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
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,
__________________
-- pr0gm3r |
|
|
|
|
|
#6 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
If you want to handle precedence properly, then things will get more complicated.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|