Calculations

  • Thread starter Thread starter Pitaridis Aristotelis
  • Start date Start date
P

Pitaridis Aristotelis

Is there a way to make a calculation stored in a string. For example:

Dim A as string = "4 * 10 ^ 8"

Dim Result as Decimal = Calculate(A)
 
Pitaridis Aristotelis said:
Is there a way to make a calculation stored in a string. For example:

Dim A as string = "4 * 10 ^ 8"

Dim Result as Decimal = Calculate(A)
I don't think so :(
 
Is there a way to make a calculation stored in a string. For example:

Dim A as string = "4 * 10 ^ 8"

Dim Result as Decimal = Calculate(A)

Yes, but not built into the .Net framework. Basically, you need to
parse the string into it's parts (numbers and operators) and then
perform the calculations. The trickiest part will be doing the
calculations in the correct order (using a method such as reverse-
polish notation or something similar). There are a few open source
libraries available that have this functionality, so you might want to
look on Google and CodeProject.com.

Thanks,

Seth Rowe
 
You are looking for an algorithm that's called a "recursive descent parser".
Doing this type of thing is an exercise that we had to do in programming
classes 35 years ago.

I'm a little bit amazed that something like this doesn't exist in the .Net
framework. Even in VBScript I think there's the "eval" function that might
have limited parsing capability. Still ... this is a good programming
exercise to learn how to write recursive functions.

If you want to create your own class function to do this go get a
fundamental programming book and study the chapters on recursion. I'd be
real surprised if you couldn't find a good writeup on how to do this.

-bwr-
 
? "Tom Shelton" <[email protected]> ?????? ??? ??????





I was looking for vb.net code which will handle the whole thing. I used
sometime an example that I downloaded from the internet but I can not find
it.- Hide quoted text -

- Show quoted text -

Why does it have to be VB.NET. With a few lines of JScript code
(a .net language), you can accomplish what you're asking.

If you simply want the experience of doing this sort of thing, then
great. It is a good programming exercise - I MIGHT even have some old
C++ and VB5 code somewhere that does this (I wrote the C++ code for an
exercise in school, and latter adapted it to VB5).
 
Back
Top