![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2007
Posts: 8
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: May 2006
Location: The US duhhhhh!
Posts: 42
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Nov 2007
Posts: 8
Rep Power: 0
![]() |
Re: VB6 Calculator
Quote:
I'll have to wait until Monday to try, but what would I put the parentheses around, exactly? |
|
|
|
|
|
|
#4 |
|
Banned
![]() ![]() |
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. |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Nov 2007
Posts: 8
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 | ||
|
Newbie
Join Date: Nov 2007
Posts: 8
Rep Power: 0
![]() |
Re: VB6 Calculator
Quote:
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:
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. |
||
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#10 | |
|
Newbie
Join Date: Nov 2007
Posts: 8
Rep Power: 0
![]() |
Re: VB6 Calculator
Quote:
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 ? |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Can this be done in VB6? | diablo75 | Visual Basic | 1 | Oct 21st, 2007 12:49 AM |
| Python area calculator | Volkan | Python | 20 | Feb 4th, 2006 10:11 AM |
| Calculator Multiple Operations? | jl13 | C# | 5 | Oct 12th, 2005 5:32 PM |
| Javascript calculator | narroweyes | JavaScript and Client-Side Browser Scripting | 7 | Jul 30th, 2005 1:46 PM |
| simple calculator | Jessehk | Show Off Your Open Source Projects | 4 | May 25th, 2005 5:39 PM |