Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 11th, 2005, 8:07 PM   #1
jl13
Newbie
 
Join Date: Oct 2005
Posts: 2
Rep Power: 0 jl13 is on a distinguished road
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();
			}
		}
jl13 is offline   Reply With Quote
Old Oct 11th, 2005, 9:37 PM   #2
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Oct 11th, 2005, 9:56 PM   #3
Silvanus
Hobbyist Programmer
 
Silvanus's Avatar
 
Join Date: Aug 2005
Location: Hiding from... them...
Posts: 110
Rep Power: 4 Silvanus is on a distinguished road
On an only semi-related note, you'll probably want to use a switch statement instead of your multiple ifs.
Silvanus is offline   Reply With Quote
Old Oct 11th, 2005, 10:08 PM   #4
jl13
Newbie
 
Join Date: Oct 2005
Posts: 2
Rep Power: 0 jl13 is on a distinguished road
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.
jl13 is offline   Reply With Quote
Old Oct 11th, 2005, 11:02 PM   #5
pr0gm3r
Hobbyist Programmer
 
Join Date: Dec 2004
Location: CA
Posts: 102
Rep Power: 4 pr0gm3r is on a distinguished road
Send a message via MSN to pr0gm3r
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
pr0gm3r is offline   Reply With Quote
Old Oct 12th, 2005, 5:32 PM   #6
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
If you want to handle precedence properly, then things will get more complicated.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:06 AM.

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