Here is a calculator that takes input as a string in the form of
"(5/9) *(212-32)"
and returns the answer. see code for a complete list of operators.
Attachment 69419
edit
2 Mar 2009 - latest upload
Printable View
Here is a calculator that takes input as a string in the form of
"(5/9) *(212-32)"
and returns the answer. see code for a complete list of operators.
Attachment 69419
edit
2 Mar 2009 - latest upload
Test cases? Do you want specific formulas or just anything that would exercise your calculation methods?
If you have access to utilize test cases in Visual Studio I'd use that and supply as many algorithms as possible to it (maybe even create some sort of random algorithm generator!). Otherwise I'd work on overdoing it with the operators to test that it works as expected. Example:
Code:((((((5)*7)-10+2+3-4-pi) %2)/3.437)-((2*2)*3))
Code:((((((5)*7)-10+2+3-4-pi) %2)/3.437)-((2*2)*3)) = -11.750245171251
Nice... now work out if that's the correct answer :p
I'm not a math major... is this a valid test?Code:((((4*5/2-3*2.374)/2.3) * 175437.28 / 23) * ((2-283798)+(-2) %2) *-234) / pi
Code:(-(((-((-((-((-(((-((((-((((-(((-(8)+4))))))))))))))))))))))))
Code:((((4*5/2-3*2.374)/2.3)*175437.28/23)*((2-283798)+(neg(2))%2)*neg(234))/pi
= 201757300109.703
i fixed negative numbers
So, are going to tell us how (The mechanism) you are doing it, or is it a trade secret? :)
Nice though I would have went with something recursive but that's just me :D
Basically one operation per function call. So 5(2+5) would go into the method, it would see that 2+5 takes precedence and passes 2+5 into itself. When the value returns, then you'd evaluate 5*7. So 5(12(4*5)) would end of evaluating 4*5, 12 * 20, 5 * 240. This is the way most evaluation software is developed; at least that's what I thought.
what would be the result of 100 / 0 + 0 ?
I would imagine you'd get a NaN, smartass :p
this
5(2+5) won't work but this
5*(2+5)
evaluates to
5 2 5 + *
i'll fix the divided by zero stuff.
actually
100/0 returns infinity
i had to fix \
Code:Case "\"
If Not get2Args() Then Return False
If num2 <> 0 Then ans = CInt(CInt(num1) \ CInt(num2)) Else ans = Double.PositiveInfinity
TokenQ.Add(ans.ToString)
what i posted converts the infix notation to postfix (RPN) and is loosely a Shunting yard algorithm.Quote:
Originally Posted by Kasracer
Well, technically 5(2+5) = 5*(2+5)
In the math world at least. Not sure if that matters to your application or not though.
this
((((((5)*7)-10+2+3-4-pi)%2)/3.437)-((2*2)*3))
answer = -11.750245171251
evaluates to
5
7
*
10
-
2
+
3
+
4
-
3.14159265358979
-
2
%
3.437
/
2
2
*
3
*
-
i have found a couple of errors. if anyone else finds errors copy and paste the formula giving errors.
if anyone is actually looking at this i will post the latest code.
i fixed divide by 0 for integer divide. also added implicit multiplication, meaning that 5(2+5) now works.
I'll be looking at this at the weekend - I have my own RPN calculator, and am interested in comparing how we have done it. I don't have implicit multiplication, I know, based on the fact that I have the ability to call parameterized functions. The effort to differentiate between a function and implicit mult. is not really high on my priority list :)
omg - scrutiny. i'll post the project later, off to rehab now.
as changes occur i will update post #1.
@stephen - you didn't say anything about the code. is that good or bad?
Haven't had a chance to look at it just yet, unfortunately.Quote:
Originally Posted by dbasnett