|
-
Feb 23rd, 2009, 06:51 PM
#1
Text / String Calculator
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.
StringCalc.zip
edit
2 Mar 2009 - latest upload
Last edited by dbasnett; Mar 2nd, 2009 at 03:30 PM.
-
Feb 24th, 2009, 03:55 PM
#2
Re: Text / String Calculator
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))
-
Feb 24th, 2009, 04:00 PM
#3
Re: Text / String Calculator
Code:
((((((5)*7)-10+2+3-4-pi) %2)/3.437)-((2*2)*3)) = -11.750245171251
-
Feb 24th, 2009, 04:19 PM
#4
Re: Text / String Calculator
Nice... now work out if that's the correct answer 
Code:
((((4*5/2-3*2.374)/2.3) * 175437.28 / 23) * ((2-283798)+(-2) %2) *-234) / pi
I'm not a math major... is this a valid test?
Code:
(-(((-((-((-((-(((-((((-((((-(((-(8)+4))))))))))))))))))))))))
-
Feb 24th, 2009, 05:16 PM
#5
Re: Text / String Calculator
Code:
((((4*5/2-3*2.374)/2.3)*175437.28/23)*((2-283798)+(neg(2))%2)*neg(234))/pi
= 201757300109.703
-
Feb 25th, 2009, 10:17 AM
#6
Re: Text / String Calculator
-
Feb 26th, 2009, 02:37 PM
#7
Re: Text / String Calculator
So, are going to tell us how (The mechanism) you are doing it, or is it a trade secret?
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Feb 26th, 2009, 04:26 PM
#8
Re: Text / String Calculator
Nice though I would have went with something recursive but that's just me 
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.
-
Feb 26th, 2009, 04:28 PM
#9
Re: Text / String Calculator
what would be the result of 100 / 0 + 0 ?
-
Feb 26th, 2009, 04:31 PM
#10
Re: Text / String Calculator
I would imagine you'd get a NaN, smartass
-
Feb 26th, 2009, 05:30 PM
#11
Re: Text / String Calculator
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.
-
Feb 26th, 2009, 05:38 PM
#12
Re: Text / String Calculator
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)
-
Feb 26th, 2009, 05:41 PM
#13
Re: Text / String Calculator
 Originally Posted by Kasracer
Nice though I would have went with something recursive but that's just me
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 i posted converts the infix notation to postfix (RPN) and is loosely a Shunting yard algorithm.
-
Feb 26th, 2009, 05:41 PM
#14
Re: Text / String Calculator
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.
-
Feb 26th, 2009, 06:15 PM
#15
Re: Text / String Calculator
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.
-
Feb 27th, 2009, 09:28 AM
#16
Re: Text / String Calculator
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.
-
Feb 27th, 2009, 10:23 AM
#17
Re: Text / String Calculator
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
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Feb 27th, 2009, 10:35 AM
#18
Re: Text / String Calculator
omg - scrutiny. i'll post the project later, off to rehab now.
-
Mar 2nd, 2009, 09:21 AM
#19
Re: Text / String Calculator
as changes occur i will update post #1.
-
Mar 5th, 2009, 06:26 PM
#20
Re: Text / String Calculator
@stephen - you didn't say anything about the code. is that good or bad?
-
Mar 7th, 2009, 10:20 AM
#21
Re: Text / String Calculator
 Originally Posted by dbasnett
@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.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|