在 C++ 中双精度为字符串
double to string in c++
我正在尝试将表达式"5 + b * 18"转换为"5 + 16.89 * 18"。我的问题在 while 循环内。我设法删除了空格。
我的代码:
double number = 16.89;
size_t found;
ostringstream converted;
string str ("5 + b * 18");
str.erase(remove(str.begin(),str.end(),' '),str.end());
found = str.find_first_of (VALID_CHARS);
while (found != string::npos)
{
converted << fixed << setprecision (8) << number;
str.insert(found, converted.str());
found = str.find_first_of (VALID_CHARS,found + 1);
}
谁能帮我?
泰
> insert()
会将字符串的内容移到插入文本之后的右侧,但不会删除给定位置的字符。 您应该使用 replace()
,指定大小 ' (仅替换单个字符)
这是家庭作业吗?如果没有,我真的建议不要自己做这种解析。对于这种经过广泛测试的东西,有专门的库,比如muParser。
使用 sprintf。它非常适合将大多数基元类型转换为字符串。
int main()
{
double number = 16.89;
char buffer [50];
sprintf(buffer, "5 + %f * 18",number);
}
相关文章:
- 如何在C++中的同一函数中使用字符串和双精度
- C 字符串返回字符串的整数/双精度/长整型值
- 如何从字符串转换为双精度*
- C++如何将字符串逐行转换为双精度
- 如何在 C++ 中将双精度数组中的值"force convert"到字符串输出中?
- 在C++中将字符串转换为双精度,而无需科学记数法
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 如何在C++中将字符串转换为双精度?
- 将双精度值转换为 char 变量时字符串流如何工作
- 在 c++ 中从双精度转换为字符串
- 从字符串转换为双精度和整数 - C++
- 如何从char16_t字符串文字中读取双精度?
- 分析字符串中的双精度和单词
- 使用 sprintf 和 %g 将双精度转换为字符串的意外结果
- C++将包含非人类可读数据的字符串转换为 200 双精度
- 将大双精度转换为科学记数法以用作字符串?
- 字符串和双精度的麻烦,等值后再得到一个数字
- 如何从字符串流中提取双精度
- 如何调用返回双精度或字符串并将其保存在地图上但作为对象的类的方法
- C# 是否有办法将双精度数组强制转换为类似于C++转换为 char* 的字符串?