![]() |
VB6 Calculator
:
Option ExplicitThat'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 = 4No problem doing that at all. In our assignment we should be able to do equations like this: :
2 * 4 – 3 ^ 2 = 25Instead, 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. |
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.
|
Re: VB6 Calculator
Quote:
I'll have to wait until Monday to try, but what would I put the parentheses around, exactly? |
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 = |
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.
|
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? |
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. |
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 SingleOr 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. |
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.
|
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 = sngCurrentI 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