文本框中的公式 [英] Formula in a text box

查看:94
本文介绍了文本框中的公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个非常有经验的VB用户,但我试图制作一个表格

,用户可以输入一个总和的公式。


例如,我希望用户输入一个计数器的开始和结束值,并让用户输入公式为

加起来。 />

更明确地说,假设用户想要加起来


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


因此,用户输入1作为起始值,100输入结束

值,此用户也将输入公式1 / n,在文本框中,

作为求和公式(注意n将是一个变量)。


假设用户想要找到总和


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


然后,用户输入2作为起始值,1000作为结束值

并输入公式1 / n ^ 2.


VB似乎不喜欢用户输入公式。我有

试过类似这样的东西


Dim n为整数

Dim s为double


s = 0


对于n = start.text到final.text



下一页


打印s


我也尝试将input.text转换为double(内部)对于

循环),VB抛出一个未处理的异常。


由于我不是很精通VB,我不确定我有

以最佳方式解释了这种情况。所以,任何帮助或指示

将非常感激。


谢谢,


Brian

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格式1,100,2(我想这可能大概是你想要的b $ b)


Private Sub btnGo_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理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(第(2)部分)

总计+ =(1 / i)^幂

下一页

txtOutput.Text =总计。 ToString

结束子


HTH

-

(OHM) - 一名男子

AKA Terry Burns - http://TrainingOn.net


" Brian VanPelt" < BV ****** @ neo.rr.com>在消息中写道

新闻:电视******************************** @ 4ax.com ...
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
--
( OHM ) - One Handed Man
AKA Terry Burns - http://TrainingOn.net

"Brian VanPelt" <bv******@neo.rr.com> wrote in message
news:tv********************************@4ax.com...
我不是一个非常有经验的VB用户,但我试图制作一个表格,用户可以输入一个总和的公式。

例如,我希望用户输入一个计数器的开始和结束值,并让用户输入要加起来的公式。

更明确,假设用户想要加起来

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

因此,用户会输入1作为起始值,输入100作为结束值,此用户还将在文本框中输入公式1 / n,
作为求和公式(注意n将是变量) 。

假设用户想要找到总和

1/2 ^ 1 + 3 ^ 2 + 1/4 ^ 2 + ... + 1 / 100 ^ 2.
然后,用户输入2作为起始值,1000作为结束值
并输入公式1 / n ^ 2.


Dim n为整数
Dim s为double

s = 0

n = start.text到final.text

s = s + input.text

下一页

打印s
我也尝试将input.text转换为double(在For
循环内部),并且VB抛出一个未处理的异常。

因为我不是很精通VB,我不确定我是否以最好的方式解释了这种情况。所以,非常感谢任何帮助或指示。

谢谢,

Brian
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


嗨Brian,


你的项目并不像听起来那么容易。如果要输入的公式

是一个固定的(并且希望很小)设置,那么它就没问题了,但是如果它完全依赖于用户,那么

。 ..然后它可以得到相当的

复杂的编码这样的例程。


此时会出现问题:(考虑到1/1 + 1 / 2 +

1/3 ....示例)


对于i = startVal到endVal

将此公式调暗为String = formula.Replace(" n",i.ToString())

''以下行总是会抛出异常...

将thisValue变为单一= CSng (thisForm)

下一页


问题是如何解析一个未计算的表达式,例如1/1

来自将字符串转换为值。

执行此操作的唯一方法似乎是使用正则表达式分别解析单个值和

运算符,然后手动

执行计算。我相信VB 6.0有Eval功能,它可以很容易地做出类似这样的事情,但是没有找到类似它的东西

在.NET中。


以下是一些这样做的链接......你会发现它需要多少代码才能实现
!您可以考虑使用预建功能,例如

这个...


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


2.在这些论坛中提出了类似的问题:

< http://www.vbcity.com/forums /topic.asp?tid=61590>


希望这会有所帮助,


问候,


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.


2006年4月2日19:20:54 -0700,Cerebrus < ZO ***** @ sify.com>写道:
On 2 Apr 2006 19:20:54 -0700, "Cerebrus" <zo*****@sify.com> wrote:
嗨Brian,

你的项目并不像听起来那么容易。如果要输入的公式是一个固定的(并且希望很小),那么它就可以了,但是如果它完全依赖于用户......那么它可以得到相当的<复杂的编码这样的例程。

此时会出现问题:(考虑到1/1 + 1/2 +
1/3 ....例子)

对于i = startVal to endVal
Dim thisFormula As String = formula.Replace(" n",i.ToString())
''以下行总是抛出一个例外...
将此值变为单一= CSng(thisForm)



问题是如何解析未计算的表达式,例如1/1从字符串到值。
这样做的唯一方法似乎是使用正则表达式分别解析单个值和
运算符,然后手动执行计算。我相信VB 6.0具有Eval功能,它可以很容易地做到这一点,但在.NET中找不到类似的东西。

下面是一些链接这样做......你会意识到它涉及多少代码!您可以考虑使用预建功能,例如
这个......

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

2。在这些论坛中提出了类似的问题:
< http://www.vbcity.com/forums/topic.asp?tid = 61590>

希望这会有所帮助,

问候,

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.



感谢您的回复。我开始发现这是多么困难。想象一下,用户在公式

框或log(n)中输入sqrt(n)。我最初问VB6小组中的某个人他们会怎么做b $ b,他们说我需要添加一个脚本控件

组件。但是,我当然无法在VB.net中找到该组件。

看来,有了这样的脚本创意,用户就能够发送变量
到该计划。所以,我正在调查Windows

脚本主机,但我担心我不能用它做任何事情

但是(读,我不是''我知道如何使用它。)


我不知道这是否有任何帮助,但这是出于对b / b
的渴望而产生的我的计算学生用来计算一个无限系列的总和的小程序(所以可能会高达数百万)。


In任何事件,我将调查这些链接,并感谢您提供

的信息。我还要感谢OHM的回复。


Brian


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


这篇关于文本框中的公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆