如何对 Windows 文本框中的数据执行三角函数

发布于 2024-09-04 04:18:05 字数 101 浏览 6 评论 0原文

必须从字符串转换为双精度吗?如果是这样。如何?

是否有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

再可℃爱ぅ一点好了 2024-09-11 04:18:05

必须从字符串转换为双精度吗?如果是这样。怎么办?

是的。 有很多方法可以做到这一点。

是否有 trig 函数可以按原样接受文本框字符串数据?

不,但如果您真的想要一个,您可以使用上述问题中描述的技术之一轻松实现这样的功能。

通常,您希望在使用之前验证用户输入以确保其正确,并且以数字形式验证输入比验证原始字符串容易得多。

有没有办法从文本框中提取数据作为数值,而不是字符串?

或许;这完全取决于您使用的 GUI 框架。

must you convert from strings to double? if so. how?

Yes. There are quite a few ways to do this.

Are there functions for trig that will accept textbox string data as is?

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.

Is there a way to pull the data from the textbox as a numeric value, not as a string?

Maybe; it depends entirely on what GUI framework you are using.

方觉久 2024-09-11 04:18:05

必须从字符串转换为双精度吗?

是的。

如果是这样。如何?

C++的方式是使用字符串流;特别是,您可能需要使用 istringstream。一个可行的替代方案是遵循 C 方式,即使用 sscanf 以及适当的格式说明符(“%f”)。

另一个 C++ 选项是使用 boost lexical_cast,但在我看来,开始仅对 lexical_cast 使用 boost 有点矫枉过正。

istringstream 示例:

#include <sstream>
// ...
std::istringstream strParser(yourString);
double yourDouble;
strParser>>yourDouble;
if(strParser.fail())
{
    // the string couldn't be converted to a double
}
else if(!strParser.eof())
{
    // the string hasn't been fully consumed; this may or may not be a problem, depending on your needs
}

是否存在可以按原样接受文本框字符串数据的 trig 函数?

AFAIK 不,不需要它们(尽管你可以很容易地编写它们)。

有没有办法从文本框中提取数据作为数值,而不是字符串?

WinAPI 提供了一些这样的“便利函数”以供在对话框中使用,但我记得唯一提供此类帮助的是 GetDlgItemInt,顾名思义,仅适用于整数。

附录

我现在看到您正在使用 C++/CLI (您提到了 System::String):好吧,您应该说,这对选项做了很大的改变。

在托管世界中,最好的选择是使用 Double::Parse 方法;要捕获格式错误的字符串,您应该捕获由 Double::Parse 引发的异常或调用 Double::TryParse 在调用 Double::Parse.

附录之二

呃,我忘了,因为您使用的是 .NET Framework,所以最好使用 .NET 三角函数(类 System.Math)。

must you convert from strings to double?

Yes.

if so. how?

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. use sscanf with the appropriate format specifier ("%f").

Another C++ option is to use the boost lexical_cast, but starting to use boost just for lexical_cast is a bit overkill in my opinion.

Example with istringstream:

#include <sstream>
// ...
std::istringstream strParser(yourString);
double yourDouble;
strParser>>yourDouble;
if(strParser.fail())
{
    // the string couldn't be converted to a double
}
else if(!strParser.eof())
{
    // the string hasn't been fully consumed; this may or may not be a problem, depending on your needs
}

Are there functions for trig that will accept textbox string data as is?

AFAIK no, there's no need of them (although you can write them quite easily).

Is there a way to pull the data from the textbox as a numeric value, not as a string?

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 by Double::Parse or call Double::TryParse before calling Double::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).

留一抹残留的笑 2024-09-11 04:18:05

我建议使用 strtod

char *str = ...;
const char *end;
double d = strtod(str, &end);
if (*end != '\0')
{
    // questionable data
}

I'd recommend using strtod

char *str = ...;
const char *end;
double d = strtod(str, &end);
if (*end != '\0')
{
    // questionable data
}
凶凌 2024-09-11 04:18:05

据我所知,没有办法直接从文本框转换为双精度。您需要首先使用 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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文