Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Nov 1st, 2007, 6:19 PM   #1
johann vanoustren
Newbie
 
Join Date: Nov 2007
Posts: 8
Rep Power: 0 johann vanoustren is on a distinguished road
VB6 Calculator

Option Explicit

Dim intCount As Integer
Dim sngOne As Single
Dim sngTwo As Single

Private Sub cmd0_Click()
txtAnswer.Text = txtAnswer.Text & "0"
End Sub

Private Sub cmd1_Click()
txtAnswer.Text = txtAnswer.Text & "1"
End Sub

Private Sub cmd2_Click()
txtAnswer.Text = txtAnswer.Text & "2"
End Sub

Private Sub cmd3_Click()
txtAnswer.Text = txtAnswer.Text & "3"
End Sub

Private Sub cmd4_Click()
txtAnswer.Text = txtAnswer.Text & "4"
End Sub

Private Sub cmd5_Click()
txtAnswer.Text = txtAnswer.Text & "5"
End Sub

Private Sub cmd6_Click()
txtAnswer.Text = txtAnswer.Text & "6"
End Sub

Private Sub cmd7_Click()
txtAnswer.Text = txtAnswer.Text & "7"
End Sub

Private Sub cmd8_Click()
txtAnswer.Text = txtAnswer.Text & "8"
End Sub

Private Sub cmd9_Click()
txtAnswer.Text = txtAnswer.Text & "9"
End Sub

Private Sub cmdClear_Click()
txtAnswer.Text = ""
End Sub

Private Sub cmdDecimal_Click()
txtAnswer.Text = txtAnswer.Text & "."
End Sub

Private Sub cmdDLeft_Click()

sngOne = txtAnswer.Text
intCount = 0
txtAnswer.Text = ""
intCount = intCount + 5

End Sub

Private Sub cmdDRight_Click()

sngOne = txtAnswer.Text
intCount = 0
txtAnswer.Text = ""
intCount = intCount + 4

End Sub

Private Sub cmdEqual_Click()

sngTwo = txtAnswer.Text

If intCount = 1 Then
	txtAnswer.Text = sngOne + sngTwo
End If

If intCount = 2 Then
	txtAnswer.Text = sngOne - sngTwo
End If

If intCount = 3 Then
	txtAnswer.Text = sngOne * sngTwo
End If

If intCount = 4 Then
	txtAnswer.Text = sngOne \ sngTwo
End If

If intCount = 5 Then
	txtAnswer.Text = sngOne / sngTwo
End If

If intCount = 6 Then
	txtAnswer.Text = sngOne ^ sngTwo
End If

If intCount = 7 Then
	txtAnswer.Text = sngOne ^ (1 / sngTwo)
End If

If intCount = 8 Then
	txtAnswer.Text = sngOne Mod sngTwo
End If

End Sub

Private Sub cmdFix_Click()

txtAnswer.Text = Fix(Val(txtAnswer.Text))

End Sub

Private Sub cmdMinus_Click()

sngOne = txtAnswer.Text
intCount = 0
txtAnswer.Text = ""
intCount = intCount + 2

End Sub

Private Sub cmdMOD_Click()

sngOne = txtAnswer.Text
intCount = 0
txtAnswer.Text = ""
intCount = intCount + 8

End Sub

Private Sub cmdOnOff_Click()

If cmdOnOff.Caption = "Off" Then
	End
End If

If cmdOnOff.Caption = "On" Then
	frmCalculator.BackColor = &H0&
	txtName.BackColor = &H0&
	cmdOnOff.Caption = "Off"
	cmd0.Enabled = True
	cmd1.Enabled = True
	cmd2.Enabled = True
	cmd3.Enabled = True
	cmd4.Enabled = True
	cmd5.Enabled = True
	cmd6.Enabled = True
	cmd7.Enabled = True
	cmd8.Enabled = True
	cmd9.Enabled = True
	cmdDecimal.Enabled = True
	cmdClear.Enabled = True
	cmdEqual.Enabled = True
	cmdPlus.Enabled = True
	cmdMinus.Enabled = True
	cmdTimes.Enabled = True
	cmdDRight.Enabled = True
	cmdDLeft.Enabled = True
	cmdMOD.Enabled = True
	cmdFix.Enabled = True
	cmdRoot.Enabled = True
	cmdPower.Enabled = True
End If

End Sub

Private Sub cmdPlus_Click()

sngOne = txtAnswer.Text
intCount = 0
txtAnswer.Text = ""
intCount = intCount + 1

End Sub

Private Sub cmdPower_Click()

sngOne = txtAnswer.Text
intCount = 0
txtAnswer.Text = ""
intCount = intCount + 6

End Sub

Private Sub cmdRoot_Click()

sngOne = txtAnswer.Text
intCount = 0
txtAnswer.Text = ""
intCount = intCount + 7

End Sub

Private Sub cmdTimes_Click()

sngOne = txtAnswer.Text
intCount = 0
txtAnswer.Text = ""
intCount = intCount + 3

End Sub

That's my code thus far.

My problem now is basically just that I can enter numbers and calculate them, but I can't do longer equations. For example:

1 + 3 = 4

No problem doing that at all.

In our assignment we should be able to do equations like this:

2  *  4  –  3  ^  2  = 25

Instead, it would do the last two numbers along with their sign.

So it would end up being 3 ^ 2 = 9.

I don't know how to make it so that it will do the entire equation rather than the last parts of it.

We can not use things like As Double, As Boolean, etc...

So if it can be done using things like As Integer, As Single, or As String please use that method.
johann vanoustren is offline   Reply With Quote
Old Nov 2nd, 2007, 11:02 PM   #2
RobEasy
Programmer
 
RobEasy's Avatar
 
Join Date: May 2006
Location: The US duhhhhh!
Posts: 42
Rep Power: 0 RobEasy is on a distinguished road
Re: VB6 Calculator

Maybe the issue is the lack of grouping! When the program compiles it only takes the last terms, try parentheses. Never forget the order of operations: PEMDAS! Hope this helps.
__________________
Work Hard... Play Harder!
RobEasy is offline   Reply With Quote
Old Nov 3rd, 2007, 9:53 AM   #3
johann vanoustren
Newbie
 
Join Date: Nov 2007
Posts: 8
Rep Power: 0 johann vanoustren is on a distinguished road
Re: VB6 Calculator

Quote:
Originally Posted by RobEasy View Post
Maybe the issue is the lack of grouping! When the program compiles it only takes the last terms, try parentheses. Never forget the order of operations: PEMDAS! Hope this helps.
Thanks for the reply.

I'll have to wait until Monday to try, but what would I put the parentheses around, exactly?
johann vanoustren is offline   Reply With Quote
Old Nov 3rd, 2007, 10:07 AM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: VB6 Calculator

Sorry to possibly hurt your confidence, but it's not that easy. You did a good job for what you need it to do. But if you want it to be able to handle bigger equations, you'll have to do a complete rewrite.

UNLESS! You don't want it to handle BEDMAS. Then you can hack it a bit to suit your needs.

My suggestion is every time an operation button is clicked, such as "cmdPlus_Click", "cmdPower_Click", evaluate the response, as if the user pressed "Equal". But don't print it to the screen. Instead, remember it as a variable, and use it the next time an operation is clicked.

Only when the equal button is finally pressed, then calculate the last operation, and print out your background variable to the screen.

For example, if we were to call said variable 'currentValue'...
You shouldn't have to change too much to get it to do this:

5 + 10 * 2 =
  |    |   |
  ^    |   |
  First time the operation button has been pressed.
  currentValue = 5
       |   |
       ^   |
       Second time an operation has been pressed. 
       currentValue = currentValue + 10
       currentValue is now 15
           |
           ^
           The equal button was pressed.
           currentValue = currentValue * 2
           currentValue is now 30. Print that to the screen.
Sane is offline   Reply With Quote
Old Nov 3rd, 2007, 1:38 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: VB6 Calculator

If you aren't familiar with infix (or prefix or postfix) notation and precedence of operations, you should remedy that. Then you will have and idea about how to write your calculator, and people will know how to use it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 6th, 2007, 1:17 PM   #6
johann vanoustren
Newbie
 
Join Date: Nov 2007
Posts: 8
Rep Power: 0 johann vanoustren is on a distinguished road
Re: VB6 Calculator

Ok, I kinda understand but not how to apply it to the code.
Could you give me an example of how it would be in the code for the plus button please?
johann vanoustren is offline   Reply With Quote
Old Nov 6th, 2007, 1:27 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: VB6 Calculator

It involves more than the plus button. It involves what surrounds the '+' sign, as well. With typical rules of precedence, 3 * 4 + 2 is not the same thing as 3 * (4 + 2).

You can see that "what you do" with the plus depends on the surroundings, and not just the immediate surroundings (4 and 2).

That's why I recommended you do some research. There's little learning in copying any code I might post. You need to understand why it's written the way it is.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 6th, 2007, 6:19 PM   #8
johann vanoustren
Newbie
 
Join Date: Nov 2007
Posts: 8
Rep Power: 0 johann vanoustren is on a distinguished road
Re: VB6 Calculator

Quote:
Originally Posted by DaWei View Post
It involves more than the plus button. It involves what surrounds the '+' sign, as well. With typical rules of precedence, 3 * 4 + 2 is not the same thing as 3 * (4 + 2).

You can see that "what you do" with the plus depends on the surroundings, and not just the immediate surroundings (4 and 2).

That's why I recommended you do some research. There's little learning in copying any code I might post. You need to understand why it's written the way it is.
I read these three articles:

http://en.wikipedia.org/wiki/Reverse_Polish_notation
http://en.wikipedia.org/wiki/Prefix_notation
http://en.wikipedia.org/wiki/Infix_notation

Those all make some sense to me, but not how I'd apply it to the calculator here.
I don't know if that's because I'm not getting it, we didn't learn how to apply it, or what ...

In the first article some things looked familiar, but other parts didn't.
We didn't learn the doubles or stacks, we learned very little about strings (no idea how/if they reply), none of this infix, postfix, etc. has been mentioned. I saw something like "Cstr" but we didn't learn anything that had C on it, though I saw while we were leaving someone he had helped had something like that in a section of their code.

Quote:
Originally Posted by Sane View Post
UNLESS! You don't want it to handle BEDMAS. Then you can hack it a bit to suit your needs.

My suggestion is every time an operation button is clicked, such as "cmdPlus_Click", "cmdPower_Click", evaluate the response, as if the user pressed "Equal". But don't print it to the screen. Instead, remember it as a variable, and use it the next time an operation is clicked.

Only when the equal button is finally pressed, then calculate the last operation, and print out your background variable to the screen.

For example, if we were to call said variable 'currentValue'...
You shouldn't have to change too much to get it to do this:

5 + 10 * 2 =
  |    |   |
  ^    |   |
  First time the operation button has been pressed.
  currentValue = 5
       |   |
       ^   |
       Second time an operation has been pressed. 
       currentValue = currentValue + 10
       currentValue is now 15
           |
           ^
           The equal button was pressed.
           currentValue = currentValue * 2
           currentValue is now 30. Print that to the screen.
That made the most sense to me but I tried doing something like ...

Dim sngCurrent As Single

sngCurrent  = txtanswer.Text

txtAnswer.Text = sngCurrent + txtAnswer.Text

Or something like that for the plus .. I don't remember exactly.
But that didn't work unless I needed it as a string or something instead of single?

The reason I think you could ignore PEMDAS (BEMDAS ... same thing?) is because in an example he said it should do this:

2 * 4 – 3 ^ 2 = 25

And that doesn't follow order of operations.

Sorry if I'm not getting this and should, but I'm pretty confused. Willing and want to learn, but maybe it's not sinking in.
Thanks for the help and patience so far though.
johann vanoustren is offline   Reply With Quote
Old Nov 6th, 2007, 7:15 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: VB6 Calculator

If you're allowed strictly left to right evaluation, then go for it. Collect the first operand. Collect and save the operator. Collect the second operand. Perform the operation. The result is your new "first" operand. Collect the next operator. Collect the next operand. Perform the operation. Wash, rinse, and repeat until done.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 6th, 2007, 7:29 PM   #10
johann vanoustren
Newbie
 
Join Date: Nov 2007
Posts: 8
Rep Power: 0 johann vanoustren is on a distinguished road
Re: VB6 Calculator

Quote:
Originally Posted by DaWei View Post
If you're allowed strictly left to right evaluation, then go for it. Collect the first operand. Collect and save the operator. Collect the second operand. Perform the operation. The result is your new "first" operand. Collect the next operator. Collect the next operand. Perform the operation. Wash, rinse, and repeat until done.
So basically the code I have now is fine and I just need to do what Sane mentioned?
And all I'd be missing is to have it actually perform the calculation, just not update the answer box until the equal button is pressed? And that would mean a new variable to hold the current new number?

Trying to work it in my head knowing exactly what I need before I head in to school tomorrow, and trying to get it all right in what I need.

Or instead of having this:

txtAnswer.Text = sngCurrent

I need something at the end of each section before end sub like:

sngOne = sngCurrent

?
johann vanoustren 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can this be done in VB6? diablo75 Visual Basic 1 Oct 20th, 2007 11:49 PM
Python area calculator Volkan Python 20 Feb 4th, 2006 9:11 AM
Calculator Multiple Operations? jl13 C# 5 Oct 12th, 2005 4:32 PM
Javascript calculator narroweyes JavaScript and Client-Side Browser Scripting 7 Jul 30th, 2005 12:46 PM
simple calculator Jessehk Show Off Your Open Source Projects 4 May 25th, 2005 4:39 PM




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

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