如何对 Windows 文本框中的数据执行三角函数
必须从字符串转换为双精度吗?如果是这样。如何?
是否有 trig 函数可以按原样接受文本框字符串数据?
有没有办法从文本框中提取数据作为数值,而不是字符串?
must you convert from strings to double? if so. how?
Are there functions for trig that will accept textbox string data as is?
Is there a way to pull the data from the textbox as a numeric value, not as a string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。 有很多方法可以做到这一点。
不,但如果您真的想要一个,您可以使用上述问题中描述的技术之一轻松实现这样的功能。
通常,您希望在使用之前验证用户输入以确保其正确,并且以数字形式验证输入比验证原始字符串容易得多。
或许;这完全取决于您使用的 GUI 框架。
Yes. There are quite a few ways to do this.
No, but if you really wanted one, you could easily implement such a function using one of the techniques described in the aforementioned question.
Usually you want to validate user input to be sure it is correct before you use it, and validation of the input in numeric form is much easier than validation of the raw string.
Maybe; it depends entirely on what GUI framework you are using.
是的。
C++的方式是使用字符串流;特别是,您可能需要使用
istringstream
。一个可行的替代方案是遵循 C 方式,即使用sscanf
以及适当的格式说明符(“%f”)。另一个 C++ 选项是使用 boost
lexical_cast
,但在我看来,开始仅对 lexical_cast 使用 boost 有点矫枉过正。istringstream
示例:AFAIK 不,不需要它们(尽管你可以很容易地编写它们)。
WinAPI 提供了一些这样的“便利函数”以供在对话框中使用,但我记得唯一提供此类帮助的是
GetDlgItemInt
,顾名思义,仅适用于整数。附录
我现在看到您正在使用 C++/CLI (您提到了 System::String):好吧,您应该说,这对选项做了很大的改变。
在托管世界中,最好的选择是使用
Double::Parse
方法;要捕获格式错误的字符串,您应该捕获由Double::Parse
引发的异常或调用Double::TryParse
在调用Double::Parse.
附录之二
呃,我忘了,因为您使用的是 .NET Framework,所以最好使用 .NET 三角函数(类
System.Math
)。Yes.
The C++ way is to use the string streams; in particular, you'll probably want to use
istringstream
. A viable alternative is to follow the C way, i.e. usesscanf
with the appropriate format specifier ("%f").Another C++ option is to use the boost
lexical_cast
, but starting to use boost just forlexical_cast
is a bit overkill in my opinion.Example with
istringstream
:AFAIK no, there's no need of them (although you can write them quite easily).
The WinAPIs provide a handful of such "convenience functions" for use in dialogs, but the only one I can recall that provides such help is
GetDlgItemInt
, which, as the name may suggest, works only for integers.Addendum
I see now that you are using C++/CLI (you mentioned System::String): well, you should have said that, this changes the options quite a bit.
In the managed world, your best option is to use the
Double::Parse
method; to catch bad-formatted strings, you should either catch the exceptions thrown byDouble::Parse
or callDouble::TryParse
before callingDouble::Parse
.Addendum bis
Uh, I forgot, since you're using the .NET Framework probably it should be better to use the .NET trigonometric functions (class
System.Math
).我建议使用 strtod
I'd recommend using strtod
据我所知,没有办法直接从文本框转换为双精度。您需要首先使用 strtod 之类的内容转换为双精度型。
请注意,您可以轻松创建一个通用函数,该函数接受文本框控件作为参数并返回双精度值(假设文本框包含有效数字)。
There's no way to do a straight conversion from textbox to double as far as I know. You'll need to first convert to a double with something like strtod.
You could easily make a generic function which accepts a textbox control as an argument and returns a double (assuming it's the textbox contains a valid number) mind you..