- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
首先,如果这是世界上最愚蠢的问题,让我道歉。但是,我很难过,我在这里和谷歌上都做了很多搜索。我正在自学 C++,所以我可能不需要掌握必要的词汇来知道要搜索什么。
我正在尝试编写一个有限状态机来解析方程式。我知道以前有人做过,但我正在努力学习。为此,我希望能够获取字符串、识别数字并将它们转换为 double 或 float 。 (我会接受您关于使用哪种格式的任何建议。)
我有一个将字符串转换为 double 的函数:
double convertToDouble(string value)
{
/* -- From http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
Using stringstream, convert a string to a double by treating it like a stream
*/
istringstream stream(value);
double doubleValue;
stream >> doubleValue;
return doubleValue;
}
我有一个函数可以在字符串中查找下一个数值:
string evaluateNextValue (int operatorPosition, string equation)
{
/* -- Find the next value
My idea is that, since I'm using spaces as my dividers, we'll look for
the first number and then, using insert to put the individual numbers
into a string until a space is found again. Then, the numbers--now
in the correct order--can be converted to a double and returned
*/
bool digitFound = false;
string workingNumbers;
for (int pos = operatorPosition; pos < equation.size(); pos ++)
{
if (equation.at(pos) == ' ' && digitFound == true)
{
double result = convertToDouble(workingNumbers);
cout << "Converting a string to " << result << endl;
cout << "The result plus one is: " << result +1 << endl;
return workingNumbers;
} else if (equation.at(pos) == ' ' && digitFound == false)
{
cout << "Skipping a blank space." << endl;
continue;
} else
{
if (digitFound == false)
{
digitFound = true;
cout << "First digit found." << endl;
}
cout << "Adding " << equation.at(pos) << " to the string." << endl;
workingNumbers.insert(workingNumbers.end(),equation.at(pos));
}
}
}
这是我用来调用它们的 main() 作为一种测试。
int main()
{
string dataInput;
cout << "Insert a number" << endl;
getline(cin, dataInput);
cout << "You entered: " << dataInput << endl;
double numberValue = convertToDouble(evaluateNextValue(0, dataInput));
cout << "Adding ten: " << numberValue + 10;
return 0;
}
事情是这样的:就像现在一样,通过 evaluateNextValue() 返回一个字符串,它可以工作。这对我来说似乎有点笨拙(可能对你来说这一切看起来都很笨拙),但它确实有效。
当我让代码在函数中操作变量结果时,它工作正常。我只是将字符串转换为 double ,然后就可以使用它了。
但是,当我将字符串转换为 double 并尝试返回 double 时。 . . double 在函数本身中工作正常。但是当它到达 main() 时是 nan。甚至更奇怪(或者至少同样奇怪)的事实是,尝试返回一个 int 确实会返回一个 int,但从来没有任何东西与我输入的值有任何远程关联。
如果您愿意提供任何帮助,我将不胜感激。而且,由于这是我在这里发表的第一篇文章,我愿意接受任何风格建议。
最佳答案
如果 evaluateNextValue
由于 for
循环条件到达字符串末尾,则返回值未定义(因为您没有 return
声明)。这会触发未定义的行为,其中可能包括返回 NaN 值。
您应该启用编译器的警告以捕获此类错误。
关于c++ - Return 不适用于 float 或 int,但适用于 string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11244413/
我知道问题的标题听起来很奇怪,但我不知道该怎么调用它。 首先,我有一个网格布局,我希望我的 .search-wrapper 宽度为 50% 并向右浮动。在我的演示中 jsfiddle整个 .searc
我们正在使用 QA-C 来实现 MISRA C++ 一致性,但是该工具会为这样的代码喷出错误: float a = foo(); float b = bar(); float c = a - b; 据
考虑 float a[] = { 0.1, 0.2, 0.3}; 我很困惑a稍后传递给函数 foo(float* A) .不应该是 float* 类型的变量指向单个浮点数,对吗?就像这里提到的tu
这可能是我一段时间以来收到的最好的错误消息,我很好奇出了什么问题。 原代码 float currElbowAngle = LeftArm ? Elbow.transform.localRotation
刚开始学习 F#,我正在尝试为 e 生成和评估泰勒级数的前 10 项。我最初编写了这段代码来计算它: let fact n = function | 0 -> 1 | _ -> [1
我已经使用 Erlang 读取二进制文件中的 4 个字节(小端)。 在尝试将二进制转换为浮点时,我一直遇到以下错误: ** exception error: bad argument in
假设我有: float a = 3 // (gdb) p/f a = 3 float b = 299792458 // (gdb) p/f b = 29979244
我每次都想在浏览器顶部修复这个框。但是右边有一些问题我不知道如何解决所以我寻求帮助。 #StickyBar #RightSideOfStickyBar { float : right ; }
我正在研究 C# 编译器并试图理解数学运算规则。 我发现在两种不同的原始类型之间使用 == 运算符时会出现难以理解的行为。 int a = 1; float b = 1.0f; Cons
假设我有: float a = 3 // (gdb) p/f a = 3 float b = 299792458 // (gdb) p/f b = 29979244
Denormals众所周知,与正常情况相比,表现严重不佳,大约是 100 倍。这经常导致 unexpected软件 problems . 我很好奇,从 CPU 架构的角度来看,为什么非规范化必须是 那
我有一个由两个 float 组成的区间,并且需要生成 20 个随机数,看起来介于两个 float 定义的区间之间。 比方说: float a = 12.49953f float b = 39.1123
我正在构建如下矩阵: QMatrix4x3 floatPos4x3 = QMatrix4x3( floatPos0.at(0), floatPos1.at(0), floatPos2.at(0),
给定归一化的浮点数f,在f之前/之后的下一个归一化浮点数是多少。 通过微动,提取尾数和指数,我得到了: next_normalized(double&){ if mantissa is n
关于 CSS“float”属性的某些东西一直让我感到困惑。为什么将“float”属性应用到您希望 float 的元素之前的元素? 为了帮助可视化我的问题,我创建了以下 jsFiddle http://
关于 CSS“float”属性的某些东西一直让我感到困惑。为什么将“float”属性应用到您希望 float 的元素之前的元素? 为了帮助可视化我的问题,我创建了以下 jsFiddle http://
我有一个新闻源/聊天框。每个条目包含两个跨度:#user 和#message。我希望#user 向左浮动,而#message 向左浮动。如果#message 导致行超过容器宽度,#message 应该
我想创建一个“记分卡”网格来输出一些数据。如果每个 div.item 中的数据都具有相同的高度,那么在每个 div.item 上留下一个简单的 float 会提供一个漂亮的均匀布局,它可以根据浏览器大
我正在学习使用 CSS float 属性。我想了解此属性的特定效果。 考虑以下简单的 HTML 元素: div1 div2 This is a paragraph 以及以下 CSS 规则: div {
我正在尝试从可以是 int 或 float 的文件中提取数据。我发现这个正则表达式将从文件 (\d+(\.\d+)?) 中提取这两种类型,但我遇到的问题是它将 float 拆分为两个。 >>> imp
我是一名优秀的程序员,十分优秀!