Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   VB6 Calculator (http://www.programmingforums.org/showthread.php?t=14321)

johann vanoustren Nov 1st, 2007 7:19 PM

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.

RobEasy Nov 3rd, 2007 12:02 AM

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.

johann vanoustren Nov 3rd, 2007 10:53 AM

Re: VB6 Calculator
 
Quote:

Originally Posted by RobEasy (Post 136224)
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?

Sane Nov 3rd, 2007 11:07 AM

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.


DaWei Nov 3rd, 2007 2:38 PM

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.

johann vanoustren Nov 6th, 2007 2:17 PM

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?

DaWei Nov 6th, 2007 2:27 PM

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.

johann vanoustren Nov 6th, 2007 7:19 PM

Re: VB6 Calculator
 
Quote:

Originally Posted by DaWei (Post 136336)
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 (Post 136230)
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.

DaWei Nov 6th, 2007 8:15 PM

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.

johann vanoustren Nov 6th, 2007 8:29 PM

Re: VB6 Calculator
 
Quote:

Originally Posted by DaWei (Post 136367)
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

?


All times are GMT -5. The time now is 3:15 AM.

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