Formula in a text box

  • Thread starter Thread starter Brian VanPelt
  • Start date Start date
B

Brian VanPelt

I am not a very experienced VB user, but I was trying to make a form
that a user could input a formula for summation.

For example, I would like the user to input the beginning and end
values of a counter, and also have the user input the formula to be
added up.

More explicitly, suppose a user wants to add up

1 + 1/2 + 1/3 + 1/4 + ... + 1/100

Thus, the user would enter 1 as the start value and 100 for the end
value and this user would also enter the formula 1/n, in a text box,
as the summation formula (note that n would be a variable).

Suppose a user wants to find the sum of

1/2^2 + 1/3^2 + 1/4^2 + ... +1/100^2.

Then, the user would input 2 as the start value, 1000 as the end value
and also input the formula 1/n^2.

VB doesn't seem to like having the user enter the formula. I have
tried something like this

Dim n as integer
Dim s as double

s = 0

For n = start.text to final.text

s = s + input.text

Next

Print s

I have also tried to convert input.text to double (inside of the For
loop), and VB throws up an unhandled exception.

Since I am not very well versed in VB, I am not certain that I have
explained the situation in the best way. So, any help or pointers
would be very much appreciated.

Thanks,

Brian
 
txtInput.text format 1,100,2 ( I think this may be roughly what you
wanted )

Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGo.Click
Dim parts() As String
Dim total As Double
Dim power As Integer
parts = txtInput.Text.Split(",")
power = Convert.ToInt32(parts(2))

For i As Integer = Convert.ToInt32(parts(0)) To
Convert.ToInt32(parts(2))
total += (1 / i) ^ power
Next
txtOutput.Text = total.ToString
End Sub

HTH
 
Hi Brian,

Your project isn't as easy as it sounds. If the formula to be entered
were one of a fixed (and hopefully small) set, then it would be ok, but
if it is completely dependent on the user... then it can get quite
complicated coding such a routine.

The problem would occur at this point : (Considering the 1/1 + 1/2 +
1/3.... example)

For i = startVal To endVal
Dim thisFormula As String = formula.Replace("n", i.ToString())
' The following line will always throw an exception...
Dim thisValue As Single = CSng(thisForm)
Next

The problem is how to parse a uncalculated expression such as "1/1"
from a string into a value.
The only way to do this seems to be to parse out individual values and
operators separately using Regular Expressions, and then to manually
perform the calculation. I believe VB 6.0 had the Eval function which
could do something like this easily, but haven't found anything like it
in .NET.

Below are some links that do this... You will realize how much code it
involves ! You might consider using pre-built functionality such as
this...

1. <http://www.codeproject.com/vb/net/math_expression_evaluator.asp>

2. A similar question was asked in these forums :
<http://www.vbcity.com/forums/topic.asp?tid=61590>

Hope this helps,

Regards,

Cerebrus.
 
Hi Brian,

Your project isn't as easy as it sounds. If the formula to be entered
were one of a fixed (and hopefully small) set, then it would be ok, but
if it is completely dependent on the user... then it can get quite
complicated coding such a routine.

The problem would occur at this point : (Considering the 1/1 + 1/2 +
1/3.... example)

For i = startVal To endVal
Dim thisFormula As String = formula.Replace("n", i.ToString())
' The following line will always throw an exception...
Dim thisValue As Single = CSng(thisForm)
Next

The problem is how to parse a uncalculated expression such as "1/1"
from a string into a value.
The only way to do this seems to be to parse out individual values and
operators separately using Regular Expressions, and then to manually
perform the calculation. I believe VB 6.0 had the Eval function which
could do something like this easily, but haven't found anything like it
in .NET.

Below are some links that do this... You will realize how much code it
involves ! You might consider using pre-built functionality such as
this...

1. <http://www.codeproject.com/vb/net/math_expression_evaluator.asp>

2. A similar question was asked in these forums :
<http://www.vbcity.com/forums/topic.asp?tid=61590>

Hope this helps,

Regards,

Cerebrus.


Thanks for the response. I am beginning to find out just how
difficult this is. Imagine a user entering sqrt(n) into the formula
box, or log(n). I originally asked someone in a VB6 group how they'd
do it, and they repsonded that I'd need to add a script control
component. But, of course I couldn't find that component in VB.net.
It appears that with such a scripting idea, the user would be able to
send variables to the program. So, I was investigating the Windows
Script Host, but I am afraid that I just can't do anything with it
just yet (read, I don't know how to use it).

I don't know if this is any help, but this arises out of a desire to
create a small program for my calc students to use to estimate the sum
of an infinite series (so may may go as high as into the millions).

In any event, I am going to investigate those links and thank you for
the information. I also want to thank OHM for his reply as well.

Brian
 
Hi again !

Cor, you're simply the coolest person !!! I've just tried it using the
MS Script control and it works !

How come those poor guys involved in the discussions (see links I
posted) didn't know about this. But then I'd never heard about this
control before too.

Many thanks...

Brian, this will do the job for you with no extra effort.

Regards,

Cerebrus.
 
Brian VanPelt said:
I am not a very experienced VB user, but I was trying to make a form
that a user could input a formula for summation.

Hand-made:

MathLib
<URL:http://www.palmbytes.de/content/dotnet/mathlib.htm>

Dynamic compilation:

Build a Custom .NET "EVAL" Provider
<URL:http://www.eggheadcafe.com/articles/20030908.asp>

Runtime Compilation (A .NET eval statement)
<URL:http://www.codeproject.com/dotnet/evaluator.asp>

Evaluation of numeric expressions using J#:

<URL:http://groups.google.de/group/microsoft.public.dotnet.languages.csharp/msg/9e95759b54a323a7>
 
you should throow vb.net out the window and build an ACCESS
application.

it can do this shit easily; there are extensive query builders /
helpers

keep your data in a database instead of making people enter it over and
over again
 
keep your data in a database instead of making people enter it over and
over again

Huh?! I am wondering where the relation to the OP's post is...
 
sorry wrong post.

ACCESS has some really nifty built-in tools for expression builders
 
Access is a custom build tool for a target market of ordinary users like
yourself. .NET is for real coders that make use of it's almost infinate
customability as a strength.


( OHM ) - One Handed Man
AKA Terry Burns - http://TrainingOn.net
 
Back
Top